-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path15-lighting.js
More file actions
308 lines (265 loc) · 9.64 KB
/
15-lighting.js
File metadata and controls
308 lines (265 loc) · 9.64 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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
let render;
// projection, viewing, and model matrices
var projMatrix = glMatrix.mat4.create();
var viewMatrix = glMatrix.mat4.create();
var modelMatrix = glMatrix.mat4.create();
var normalMatrix = glMatrix.mat4.create();
var lightPosition = glMatrix.vec3.fromValues(10.0, 10.0, 10.0); // in world space
var cameraPosition = glMatrix.vec3.fromValues(0.0, 0.0, 60.0);
var ambientColor = glMatrix.vec3.fromValues(0.2, 0.2, 0.2);
var diffuseColor = glMatrix.vec3.fromValues(1.0, 1.0, 1.0);
var specularColor = glMatrix.vec3.fromValues(1.0, 1.0, 1.0);
var Ka = 0.5; // ambient reflectivity
var Kd = 0.4; // diffuse reflectivity
var Ks = 1.0; // specular reflectivity
var shininess = 100.0; // shininess factor for specular highlights
var angle = 0.0; // rotation angle
function degToRad(degrees) {
return degrees * Math.PI / 180;
}
async function loadJSON(device, url) {
const res = await fetch(url);
const data = await res.json();
if (!data) {
fail('failed to load teapot data');
return;
}
const positions = new Float32Array(data.vertexPositions);
const normals = new Float32Array(data.vertexNormals);
const texcoords = new Float32Array(data.vertexTextureCoords);
const indices = new Uint32Array(data.indices);
// create a data buffer to interleave the vertex data
const interleavedData = new Float32Array(positions.length + normals.length + texcoords.length);
for (let i = 0, j = 0; i < positions.length; i += 3, j += 8) {
interleavedData[j] = positions[i];
interleavedData[j + 1] = positions[i + 1];
interleavedData[j + 2] = positions[i + 2];
interleavedData[j + 3] = normals[i];
interleavedData[j + 4] = normals[i + 1];
interleavedData[j + 5] = normals[i + 2];
interleavedData[j + 6] = texcoords[i];
interleavedData[j + 7] = texcoords[i + 1];
}
const vertex = device.createBuffer({
label: 'vertex buffer',
size: positions.byteLength + normals.byteLength + texcoords.byteLength,
usage: GPUBufferUsage.VERTEX | GPUBufferUsage.COPY_DST,
});
device.queue.writeBuffer(vertex, 0, interleavedData);
const index = device.createBuffer({
label: 'index buffer',
size: indices.byteLength,
usage: GPUBufferUsage.INDEX | GPUBufferUsage.COPY_DST,
});
device.queue.writeBuffer(index, 0, indices);
return {
vertex, index,
vertexCount: positions.length / 3,
indexCount: indices.length
};
}
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: 'simple lighting',
code: `
struct Uniforms{
projMatrix: mat4x4<f32>,
viewMatrix: mat4x4<f32>,
modelMatrix: mat4x4<f32>,
normalMatrix: mat4x4<f32>,
lightPosition: vec3f,
cameraPosition: vec3f,
ambientColor: vec3f,
diffuseColor: vec3f,
specularColor: vec3f,
_pad1: f32,
Ka: f32,
Kd: f32,
Ks: f32,
shininess: f32,
_pad: vec3f, // padding to 16-byte alignment
};
@group(0) @binding(0) var<uniform> uniforms : Uniforms;
struct VSIn {
@location(0) pos : vec3f,
@location(1) normal : vec3f,
@location(2) texcoords : vec2f,
};
struct VSOut {
@builtin(position) pos : vec4f,
@location(0) color : vec4f,
};
@vertex fn vs(in : VSIn) -> VSOut
{
// position in the eye space
let pos_in_eye_space = (uniforms.viewMatrix * uniforms.modelMatrix * vec4f(in.pos, 1.0)).xyz;
// light direction in the eye space
let light_position_in_eye_space = (uniforms.viewMatrix * vec4f(uniforms.lightPosition, 1.0)).xyz;
var light_dir_in_eye_space = normalize(light_position_in_eye_space - pos_in_eye_space);
// normal in the eye space
var normal = normalize((uniforms.normalMatrix * vec4f(in.normal, 0.0)).xyz);
// viewing direction in the eye space
var eye_vector = normalize(-uniforms.cameraPosition);
// ambient
let ambient = uniforms.ambientColor * uniforms.Ka;
// diffuse
let ndotl = max(dot(normal, light_dir_in_eye_space), 0.0);
let diffuse = uniforms.diffuseColor * uniforms.Kd * ndotl;
// specular
let reflectDir = reflect(light_dir_in_eye_space, normal);
let rdotv = max(dot(reflectDir, eye_vector), 0.0);
let spec = pow(rdotv, uniforms.shininess);
let specular = uniforms.specularColor * uniforms.Ks * spec;
var out : VSOut;
out.pos = uniforms.projMatrix * uniforms.viewMatrix * uniforms.modelMatrix * vec4f(in.pos, 1.0);
out.color = vec4(ambient + diffuse + specular, 1.0);
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: 8 * 4,
attributes: [
{
shaderLocation: 0, // position
offset: 0,
format: 'float32x3',
},
{
shaderLocation: 1, // normals
offset: 3 * 4, // the offset is two floating-point numbers
format: 'float32x3',
},
{
shaderLocation: 2, // texcoords
offset: 6 * 4, // the offset is three floating-point numbers
format: 'float32x2',
}
]
},
],
},
fragment: {
entryPoint: 'fs',
module: module,
targets: [{ format: format }],
},
depthStencil: { // enable depth testing
format: 'depth24plus',
depthWriteEnabled: true,
depthCompare: 'less',
},
});
const teapotData = await loadJSON(device, 'teapot.json');
// --- Uniform buffer size calculation ---
// 4 matrices: 4*64 = 256 bytes
// 5 vec3: 5*16 = 80 bytes (lightPosition, ambientColor, diffuseColor, specularColor, cameraPosition)
// 4 f32: 16 bytes (Ka, Kd, Ks, shininess)
// 1 vec3: 16 bytes (_pad)
// Total: 256 + 80 + 16 + 16 = 368 bytes
const uniformBuffer = device.createBuffer({
size: 368,
usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST,
});
// bind groups
const bindGroup = device.createBindGroup({
layout: pipeline.getBindGroupLayout(0),
entries: [{ binding: 0, resource: { buffer: uniformBuffer } }],
});
// the depth texture
const depthTexture = device.createTexture({
size: [canvas.width, canvas.height],
format: 'depth24plus',
usage: GPUTextureUsage.RENDER_ATTACHMENT,
});
const depthTextureView = depthTexture.createView();
render = () => {
const textureView = context.getCurrentTexture().createView();
const renderPassDescriptor = {
colorAttachments: [{
view: textureView,
clearValue: [1.0, 1.0, 1.0, 1.0],
storeOp: 'store',
loadOp: 'clear',
}],
depthStencilAttachment: { // add the depth stencil attachment to enable the depth test
view: depthTextureView,
depthClearValue: 1.0,
depthLoadOp: 'clear',
depthStoreOp: 'store',
},
};
const commandEncoder = device.createCommandEncoder();
const passEncoder = commandEncoder.beginRenderPass(renderPassDescriptor);
passEncoder.setPipeline(pipeline);
passEncoder.setVertexBuffer(0, teapotData.vertex);
passEncoder.setIndexBuffer(teapotData.index, 'uint32');
passEncoder.setBindGroup(0, bindGroup);
// projection
glMatrix.mat4.identity(projMatrix);
glMatrix.mat4.perspective(projMatrix, degToRad(45), 1.0, 0.1, 100);
device.queue.writeBuffer(uniformBuffer, 0, projMatrix);
// viewing
glMatrix.mat4.identity(viewMatrix);
glMatrix.mat4.lookAt(viewMatrix, cameraPosition, [0,0,0], [0,1,0]);
device.queue.writeBuffer(uniformBuffer, 64, viewMatrix);
// model
glMatrix.mat4.identity(modelMatrix);
glMatrix.mat4.rotateY(modelMatrix, modelMatrix, degToRad(angle));
device.queue.writeBuffer(uniformBuffer, 128, modelMatrix);
// normal matrix
glMatrix.mat4.identity(normalMatrix);
glMatrix.mat4.invert(normalMatrix, modelMatrix);
glMatrix.mat4.transpose(normalMatrix, normalMatrix);
device.queue.writeBuffer(uniformBuffer, 192, normalMatrix);
// lightPosition (vec3)
device.queue.writeBuffer(uniformBuffer, 256, lightPosition);
// cameraPosition (vec3)
device.queue.writeBuffer(uniformBuffer, 272, cameraPosition);
// ambientColor (vec3)
device.queue.writeBuffer(uniformBuffer, 288, ambientColor);
// diffuseColor (vec3)
device.queue.writeBuffer(uniformBuffer, 304, diffuseColor);
// specularColor (vec3)
device.queue.writeBuffer(uniformBuffer, 320, specularColor);
// Ka, Kd, Ks, shininess (all f32)
device.queue.writeBuffer(uniformBuffer, 336, new Float32Array([Ka, Kd, Ks, shininess]));
// draw the object
passEncoder.drawIndexed(teapotData.indexCount);
passEncoder.end();
device.queue.submit([commandEncoder.finish()]);
};
function animate() {
angle += 1.0;
render();
requestAnimationFrame(animate);
}
animate();
}
main();