-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathfull-screen-triangle.js
More file actions
executable file
·157 lines (135 loc) · 3.5 KB
/
Copy pathfull-screen-triangle.js
File metadata and controls
executable file
·157 lines (135 loc) · 3.5 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
import {
Attachment,
Attribute,
BindGroup,
BindGroupLayout,
Buffer,
Clock,
Command,
Context,
GPUIndexFormat,
GPUShaderStage,
Pass,
Pipeline,
Shaders,
State,
Uniform,
WGSLBuiltIn,
} from "../lib/index.js";
import typedArrayInterleave from "typed-array-interleave";
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();
// Layouts
const systemBindGroupLayout = new BindGroupLayout([
{
buffer: {},
visibility: GPUShaderStage.VERTEX | GPUShaderStage.FRAGMENT,
name: "System",
uniforms: [new Uniform("resolution", "vec2"), new Uniform("time", "float")],
},
]);
// Buffers
const systemUniformsBuffer = new Buffer();
systemUniformsBuffer.uniformBuffer(systemBindGroupLayout.getBindGroupSize());
// Bindings
const systemUniformBindGroup = new BindGroup({
layout: systemBindGroupLayout.gpuBindGroupLayout,
resources: [
{
buffer: systemUniformsBuffer.gpuBuffer,
offset: 0,
size: systemBindGroupLayout.getBindGroupSize(),
},
],
});
// Geometry
const geometryVertexBuffer = new Buffer();
const geometryIndicesBuffer = new Buffer();
geometryVertexBuffer.vertexBuffer(
typedArrayInterleave(
Float32Array,
[2, 2],
new Float32Array([-1, -1, 3, -1, -1, 3]),
new Float32Array([0, 0, 2, 0, 0, 2])
)
);
geometryIndicesBuffer.indexBuffer(Uint32Array.of(0, 1, 2));
// Pipeline
const pipeline = new Pipeline({
bindGroupLayouts: [systemBindGroupLayout],
ins: [
new Attribute("position", "vec2<f32>"),
new Attribute("uv", "vec2<f32>"),
],
outs: [WGSLBuiltIn.Position, new Attribute("vUv", "vec2<f32>")],
// WGSL
vertex: /* wgsl */ `
var output: Output;
output.vUv = uv;
output.vUv.x = output.vUv.x * (system.resolution.x / system.resolution.y);
output.position = vec4<f32>(position, 0.0, 1.0);
return output;
`,
fragmentBody: /* wgsl */ `${Shaders.NOISE.WORLEY.WORLEY2D.replace(
"let jitter = 1.0;",
"let jitter = 1.0 + 0.5 * sin(2.0 * system.time);"
)}
`,
fragment: /* wgsl */ `
var p = vUv;
p.x = p.x + system.time * 0.01;
p.y = p.y + system.time * 0.01;
let uv = 1.2 * worley2d(p * 20.0);
var color = mix(vec3<f32>(0.5, 0.0, 0.0), vec3<f32>(0.9, 0.0, 0.0), uv.y);
color = mix(color, vec3<f32>(1.0), uv.x);
return vec4<f32>(color.x, color.y, color.z, 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 drawTriangleCommand = new Command({
pipeline,
bindGroups: [systemUniformBindGroup],
vertexBuffers: [geometryVertexBuffer],
indexBuffer: geometryIndicesBuffer,
indexFormat: GPUIndexFormat.Uint32,
count: 3,
});
const onResize = () => {
context.resize(window.innerWidth, window.innerHeight);
systemUniformsBuffer.setSubData(
0,
Float32Array.of(window.innerWidth, window.innerHeight)
);
};
onResize();
// Frame
requestAnimationFrame(function frame() {
if (State.error) return;
clock.getDelta();
systemUniformsBuffer.setSubData(
2 * Float32Array.BYTES_PER_ELEMENT,
Float32Array.of(clock.time)
);
context.render(() => {
context.submit(clearCommand, () => {
context.submit(drawTriangleCommand);
});
});
requestAnimationFrame(frame);
});
clock.start();
window.addEventListener("resize", onResize);