-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path12-matrix-stack.js
More file actions
278 lines (238 loc) · 8.52 KB
/
12-matrix-stack.js
File metadata and controls
278 lines (238 loc) · 8.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
// status for the rotation and translation control
var sunTrans = [0.0, 0.0];
var sunScale = [0.25, 0.25];
var sunRot = 30;
var sunMatrix = glMatrix.mat4.create();
var earthTrans = [2.0, 0.0];
var earthScale = [0.5, 0.5];
var earthRot = 60;
var earthMatrix = glMatrix.mat4.create();
var moonTrans = [2.0, 0.0];
var moonScale = [0.25, 0.25];
var moonRot = 45;
var moonMatrix = glMatrix.mat4.create();
var marsTrans = [2.0, 0.0];
var marsScale = [0.5, 0.5];
var marsRot = 30;
var marsMatrix = glMatrix.mat4.create();
let render;
function degToRad(degrees) {
return degrees * Math.PI / 180;
}
async function updateTransformations() {
// Read values from the form
var vSunTrans = parseFloat(document.getElementById("sunTrans").value);
var vSunScale = parseFloat(document.getElementById("sunScale").value);
var vSunRot = parseFloat(document.getElementById("sunRot").value);
var vEarthTrans = parseFloat(document.getElementById("earthTrans").value);
var vEarthScale = parseFloat(document.getElementById("earthScale").value);
var vEarthRot = parseFloat(document.getElementById("earthRot").value);
var vMoonTrans = parseFloat(document.getElementById("moonTrans").value);
var vMoonScale = parseFloat(document.getElementById("moonScale").value);
var vMoonRot = parseFloat(document.getElementById("moonRot").value);
var vMarsTrans = parseFloat(document.getElementById("marsTrans").value);
var vMarsScale = parseFloat(document.getElementById("marsScale").value);
var vMarsRot = parseFloat(document.getElementById("marsRot").value);
// Matrix stack
let stack = [];
let M = glMatrix.mat4.create();
// --- Sun ---
glMatrix.mat4.identity(M);
glMatrix.mat4.translate(M, M, [vSunTrans, 0, 0]);
glMatrix.mat4.scale(M, M, [vSunScale, vSunScale, 1]);
glMatrix.mat4.rotate(M, M, degToRad(vSunRot), [0, 0, 1]);
glMatrix.mat4.copy(sunMatrix, M);
// Push sun matrix
stack.push(glMatrix.mat4.clone(M));
// --- Earth ---
glMatrix.mat4.translate(M, M, [vEarthTrans, 0, 0]);
glMatrix.mat4.scale(M, M, [vEarthScale, vEarthScale, 1]);
glMatrix.mat4.rotate(M, M, degToRad(vEarthRot), [0, 0, 1]);
glMatrix.mat4.copy(earthMatrix, M);
// Push earth matrix
stack.push(glMatrix.mat4.clone(M));
// --- Moon ---
glMatrix.mat4.translate(M, M, [vMoonTrans, 0, 0]);
glMatrix.mat4.scale(M, M, [vMoonScale, vMoonScale, 1]);
glMatrix.mat4.rotate(M, M, degToRad(vMoonRot), [0, 0, 1]);
glMatrix.mat4.copy(moonMatrix, M);
// Pop moon matrix
M = stack.pop();
// Pop earth matrix, now M is the sun matrix
M = stack.pop();
// --- Mars (as another child of Sun) ---
glMatrix.mat4.translate(M, M, [vMarsTrans, 0, 0]);
glMatrix.mat4.scale(M, M, [vMarsScale, vMarsScale, 1]);
glMatrix.mat4.rotate(M, M, degToRad(vMarsRot), [0, 0, 1]);
glMatrix.mat4.copy(marsMatrix, M);
render();
}
async function main()
{
// get webgpu adapter and device
const adaptor = await navigator.gpu?.requestAdapter();
const device = await adaptor?.requestDevice();
if (!device) {
fail('your browser does not support WebGPU');
return;
}
// create a webgpu context with the canvas
const canvas = document.getElementById("webgpu-canvas");
const context = canvas.getContext("webgpu");
const format = navigator.gpu.getPreferredCanvasFormat();
context.configure({device, format});
// vertex and fragment shaders (in one single module)
const module = device.createShaderModule({
label: 'moving the square',
code: `
struct Uniforms{
M: mat4x4<f32>, // model matrix
};
@group(0) @binding(0) var<uniform> uniforms : Uniforms;
struct VSIn {
@location(0) pos : vec2f,
@location(1) color : vec4f,
};
struct VSOut {
@builtin(position) pos : vec4f,
@location(0) color : vec4f,
};
@vertex fn vs(in : VSIn) -> VSOut
{
var out : VSOut;
out.pos = uniforms.M * vec4f(in.pos, 0.0, 1.0);
out.color = in.color;
return out;
}
@fragment fn fs(vsOut : VSOut) -> @location(0) vec4f
{
return vsOut.color;
}
`,
});
// the rendering pipeline
const pipeline = device.createRenderPipeline({
label: 'vertex buffer triangle pipeline',
layout: 'auto',
vertex: {
entryPoint: 'vs',
module: module,
buffers: [
{
arrayStride: 5 * 4, // 5 floating-point numbers with 4 bytes each
attributes: [
{
shaderLocation: 0,
offset: 0,
format: 'float32x2',
},
{
shaderLocation: 1,
offset: 2 * 4, // the offset is two floating-point numbers
format: 'float32x3',
}
]
},
],
},
fragment: {
entryPoint: 'fs',
module: module,
targets: [{ format: format }],
},
});
// vertex position and color data in one buffer, now removed
const vertexData = new Float32Array([
-1.0, -1.0, 1.0, 0.0, 0.0,
1.0, -1.0, 0.0, 1.0, 0.0,
1.0, 1.0, 0.0, 0.0, 1.0,
-1.0, 1.0, 1.0, 1.0, 0.0,
]);
// here goes the indices for the two triangles
const indexData = new Uint32Array([
0, 1, 2, // first triangle
0, 2, 3, // second triangle
]);
// vertex buffer for both positions and colors
const vertexBuffer = device.createBuffer({
label: 'vertex position buffer of both positions and colors for two triangles',
size: vertexData.byteLength,
usage: GPUBufferUsage.VERTEX | GPUBufferUsage.COPY_DST,
});
device.queue.writeBuffer(vertexBuffer, 0, vertexData);
// index buffer for the two triangles
const indexBuffer = device.createBuffer({
label: 'index buffer for two triangles',
size: indexData.byteLength,
usage: GPUBufferUsage.INDEX | GPUBufferUsage.COPY_DST,
});
device.queue.writeBuffer(indexBuffer, 0, indexData);
// uniform buffers for the sun, earth, and moon
const sunUniformBuffer = device.createBuffer({
size: 16 * 4,
usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST,
});
const earthUniformBuffer = device.createBuffer({
size: 16 * 4,
usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST,
});
const moonUniformBuffer = device.createBuffer({
size: 16 * 4,
usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST,
});
const marsUniformBuffer = device.createBuffer({
size: 16 * 4,
usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST,
});
// bind groups for the sun, earth, and moon
const sunBindGroup = device.createBindGroup({
layout: pipeline.getBindGroupLayout(0),
entries: [{ binding: 0, resource: { buffer: sunUniformBuffer } }],
});
const earthBindGroup = device.createBindGroup({
layout: pipeline.getBindGroupLayout(0),
entries: [{ binding: 0, resource: { buffer: earthUniformBuffer } }],
});
const moonBindGroup = device.createBindGroup({
layout: pipeline.getBindGroupLayout(0),
entries: [{ binding: 0, resource: { buffer: moonUniformBuffer } }],
});
const marsBindGroup = device.createBindGroup({
layout: pipeline.getBindGroupLayout(0),
entries: [{ binding: 0, resource: { buffer: marsUniformBuffer } }],
});
render = () => {
const textureView = context.getCurrentTexture().createView(); // the output is a texture, and we are getting a "view" of texture as the output of the render pass
const renderPassDescriptor = {
colorAttachments: [{
view: textureView,
clearValue: [1.0, 1.0, 1.0, 1.0], // an arbitrary color you prefer
storeOp: 'store',
loadOp: 'clear',
}],
};
const commandEncoder = device.createCommandEncoder();
const passEncoder = commandEncoder.beginRenderPass(renderPassDescriptor);
passEncoder.setPipeline(pipeline);
passEncoder.setVertexBuffer(0, vertexBuffer);
passEncoder.setIndexBuffer(indexBuffer, 'uint32');
passEncoder.setBindGroup(0, sunBindGroup);
device.queue.writeBuffer(sunUniformBuffer, 0, sunMatrix);
passEncoder.drawIndexed(6);
passEncoder.setBindGroup(0, earthBindGroup);
device.queue.writeBuffer(earthUniformBuffer, 0, earthMatrix);
passEncoder.drawIndexed(6);
passEncoder.setBindGroup(0, moonBindGroup);
device.queue.writeBuffer(moonUniformBuffer, 0, moonMatrix);
passEncoder.drawIndexed(6);
passEncoder.setBindGroup(0, marsBindGroup);
device.queue.writeBuffer(marsUniformBuffer, 0, marsMatrix);
passEncoder.drawIndexed(6);
passEncoder.end();
// fire up the GPU to render the load value to the output texture
device.queue.submit([commandEncoder.finish()]);
};
updateTransformations();
render();
}
main();