-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.js
More file actions
724 lines (553 loc) · 24.2 KB
/
Main.js
File metadata and controls
724 lines (553 loc) · 24.2 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
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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
import * as Utils from "./utils/utils.js";
import {calculateBlur3D} from './blur3D/Blur3D.js'
import {Camera} from './utils/camera.js';
import * as PBF from "./simulation/PBF.js";
import { generateMipMap } from "./mipmaps/CalculateMipMap.js";
import * as MarchingCubes from "./marchingCubes/TrianglesGenerator.js";
import * as Bloom from "./unrealBloom/UnrealBloom.js";
import * as dat from 'dat.gui';
import { vec3 } from "gl-matrix";
//Shaders
import marchingCubesShader from "./rendering/RenderMC.wgsl?raw";
import postProcessingShader from "./rendering/Postprocessing.wgsl?raw";
import backgroundShader from "./rendering/RenderBackground.wgsl?raw";
import blurOffsetsShader from "./rendering/BlurOffset.wgsl?raw";
import compositeShader from "./rendering/RenderComposite.wgsl?raw"
const device = await Utils.getDevice();
const PBF_RESOLUTION = 120; //More than 203 this breaks the buffer
const MC_RESOLUTION = PBF_RESOLUTION; //Maximum resolution available
const VERTEX_MEMORY = 20000000;
const totalParticles = 40000;
const MULTI_SAMPLER = 1;
//Load the logo image
async function loadImageBitmap(url) {
const res = await fetch(url);
const blob = await res.blob();
return await createImageBitmap(blob, { colorSpaceConversion: 'none' });
}
let letters = document.body.querySelectorAll(".letter");
letters = Array.from(letters);
const logoImage = await loadImageBitmap("./assets/codrops.png");
const logoTexture = device.createTexture({
size: [1650, 200],
format: 'rgba8unorm',
dimension: '2d',
usage: GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.COPY_DST | GPUTextureUsage.RENDER_ATTACHMENT,
})
device.queue.copyExternalImageToTexture(
{ source: logoImage},
{ texture: logoTexture },
{ width: logoTexture.width,
height: logoTexture.height },
);
//Get the context to render
const canvas = document.querySelector("canvas");
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
const context = canvas.getContext('webgpu');
const presentationFormat = navigator.gpu.getPreferredCanvasFormat();
context.configure({
device,
format: presentationFormat
});
let textureSize = MC_RESOLUTION;
let currentFrame = 0;
// let querySet, queryBuffer, capacity;
// capacity = 2;//Max number of timestamps we can store
// querySet = device.createQuerySet({
// type: "timestamp",
// count: capacity,
// });
// queryBuffer = device.createBuffer({
// size: 8 * capacity,
// usage: GPUBufferUsage.QUERY_RESOLVE
// | GPUBufferUsage.STORAGE
// | GPUBufferUsage.COPY_SRC
// | GPUBufferUsage.COPY_DST,
// });
let colors = [
[18, 97, 115],
[78, 157, 166],
[217, 166, 121],
[191, 116, 73],
[140, 68, 42],
]
let colorsId = 0 ;
let colorIdOut = 0;
let colorIdIn = 0;
let frameData;
//Define the GUI
var params = {
depthTest: 1,
mixAlpha: 1,
size: 5,
deltaTime: 0.05,
coneAngle: 0.83,
coneRotation: 45,
coneAngle2: 0.76,
coneRotation2: 64,
gridRadius: 5,
lightIntensity: 14,
separation: 0.,
voxelWorldSize: 0.022,
smoothness: 12,
mc_range: 0.6,
thickness: 0.5,
gamma: 1,
brightness: 0,
contrast: 1
}
const queryString = window.location.search;
const urlParams = new URLSearchParams(queryString);
const guiMenu = urlParams.get('ui') == "true";
const splitSteps = urlParams.get('split') == "true" || false;
if(guiMenu) {
var gui = new dat.GUI();
var postFolder = gui.addFolder("postprocessing");
postFolder.add(params, "gamma", -1, 1).name("gamma").step(0.01);
postFolder.add(params, "brightness", -1, 1).name("brightness").step(0.01);
postFolder.add(params, "contrast", -1, 3).name("contrast").step(0.01);
var mcFolder = gui.addFolder('marchingCubes');
mcFolder.add(params, "smoothness", 1, 30).name("smoothness").step(1);
mcFolder.add(params, "mc_range", 0.001, 1).name("range").step(0.001);
mcFolder.add(params, "thickness", 0.001, 1).name("thickness").step(0.001);
mcFolder.add(params, "voxelWorldSize", 0.001, 0.1).name("voxel size ").step(0.0001);
mcFolder.add(params, "coneAngle2", 0.1, 1, 1).name("cone angle ").step(0.01);
mcFolder.add(params, "coneRotation2", 0, 90, 1).name("cone rotation ").step(1);
var simulationFolder = gui.addFolder('simulation');
simulationFolder.add(params, "deltaTime", 0, 0.05, 0).name("delta time").step(0.001);
simulationFolder.add(params, "separation", 0, 0.4, 0).name("separation").step(0.01);
}
const texturePotential = device.createTexture({
size: [textureSize, textureSize, textureSize],
format: 'rgba32float',
dimension: '3d',
usage: GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.STORAGE_BINDING | GPUTextureUsage.COPY_DST | GPUBufferUsage.COPY_SRC,
})
const texture_VCT = device.createTexture({
size: [textureSize, textureSize, textureSize],
format: 'rgba32float',
dimension: '3d',
mipLevelCount: Math.ceil(Math.log2(textureSize)),
usage: GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.STORAGE_BINDING | GPUTextureUsage.COPY_DST | GPUBufferUsage.COPY_SRC,
})
const texturePotentialOut = device.createTexture({
size: [textureSize, textureSize, textureSize],
format: 'rgba32float',
dimension: '3d',
usage: GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.STORAGE_BINDING | GPUTextureUsage.COPY_DST | GPUBufferUsage.COPY_SRC,
})
const sampler = device.createSampler({
magFilter: "linear",
minFilter: "linear",
mipmapFilter: "linear",
addressModeU: 'clamp-to-edge',
addressModeV: 'clamp-to-edge',
addressModeW: 'clamp-to-edge'
});
console.log("the PBF resolution is: " + PBF_RESOLUTION);
console.log("the amount of parrticles are: " + totalParticles);
//Define the camera
let camera = new Camera(canvas);
let cameraDistance = 3;
let FOV = 30;
//Setup the position based fluids
await PBF.setupPBF(PBF_RESOLUTION, totalParticles, texturePotential, camera);
//Setup the marching cubes
const [verticesBuffer,
normalsBuffer,
velocityBuffer,
checkBuffer] = await MarchingCubes.setupMarchingCubes(VERTEX_MEMORY,
texturePotentialOut,
texture_VCT);
//transform matrix
let uniformsData = new Array(64).fill(0);
let uniforms = new Float32Array(uniformsData);
const uniformsBuffer = device.createBuffer(
{
label: "uniforms buffer",
size: uniforms.byteLength,
usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST
}
)
const uniformsMirrorBuffer = device.createBuffer(
{
label: "uniforms mirror buffer",
size: uniforms.byteLength,
usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST
}
)
const marchingCubesData = await Utils.setupRenderingPipeline("marching cubes",
marchingCubesShader,
MULTI_SAMPLER,
[{format: "rgba8unorm"},
{format: "rgba8unorm"}
]
);
const marchingCubesBindings = [];
marchingCubesBindings[0] = device.createBindGroup({
label: "binding for non reflective shape",
layout: marchingCubesData.pipeline.getBindGroupLayout(0),
entries: [
{binding: 0, resource: {buffer: verticesBuffer}},
{binding: 1, resource: {buffer: uniformsBuffer}},
{binding: 2, resource: {buffer: normalsBuffer}},
{binding: 3, resource: texture_VCT.createView()},
{binding: 4, resource: sampler},
{binding: 5, resource: {buffer: velocityBuffer}},
{binding: 6, resource: texturePotentialOut.createView()},
]
}
)
marchingCubesBindings[1] = device.createBindGroup({
label: "binding for reflective shape",
layout: marchingCubesData.pipeline.getBindGroupLayout(0),
entries: [
{binding: 0, resource: {buffer: verticesBuffer}},
{binding: 1, resource: {buffer: uniformsMirrorBuffer}},
{binding: 2, resource: {buffer: normalsBuffer}},
{binding: 3, resource: texture_VCT.createView()},
{binding: 4, resource: sampler},
{binding: 5, resource: {buffer: velocityBuffer}},
{binding: 6, resource: texturePotentialOut.createView()},
]
}
)
const postProData = await Utils.setupPipeline("post processing", postProcessingShader);
const uniformsXData = new Float32Array([0, 1, params.deltaTime, 1]);
const uniformsYData = new Float32Array([1, 0, params.deltaTime, 1]);
const uniformsXBuffer = device.createBuffer({
label: "uniforms X buffer",
size: uniformsXData.byteLength,
usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_SRC | GPUBufferUsage.COPY_DST
})
const uniformsYBuffer = device.createBuffer({
label: "uniforms Y buffer",
size: uniformsYData.byteLength,
usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_SRC | GPUBufferUsage.COPY_DST
})
device.queue.writeBuffer(uniformsXBuffer, 0, uniformsXData);
device.queue.writeBuffer(uniformsYBuffer, 0, uniformsYData);
const backgroundData = await Utils.setupRenderingPipeline("background quad",
backgroundShader,
MULTI_SAMPLER,
[{format: "rgba8unorm"},
{format: "rgba8unorm"}
],
false
);
const backgroundUniforms = new Float32Array(8);
const backgroundBuffer = device.createBuffer({
label: "background buffer",
size: backgroundUniforms.byteLength,
usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST
})
const backogroundBindGroup = device.createBindGroup({
label: "background bind group",
layout: backgroundData.pipeline.getBindGroupLayout(0),
entries: [
{binding: 0, resource: {buffer: backgroundBuffer}}
]
})
const offsetData = await Utils.setupPipeline("offset pass", blurOffsetsShader);
const quadUniforms = new Float32Array(8);
const quadBuffer = device.createBuffer({
label: "quad buffer",
size: quadUniforms.byteLength,
usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST
})
const quadData = await Utils.setupRenderingPipeline("screen quad",
compositeShader,
1,
[{format: navigator.gpu.getPreferredCanvasFormat()}],
false);
let defaultDepthTexture,
singleColorTexture,
singleMirrorTexture,
singleScreenTexture,
singleMirrorTexture2,
singleMirrorBase;
let multiColorTexture,
multiMirrorTexture,
multiDepthTexture;
const postProcessingBindings = [];
const offsetsBindings = [];
function updateRenderTexures() {
if(defaultDepthTexture != null) {
defaultDepthTexture.destroy();
singleColorTexture.destroy();
singleMirrorTexture.destroy();
singleMirrorTexture2.destroy();
singleMirrorBase.destroy();
singleScreenTexture.destroy();
multiColorTexture.destroy();
multiMirrorTexture.destroy();
multiDepthTexture.destroy();
}
defaultDepthTexture = device.createTexture({
size: [window.innerWidth, window.innerHeight],
sampleCount: 1,
format: 'depth32float',
usage: GPUTextureUsage.RENDER_ATTACHMENT | GPUTextureUsage.TEXTURE_BINDING
});
multiDepthTexture = device.createTexture({
size: [window.innerWidth, window.innerHeight],
sampleCount: MULTI_SAMPLER,
format: 'depth32float',
usage: GPUTextureUsage.RENDER_ATTACHMENT | GPUTextureUsage.TEXTURE_BINDING
});
singleColorTexture = device.createTexture({
size: [window.innerWidth, window.innerHeight],
sampleCount: 1,
format: 'rgba8unorm',
usage: GPUTextureUsage.RENDER_ATTACHMENT | GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.STORAGE_BINDING
});
singleMirrorTexture = device.createTexture({
size: [window.innerWidth, window.innerHeight],
sampleCount: 1,
format: 'rgba8unorm',
usage: GPUTextureUsage.RENDER_ATTACHMENT | GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.STORAGE_BINDING | GPUTextureUsage.COPY_SRC
});
multiColorTexture = device.createTexture({
size: [window.innerWidth, window.innerHeight],
sampleCount: MULTI_SAMPLER,
format: 'rgba8unorm',
usage: GPUTextureUsage.RENDER_ATTACHMENT | GPUTextureUsage.TEXTURE_BINDING
});
multiMirrorTexture = device.createTexture({
size: [window.innerWidth, window.innerHeight],
sampleCount: MULTI_SAMPLER,
format: 'rgba8unorm',
usage: GPUTextureUsage.RENDER_ATTACHMENT | GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.COPY_SRC
});
singleMirrorTexture2 = device.createTexture({
size: [window.innerWidth, window.innerHeight],
sampleCount: 1,
format: 'rgba8unorm',
usage: GPUTextureUsage.RENDER_ATTACHMENT | GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.STORAGE_BINDING
});
singleMirrorBase = device.createTexture({
size: [window.innerWidth, window.innerHeight],
sampleCount: 1,
format: 'rgba8unorm',
usage: GPUTextureUsage.RENDER_ATTACHMENT | GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.STORAGE_BINDING | GPUTextureUsage.COPY_DST
});
singleScreenTexture = device.createTexture({
size: [window.innerWidth, window.innerHeight],
sampleCount: 1,
format: 'rgba8unorm',
usage: GPUTextureUsage.RENDER_ATTACHMENT | GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.STORAGE_BINDING
});
Bloom.setup(window.innerWidth, window.innerHeight, singleColorTexture, singleScreenTexture, sampler);
postProcessingBindings[0] = device.createBindGroup( {
label:`post processing pass X bind group`,
layout: postProData.pipeline.getBindGroupLayout(0),
entries: [
{binding: 0, resource: singleColorTexture.createView()},
{binding: 1, resource: singleMirrorTexture.createView()},
{binding: 2, resource: sampler},
{binding: 3, resource: {buffer: uniformsXBuffer}},
{binding: 4, resource: singleScreenTexture.createView()},
]
})
postProcessingBindings[1] = device.createBindGroup( {
label:`post processing pass Y bind group`,
layout: postProData.pipeline.getBindGroupLayout(0),
entries: [
{binding: 0, resource: singleScreenTexture.createView()},
{binding: 1, resource: singleMirrorTexture.createView()},
{binding: 2, resource: sampler},
{binding: 3, resource: {buffer: uniformsYBuffer}},
{binding: 4, resource: singleColorTexture.createView()},
]
})
offsetsBindings[0] = device.createBindGroup( {
label:`offset X bind group`,
layout: offsetData.pipeline.getBindGroupLayout(0),
entries: [
{binding: 0, resource: {buffer: uniformsXBuffer}},
{binding: 1, resource: singleMirrorBase.createView()},
{binding: 2, resource: singleMirrorTexture.createView()},
{binding: 3, resource: singleMirrorTexture2.createView()},
]
})
offsetsBindings[1] = device.createBindGroup( {
label:`offset Y bind group`,
layout: offsetData.pipeline.getBindGroupLayout(0),
entries: [
{binding: 0, resource: {buffer: uniformsYBuffer}},
{binding: 1, resource: singleMirrorBase.createView()},
{binding: 2, resource: singleMirrorTexture2.createView()},
{binding: 3, resource: singleMirrorTexture.createView()},
]
})
quadData.setBindGroup(
[
{binding: 0, resource: {buffer: quadBuffer}},
{binding: 1, resource: singleColorTexture.createView()},
{binding: 2, resource: sampler},
{binding: 3, resource: logoTexture.createView()}
]
)
}
window.onresize = updateRenderTexures;
updateRenderTexures();
//Rendering
async function render() {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
//Quick performance test to update a frame
if((splitSteps && currentFrame % 2 == 0) || !splitSteps) {
frameData = PBF.updateFrame({x: 0 , y: -18 , z: 0}, params.deltaTime, params.lightIntensity, params.separation, camera);
if(frameData.relativeFrame == 0) {
colorIdIn = (colorsId) % colors.length;
colorIdOut = (colorsId + 1) % colors.length;
colorsId ++;
}
//Generate the potential for the marching cubes.
calculateBlur3D(texturePotential, texturePotentialOut, params.smoothness);
}
if((splitSteps && currentFrame % 2 != 0) || !splitSteps) {
//Calculate the triangles using the marching cubes.
MarchingCubes.generateTriangles(params.mc_range);
//Calculate the mipmap of the texture for the voxel cone tracing
generateMipMap(texture_VCT, device);
}
//Update the camera
camera.updateCamera(FOV, window.innerWidth / window.innerHeight, cameraDistance);
var n = vec3.fromValues(0, 1, 0);
camera.calculateReflection([0., 0.03, 0], n);
//make a command enconder to start encoding thigns
const encoder = device.createCommandEncoder({ label: 'rendering encoder'});
//Render the background
backgroundUniforms[0] = colors[colorIdIn][0] / 255;
backgroundUniforms[1] = colors[colorIdIn][1] /255;
backgroundUniforms[2] = colors[colorIdIn][2] /255;
backgroundUniforms[3] = frameData.relativeFrame;
backgroundUniforms[4] = colors[colorIdOut][0] / 255;
backgroundUniforms[5] = colors[colorIdOut][1] /255;
backgroundUniforms[6] = colors[colorIdOut][2] /255;
device.queue.writeBuffer(backgroundBuffer, 0, backgroundUniforms);
backgroundData.passDescriptor.colorAttachments[0].view = MULTI_SAMPLER == 4 ? multiColorTexture.createView() : singleColorTexture.createView();
if(MULTI_SAMPLER == 4) backgroundData.passDescriptor.colorAttachments[0].resolveTarget = singleColorTexture.createView();
backgroundData.passDescriptor.colorAttachments[0].loadOp = 'clear';
backgroundData.passDescriptor.colorAttachments[1].view = MULTI_SAMPLER == 4 ? multiMirrorTexture.createView() : singleMirrorTexture.createView();
if(MULTI_SAMPLER == 4) backgroundData.passDescriptor.colorAttachments[1].resolveTarget = singleMirrorTexture.createView();
backgroundData.passDescriptor.colorAttachments[1].loadOp = 'clear';
const backgroundPass = encoder.beginRenderPass(backgroundData.passDescriptor);
backgroundPass.setPipeline(backgroundData.pipeline);
backgroundPass.setBindGroup(0, backogroundBindGroup);
backgroundPass.draw(6, 1);
backgroundPass.end();
const uniformsOffset = 16;
for(let i = 0; i < 16; i ++) {
uniforms[i] = camera.transformMatrix[i];
}
uniforms[uniformsOffset] = camera.position[0];
uniforms[uniformsOffset + 1] = camera.position[1];
uniforms[uniformsOffset + 2] = camera.position[2];
uniforms[uniformsOffset + 3] = PBF_RESOLUTION;
uniforms[uniformsOffset + 4] = params.coneAngle2;
uniforms[uniformsOffset + 5] = params.coneRotation2;
uniforms[uniformsOffset + 6] = frameData.relativeFrame;
uniforms[uniformsOffset + 7] = params.voxelWorldSize;
uniforms[uniformsOffset + 8] = colors[colorIdIn][0] / 255;
uniforms[uniformsOffset + 9] = colors[colorIdIn][1] / 255;
uniforms[uniformsOffset + 10] = colors[colorIdIn][2] / 255;
uniforms[uniformsOffset + 11] = 0;
uniforms[uniformsOffset + 12] = colors[colorIdOut][0] / 255;
uniforms[uniformsOffset + 13] = colors[colorIdOut][1] / 255;
uniforms[uniformsOffset + 14] = colors[colorIdOut][2] / 255;
uniforms[uniformsOffset + 15] = params.thickness;
device.queue.writeBuffer(uniformsBuffer, 0, uniforms);
//For the mirror
for(let i = 0; i < 16; i ++) {
uniforms[i] = camera.transformMatrixReflection[i];
}
uniforms[uniformsOffset + 11] = 1;
device.queue.writeBuffer(uniformsMirrorBuffer, 0, uniforms);
//Render the fluid
marchingCubesData.passDescriptor.colorAttachments[0].view = MULTI_SAMPLER == 4? multiColorTexture.createView() : singleColorTexture.createView();
if(MULTI_SAMPLER == 4) marchingCubesData.passDescriptor.colorAttachments[0].resolveTarget = singleColorTexture.createView();
marchingCubesData.passDescriptor.colorAttachments[0].loadOp = 'load';
marchingCubesData.passDescriptor.colorAttachments[1].view = MULTI_SAMPLER == 4? multiMirrorTexture.createView() : singleMirrorTexture.createView();
if(MULTI_SAMPLER == 4) marchingCubesData.passDescriptor.colorAttachments[1].resolveTarget = singleMirrorTexture.createView();
marchingCubesData.passDescriptor.colorAttachments[1].loadOp = 'load';
marchingCubesData.passDescriptor.depthStencilAttachment.view = multiDepthTexture.createView();
marchingCubesData.passDescriptor.depthStencilAttachment.depthLoadOp = 'clear';
const pass = encoder.beginRenderPass(marchingCubesData.passDescriptor);
pass.setPipeline(marchingCubesData.pipeline);
marchingCubesBindings.map(bindGroup => {
pass.setBindGroup(0, bindGroup);
pass.drawIndirect(checkBuffer, 4 * 3);
})
pass.end();
encoder.copyTextureToTexture(
{
texture: singleMirrorTexture,
},
{
texture: singleMirrorBase,
},
{
width: window.innerWidth,
height: window.innerHeight
},
);
// let postX = Math.ceil(window.innerWidth / 16);
// let postY = Math.ceil(window.innerHeight / 16);
// //Offset passes
// const offsetPass = encoder.beginComputePass(offsetData.passDescriptor);
// offsetPass.setPipeline(offsetData.pipeline);
// offsetPass.setBindGroup(0, offsetsBindings[0]);
// offsetPass.dispatchWorkgroups(postX, postY);
// offsetPass.setBindGroup(0, offsetsBindings[1]);
// offsetPass.dispatchWorkgroups(postX, postY);
// offsetPass.end();
// //Post processing passes
// const postProPass = encoder.beginComputePass(postProData.passDescriptor);
// postProPass.setPipeline(postProData.pipeline);
// postProPass.setBindGroup(0, postProcessingBindings[0]);
// postProPass.dispatchWorkgroups(postX, postY);
// postProPass.setBindGroup(0, postProcessingBindings[1]);
// postProPass.dispatchWorkgroups(postX, postY);
// postProPass.end();
// Bloom.applyBloom(encoder);
//Render the quad
quadData.passDescriptor.colorAttachments[0].view = context.getCurrentTexture().createView();
quadData.passDescriptor.colorAttachments[0].loadOp = "clear";
quadUniforms[0] = window.innerWidth;
quadUniforms[1] = window.innerHeight;
quadUniforms[2] = frameData.relativeFrame;
quadUniforms[3] = frameData.animationFrame;
quadUniforms[4] = frameData.currentLetter;
quadUniforms[5] = params.brightness;
quadUniforms[6] = params.contrast;
quadUniforms[7] = params.gamma;
device.queue.writeBuffer(quadBuffer, 0, quadUniforms);
const screenPass = encoder.beginRenderPass(quadData.passDescriptor);
screenPass.setPipeline(quadData.pipeline);
screenPass.setBindGroup(0, quadData.bindGroup);
screenPass.draw(6, 1);
screenPass.end();
const commandBuffer = encoder.finish();
device.queue.submit([commandBuffer]);
currentFrame ++;
}
// set the expected frame rate
let frames_per_second = 60;
let interval = Math.floor(1000 / frames_per_second);
let startTime = performance.now();
let previousTime = startTime;
let currentTime = 0;
let deltaTime = 0;
function animationLoop(timestamp) {
currentTime = timestamp;
deltaTime = currentTime - previousTime;
if (deltaTime > interval) {
previousTime = currentTime - (deltaTime % interval);
render();
}
requestAnimationFrame(animationLoop);
}
requestAnimationFrame(animationLoop);