@woosh/meep-engine 2.33.61 → 2.33.65
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.
- package/core/FilePath.js +28 -2
- package/core/binary/BinaryBuffer.js +13 -0
- package/ecs/grid/pick.js +44 -0
- package/editor/actions/concrete/PaintTerrainOverlayAction.js +1 -1
- package/editor/process/ObstacleGridDisplayProcess.js +1 -1
- package/editor/process/symbolic/makeGridPositionSymbolDisplay.js +1 -1
- package/editor/tools/FoliagePaintTool.js +1 -1
- package/editor/tools/GridPaintTool.js +2 -2
- package/editor/tools/paint/TerrainPaintTool.js +2 -2
- package/editor/tools/paint/TerrainTexturePaintTool.js +1 -1
- package/editor/view/EditorView.js +1 -1
- package/editor/view/GridPickCoordinateView.js +2 -2
- package/engine/asset/AssetManager.js +1 -1
- package/engine/asset/AssetTransformer.js +2 -1
- package/engine/asset/loaders/GLTFAssetLoader.js +5 -21
- package/engine/asset/loaders/async_traverse_three_object.js +24 -0
- package/engine/ecs/animation/InverseKinematicsSystem.js +1 -1
- package/engine/ecs/attachment/AttachmentSystem.js +3 -51
- package/engine/ecs/evaluation/world/ReadTerrainLayerWeightPointFitness.js +1 -1
- package/engine/ecs/fow/FogOfWarRevealerSystem.js +2 -2
- package/engine/ecs/team/Team.js +82 -0
- package/engine/ecs/terrain/ecs/ClingToTerrainSystem.js +2 -2
- package/engine/ecs/terrain/util/loadVisibleTerrainTiles.js +112 -0
- package/engine/ecs/terrain/util/obtainTerrain.js +23 -0
- package/engine/ecs/tooltip/TooltipComponentSystem.js +1 -1
- package/engine/graphics/ecs/camera/topdown/TopDownCameraLanderSystem.js +1 -1
- package/engine/graphics/ecs/water/WaterSystem.js +1 -1
- package/engine/graphics/ecs/water2/shader/testWaterShader.js +1 -1
- package/engine/graphics/geometry/AttributeSpec.d.ts +8 -0
- package/engine/graphics/geometry/VertexDataSpec.d.ts +15 -0
- package/engine/graphics/geometry/buffered/deinterleaveBufferAttribute.js +39 -0
- package/engine/graphics/geometry/buffered/deinterleaveBufferGeometry.js +50 -0
- package/engine/graphics/micron/MICRON_GEOMETRY_FIELD.js +1 -0
- package/engine/graphics/micron/MICRON_URI_FIELD.js +1 -0
- package/engine/graphics/micron/build/buildMicronGeometryFromBufferGeometry.js +10 -7
- package/engine/graphics/micron/convert_three_object_to_micron.js +17 -2
- package/engine/graphics/micron/format/MicronGeometry.d.ts +4 -0
- package/engine/graphics/micron/format/MicronGeometry.js +6 -0
- package/engine/graphics/micron/plugin/MicronRenderPlugin.js +107 -6
- package/engine/graphics/particles/particular/engine/utils/volume/prototypeParticleVolume.js +5 -3
- package/engine/graphics/render/forward_plus/plugin/ptototypeFPPlugin.js +2 -2
- package/engine/graphics/render/webgpu/sample/MeshInstance.js +108 -0
- package/engine/graphics/render/webgpu/sample/fragmentDeferredRendering.wgsl +7 -3
- package/engine/graphics/render/webgpu/sample/fragmentGBuffersDebugView.wgsl +4 -3
- package/engine/graphics/render/webgpu/sample/lightUpdate.wgsl +13 -6
- package/engine/graphics/render/webgpu/sample/main.js +83 -45
- package/engine/graphics/render/webgpu/sample/vertexWriteGBuffers.wgsl +5 -3
- package/engine/graphics/util/modelHitTest.js +63 -0
- package/engine/grid/systems/GridPosition2TransformSystem.js +1 -1
- package/engine/grid/systems/ViewportGridProjectionSystem.js +1 -1
- package/engine/grid/transform2grid/Transform2GridPositionSystem.js +1 -1
- package/engine/scene/transitionToScene.js +1 -1
- package/generation/grid/actions/ContinuousGridCellActionSetTerrainHeight.js +1 -1
- package/generation/markers/actions/placement/MarkerNodeEntityProcessorClingToTerrain.js +1 -1
- package/generation/markers/debug/visualizeMarkers.js +1 -1
- package/generation/theme/ThemeEngine.js +1 -1
- package/package.json +1 -1
- package/samples/terrain/main.js +1 -1
- package/view/minimap/Minimap.js +1 -1
- package/engine/graphics/render/webgpu/sample/_main.ts +0 -650
|
@@ -1,650 +0,0 @@
|
|
|
1
|
-
import { mat4, vec3, vec4 } from 'gl-matrix';
|
|
2
|
-
|
|
3
|
-
import lightUpdate from './lightUpdate.wgsl';
|
|
4
|
-
import vertexWriteGBuffers from './vertexWriteGBuffers.wgsl';
|
|
5
|
-
import fragmentWriteGBuffers from './fragmentWriteGBuffers.wgsl';
|
|
6
|
-
import vertexTextureQuad from './vertexTextureQuad.wgsl';
|
|
7
|
-
import fragmentGBuffersDebugView from './fragmentGBuffersDebugView.wgsl';
|
|
8
|
-
import fragmentDeferredRendering from './fragmentDeferredRendering.wgsl';
|
|
9
|
-
|
|
10
|
-
const kMaxNumLights = 1024;
|
|
11
|
-
const lightExtentMin = vec3.fromValues(-50, -30, -50);
|
|
12
|
-
const lightExtentMax = vec3.fromValues(50, 50, 50);
|
|
13
|
-
|
|
14
|
-
const mesh = [];
|
|
15
|
-
|
|
16
|
-
const init = async ({ canvasRef, gui }) => {
|
|
17
|
-
const adapter = await navigator.gpu.requestAdapter();
|
|
18
|
-
const device = await adapter.requestDevice();
|
|
19
|
-
|
|
20
|
-
if (canvasRef.current === null) return;
|
|
21
|
-
const context = canvasRef.current.getContext('webgpu');
|
|
22
|
-
|
|
23
|
-
const devicePixelRatio = window.devicePixelRatio || 1;
|
|
24
|
-
const presentationSize = [
|
|
25
|
-
canvasRef.current.clientWidth * devicePixelRatio,
|
|
26
|
-
canvasRef.current.clientHeight * devicePixelRatio,
|
|
27
|
-
];
|
|
28
|
-
const aspect = presentationSize[0] / presentationSize[1];
|
|
29
|
-
const presentationFormat = context.getPreferredFormat(adapter);
|
|
30
|
-
context.configure({
|
|
31
|
-
device,
|
|
32
|
-
format: presentationFormat,
|
|
33
|
-
size: presentationSize,
|
|
34
|
-
});
|
|
35
|
-
|
|
36
|
-
// Create the model vertex buffer.
|
|
37
|
-
const kVertexStride = 8;
|
|
38
|
-
const vertexBuffer = device.createBuffer({
|
|
39
|
-
// position: vec3, normal: vec3, uv: vec2
|
|
40
|
-
size:
|
|
41
|
-
mesh.positions.length * kVertexStride * Float32Array.BYTES_PER_ELEMENT,
|
|
42
|
-
usage: GPUBufferUsage.VERTEX,
|
|
43
|
-
mappedAtCreation: true,
|
|
44
|
-
});
|
|
45
|
-
{
|
|
46
|
-
const mapping = new Float32Array(vertexBuffer.getMappedRange());
|
|
47
|
-
for (let i = 0; i < mesh.positions.length; ++i) {
|
|
48
|
-
mapping.set(mesh.positions[i], kVertexStride * i);
|
|
49
|
-
mapping.set(mesh.normals[i], kVertexStride * i + 3);
|
|
50
|
-
mapping.set(mesh.uvs[i], kVertexStride * i + 6);
|
|
51
|
-
}
|
|
52
|
-
vertexBuffer.unmap();
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
// Create the model index buffer.
|
|
56
|
-
const indexCount = mesh.triangles.length * 3;
|
|
57
|
-
const indexBuffer = device.createBuffer({
|
|
58
|
-
size: indexCount * Uint16Array.BYTES_PER_ELEMENT,
|
|
59
|
-
usage: GPUBufferUsage.INDEX,
|
|
60
|
-
mappedAtCreation: true,
|
|
61
|
-
});
|
|
62
|
-
{
|
|
63
|
-
const mapping = new Uint16Array(indexBuffer.getMappedRange());
|
|
64
|
-
for (let i = 0; i < mesh.triangles.length; ++i) {
|
|
65
|
-
mapping.set(mesh.triangles[i], 3 * i);
|
|
66
|
-
}
|
|
67
|
-
indexBuffer.unmap();
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
// GBuffer texture render targets
|
|
71
|
-
const gBufferTexture2DFloat = device.createTexture({
|
|
72
|
-
size: [...presentationSize, 3],
|
|
73
|
-
usage: GPUTextureUsage.RENDER_ATTACHMENT | GPUTextureUsage.TEXTURE_BINDING,
|
|
74
|
-
format: 'rgba32float',
|
|
75
|
-
});
|
|
76
|
-
const gBufferTextureAlbedo = device.createTexture({
|
|
77
|
-
size: presentationSize,
|
|
78
|
-
usage: GPUTextureUsage.RENDER_ATTACHMENT | GPUTextureUsage.TEXTURE_BINDING,
|
|
79
|
-
format: 'bgra8unorm',
|
|
80
|
-
});
|
|
81
|
-
const gBufferTextureViews = [
|
|
82
|
-
gBufferTexture2DFloat.createView({ baseArrayLayer: 0, arrayLayerCount: 1 }),
|
|
83
|
-
gBufferTexture2DFloat.createView({ baseArrayLayer: 1, arrayLayerCount: 1 }),
|
|
84
|
-
gBufferTextureAlbedo.createView(),
|
|
85
|
-
];
|
|
86
|
-
|
|
87
|
-
const vertexBuffers: Iterable<GPUVertexBufferLayout> = [
|
|
88
|
-
{
|
|
89
|
-
arrayStride: Float32Array.BYTES_PER_ELEMENT * 8,
|
|
90
|
-
attributes: [
|
|
91
|
-
{
|
|
92
|
-
// position
|
|
93
|
-
shaderLocation: 0,
|
|
94
|
-
offset: 0,
|
|
95
|
-
format: 'float32x3',
|
|
96
|
-
},
|
|
97
|
-
{
|
|
98
|
-
// normal
|
|
99
|
-
shaderLocation: 1,
|
|
100
|
-
offset: Float32Array.BYTES_PER_ELEMENT * 3,
|
|
101
|
-
format: 'float32x3',
|
|
102
|
-
},
|
|
103
|
-
{
|
|
104
|
-
// uv
|
|
105
|
-
shaderLocation: 2,
|
|
106
|
-
offset: Float32Array.BYTES_PER_ELEMENT * 6,
|
|
107
|
-
format: 'float32x2',
|
|
108
|
-
},
|
|
109
|
-
],
|
|
110
|
-
},
|
|
111
|
-
];
|
|
112
|
-
|
|
113
|
-
const primitive: GPUPrimitiveState = {
|
|
114
|
-
topology: 'triangle-list',
|
|
115
|
-
cullMode: 'back',
|
|
116
|
-
};
|
|
117
|
-
|
|
118
|
-
const writeGBuffersPipeline = device.createRenderPipeline({
|
|
119
|
-
vertex: {
|
|
120
|
-
module: device.createShaderModule({
|
|
121
|
-
code: vertexWriteGBuffers,
|
|
122
|
-
}),
|
|
123
|
-
entryPoint: 'main',
|
|
124
|
-
buffers: vertexBuffers,
|
|
125
|
-
},
|
|
126
|
-
fragment: {
|
|
127
|
-
module: device.createShaderModule({
|
|
128
|
-
code: fragmentWriteGBuffers,
|
|
129
|
-
}),
|
|
130
|
-
entryPoint: 'main',
|
|
131
|
-
targets: [
|
|
132
|
-
// position
|
|
133
|
-
{ format: 'rgba32float' },
|
|
134
|
-
// normal
|
|
135
|
-
{ format: 'rgba32float' },
|
|
136
|
-
// albedo
|
|
137
|
-
{ format: 'bgra8unorm' },
|
|
138
|
-
],
|
|
139
|
-
},
|
|
140
|
-
depthStencil: {
|
|
141
|
-
depthWriteEnabled: true,
|
|
142
|
-
depthCompare: 'less',
|
|
143
|
-
format: 'depth24plus',
|
|
144
|
-
},
|
|
145
|
-
primitive,
|
|
146
|
-
});
|
|
147
|
-
|
|
148
|
-
const gBufferTexturesBindGroupLayout = device.createBindGroupLayout({
|
|
149
|
-
entries: [
|
|
150
|
-
{
|
|
151
|
-
binding: 0,
|
|
152
|
-
visibility: GPUShaderStage.FRAGMENT,
|
|
153
|
-
texture: {
|
|
154
|
-
sampleType: 'unfilterable-float',
|
|
155
|
-
},
|
|
156
|
-
},
|
|
157
|
-
{
|
|
158
|
-
binding: 1,
|
|
159
|
-
visibility: GPUShaderStage.FRAGMENT,
|
|
160
|
-
texture: {
|
|
161
|
-
sampleType: 'unfilterable-float',
|
|
162
|
-
},
|
|
163
|
-
},
|
|
164
|
-
{
|
|
165
|
-
binding: 2,
|
|
166
|
-
visibility: GPUShaderStage.FRAGMENT,
|
|
167
|
-
texture: {
|
|
168
|
-
sampleType: 'unfilterable-float',
|
|
169
|
-
},
|
|
170
|
-
},
|
|
171
|
-
],
|
|
172
|
-
});
|
|
173
|
-
|
|
174
|
-
const lightsBufferBindGroupLayout = device.createBindGroupLayout({
|
|
175
|
-
entries: [
|
|
176
|
-
{
|
|
177
|
-
binding: 0,
|
|
178
|
-
visibility: GPUShaderStage.FRAGMENT | GPUShaderStage.COMPUTE,
|
|
179
|
-
buffer: {
|
|
180
|
-
type: 'storage',
|
|
181
|
-
},
|
|
182
|
-
},
|
|
183
|
-
{
|
|
184
|
-
binding: 1,
|
|
185
|
-
visibility: GPUShaderStage.FRAGMENT | GPUShaderStage.COMPUTE,
|
|
186
|
-
buffer: {
|
|
187
|
-
type: 'uniform',
|
|
188
|
-
},
|
|
189
|
-
},
|
|
190
|
-
],
|
|
191
|
-
});
|
|
192
|
-
|
|
193
|
-
const canvasSizeUniformBindGroupLayout = device.createBindGroupLayout({
|
|
194
|
-
entries: [
|
|
195
|
-
{
|
|
196
|
-
binding: 0,
|
|
197
|
-
visibility: GPUShaderStage.FRAGMENT,
|
|
198
|
-
buffer: {
|
|
199
|
-
type: 'uniform',
|
|
200
|
-
},
|
|
201
|
-
},
|
|
202
|
-
],
|
|
203
|
-
});
|
|
204
|
-
|
|
205
|
-
const gBuffersDebugViewPipeline = device.createRenderPipeline({
|
|
206
|
-
layout: device.createPipelineLayout({
|
|
207
|
-
bindGroupLayouts: [
|
|
208
|
-
gBufferTexturesBindGroupLayout,
|
|
209
|
-
canvasSizeUniformBindGroupLayout,
|
|
210
|
-
],
|
|
211
|
-
}),
|
|
212
|
-
vertex: {
|
|
213
|
-
module: device.createShaderModule({
|
|
214
|
-
code: vertexTextureQuad,
|
|
215
|
-
}),
|
|
216
|
-
entryPoint: 'main',
|
|
217
|
-
},
|
|
218
|
-
fragment: {
|
|
219
|
-
module: device.createShaderModule({
|
|
220
|
-
code: fragmentGBuffersDebugView,
|
|
221
|
-
}),
|
|
222
|
-
entryPoint: 'main',
|
|
223
|
-
targets: [
|
|
224
|
-
{
|
|
225
|
-
format: presentationFormat,
|
|
226
|
-
},
|
|
227
|
-
],
|
|
228
|
-
},
|
|
229
|
-
primitive,
|
|
230
|
-
});
|
|
231
|
-
|
|
232
|
-
const deferredRenderPipeline = device.createRenderPipeline({
|
|
233
|
-
layout: device.createPipelineLayout({
|
|
234
|
-
bindGroupLayouts: [
|
|
235
|
-
gBufferTexturesBindGroupLayout,
|
|
236
|
-
lightsBufferBindGroupLayout,
|
|
237
|
-
canvasSizeUniformBindGroupLayout,
|
|
238
|
-
],
|
|
239
|
-
}),
|
|
240
|
-
vertex: {
|
|
241
|
-
module: device.createShaderModule({
|
|
242
|
-
code: vertexTextureQuad,
|
|
243
|
-
}),
|
|
244
|
-
entryPoint: 'main',
|
|
245
|
-
},
|
|
246
|
-
fragment: {
|
|
247
|
-
module: device.createShaderModule({
|
|
248
|
-
code: fragmentDeferredRendering,
|
|
249
|
-
}),
|
|
250
|
-
entryPoint: 'main',
|
|
251
|
-
targets: [
|
|
252
|
-
{
|
|
253
|
-
format: presentationFormat,
|
|
254
|
-
},
|
|
255
|
-
],
|
|
256
|
-
},
|
|
257
|
-
primitive,
|
|
258
|
-
});
|
|
259
|
-
|
|
260
|
-
const depthTexture = device.createTexture({
|
|
261
|
-
size: presentationSize,
|
|
262
|
-
format: 'depth24plus',
|
|
263
|
-
usage: GPUTextureUsage.RENDER_ATTACHMENT,
|
|
264
|
-
});
|
|
265
|
-
|
|
266
|
-
const writeGBufferPassDescriptor: GPURenderPassDescriptor = {
|
|
267
|
-
colorAttachments: [
|
|
268
|
-
{
|
|
269
|
-
view: gBufferTextureViews[0],
|
|
270
|
-
|
|
271
|
-
loadValue: {
|
|
272
|
-
r: Number.MAX_VALUE,
|
|
273
|
-
g: Number.MAX_VALUE,
|
|
274
|
-
b: Number.MAX_VALUE,
|
|
275
|
-
a: 1.0,
|
|
276
|
-
},
|
|
277
|
-
storeOp: 'store',
|
|
278
|
-
},
|
|
279
|
-
{
|
|
280
|
-
view: gBufferTextureViews[1],
|
|
281
|
-
|
|
282
|
-
loadValue: { r: 0.0, g: 0.0, b: 1.0, a: 1.0 },
|
|
283
|
-
storeOp: 'store',
|
|
284
|
-
},
|
|
285
|
-
{
|
|
286
|
-
view: gBufferTextureViews[2],
|
|
287
|
-
|
|
288
|
-
loadValue: { r: 0.0, g: 0.0, b: 0.0, a: 1.0 },
|
|
289
|
-
storeOp: 'store',
|
|
290
|
-
},
|
|
291
|
-
],
|
|
292
|
-
depthStencilAttachment: {
|
|
293
|
-
view: depthTexture.createView(),
|
|
294
|
-
|
|
295
|
-
depthLoadValue: 1.0,
|
|
296
|
-
depthStoreOp: 'store',
|
|
297
|
-
stencilLoadValue: 0,
|
|
298
|
-
stencilStoreOp: 'store',
|
|
299
|
-
},
|
|
300
|
-
};
|
|
301
|
-
|
|
302
|
-
const textureQuadPassDescriptor: GPURenderPassDescriptor = {
|
|
303
|
-
colorAttachments: [
|
|
304
|
-
{
|
|
305
|
-
// view is acquired and set in render loop.
|
|
306
|
-
view: undefined,
|
|
307
|
-
|
|
308
|
-
loadValue: { r: 0.0, g: 0.0, b: 0.0, a: 1.0 },
|
|
309
|
-
storeOp: 'store',
|
|
310
|
-
},
|
|
311
|
-
],
|
|
312
|
-
};
|
|
313
|
-
|
|
314
|
-
const settings = {
|
|
315
|
-
mode: 'rendering',
|
|
316
|
-
numLights: 128,
|
|
317
|
-
};
|
|
318
|
-
const configUniformBuffer = (() => {
|
|
319
|
-
const buffer = device.createBuffer({
|
|
320
|
-
size: Uint32Array.BYTES_PER_ELEMENT,
|
|
321
|
-
mappedAtCreation: true,
|
|
322
|
-
usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST,
|
|
323
|
-
});
|
|
324
|
-
new Uint32Array(buffer.getMappedRange())[0] = settings.numLights;
|
|
325
|
-
buffer.unmap();
|
|
326
|
-
return buffer;
|
|
327
|
-
})();
|
|
328
|
-
|
|
329
|
-
gui.add(settings, 'mode', ['rendering', 'gBuffers view']);
|
|
330
|
-
gui
|
|
331
|
-
.add(settings, 'numLights', 1, kMaxNumLights)
|
|
332
|
-
.step(1)
|
|
333
|
-
.onChange(() => {
|
|
334
|
-
device.queue.writeBuffer(
|
|
335
|
-
configUniformBuffer,
|
|
336
|
-
0,
|
|
337
|
-
new Uint32Array([settings.numLights])
|
|
338
|
-
);
|
|
339
|
-
});
|
|
340
|
-
|
|
341
|
-
const modelUniformBuffer = device.createBuffer({
|
|
342
|
-
size: 4 * 16 * 2, // two 4x4 matrix
|
|
343
|
-
usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST,
|
|
344
|
-
});
|
|
345
|
-
|
|
346
|
-
const cameraUniformBuffer = device.createBuffer({
|
|
347
|
-
size: 4 * 16, // 4x4 matrix
|
|
348
|
-
usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST,
|
|
349
|
-
});
|
|
350
|
-
|
|
351
|
-
const sceneUniformBindGroup = device.createBindGroup({
|
|
352
|
-
layout: writeGBuffersPipeline.getBindGroupLayout(0),
|
|
353
|
-
entries: [
|
|
354
|
-
{
|
|
355
|
-
binding: 0,
|
|
356
|
-
resource: {
|
|
357
|
-
buffer: modelUniformBuffer,
|
|
358
|
-
},
|
|
359
|
-
},
|
|
360
|
-
{
|
|
361
|
-
binding: 1,
|
|
362
|
-
resource: {
|
|
363
|
-
buffer: cameraUniformBuffer,
|
|
364
|
-
},
|
|
365
|
-
},
|
|
366
|
-
],
|
|
367
|
-
});
|
|
368
|
-
|
|
369
|
-
const canvasSizeUniformBuffer = device.createBuffer({
|
|
370
|
-
size: 4 * 2,
|
|
371
|
-
usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST,
|
|
372
|
-
});
|
|
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
|
-
|
|
386
|
-
const gBufferTexturesBindGroup = device.createBindGroup({
|
|
387
|
-
layout: gBufferTexturesBindGroupLayout,
|
|
388
|
-
entries: [
|
|
389
|
-
{
|
|
390
|
-
binding: 0,
|
|
391
|
-
resource: gBufferTextureViews[0],
|
|
392
|
-
},
|
|
393
|
-
{
|
|
394
|
-
binding: 1,
|
|
395
|
-
resource: gBufferTextureViews[1],
|
|
396
|
-
},
|
|
397
|
-
{
|
|
398
|
-
binding: 2,
|
|
399
|
-
resource: gBufferTextureViews[2],
|
|
400
|
-
},
|
|
401
|
-
],
|
|
402
|
-
});
|
|
403
|
-
|
|
404
|
-
// Lights data are uploaded in a storage buffer
|
|
405
|
-
// which could be updated/culled/etc. with a compute shader
|
|
406
|
-
const extent = vec3.create();
|
|
407
|
-
vec3.sub(extent, lightExtentMax, lightExtentMin);
|
|
408
|
-
const lightDataStride = 8;
|
|
409
|
-
const bufferSizeInByte =
|
|
410
|
-
Float32Array.BYTES_PER_ELEMENT * lightDataStride * kMaxNumLights;
|
|
411
|
-
const lightsBuffer = device.createBuffer({
|
|
412
|
-
size: bufferSizeInByte,
|
|
413
|
-
usage: GPUBufferUsage.STORAGE,
|
|
414
|
-
mappedAtCreation: true,
|
|
415
|
-
});
|
|
416
|
-
|
|
417
|
-
// We randomaly populate lights randomly in a box range
|
|
418
|
-
// And simply move them along y-axis per frame to show they are
|
|
419
|
-
// dynamic lightings
|
|
420
|
-
const lightData = new Float32Array(lightsBuffer.getMappedRange());
|
|
421
|
-
const tmpVec4 = vec4.create();
|
|
422
|
-
let offset = 0;
|
|
423
|
-
for (let i = 0; i < kMaxNumLights; i++) {
|
|
424
|
-
offset = lightDataStride * i;
|
|
425
|
-
// position
|
|
426
|
-
for (let i = 0; i < 3; i++) {
|
|
427
|
-
tmpVec4[i] = Math.random() * extent[i] + lightExtentMin[i];
|
|
428
|
-
}
|
|
429
|
-
tmpVec4[3] = 1;
|
|
430
|
-
lightData.set(tmpVec4, offset);
|
|
431
|
-
// color
|
|
432
|
-
tmpVec4[0] = Math.random() * 2;
|
|
433
|
-
tmpVec4[1] = Math.random() * 2;
|
|
434
|
-
tmpVec4[2] = Math.random() * 2;
|
|
435
|
-
// radius
|
|
436
|
-
tmpVec4[3] = 20.0;
|
|
437
|
-
lightData.set(tmpVec4, offset + 4);
|
|
438
|
-
}
|
|
439
|
-
lightsBuffer.unmap();
|
|
440
|
-
|
|
441
|
-
const lightExtentBuffer = device.createBuffer({
|
|
442
|
-
size: 4 * 8,
|
|
443
|
-
usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST,
|
|
444
|
-
});
|
|
445
|
-
const lightExtentData = new Float32Array(8);
|
|
446
|
-
lightExtentData.set(lightExtentMin, 0);
|
|
447
|
-
lightExtentData.set(lightExtentMax, 4);
|
|
448
|
-
device.queue.writeBuffer(
|
|
449
|
-
lightExtentBuffer,
|
|
450
|
-
0,
|
|
451
|
-
lightExtentData.buffer,
|
|
452
|
-
lightExtentData.byteOffset,
|
|
453
|
-
lightExtentData.byteLength
|
|
454
|
-
);
|
|
455
|
-
|
|
456
|
-
const lightUpdateComputePipeline = device.createComputePipeline({
|
|
457
|
-
compute: {
|
|
458
|
-
module: device.createShaderModule({
|
|
459
|
-
code: lightUpdate,
|
|
460
|
-
}),
|
|
461
|
-
entryPoint: 'main',
|
|
462
|
-
},
|
|
463
|
-
});
|
|
464
|
-
const lightsBufferBindGroup = device.createBindGroup({
|
|
465
|
-
layout: lightsBufferBindGroupLayout,
|
|
466
|
-
entries: [
|
|
467
|
-
{
|
|
468
|
-
binding: 0,
|
|
469
|
-
resource: {
|
|
470
|
-
buffer: lightsBuffer,
|
|
471
|
-
},
|
|
472
|
-
},
|
|
473
|
-
{
|
|
474
|
-
binding: 1,
|
|
475
|
-
resource: {
|
|
476
|
-
buffer: configUniformBuffer,
|
|
477
|
-
},
|
|
478
|
-
},
|
|
479
|
-
],
|
|
480
|
-
});
|
|
481
|
-
const lightsBufferComputeBindGroup = device.createBindGroup({
|
|
482
|
-
layout: lightUpdateComputePipeline.getBindGroupLayout(0),
|
|
483
|
-
entries: [
|
|
484
|
-
{
|
|
485
|
-
binding: 0,
|
|
486
|
-
resource: {
|
|
487
|
-
buffer: lightsBuffer,
|
|
488
|
-
},
|
|
489
|
-
},
|
|
490
|
-
{
|
|
491
|
-
binding: 1,
|
|
492
|
-
resource: {
|
|
493
|
-
buffer: configUniformBuffer,
|
|
494
|
-
},
|
|
495
|
-
},
|
|
496
|
-
{
|
|
497
|
-
binding: 2,
|
|
498
|
-
resource: {
|
|
499
|
-
buffer: lightExtentBuffer,
|
|
500
|
-
},
|
|
501
|
-
},
|
|
502
|
-
],
|
|
503
|
-
});
|
|
504
|
-
//--------------------
|
|
505
|
-
|
|
506
|
-
// Scene matrices
|
|
507
|
-
const eyePosition = vec3.fromValues(0, 50, -100);
|
|
508
|
-
const upVector = vec3.fromValues(0, 1, 0);
|
|
509
|
-
const origin = vec3.fromValues(0, 0, 0);
|
|
510
|
-
|
|
511
|
-
const projectionMatrix = mat4.create();
|
|
512
|
-
mat4.perspective(projectionMatrix, (2 * Math.PI) / 5, aspect, 1, 2000.0);
|
|
513
|
-
|
|
514
|
-
const viewMatrix = mat4.create();
|
|
515
|
-
mat4.lookAt(viewMatrix, eyePosition, origin, upVector);
|
|
516
|
-
|
|
517
|
-
const viewProjMatrix = mat4.create();
|
|
518
|
-
mat4.multiply(viewProjMatrix, projectionMatrix, viewMatrix);
|
|
519
|
-
|
|
520
|
-
// Move the model so it's centered.
|
|
521
|
-
const modelMatrix = mat4.create();
|
|
522
|
-
mat4.translate(modelMatrix, modelMatrix, vec3.fromValues(0, -5, 0));
|
|
523
|
-
mat4.translate(modelMatrix, modelMatrix, vec3.fromValues(0, -40, 0));
|
|
524
|
-
|
|
525
|
-
const cameraMatrixData = viewProjMatrix as Float32Array;
|
|
526
|
-
device.queue.writeBuffer(
|
|
527
|
-
cameraUniformBuffer,
|
|
528
|
-
0,
|
|
529
|
-
cameraMatrixData.buffer,
|
|
530
|
-
cameraMatrixData.byteOffset,
|
|
531
|
-
cameraMatrixData.byteLength
|
|
532
|
-
);
|
|
533
|
-
const modelData = modelMatrix as Float32Array;
|
|
534
|
-
device.queue.writeBuffer(
|
|
535
|
-
modelUniformBuffer,
|
|
536
|
-
0,
|
|
537
|
-
modelData.buffer,
|
|
538
|
-
modelData.byteOffset,
|
|
539
|
-
modelData.byteLength
|
|
540
|
-
);
|
|
541
|
-
const invertTransposeModelMatrix = mat4.create();
|
|
542
|
-
mat4.invert(invertTransposeModelMatrix, modelMatrix);
|
|
543
|
-
mat4.transpose(invertTransposeModelMatrix, invertTransposeModelMatrix);
|
|
544
|
-
const normalModelData = invertTransposeModelMatrix as Float32Array;
|
|
545
|
-
device.queue.writeBuffer(
|
|
546
|
-
modelUniformBuffer,
|
|
547
|
-
64,
|
|
548
|
-
normalModelData.buffer,
|
|
549
|
-
normalModelData.byteOffset,
|
|
550
|
-
normalModelData.byteLength
|
|
551
|
-
);
|
|
552
|
-
// Pass the canvas size to shader to help sample from gBuffer textures using coord
|
|
553
|
-
const canvasSizeData = new Float32Array(presentationSize);
|
|
554
|
-
device.queue.writeBuffer(
|
|
555
|
-
canvasSizeUniformBuffer,
|
|
556
|
-
0,
|
|
557
|
-
canvasSizeData.buffer,
|
|
558
|
-
canvasSizeData.byteOffset,
|
|
559
|
-
canvasSizeData.byteLength
|
|
560
|
-
);
|
|
561
|
-
|
|
562
|
-
// Rotates the camera around the origin based on time.
|
|
563
|
-
function getCameraViewProjMatrix() {
|
|
564
|
-
const eyePosition = vec3.fromValues(0, 50, -100);
|
|
565
|
-
|
|
566
|
-
const rad = Math.PI * (Date.now() / 5000);
|
|
567
|
-
vec3.rotateY(eyePosition, eyePosition, origin, rad);
|
|
568
|
-
|
|
569
|
-
const viewMatrix = mat4.create();
|
|
570
|
-
mat4.lookAt(viewMatrix, eyePosition, origin, upVector);
|
|
571
|
-
|
|
572
|
-
mat4.multiply(viewProjMatrix, projectionMatrix, viewMatrix);
|
|
573
|
-
return viewProjMatrix as Float32Array;
|
|
574
|
-
}
|
|
575
|
-
|
|
576
|
-
function frame() {
|
|
577
|
-
// Sample is no longer the active page.
|
|
578
|
-
if (!canvasRef.current) return;
|
|
579
|
-
|
|
580
|
-
const cameraViewProj = getCameraViewProjMatrix();
|
|
581
|
-
device.queue.writeBuffer(
|
|
582
|
-
cameraUniformBuffer,
|
|
583
|
-
0,
|
|
584
|
-
cameraViewProj.buffer,
|
|
585
|
-
cameraViewProj.byteOffset,
|
|
586
|
-
cameraViewProj.byteLength
|
|
587
|
-
);
|
|
588
|
-
|
|
589
|
-
const commandEncoder = device.createCommandEncoder();
|
|
590
|
-
{
|
|
591
|
-
// Write position, normal, albedo etc. data to gBuffers
|
|
592
|
-
const gBufferPass = commandEncoder.beginRenderPass(
|
|
593
|
-
writeGBufferPassDescriptor
|
|
594
|
-
);
|
|
595
|
-
gBufferPass.setPipeline(writeGBuffersPipeline);
|
|
596
|
-
gBufferPass.setBindGroup(0, sceneUniformBindGroup);
|
|
597
|
-
gBufferPass.setVertexBuffer(0, vertexBuffer);
|
|
598
|
-
gBufferPass.setIndexBuffer(indexBuffer, 'uint16');
|
|
599
|
-
gBufferPass.drawIndexed(indexCount);
|
|
600
|
-
gBufferPass.endPass();
|
|
601
|
-
}
|
|
602
|
-
{
|
|
603
|
-
// Update lights position
|
|
604
|
-
const lightPass = commandEncoder.beginComputePass();
|
|
605
|
-
lightPass.setPipeline(lightUpdateComputePipeline);
|
|
606
|
-
lightPass.setBindGroup(0, lightsBufferComputeBindGroup);
|
|
607
|
-
lightPass.dispatch(Math.ceil(kMaxNumLights / 64));
|
|
608
|
-
lightPass.endPass();
|
|
609
|
-
}
|
|
610
|
-
{
|
|
611
|
-
if (settings.mode === 'gBuffers view') {
|
|
612
|
-
// GBuffers debug view
|
|
613
|
-
// Left: position
|
|
614
|
-
// Middle: normal
|
|
615
|
-
// Right: albedo (use uv to mimic a checkerboard texture)
|
|
616
|
-
textureQuadPassDescriptor.colorAttachments[0].view = context
|
|
617
|
-
.getCurrentTexture()
|
|
618
|
-
.createView();
|
|
619
|
-
const debugViewPass = commandEncoder.beginRenderPass(
|
|
620
|
-
textureQuadPassDescriptor
|
|
621
|
-
);
|
|
622
|
-
debugViewPass.setPipeline(gBuffersDebugViewPipeline);
|
|
623
|
-
debugViewPass.setBindGroup(0, gBufferTexturesBindGroup);
|
|
624
|
-
debugViewPass.setBindGroup(1, canvasSizeUniformBindGroup);
|
|
625
|
-
debugViewPass.draw(6);
|
|
626
|
-
debugViewPass.endPass();
|
|
627
|
-
} else {
|
|
628
|
-
// Deferred rendering
|
|
629
|
-
textureQuadPassDescriptor.colorAttachments[0].view = context
|
|
630
|
-
.getCurrentTexture()
|
|
631
|
-
.createView();
|
|
632
|
-
const deferredRenderingPass = commandEncoder.beginRenderPass(
|
|
633
|
-
textureQuadPassDescriptor
|
|
634
|
-
);
|
|
635
|
-
deferredRenderingPass.setPipeline(deferredRenderPipeline);
|
|
636
|
-
deferredRenderingPass.setBindGroup(0, gBufferTexturesBindGroup);
|
|
637
|
-
deferredRenderingPass.setBindGroup(1, lightsBufferBindGroup);
|
|
638
|
-
deferredRenderingPass.setBindGroup(2, canvasSizeUniformBindGroup);
|
|
639
|
-
deferredRenderingPass.draw(6);
|
|
640
|
-
deferredRenderingPass.endPass();
|
|
641
|
-
}
|
|
642
|
-
}
|
|
643
|
-
device.queue.submit([commandEncoder.finish()]);
|
|
644
|
-
|
|
645
|
-
requestAnimationFrame(frame);
|
|
646
|
-
}
|
|
647
|
-
requestAnimationFrame(frame);
|
|
648
|
-
};
|
|
649
|
-
|
|
650
|
-
init();
|