@woosh/meep-engine 2.33.60 → 2.33.64

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (89) hide show
  1. package/core/geom/3d/SurfacePoint3.js +44 -5
  2. package/core/geom/Vector2.d.ts +2 -0
  3. package/core/geom/packing/MaxRectangles.js +13 -4
  4. package/core/math/hash/murmur3_32.js +3 -3
  5. package/ecs/grid/pick.js +44 -0
  6. package/editor/actions/concrete/PaintTerrainOverlayAction.js +1 -1
  7. package/editor/process/ObstacleGridDisplayProcess.js +1 -1
  8. package/editor/process/symbolic/makeGridPositionSymbolDisplay.js +1 -1
  9. package/editor/tools/FoliagePaintTool.js +1 -1
  10. package/editor/tools/GridPaintTool.js +2 -2
  11. package/editor/tools/paint/TerrainPaintTool.js +2 -2
  12. package/editor/tools/paint/TerrainTexturePaintTool.js +1 -1
  13. package/editor/view/EditorView.js +1 -1
  14. package/editor/view/GridPickCoordinateView.js +2 -2
  15. package/engine/asset/loaders/material/computeTextureHash.js +7 -13
  16. package/engine/ecs/animation/InverseKinematicsSystem.js +1 -1
  17. package/engine/ecs/attachment/AttachmentSystem.js +9 -56
  18. package/engine/ecs/evaluation/world/ReadTerrainLayerWeightPointFitness.js +1 -1
  19. package/engine/ecs/fow/FogOfWarRevealerSystem.js +2 -2
  20. package/engine/ecs/grid/HeightMap2NormalMap.js +1 -1
  21. package/engine/ecs/grid/NormalMap2AOMap.js +1 -1
  22. package/engine/ecs/gui/position/ViewportPositionSystem.js +5 -5
  23. package/engine/ecs/parent/EntityNode.d.ts +20 -0
  24. package/engine/ecs/parent/EntityNode.js +284 -0
  25. package/engine/ecs/parent/FaceEditor.js +137 -0
  26. package/engine/ecs/parent/testCompositionEntityNode.js +153 -0
  27. package/engine/ecs/parent/testHuDPerf.js +123 -0
  28. package/engine/ecs/team/Team.js +82 -0
  29. package/engine/ecs/terrain/ecs/ClingToTerrainSystem.js +2 -2
  30. package/engine/ecs/terrain/ecs/Terrain.js +14 -5
  31. package/engine/ecs/terrain/util/loadVisibleTerrainTiles.js +112 -0
  32. package/engine/ecs/terrain/util/obtainTerrain.js +23 -0
  33. package/engine/ecs/tooltip/TooltipComponentSystem.js +1 -1
  34. package/engine/graphics/GraphicsEngine.js +1 -2
  35. package/engine/graphics/camera/testClippingPlaneComputation.js +0 -4
  36. package/engine/graphics/ecs/camera/topdown/TopDownCameraLanderSystem.js +1 -1
  37. package/engine/graphics/ecs/light/LightSystem.js +1 -1
  38. package/engine/graphics/ecs/mesh-v2/ShadedGeometry.js +29 -0
  39. package/engine/graphics/ecs/mesh-v2/ShadedGeometrySystem.js +158 -1
  40. package/engine/graphics/ecs/path/testPathDisplaySystem.js +0 -2
  41. package/engine/graphics/ecs/water/WaterSystem.js +1 -1
  42. package/engine/graphics/ecs/water2/shader/testWaterShader.js +1 -1
  43. package/engine/graphics/geometry/AttributeSpec.d.ts +8 -0
  44. package/engine/graphics/geometry/VertexDataSpec.d.ts +15 -0
  45. package/engine/graphics/geometry/buffered/deinterleaveBufferAttribute.js +39 -0
  46. package/engine/graphics/geometry/buffered/deinterleaveBufferGeometry.js +50 -0
  47. package/engine/graphics/geometry/buffered/query/GeometrySpatialQueryAccelerator.js +55 -11
  48. package/engine/graphics/micron/build/buildMicronGeometryFromBufferGeometry.js +10 -7
  49. package/engine/graphics/micron/format/MicronGeometry.d.ts +4 -0
  50. package/engine/graphics/micron/render/v1/MaterialContext.js +1 -0
  51. package/engine/graphics/particles/particular/engine/simulator/SimulationStepFixedPhysics.js +19 -3
  52. package/engine/graphics/particles/particular/engine/utils/volume/prototypeParticleVolume.js +2 -4
  53. package/engine/graphics/render/buffer/buffers/prototypeNormalFrameBuffer.js +0 -2
  54. package/engine/graphics/render/forward_plus/plugin/ptototypeFPPlugin.js +2 -4
  55. package/engine/graphics/render/frame_graph/FrameGraphBuilder.js +36 -0
  56. package/engine/graphics/render/frame_graph/IRenderContext.js +8 -0
  57. package/engine/graphics/render/frame_graph/RenderPass.js +32 -0
  58. package/engine/graphics/render/frame_graph/RenderPassFlags.js +6 -0
  59. package/engine/graphics/render/frame_graph/RenderPassResources.js +15 -0
  60. package/engine/graphics/render/frame_graph/Resource.js +21 -0
  61. package/engine/graphics/render/frame_graph/ResourceReference.js +14 -0
  62. package/engine/graphics/render/frame_graph/TextureDescriptor.js +30 -0
  63. package/engine/graphics/render/frame_graph/UsageModeType.js +5 -0
  64. package/engine/graphics/render/frame_graph/sample/deferred/GBufferDrawPass.js +27 -0
  65. package/engine/graphics/render/frame_graph/sample/deferred/PresentPass.js +17 -0
  66. package/engine/graphics/render/visibility/hiz/prototypeHiZ.js +0 -2
  67. package/engine/graphics/render/webgpu/sample/MeshInstance.js +108 -0
  68. package/engine/graphics/render/webgpu/sample/fragmentDeferredRendering.wgsl +71 -0
  69. package/engine/graphics/render/webgpu/sample/fragmentGBuffersDebugView.wgsl +39 -0
  70. package/engine/graphics/render/webgpu/sample/fragmentWriteGBuffers.wgsl +21 -0
  71. package/engine/graphics/render/webgpu/sample/lightUpdate.wgsl +41 -0
  72. package/engine/graphics/render/webgpu/sample/main.js +604 -0
  73. package/engine/graphics/render/webgpu/sample/vertexTextureQuad.wgsl +9 -0
  74. package/engine/graphics/render/webgpu/sample/vertexWriteGBuffers.wgsl +30 -0
  75. package/engine/graphics/shadows/testShadowMapRendering.js +0 -2
  76. package/engine/{ecs/grid → graphics/texture/sampler}/sampler2DToFloat32Texture.js +1 -1
  77. package/engine/graphics/util/modelHitTest.js +63 -0
  78. package/engine/grid/systems/GridPosition2TransformSystem.js +1 -1
  79. package/engine/grid/systems/ViewportGridProjectionSystem.js +1 -1
  80. package/engine/grid/transform2grid/Transform2GridPositionSystem.js +1 -1
  81. package/engine/scene/transitionToScene.js +1 -1
  82. package/generation/grid/actions/ContinuousGridCellActionSetTerrainHeight.js +1 -1
  83. package/generation/markers/actions/placement/MarkerNodeEntityProcessorClingToTerrain.js +1 -1
  84. package/generation/markers/debug/visualizeMarkers.js +1 -1
  85. package/generation/theme/ThemeEngine.js +1 -1
  86. package/package.json +1 -1
  87. package/samples/terrain/main.js +117 -0
  88. package/view/minimap/Minimap.js +1 -1
  89. package/engine/ecs/sockets/AttachmentSocketsSystem.js +0 -10
@@ -0,0 +1,604 @@
1
+ import { mat4, vec3, vec4 } from 'gl-matrix';
2
+ import lightUpdate from './lightUpdate.wgsl';
3
+ import vertexWriteGBuffers from './vertexWriteGBuffers.wgsl';
4
+ import fragmentWriteGBuffers from './fragmentWriteGBuffers.wgsl';
5
+ import vertexTextureQuad from './vertexTextureQuad.wgsl';
6
+ import fragmentGBuffersDebugView from './fragmentGBuffersDebugView.wgsl';
7
+ import fragmentDeferredRendering from './fragmentDeferredRendering.wgsl';
8
+ import dat from "dat.gui";
9
+ import { PLYLoader } from "three/examples/jsm/loaders/PLYLoader.js";
10
+ import { noop } from "../../../../../core/function/Functions.js";
11
+ import { GLTFLoader } from "three/examples/jsm/loaders/GLTFLoader.js";
12
+ import { MeshInstance } from "./MeshInstance.js";
13
+
14
+ const kMaxNumLights = 1024;
15
+ const lightExtentMin = vec3.fromValues(-50, -30, -50);
16
+ const lightExtentMax = vec3.fromValues(50, 50, 50);
17
+
18
+ /**
19
+ *
20
+ * @param {string} path
21
+ * @return {Promise<THREE.BufferGeometry>}
22
+ */
23
+ function load_geometry(path) {
24
+ const loader = new PLYLoader();
25
+
26
+ return new Promise((resolve, reject) => {
27
+
28
+ loader.load(path, resolve, noop, reject);
29
+ });
30
+ }
31
+
32
+ function load_geometry_gltf(path) {
33
+ const loader = new GLTFLoader();
34
+
35
+ return new Promise((resolve, reject) => {
36
+ loader.load(path, (gltf) => {
37
+ let r = null;
38
+
39
+ gltf.scene.traverse(o => {
40
+ if (o.isMesh) {
41
+ r = o.geometry;
42
+ }
43
+ });
44
+
45
+ resolve(r);
46
+
47
+ }, noop, reject);
48
+ });
49
+ }
50
+
51
+ const init = async ({ canvasRef, gui }) => {
52
+
53
+ // const mesh = await load_geometry("data/models/stanford/dragon_recon/dragon_vrip.ply");
54
+ const mesh = await load_geometry_gltf("data/models/stanford/DragonAttenuation.glb");
55
+
56
+
57
+ const adapter = await navigator.gpu.requestAdapter();
58
+ const device = await adapter.requestDevice();
59
+
60
+ /**
61
+ *
62
+ * @type {HTMLCanvasElement}
63
+ */
64
+ const canvas = canvasRef.current;
65
+
66
+ if (canvas === null) {
67
+ return;
68
+ }
69
+
70
+ /**
71
+ *
72
+ * @type {GPUCanvasContext}
73
+ */
74
+ const context = canvas.getContext('webgpu');
75
+
76
+ const devicePixelRatio = window.devicePixelRatio || 1;
77
+
78
+ const presentationSize = [
79
+ canvas.clientWidth * devicePixelRatio,
80
+ canvas.clientHeight * devicePixelRatio,
81
+ ];
82
+
83
+ const aspect = presentationSize[0] / presentationSize[1];
84
+
85
+ const presentationFormat = context.getPreferredFormat(adapter);
86
+
87
+ context.configure({
88
+ device,
89
+ format: presentationFormat,
90
+ size: presentationSize,
91
+ });
92
+
93
+ const m_instance = new MeshInstance();
94
+ m_instance.geometry = mesh;
95
+ m_instance.init(device);
96
+
97
+ // GBuffer texture render targets
98
+ const gBufferTexture2DFloat = device.createTexture({
99
+ size: [...presentationSize, 3],
100
+ usage: GPUTextureUsage.RENDER_ATTACHMENT | GPUTextureUsage.TEXTURE_BINDING,
101
+ format: 'rgba32float',
102
+ });
103
+ const gBufferTextureAlbedo = device.createTexture({
104
+ size: presentationSize,
105
+ usage: GPUTextureUsage.RENDER_ATTACHMENT | GPUTextureUsage.TEXTURE_BINDING,
106
+ format: 'bgra8unorm',
107
+ });
108
+ const gBufferTextureViews = [
109
+ gBufferTexture2DFloat.createView({ baseArrayLayer: 0, arrayLayerCount: 1 }),
110
+ gBufferTexture2DFloat.createView({ baseArrayLayer: 1, arrayLayerCount: 1 }),
111
+ gBufferTextureAlbedo.createView(),
112
+ ];
113
+ const vertexBuffers = [
114
+ {
115
+ arrayStride: Float32Array.BYTES_PER_ELEMENT * 8,
116
+ attributes: [
117
+ {
118
+ // position
119
+ shaderLocation: 0,
120
+ offset: 0,
121
+ format: 'float32x3',
122
+ },
123
+ {
124
+ // normal
125
+ shaderLocation: 1,
126
+ offset: Float32Array.BYTES_PER_ELEMENT * 3,
127
+ format: 'float32x3',
128
+ },
129
+ {
130
+ // uv
131
+ shaderLocation: 2,
132
+ offset: Float32Array.BYTES_PER_ELEMENT * 6,
133
+ format: 'float32x2',
134
+ },
135
+ ],
136
+ },
137
+ ];
138
+ const primitive = {
139
+ topology: 'triangle-list',
140
+ cullMode: 'back',
141
+ };
142
+ const writeGBuffersPipeline = device.createRenderPipeline({
143
+ vertex: {
144
+ module: device.createShaderModule({
145
+ code: vertexWriteGBuffers,
146
+ }),
147
+ entryPoint: 'main',
148
+ buffers: vertexBuffers,
149
+ },
150
+ fragment: {
151
+ module: device.createShaderModule({
152
+ code: fragmentWriteGBuffers,
153
+ }),
154
+ entryPoint: 'main',
155
+ targets: [
156
+ // position
157
+ { format: 'rgba32float' },
158
+ // normal
159
+ { format: 'rgba32float' },
160
+ // albedo
161
+ { format: 'bgra8unorm' },
162
+ ],
163
+ },
164
+ depthStencil: {
165
+ depthWriteEnabled: true,
166
+ depthCompare: 'less',
167
+ format: 'depth24plus',
168
+ },
169
+ primitive,
170
+ });
171
+ const gBufferTexturesBindGroupLayout = device.createBindGroupLayout({
172
+ entries: [
173
+ {
174
+ binding: 0,
175
+ visibility: GPUShaderStage.FRAGMENT,
176
+ texture: {
177
+ sampleType: 'unfilterable-float',
178
+ },
179
+ },
180
+ {
181
+ binding: 1,
182
+ visibility: GPUShaderStage.FRAGMENT,
183
+ texture: {
184
+ sampleType: 'unfilterable-float',
185
+ },
186
+ },
187
+ {
188
+ binding: 2,
189
+ visibility: GPUShaderStage.FRAGMENT,
190
+ texture: {
191
+ sampleType: 'unfilterable-float',
192
+ },
193
+ },
194
+ ],
195
+ });
196
+ const lightsBufferBindGroupLayout = device.createBindGroupLayout({
197
+ entries: [
198
+ {
199
+ binding: 0,
200
+ visibility: GPUShaderStage.FRAGMENT | GPUShaderStage.COMPUTE,
201
+ buffer: {
202
+ type: 'storage',
203
+ },
204
+ },
205
+ {
206
+ binding: 1,
207
+ visibility: GPUShaderStage.FRAGMENT | GPUShaderStage.COMPUTE,
208
+ buffer: {
209
+ type: 'uniform',
210
+ },
211
+ },
212
+ ],
213
+ });
214
+ const canvasSizeUniformBindGroupLayout = device.createBindGroupLayout({
215
+ entries: [
216
+ {
217
+ binding: 0,
218
+ visibility: GPUShaderStage.FRAGMENT,
219
+ buffer: {
220
+ type: 'uniform',
221
+ },
222
+ },
223
+ ],
224
+ });
225
+ const gBuffersDebugViewPipeline = device.createRenderPipeline({
226
+ layout: device.createPipelineLayout({
227
+ bindGroupLayouts: [
228
+ gBufferTexturesBindGroupLayout,
229
+ canvasSizeUniformBindGroupLayout,
230
+ ],
231
+ }),
232
+ vertex: {
233
+ module: device.createShaderModule({
234
+ code: vertexTextureQuad,
235
+ }),
236
+ entryPoint: 'main',
237
+ },
238
+ fragment: {
239
+ module: device.createShaderModule({
240
+ code: fragmentGBuffersDebugView,
241
+ }),
242
+ entryPoint: 'main',
243
+ targets: [
244
+ {
245
+ format: presentationFormat,
246
+ },
247
+ ],
248
+ },
249
+ primitive,
250
+ });
251
+ const deferredRenderPipeline = device.createRenderPipeline({
252
+ layout: device.createPipelineLayout({
253
+ bindGroupLayouts: [
254
+ gBufferTexturesBindGroupLayout,
255
+ lightsBufferBindGroupLayout,
256
+ canvasSizeUniformBindGroupLayout,
257
+ ],
258
+ }),
259
+ vertex: {
260
+ module: device.createShaderModule({
261
+ code: vertexTextureQuad,
262
+ }),
263
+ entryPoint: 'main',
264
+ },
265
+ fragment: {
266
+ module: device.createShaderModule({
267
+ code: fragmentDeferredRendering,
268
+ }),
269
+ entryPoint: 'main',
270
+ targets: [
271
+ {
272
+ format: presentationFormat,
273
+ },
274
+ ],
275
+ },
276
+ primitive,
277
+ });
278
+ const depthTexture = device.createTexture({
279
+ size: presentationSize,
280
+ format: 'depth24plus',
281
+ usage: GPUTextureUsage.RENDER_ATTACHMENT,
282
+ });
283
+ const writeGBufferPassDescriptor = {
284
+ colorAttachments: [
285
+ {
286
+ view: gBufferTextureViews[0],
287
+ loadValue: {
288
+ r: Number.MAX_VALUE,
289
+ g: Number.MAX_VALUE,
290
+ b: Number.MAX_VALUE,
291
+ a: 1.0,
292
+ },
293
+ storeOp: 'store',
294
+ },
295
+ {
296
+ view: gBufferTextureViews[1],
297
+ loadValue: { r: 0.0, g: 0.0, b: 1.0, a: 1.0 },
298
+ storeOp: 'store',
299
+ },
300
+ {
301
+ view: gBufferTextureViews[2],
302
+ loadValue: { r: 0.0, g: 0.0, b: 0.0, a: 1.0 },
303
+ storeOp: 'store',
304
+ },
305
+ ],
306
+ depthStencilAttachment: {
307
+ view: depthTexture.createView(),
308
+ depthLoadValue: 1.0,
309
+ depthStoreOp: 'store',
310
+ stencilLoadValue: 0,
311
+ stencilStoreOp: 'store',
312
+ },
313
+ };
314
+ const textureQuadPassDescriptor = {
315
+ colorAttachments: [
316
+ {
317
+ // view is acquired and set in render loop.
318
+ view: undefined,
319
+ loadValue: { r: 0.0, g: 0.0, b: 0.0, a: 1.0 },
320
+ storeOp: 'store',
321
+ },
322
+ ],
323
+ };
324
+ const settings = {
325
+ mode: 'rendering',
326
+ numLights:1,
327
+ };
328
+ const configUniformBuffer = (() => {
329
+ const buffer = device.createBuffer({
330
+ size: Uint32Array.BYTES_PER_ELEMENT,
331
+ mappedAtCreation: true,
332
+ usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST,
333
+ });
334
+ new Uint32Array(buffer.getMappedRange())[0] = settings.numLights;
335
+ buffer.unmap();
336
+ return buffer;
337
+ })();
338
+ gui.add(settings, 'mode', ['rendering', 'gBuffers view']);
339
+ gui
340
+ .add(settings, 'numLights', 1, kMaxNumLights)
341
+ .step(1)
342
+ .onChange(() => {
343
+ device.queue.writeBuffer(configUniformBuffer, 0, new Uint32Array([settings.numLights]));
344
+ });
345
+ const modelUniformBuffer = device.createBuffer({
346
+ size: 4 * 16 * 2,
347
+ usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST,
348
+ });
349
+ const cameraUniformBuffer = device.createBuffer({
350
+ size: 4 * 16,
351
+ usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST,
352
+ });
353
+ const sceneUniformBindGroup = device.createBindGroup({
354
+ layout: writeGBuffersPipeline.getBindGroupLayout(0),
355
+ entries: [
356
+ {
357
+ binding: 0,
358
+ resource: {
359
+ buffer: modelUniformBuffer,
360
+ },
361
+ },
362
+ {
363
+ binding: 1,
364
+ resource: {
365
+ buffer: cameraUniformBuffer,
366
+ },
367
+ },
368
+ ],
369
+ });
370
+ const canvasSizeUniformBuffer = device.createBuffer({
371
+ size: 4 * 2,
372
+ usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST,
373
+ });
374
+ const canvasSizeUniformBindGroup = device.createBindGroup({
375
+ layout: canvasSizeUniformBindGroupLayout,
376
+ entries: [
377
+ {
378
+ binding: 0,
379
+ resource: {
380
+ buffer: canvasSizeUniformBuffer,
381
+ },
382
+ },
383
+ ],
384
+ });
385
+ const gBufferTexturesBindGroup = device.createBindGroup({
386
+ layout: gBufferTexturesBindGroupLayout,
387
+ entries: [
388
+ {
389
+ binding: 0,
390
+ resource: gBufferTextureViews[0],
391
+ },
392
+ {
393
+ binding: 1,
394
+ resource: gBufferTextureViews[1],
395
+ },
396
+ {
397
+ binding: 2,
398
+ resource: gBufferTextureViews[2],
399
+ },
400
+ ],
401
+ });
402
+ // Lights data are uploaded in a storage buffer
403
+ // which could be updated/culled/etc. with a compute shader
404
+ const extent = vec3.create();
405
+ vec3.sub(extent, lightExtentMax, lightExtentMin);
406
+ const lightDataStride = 8;
407
+ const bufferSizeInByte = Float32Array.BYTES_PER_ELEMENT * lightDataStride * kMaxNumLights;
408
+ const lightsBuffer = device.createBuffer({
409
+ size: bufferSizeInByte,
410
+ usage: GPUBufferUsage.STORAGE,
411
+ mappedAtCreation: true,
412
+ });
413
+ // We randomaly populate lights randomly in a box range
414
+ // And simply move them along y-axis per frame to show they are
415
+ // dynamic lightings
416
+ const lightData = new Float32Array(lightsBuffer.getMappedRange());
417
+ const tmpVec4 = vec4.create();
418
+ let offset = 0;
419
+ for (let i = 0; i < kMaxNumLights; i++) {
420
+ offset = lightDataStride * i;
421
+ // position
422
+ for (let i = 0; i < 3; i++) {
423
+ tmpVec4[i] = Math.random() * extent[i] + lightExtentMin[i];
424
+ }
425
+ tmpVec4[3] = 1;
426
+ lightData.set(tmpVec4, offset);
427
+ // color
428
+ tmpVec4[0] = Math.random() * 2;
429
+ tmpVec4[1] = Math.random() * 2;
430
+ tmpVec4[2] = Math.random() * 2;
431
+ // radius
432
+ tmpVec4[3] = 20.0;
433
+ lightData.set(tmpVec4, offset + 4);
434
+ }
435
+ lightsBuffer.unmap();
436
+ const lightExtentBuffer = device.createBuffer({
437
+ size: 4 * 8,
438
+ usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST,
439
+ });
440
+ const lightExtentData = new Float32Array(8);
441
+ lightExtentData.set(lightExtentMin, 0);
442
+ lightExtentData.set(lightExtentMax, 4);
443
+ device.queue.writeBuffer(lightExtentBuffer, 0, lightExtentData.buffer, lightExtentData.byteOffset, lightExtentData.byteLength);
444
+ const lightUpdateComputePipeline = device.createComputePipeline({
445
+ compute: {
446
+ module: device.createShaderModule({
447
+ code: lightUpdate,
448
+ }),
449
+ entryPoint: 'main',
450
+ },
451
+ });
452
+ const lightsBufferBindGroup = device.createBindGroup({
453
+ layout: lightsBufferBindGroupLayout,
454
+ entries: [
455
+ {
456
+ binding: 0,
457
+ resource: {
458
+ buffer: lightsBuffer,
459
+ },
460
+ },
461
+ {
462
+ binding: 1,
463
+ resource: {
464
+ buffer: configUniformBuffer,
465
+ },
466
+ },
467
+ ],
468
+ });
469
+ const lightsBufferComputeBindGroup = device.createBindGroup({
470
+ layout: lightUpdateComputePipeline.getBindGroupLayout(0),
471
+ entries: [
472
+ {
473
+ binding: 0,
474
+ resource: {
475
+ buffer: lightsBuffer,
476
+ },
477
+ },
478
+ {
479
+ binding: 1,
480
+ resource: {
481
+ buffer: configUniformBuffer,
482
+ },
483
+ },
484
+ {
485
+ binding: 2,
486
+ resource: {
487
+ buffer: lightExtentBuffer,
488
+ },
489
+ },
490
+ ],
491
+ });
492
+ //--------------------
493
+ // Scene matrices
494
+ const eyePosition = vec3.fromValues(0, 50, -100);
495
+ const upVector = vec3.fromValues(0, 1, 0);
496
+ const origin = vec3.fromValues(0, 0, 0);
497
+ const projectionMatrix = mat4.create();
498
+ mat4.perspective(projectionMatrix, (2 * Math.PI) / 5, aspect, 1, 2000.0);
499
+ const viewMatrix = mat4.create();
500
+ mat4.lookAt(viewMatrix, eyePosition, origin, upVector);
501
+ const viewProjMatrix = mat4.create();
502
+ mat4.multiply(viewProjMatrix, projectionMatrix, viewMatrix);
503
+ // Move the model so it's centered.
504
+ const modelMatrix = mat4.create();
505
+ mat4.translate(modelMatrix, modelMatrix, vec3.fromValues(0, -5, 0));
506
+ mat4.translate(modelMatrix, modelMatrix, vec3.fromValues(0, -40, 0));
507
+ const cameraMatrixData = viewProjMatrix;
508
+ device.queue.writeBuffer(cameraUniformBuffer, 0, cameraMatrixData.buffer, cameraMatrixData.byteOffset, cameraMatrixData.byteLength);
509
+ const modelData = modelMatrix;
510
+ device.queue.writeBuffer(modelUniformBuffer, 0, modelData.buffer, modelData.byteOffset, modelData.byteLength);
511
+ const invertTransposeModelMatrix = mat4.create();
512
+ mat4.invert(invertTransposeModelMatrix, modelMatrix);
513
+ mat4.transpose(invertTransposeModelMatrix, invertTransposeModelMatrix);
514
+ const normalModelData = invertTransposeModelMatrix;
515
+ device.queue.writeBuffer(modelUniformBuffer, 64, normalModelData.buffer, normalModelData.byteOffset, normalModelData.byteLength);
516
+ // Pass the canvas size to shader to help sample from gBuffer textures using coord
517
+ const canvasSizeData = new Float32Array(presentationSize);
518
+ device.queue.writeBuffer(canvasSizeUniformBuffer, 0, canvasSizeData.buffer, canvasSizeData.byteOffset, canvasSizeData.byteLength);
519
+
520
+ // Rotates the camera around the origin based on time.
521
+ function getCameraViewProjMatrix() {
522
+ const eyePosition = vec3.fromValues(0, 50, -10);
523
+ const rad = Math.PI * (Date.now() / 5000);
524
+ vec3.rotateY(eyePosition, eyePosition, origin, rad);
525
+ const viewMatrix = mat4.create();
526
+ mat4.lookAt(viewMatrix, eyePosition, origin, upVector);
527
+ mat4.multiply(viewProjMatrix, projectionMatrix, viewMatrix);
528
+ return viewProjMatrix;
529
+ }
530
+
531
+ function frame() {
532
+ // Sample is no longer the active page.
533
+ if (!canvas)
534
+ return;
535
+ const cameraViewProj = getCameraViewProjMatrix();
536
+ device.queue.writeBuffer(cameraUniformBuffer, 0, cameraViewProj.buffer, cameraViewProj.byteOffset, cameraViewProj.byteLength);
537
+ const commandEncoder = device.createCommandEncoder();
538
+ {
539
+ // Write position, normal, albedo etc. data to gBuffers
540
+ const gBufferPass = commandEncoder.beginRenderPass(writeGBufferPassDescriptor);
541
+ gBufferPass.setPipeline(writeGBuffersPipeline);
542
+ gBufferPass.setBindGroup(0, sceneUniformBindGroup);
543
+
544
+ m_instance.draw(gBufferPass);
545
+
546
+ gBufferPass.endPass();
547
+ }
548
+ {
549
+ // Update lights position
550
+ const lightPass = commandEncoder.beginComputePass();
551
+ lightPass.setPipeline(lightUpdateComputePipeline);
552
+ lightPass.setBindGroup(0, lightsBufferComputeBindGroup);
553
+ lightPass.dispatch(Math.ceil(kMaxNumLights / 64));
554
+ lightPass.endPass();
555
+ }
556
+ {
557
+ if (settings.mode === 'gBuffers view') {
558
+ // GBuffers debug view
559
+ // Left: position
560
+ // Middle: normal
561
+ // Right: albedo (use uv to mimic a checkerboard texture)
562
+ textureQuadPassDescriptor.colorAttachments[0].view = context
563
+ .getCurrentTexture()
564
+ .createView();
565
+ const debugViewPass = commandEncoder.beginRenderPass(textureQuadPassDescriptor);
566
+ debugViewPass.setPipeline(gBuffersDebugViewPipeline);
567
+ debugViewPass.setBindGroup(0, gBufferTexturesBindGroup);
568
+ debugViewPass.setBindGroup(1, canvasSizeUniformBindGroup);
569
+ debugViewPass.draw(6);
570
+ debugViewPass.endPass();
571
+ } else {
572
+ // Deferred rendering
573
+ textureQuadPassDescriptor.colorAttachments[0].view = context
574
+ .getCurrentTexture()
575
+ .createView();
576
+ const deferredRenderingPass = commandEncoder.beginRenderPass(textureQuadPassDescriptor);
577
+ deferredRenderingPass.setPipeline(deferredRenderPipeline);
578
+ deferredRenderingPass.setBindGroup(0, gBufferTexturesBindGroup);
579
+ deferredRenderingPass.setBindGroup(1, lightsBufferBindGroup);
580
+ deferredRenderingPass.setBindGroup(2, canvasSizeUniformBindGroup);
581
+ deferredRenderingPass.draw(6);
582
+ deferredRenderingPass.endPass();
583
+ }
584
+ }
585
+ device.queue.submit([commandEncoder.finish()]);
586
+ requestAnimationFrame(frame);
587
+ }
588
+
589
+ requestAnimationFrame(frame);
590
+ };
591
+
592
+ const canvas_el = document.createElement('canvas');
593
+ canvas_el.width = window.innerWidth;
594
+ canvas_el.height = window.innerHeight;
595
+
596
+ document.body.appendChild(canvas_el);
597
+
598
+ const gui = new dat.GUI();
599
+ init({
600
+ canvasRef: {
601
+ current: canvas_el,
602
+ },
603
+ gui: gui
604
+ });
@@ -0,0 +1,9 @@
1
+ [[stage(vertex)]]
2
+ fn main([[builtin(vertex_index)]] VertexIndex : u32)
3
+ -> [[builtin(position)]] vec4<f32> {
4
+ var pos = array<vec2<f32>, 6>(
5
+ vec2<f32>(-1.0, -1.0), vec2<f32>(1.0, -1.0), vec2<f32>(-1.0, 1.0),
6
+ vec2<f32>(-1.0, 1.0), vec2<f32>(1.0, -1.0), vec2<f32>(1.0, 1.0));
7
+
8
+ return vec4<f32>(pos[VertexIndex], 0.0, 1.0);
9
+ }
@@ -0,0 +1,30 @@
1
+ struct Uniforms {
2
+ modelMatrix : mat4x4<f32>;
3
+ normalModelMatrix : mat4x4<f32>;
4
+ };
5
+
6
+ struct Camera {
7
+ viewProjectionMatrix : mat4x4<f32>;
8
+ };
9
+
10
+ [[group(0), binding(0)]] var<uniform> uniforms : Uniforms;
11
+ [[group(0), binding(1)]] var<uniform> camera : Camera;
12
+
13
+ struct VertexOutput {
14
+ [[builtin(position)]] Position : vec4<f32>;
15
+ [[location(0)]] fragPosition: vec3<f32>; // position in world space
16
+ [[location(1)]] fragNormal: vec3<f32>; // normal in world space
17
+ [[location(2)]] fragUV: vec2<f32>;
18
+ };
19
+
20
+ [[stage(vertex)]]
21
+ fn main([[location(0)]] position : vec3<f32>,
22
+ [[location(1)]] normal : vec3<f32>,
23
+ [[location(2)]] uv : vec2<f32>) -> VertexOutput {
24
+ var output : VertexOutput;
25
+ output.fragPosition = (uniforms.modelMatrix * vec4<f32>(position, 1.0)).xyz;
26
+ output.Position = camera.viewProjectionMatrix * vec4<f32>(output.fragPosition, 1.0);
27
+ output.fragNormal = normalize((uniforms.normalModelMatrix * vec4<f32>(normal, 1.0)).xyz);
28
+ output.fragUV = uv;
29
+ return output;
30
+ }
@@ -5,7 +5,6 @@ import { MeshSystem } from "../ecs/mesh/MeshSystem.js";
5
5
  import GUIElementSystem from "../../ecs/gui/GUIElementSystem.js";
6
6
  import HeadsUpDisplaySystem from "../../ecs/gui/hud/HeadsUpDisplaySystem.js";
7
7
  import TagSystem from "../../ecs/systems/TagSystem.js";
8
- import { AttachmentSocketsSystem } from "../../ecs/sockets/AttachmentSocketsSystem.js";
9
8
  import { AttachmentSystem } from "../../ecs/attachment/AttachmentSystem.js";
10
9
  import { SoundEmitterSystem } from "../../sound/ecs/emitter/SoundEmitterSystem.js";
11
10
  import SoundControllerSystem from "../../sound/ecs/SoundControllerSystem.js";
@@ -72,7 +71,6 @@ function makeConfig(engine) {
72
71
 
73
72
  config.addManySystems(
74
73
  new TagSystem(),
75
- new AttachmentSocketsSystem(),
76
74
  new AttachmentSystem(),
77
75
  new SoundEmitterSystem(assetManager, sound.destination, sound.context),
78
76
  new SoundControllerSystem(),
@@ -1,5 +1,5 @@
1
1
  import { ClampToEdgeWrapping, DataTexture, FloatType, LinearFilter, NearestFilter, RedFormat } from "three";
2
- import { assert } from "../../../core/assert.js";
2
+ import { assert } from "../../../../core/assert.js";
3
3
 
4
4
  /**
5
5
  *