This repository was archived by the owner on Nov 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathwebgpu_sandbox.html
240 lines (161 loc) · 7.55 KB
/
webgpu_sandbox.html
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
<html lang="en">
<head>
<title>Verge3D - WebGPU - Sandbox</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<link type="text/css" rel="stylesheet" href="main.css">
</head>
<body>
<div id="info">
<a href="https://www.soft8soft.com/verge3d" target="_blank" rel="noopener">Verge3D</a> WebGPU - Sandbox
</div>
<script async src="https://unpkg.com/es-module-shims@1.3.6/dist/es-module-shims.js"></script>
<script type="importmap">
{
"imports": {
"v3d": "../build/v3d.module.js",
"v3d/addons/": "./jsm/",
"three/nodes": "./jsm/nodes/Nodes.js"
}
}
</script>
<script type="module">
import * as v3d from 'v3d';
import * as Nodes from 'three/nodes';
import { DDSLoader } from 'v3d/addons/loaders/DDSLoader.js';
import WebGPU from 'v3d/addons/capabilities/WebGPU.js';
import WebGPURenderer from 'v3d/addons/renderers/webgpu/WebGPURenderer.js';
let camera, scene, renderer;
let box;
init();
function init() {
if (WebGPU.isAvailable() === false) {
document.body.appendChild(WebGPU.getErrorMessage());
throw new Error('No WebGPU support');
}
camera = new v3d.PerspectiveCamera(70, window.innerWidth / window.innerHeight, 0.1, 10);
camera.position.z = 4;
scene = new v3d.Scene();
scene.background = new v3d.Color(0x222222);
// textures
const textureLoader = new v3d.TextureLoader();
const texture = textureLoader.load('./textures/uv_grid_opengl.jpg');
texture.wrapS = v3d.RepeatWrapping;
texture.wrapT = v3d.RepeatWrapping;
texture.name = 'uv_grid';
const textureDisplace = textureLoader.load('./textures/transition/transition1.png');
textureDisplace.wrapS = v3d.RepeatWrapping;
textureDisplace.wrapT = v3d.RepeatWrapping;
// compressed texture
const ddsLoader = new DDSLoader();
const dxt5Texture = ddsLoader.load('./textures/compressed/explosion_dxt5_mip.dds');
// box mesh
const geometryBox = new v3d.BoxGeometry();
const materialBox = new Nodes.MeshBasicNodeMaterial();
const timerNode = new Nodes.TimerNode();
// birection speed
const timerScaleNode = new Nodes.OperatorNode('*', timerNode, new Nodes.ConstNode(new v3d.Vector2(- 0.5, 0.1)));
const animateUV = new Nodes.OperatorNode('+', new Nodes.UVNode(), timerScaleNode);
const textureNode = new Nodes.TextureNode(texture, animateUV);
materialBox.colorNode = new Nodes.MathNode('mix', textureNode, new Nodes.CheckerNode(animateUV), new Nodes.UniformNode(.5));
// test uv 2
//geometryBox.setAttribute('uv2', geometryBox.getAttribute('uv'));
//materialBox.colorNode = new TextureNode(texture, new UVNode(1));
box = new v3d.Mesh(geometryBox, materialBox);
box.position.set(0, 1, 0);
scene.add(box);
// displace example
const geometrySphere = new v3d.SphereGeometry(.5, 64, 64);
const materialSphere = new Nodes.MeshBasicNodeMaterial();
const displaceAnimated = new Nodes.SplitNode(new Nodes.TextureNode(textureDisplace), 'x');
const displaceY = new Nodes.OperatorNode('*', displaceAnimated, new Nodes.ConstNode(.25));
const displace = new Nodes.OperatorNode('*', new Nodes.NormalNode(Nodes.NormalNode.LOCAL), displaceY);
materialSphere.colorNode = displaceY;
materialSphere.positionNode = new Nodes.OperatorNode('+', new Nodes.PositionNode(Nodes.PositionNode.LOCAL), displace);
const sphere = new v3d.Mesh(geometrySphere, materialSphere);
sphere.position.set(- 2, -1, 0);
scene.add(sphere);
// data texture
const geometryPlane = new v3d.PlaneGeometry();
const materialPlane = new Nodes.MeshBasicNodeMaterial();
materialPlane.colorNode = new Nodes.OperatorNode('+', new Nodes.TextureNode(createDataTexture()), new Nodes.UniformNode(new v3d.Color(0x0000FF)));
materialPlane.opacityNode = new Nodes.SplitNode(new Nodes.TextureNode(dxt5Texture), 'a');
materialPlane.transparent = true;
const plane = new v3d.Mesh(geometryPlane, materialPlane);
plane.position.set(0, -1, 0);
scene.add(plane);
// compressed texture
const materialCompressed = new Nodes.MeshBasicNodeMaterial();
materialCompressed.colorNode = new Nodes.TextureNode(dxt5Texture);
materialCompressed.emissiveNode = new Nodes.UniformNode(new v3d.Color(0x663300));
materialCompressed.alphaTestNode = new Nodes.OscNode();
materialCompressed.transparent = true;
const boxCompressed = new v3d.Mesh(geometryBox, materialCompressed);
boxCompressed.position.set(- 2, 1, 0);
scene.add(boxCompressed);
// points
const points = [];
for (let i = 0; i < 1000; i++) {
const point = new v3d.Vector3().random().subScalar(0.5);
points.push(point);
}
const geometryPoints = new v3d.BufferGeometry().setFromPoints(points);
const materialPoints = new Nodes.PointsNodeMaterial();
materialPoints.colorNode = new Nodes.OperatorNode('*', new Nodes.PositionNode(), new Nodes.ConstNode(3));
const pointCloud = new v3d.Points(geometryPoints, materialPoints);
pointCloud.position.set(2, -1, 0);
scene.add(pointCloud);
// lines
const geometryLine = new v3d.BufferGeometry().setFromPoints([
new v3d.Vector3(- 0.5, -0.5, 0),
new v3d.Vector3(0.5, -0.5, 0),
new v3d.Vector3(0.5, 0.5, 0),
new v3d.Vector3(- 0.5, 0.5, 0)
]);
geometryLine.setAttribute('color', geometryLine.getAttribute('position'));
const materialLine = new Nodes.LineBasicNodeMaterial();
materialLine.colorNode = new Nodes.AttributeNode('color');
const line = new v3d.Line(geometryLine, materialLine);
line.position.set(2, 1, 0);
scene.add(line);
//
renderer = new WebGPURenderer({ requiredFeatures: ['texture-compression-bc'] });
renderer.setPixelRatio(window.devicePixelRatio);
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setAnimationLoop(animate);
document.body.appendChild(renderer.domElement);
window.addEventListener('resize', onWindowResize);
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}
function animate() {
box.rotation.x += 0.01;
box.rotation.y += 0.02;
renderer.render(scene, camera);
}
function createDataTexture() {
const color = new v3d.Color(0xff0000);
const width = 512;
const height = 512;
const size = width * height;
const data = new Uint8Array(4 * size);
const r = Math.floor(color.r * 255);
const g = Math.floor(color.g * 255);
const b = Math.floor(color.b * 255);
for (let i = 0; i < size; i++) {
const stride = i * 4;
data[stride] = r;
data[stride + 1] = g;
data[stride + 2] = b;
data[stride + 3] = 255;
}
const texture = new v3d.DataTexture(data, width, height, v3d.RGBAFormat);
texture.needsUpdate = true;
return texture;
}
</script>
</body>
</html>