-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
198 lines (155 loc) · 6.16 KB
/
main.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
import { setupNoiseStage } from './stages/noise.js';
import { setupDebugNoiseStage } from './stages/debugNoise.js';
import { setupMarchingCubesStage } from './stages/marchingcubes.js';
import { setupRenderingStage } from './stages/rendering.js';
import { setupUI } from './util/ui.js';
import { setupCamera } from './util/camera.js';
import { setupInput } from './util/input.js';
import Stats from 'stats.js';
const config = {
cellCountX: 24 * 4, // must be a multiple of 4
cellCountY: 24 * 4,
cellCountZ: 24 * 4,
outputWidth: 16 * 120,
outputHeight: 9 * 120,
toggleDebugNoise: false,
animateIsoValue: false,
speed: 0.015,
mouseSensitivity: 0.0002,
timestamp_queries: true,
ambient_factor: 0.35,
diffuse_factor: 0.9,
specular_factor: 0.4,
shininess: 32.0,
};
let state = {
time: 0,
keyboard: {
w: false,
a: false,
s: false,
d: false,
' ': false,
Control: false,
},
mouse: {
movementX: 0,
movementY: 0,
}
};
async function init() {
//Get a WebGPU device
let adapter;
try {
adapter = await navigator.gpu?.requestAdapter();
} catch (e) {
console.error(e)
return alert('Need a browser that supports WebGPU');
}
let device;
await adapter?.requestDevice({
requiredLimits: {
maxStorageBufferBindingSize: 1024 * 1024 * 512, // 512 MB
maxBufferSize: 1024 * 1024 * 512, // 512 MB
},
}).catch(e => {
return adapter.requestDevice(); // fallback to no limits
}).then(d => {
device = d;
}).catch(e => {
console.error(e);
return alert('Need a browser that supports WebGPU')
});
config.maxBufferSize = device.limits.maxStorageBufferBindingSize;
const presentationFormat = navigator.gpu.getPreferredCanvasFormat();
// Get a WebGPU context from the canvas and configure it
const canvas = document.getElementById('webgpu_canvas');
canvas.width = config.outputWidth;
canvas.height = config.outputHeight;
const context = canvas.getContext('webgpu');
if(!context) return alert('Need a browser that supports WebGPU');
console.log('Context created successfully');
context.configure({
device,
format: presentationFormat,
});
//Setup Stages
const noiseStage = await setupNoiseStage(device, config);
const debugNoiseStage = await setupDebugNoiseStage(device, config, noiseStage.noiseTexture, presentationFormat);
const marchingCubesStage = await setupMarchingCubesStage(device, config, noiseStage);
const renderingStage = await setupRenderingStage(device, config, presentationFormat, noiseStage);
//Setup UI / Input
setupInput(config, state, canvas)
const camera = setupCamera(config, state, renderingStage, noiseStage);
setupUI(config, state, noiseStage, marchingCubesStage, renderingStage, camera, debugNoiseStage);
//Performance Monitor Library stats.js
const stats = new Stats();
document.body.appendChild(stats.dom);
//Setup
function resizeIfNeeded() {
const width = Math.max(1, Math.min(device.limits.maxTextureDimension2D, canvas.clientWidth));
const height = Math.max(1, Math.min(device.limits.maxTextureDimension2D, canvas.clientHeight));
if (canvas.width === width && canvas.height === height) return;
config.outputWidth = canvas.width = width;
config.outputHeight = canvas.height = height;
console.log(`Resized canvas to ${width}x${height}`);
renderingStage.createDepthTexture();
}
async function render(time) {
const deltaTime = time - state.time;
state.time = time;
stats.begin();
resizeIfNeeded();
//Update Camera
camera.update(state.keyboard, state.mouse, deltaTime)
noiseStage.updateSettingsBuffer();
renderingStage.updateRenderSettingsBuffer();
const encoder = device.createCommandEncoder({label: 'Command encoder'});
let computePass = encoder.beginComputePass({label: 'Compute Pass'});
/* Noise Stage */
computePass.setPipeline(noiseStage.pipeline);
computePass.setBindGroup(0, marchingCubesStage.LUTBindGroup);
computePass.setBindGroup(1, noiseStage.bindGroup);
computePass.dispatchWorkgroups(config.cellCountX / 4, config.cellCountY / 4, config.cellCountZ / 4);
/* Marching Cubes Stage */
if(config.animateIsoValue === true) {
marchingCubesStage.settings.isoValue = Math.sin(time / 400) / 40 + 0.5;
marchingCubesStage.updateSettingsBuffer();
}
computePass.setPipeline(marchingCubesStage.pipeline);
computePass.setBindGroup(1, marchingCubesStage.bindGroup);
computePass.dispatchWorkgroups(config.cellCountX / 4, config.cellCountY / 4, config.cellCountZ /4);
computePass.end();
if(config.toggleDebugNoise === true){
/* Debug Noise Stage */
debugNoiseStage.renderPassDescriptor.colorAttachments[0].view = context.getCurrentTexture().createView(); // Update render pass view
const renderPass = encoder.beginRenderPass(debugNoiseStage.renderPassDescriptor);
renderPass.setPipeline(debugNoiseStage.pipeline);
renderPass.setBindGroup(0, debugNoiseStage.bindGroup);
renderPass.draw(3);
renderPass.end();
}else{
/* Rendering Stage */
renderingStage.renderPassDescriptor.colorAttachments[0].view = context.getCurrentTexture().createView(); // Update render pass view
const renderPass = encoder.beginRenderPass(renderingStage.renderPassDescriptor);
renderPass.setPipeline(renderingStage.pipeline);
renderPass.setBindGroup(0, renderingStage.bindGroup);
renderPass.setVertexBuffer(0, marchingCubesStage.positionBuffer);
renderPass.setVertexBuffer(1, marchingCubesStage.normalBuffer);
renderPass.setIndexBuffer(marchingCubesStage.indexBuffer, "uint32");
renderPass.drawIndexedIndirect(marchingCubesStage.indirectArgsBuffer, 4);
renderPass.end();
}
const commandBuffer = encoder.finish();
device.queue.submit([commandBuffer]);
//Reset needed buffers for next frame
device.queue.writeBuffer(marchingCubesStage.indirectArgsBuffer, 0, new Uint32Array([0, 0, 1, 0, 0, 0]));
debugNoiseStage.updateSettingsBuffer(Math.round(time / 50));
state.mouse.movementX = 0;
state.mouse.movementY = 0;
stats.end();
requestAnimationFrame(render);
}
requestAnimationFrame(render);
}
init();