-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathmaterial-phong.js
executable file
·475 lines (421 loc) · 13 KB
/
material-phong.js
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
import {
Attachment,
Attribute,
Axes,
BindGroup,
BindGroupLayout,
Buffer,
Clock,
Command,
Context,
GPUShaderStage,
Pass,
Pipeline,
Shaders,
State,
Struct,
Uniform,
Variable,
WGSLBuiltIn,
} from "../lib/index.js";
import { mat4 } from "gl-matrix";
import typedArrayInterleave from "typed-array-interleave";
import typedArrayConcat from "typed-array-concat";
import { PerspectiveCamera, Controls } from "cameras";
import { torus } from "primitive-geometry";
import { Pane } from "tweakpane";
State.debug = true;
const context = new Context();
if (!(await context.init())) {
const message = "Your browser doesn't support WebGPU.";
document.body.textContent = message;
throw new Error(message);
}
document.body.appendChild(context.canvas);
const clock = new Clock();
// Camera
const camera = new PerspectiveCamera({
position: new Float32Array([3, 3, 3]),
});
camera.update();
const controls = new Controls({
element: context.canvas,
camera,
position: camera.position,
target: camera.target,
distanceBounds: [camera.near, camera.far],
});
const onResize = () => {
context.resize(window.innerWidth, window.innerHeight);
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
};
onResize();
// Layouts
const systemBindGroupLayout = new BindGroupLayout([
{
buffer: {},
visibility: GPUShaderStage.VERTEX | GPUShaderStage.FRAGMENT,
name: "System",
uniforms: [
new Uniform("projectionMatrix", "mat4"),
new Uniform("viewMatrix", "mat4"),
new Uniform("inverseViewMatrix", "mat4"),
new Uniform("cameraPosition", "vec3"),
new Uniform("time", "float"),
],
},
]);
const meshBindGroupLayout = new BindGroupLayout([
{
buffer: {},
visibility: GPUShaderStage.VERTEX,
name: "Mesh",
uniforms: [
new Uniform("modelMatrix", "mat4"),
new Uniform("normalMatrix", "mat3"),
],
qualifiers: { layout: "std140" },
},
]);
// Buffers
const systemUniformsBuffer = new Buffer();
systemUniformsBuffer.uniformBuffer(systemBindGroupLayout.getBindGroupSize());
const meshUniformsBuffer = new Buffer();
meshUniformsBuffer.uniformBuffer(meshBindGroupLayout.getBindGroupSize());
// Bindings
const systemUniformBindGroup = new BindGroup({
layout: systemBindGroupLayout.gpuBindGroupLayout,
resources: [
{
buffer: systemUniformsBuffer.gpuBuffer,
offset: 0,
size: systemBindGroupLayout.getBindGroupSize(),
},
],
});
const meshUniformBindGroup = new BindGroup({
layout: meshBindGroupLayout.gpuBindGroupLayout,
resources: [
{
buffer: meshUniformsBuffer.gpuBuffer,
offset: 0,
size: meshBindGroupLayout.getBindGroupSize(),
},
],
});
// Geometry
const modelMatrix = mat4.create();
const normalMatrix = mat4.create();
// const geometry = sphere();
// const geometry = cube();
const geometry = torus({ radius: 0.5, minorRadius: 0.25 });
const geometryVertexBuffer = new Buffer();
const geometryIndicesBuffer = new Buffer();
geometryVertexBuffer.vertexBuffer(
typedArrayInterleave(
Float32Array,
[3, 3, 2],
geometry.positions,
geometry.normals,
geometry.uvs
)
);
geometryIndicesBuffer.indexBuffer(new Uint32Array(geometry.cells));
// Pipeline
const pipeline = new Pipeline({
bindGroupLayouts: [systemBindGroupLayout, meshBindGroupLayout],
ins: [
new Attribute("position", "vec3"),
new Attribute("normal", "vec3"),
new Attribute("uv", "vec2"),
],
outs: [
WGSLBuiltIn.FrontFacing,
WGSLBuiltIn.Position,
new Attribute("vPositionWorld", "vec3"),
new Attribute("vPositionView", "vec3"),
new Attribute("vNormalView", "vec3"),
new Attribute("vNormalWorld", "vec3"),
],
structs: [
new Struct(
"Material",
[
new Variable("ambientColor", "vec3<f32>"),
new Variable("diffuseColor", "vec3<f32>"),
new Variable("specularColor", "vec3<f32>"),
new Variable("shininess", "f32"),
new Variable("roughness", "f32"),
new Variable("albedo", "f32"),
new Variable("fresnel", "f32"),
],
GPUShaderStage.FRAGMENT
),
new Struct(
"Light",
[
new Variable("position", "vec3<f32>"),
new Variable("color", "vec4<f32>"),
new Variable("attenuation", "f32"),
// Spot
new Variable("direction", "vec3<f32>"),
new Variable("innerAngle", "f32"),
new Variable("angle", "f32"),
// Spot and Point
new Variable("range", "f32"),
],
GPUShaderStage.FRAGMENT
),
],
vertex: /* wgsl */ `
var output: Output;
output.vPositionWorld = (mesh.modelMatrix * vec4<f32>(position, 1.0)).xyz;
output.vPositionView = (system.viewMatrix * vec4<f32>(output.vPositionWorld, 1.0)).xyz;
output.vNormalView = mesh.normalMatrix * normal;
output.vNormalWorld = normalize((system.inverseViewMatrix * vec4<f32>(output.vNormalView, 0.0)).xyz);
output.position = system.projectionMatrix * vec4<f32>(output.vPositionView, 1.0);
return output;
`,
fragmentBody: /* wgsl */ `
${Shaders.CONSTANTS.PI}
${Shaders.CONSTANTS.EPSILON}
${Shaders.DIFFUSE.OREN_NAYAR}
${Shaders.SPECULAR.PHONG}
${Shaders.DIRECT.PHONG}
`,
fragment: /* wgsl */ `
var color = vec3<f32>(0.0);
// Renormalize
var normalWorld = normalize(vNormalWorld);
normalWorld = normalWorld * (f32(front_facing) * 2.0 - 1.0);
var normalView = normalize(vNormalView);
normalView = normalView * (f32(front_facing) * 2.0 - 1.0);
var material: Material;
material.ambientColor = vec3<f32>(0.05, 0.05, 0.05);
material.diffuseColor = vec3<f32>(1.0, 1.0, 1.0);
material.diffuseColor = normalWorld * 0.5 + 0.5;
material.specularColor = vec3<f32>(0.9, 0.9, 0.9);
material.shininess = 20.0;
material.roughness = 0.2;
// #ifdef DIFFUSE_OREN_NAYAR
material.albedo = 2.0;
// #endif
// #ifdef SPECULAR_GAUSSIAN
// material.shininess = 0.2;
// #endif
// #ifdef SPECULAR_COOK_TORRANCE
// material.fresnel = 0.1;
// #endif
let viewDirectionWorld = normalize(system.cameraPosition - vPositionWorld);
let viewDirectionView = normalize(-vPositionView);
// Directional
var directionnalLight: Light;
directionnalLight.position = vec3<f32>(-1.0, 0.0, -1.0);
// directionnalLight.position.x = 2.0 * cos(system.time);
// directionnalLight.position.z = 2.0 * sin(system.time);
directionnalLight.color = vec4<f32>(0.2, 0.1, 0.1, 1.0);
let positionToLight = -directionnalLight.position;
color = color + getDirectShading(material, directionnalLight, normalWorld, viewDirectionWorld, positionToLight, false, false);
// View space
// let lightPositionView = (system.viewMatrix * vec4(directionnalLight.position, 1.0)).xyz;
// directionnalLight.position = lightPositionView - vPositionView;
// let positionToLight = -directionnalLight.position;
// color += getDirectShading(material, light, normalView, viewDirectionView, positionToLight, false, false);
// Point
var pointLight: Light;
pointLight.position = vec3<f32>(0.0, 0.0, 0.0);
// pointLight.position.x = 2.0 * cos(system.time);
// pointLight.position.z = 2.0 * sin(system.time);
pointLight.color = vec4<f32>(0.1, 0.1, 0.2, 2.5);
pointLight.range = 2.5;
let positionToPointLight = pointLight.position - vPositionWorld;
color = color + getDirectShading(
material,
pointLight,
normalWorld,
viewDirectionWorld,
positionToPointLight,
true,
false
);
// Spot
var spotLight: Light;
spotLight.position = vec3<f32>(0.0, 1.0, 0.0);
spotLight.direction = vec3<f32>(0.0, -1.0, 0.0);
spotLight.color = vec4<f32>(0.1, 0.2, 0.1, 2.5);
spotLight.range = 2.5;
spotLight.angle = PI / 4.0;
spotLight.innerAngle = PI / 5.0;
let positionToSpotLight = spotLight.position - vPositionWorld;
color = color + getDirectShading(
material,
spotLight,
normalWorld,
viewDirectionWorld,
positionToSpotLight,
true,
true
);
return vec4<f32>(color, 1.0);
`,
// GLSL
glsl: {
language: "glsl",
vertex: /* glsl */ `
vPositionWorld = (mesh.modelMatrix * vec4(position, 1.0)).xyz;
vPositionView = (system.viewMatrix * vec4(vPositionWorld, 1.0)).xyz;
vNormalView = mesh.normalMatrix * normal;
vNormalWorld = normalize((system.inverseViewMatrix * vec4(vNormalView, 0.0)).xyz);
gl_Position = system.projectionMatrix * vec4(vPositionView, 1.0);
`,
fragmentBody: /* glsl */ `
${Shaders.GLSL.UTILS.SATURATE}
${Shaders.GLSL.CONSTANTS.PI}
${Shaders.GLSL.CONSTANTS.EPSILON}
${Shaders.GLSL.DIFFUSE.OREN_NAYAR}
${Shaders.GLSL.SPECULAR.PHONG}
${Shaders.GLSL.DIRECT.PHONG}
`,
fragment: /* glsl */ `
vec3 color = vec3(0.0);
// Renormalize
vec3 normalWorld = normalize(vNormalWorld);
normalWorld *= float(gl_FrontFacing) * 2.0 - 1.0;
vec3 normalView = normalize(vNormalView);
normalView *= float(gl_FrontFacing) * 2.0 - 1.0;
Material material;
material.ambientColor = vec3(0.05, 0.05, 0.05);
material.diffuseColor = vec3(1.0, 1.0, 1.0);
material.diffuseColor = normalWorld * 0.5 + 0.5;
material.specularColor = vec3(0.9, 0.9, 0.9);
material.shininess = 20.0;
material.roughness = 0.2;
#ifdef DIFFUSE_OREN_NAYAR
material.albedo = 2.0;
#endif
#ifdef SPECULAR_GAUSSIAN
material.shininess = 0.2;
#endif
#ifdef SPECULAR_COOK_TORRANCE
material.fresnel = 0.1;
#endif
vec3 viewDirectionWorld = normalize(system.cameraPosition - vPositionWorld);
vec3 viewDirectionView = normalize(-vPositionView);
// Directional
Light directionnalLight;
directionnalLight.position = vec3(-1.0, 0.0, -1.0);
// directionnalLight.position.x = 2.0 * cos(system.time);
// directionnalLight.position.z = 2.0 * sin(system.time);
directionnalLight.color = vec4(0.2, 0.1, 0.1, 1.0);
vec3 positionToLight = -directionnalLight.position;
color += getDirectShading(material, directionnalLight, normalWorld, viewDirectionWorld, positionToLight, false, false);
// View space
// vec3 lightPositionView = (system.viewMatrix * vec4(directionnalLight.position, 1.0)).xyz;
// directionnalLight.position = lightPositionView - vPositionView;
// vec3 positionToLight = -directionnalLight.position;
// color += getDirectShading(material, light, normalView, viewDirectionView, positionToLight, false, false);
// Point
Light pointLight;
pointLight.position = vec3(0.0, 0.0, 0.0);
// pointLight.position.x = 2.0 * cos(system.time);
// pointLight.position.z = 2.0 * sin(system.time);
pointLight.color = vec4(0.1, 0.1, 0.2, 2.5);
pointLight.range = 2.5;
vec3 positionToPointLight = pointLight.position - vPositionWorld;
color += getDirectShading(
material,
pointLight,
normalWorld,
viewDirectionWorld,
positionToPointLight,
true,
false
);
// Spot
Light spotLight;
spotLight.position = vec3(0.0, 1.0, 0.0);
spotLight.direction = vec3(0.0, -1.0, 0.0);
spotLight.color = vec4(0.1, 0.2, 0.1, 2.5);
spotLight.range = 2.5;
spotLight.angle = PI / 4.0;
spotLight.innerAngle = PI / 5.0;
vec3 positionToSpotLight = spotLight.position - vPositionWorld;
color += getDirectShading(
material,
spotLight,
normalWorld,
viewDirectionWorld,
positionToSpotLight,
true,
true
);
outColor = vec4(color, 1.0);
`,
},
});
// Command
const clearCommand = new Command({
pass: new Pass(
"render",
[new Attachment({ r: 0.07, g: 0.07, b: 0.07, a: 1 })],
new Attachment(1)
),
});
const drawGeometryCommand = new Command({
pipeline,
bindGroups: [systemUniformBindGroup, meshUniformBindGroup],
vertexBuffers: [geometryVertexBuffer],
indexBuffer: geometryIndicesBuffer,
count: geometry.cells.length,
});
// Helpers
// const axes = new Axes(systemBindGroupLayout, systemUniformBindGroup);
// Frame
requestAnimationFrame(function frame() {
if (State.error) return;
clock.getDelta();
controls.update();
camera.position = controls.position;
camera.target = controls.target;
camera.update();
mat4.identity(modelMatrix);
mat4.rotateX(modelMatrix, modelMatrix, clock.time * 0.25);
mat4.rotateY(modelMatrix, modelMatrix, clock.time);
mat4.rotateZ(modelMatrix, modelMatrix, clock.time * 0.75);
systemUniformsBuffer.setSubData(
0,
typedArrayConcat(
Float32Array,
camera.projectionMatrix,
camera.viewMatrix,
camera.inverseViewMatrix,
camera.position,
Float32Array.of(clock.time)
)
);
mat4.multiply(normalMatrix, camera.viewMatrix, modelMatrix);
mat4.invert(normalMatrix, normalMatrix);
mat4.transpose(normalMatrix, normalMatrix);
// TODO: alignment seems to be wrong, using multiple of 4 bytes instead
// mat3.fromMat4(mat3.create(), normalMatrix)
meshUniformsBuffer.setSubData(
0,
typedArrayConcat(Float32Array, modelMatrix, normalMatrix)
);
context.render(() => {
context.submit(clearCommand, () => {
// context.submit(axes.command);
context.submit(drawGeometryCommand);
});
});
requestAnimationFrame(frame);
});
clock.start();
window.addEventListener("resize", onResize);
// GUI
const pane = new Pane({ title: "Parameters" });
pane.addButton({ title: "Use GLSL (see in console)" }).on("click", (event) => {
Object.assign(pipeline, pipeline.glsl);
pipeline.init();
});