This repository has been archived by the owner on Jun 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathindex.html
362 lines (256 loc) · 10.3 KB
/
index.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
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
<!DOCTYPE html>
<meta charset="utf-8">
<title>three.js • examples • shade node loader</title>
<style>
html, body {
margin: 0;
padding: 0;
background: #000;
width: 100%;
height: 100%;
overflow: hidden;
}
p {
margin: 0;
}
.attribution {
width: 100%;
max-width: 400px;
color: #424242;
position: fixed;
bottom: 2em;
left: 0;
right: 0;
margin: 0 auto;
background: rgba(255,255,255, 0.8);
padding: 0.5em 1em 0.7em;
border-radius: 2px;
font-family: monospace;
text-align: center;
max-width: 402px;
}
</style>
<script src="./node_modules/dat.gui/build/dat.gui.js"></script>
<script src="./node_modules/three/build/three.js"></script>
<script src="./node_modules/three/examples/js/controls/OrbitControls.js"></script>
<script src="./node_modules/three/examples/js/loaders/GLTFLoader.js"></script>
<script src="./node_modules/three/examples/js/loaders/RGBELoader.js"></script>
<script src="./node_modules/three/examples/js/loaders/HDRCubeTextureLoader.js"></script>
<script src="./node_modules/three/examples/js/pmrem/PMREMGenerator.js"></script>
<script src="./node_modules/three/examples/js/pmrem/PMREMCubeUVPacker.js"></script>
<script type="module">
import { ShadeNodeLoader } from './ShadeNodeLoader.js';
import { TextureNode, TimerNode, FloatNode, Vector3Node } from './node_modules/three/examples/js/nodes/Nodes.js';
const renderer = new THREE.WebGLRenderer( { antialias: true } );
renderer.setSize( window.innerWidth, window.innerHeight );
renderer.setClearColor( 0x202020 );
renderer.physicallyCorrectLights = true;
renderer.gammaOutput = true;
renderer.gammaFactor = 2.2;
document.body.appendChild( renderer.domElement );
const timerNode = new TimerNode( 1.0 );
const micNode = new FloatNode( 0.01 );
const otherProps = [];
let mesh, geometry, material, envMap;
//
const props = {
suzanne: null,
grass: null,
roundedCube: null,
cube: new THREE.BoxBufferGeometry(1.8, 1.8, 1.8),
sphere: new THREE.SphereBufferGeometry( 1.8, 32, 32 ),
plane: new THREE.PlaneBufferGeometry( 5, 5, 64, 64 ).rotateX( - 1 * Math.PI / 2 )
};
const examples = [
{name: 'Instancing Grid', shader: 'assets/shaders/InstancingGrid/Graph.json', prop: 'roundedCube', camera: [ 25, 20, 25 ], grid: false },
{name: 'Checkers', shader: 'assets/shaders/Checkers/Graph.json', prop: 'cube', camera: [ 5, 3, 4 ] },
{name: 'Dissolve', shader: 'assets/shaders/Dissolve/Graph.json', prop: 'suzanne', camera: [ 5, 3, 4 ] },
{name: 'Fake Volume', shader: 'assets/shaders/FakeVolume/Graph.json', prop: 'sphere', camera: [ 5, 3, 4 ] },
{name: 'Grass', shader: 'assets/shaders/Grass/Graph.json', prop: 'grass', camera: [ 10, 8, 10 ] },
{name: 'Hologram', shader: 'assets/shaders/Hologram/Graph.json', prop: 'suzanne', camera: [ 5, 3, 4 ] },
{name: 'Wire Ripple', shader: 'assets/shaders/WireRipple/Graph.json', prop: 'plane', camera: [ 5, 3, 4 ] },
{name: 'Wireframe', shader: 'assets/shaders/Wireframe/Graph.json', prop: 'sphere', camera: [ 5, 3, 4 ] },
// {name: '[broken] Clouds', shader: 'assets/shaders/Clouds/Graph.json', prop: 'cube', camera: [ 5, 3, 4 ] },
{name: '[broken] Explosion', shader: 'assets/shaders/Explosion/Graph.json', prop: 'sphere', camera: [ 25, 20, 25 ] },
// {name: '[broken] Mask Blend', shader: 'assets/shaders/MaskBlend/Graph.json', prop: 'cube', camera: [ 5, 3, 4 ] },
{name: '[broken] Ripple', shader: 'assets/shaders/Ripple/Graph.json', prop: 'plane', camera: [ 5, 3, 4 ] },
];
let selectedExample = examples[ 0 ];
const api = { model: selectedExample.name };
const gui = new dat.GUI();
gui.add( api, 'model', examples.map( ( d ) => d.name ) ).onChange( () => {
selectedExample = examples.find( ( d ) => api.model === d.name );
mesh.parent.remove( mesh );
mesh.geometry.dispose();
mesh.material.dispose();
otherProps.forEach( ( o ) => o.parent.remove( o ) );
otherProps.length = 0;
loadShader( selectedExample.shader ).then( ( _material ) => {
material = _material;
animate();
} );
} );
//
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.01, 100 );
scene.add( camera );
const controls = new THREE.OrbitControls( camera, renderer.domElement );
controls.target.set( 0, 1, 0 );
const dirLight = new THREE.DirectionalLight( 0xBBBBDD, 1.0 );
dirLight.position.set( 20, 12, 5 );
scene.add( dirLight );
const hemiLight = new THREE.HemisphereLight( 0xDDBBBB, 0xBBBBDD, 1.0 );
scene.add( hemiLight );
const directLight = new THREE.PointLight( 0x888888, 1.0 );
directLight.position.set( 3, 5, 3 );
scene.add( directLight );
const gridHelper = new THREE.GridHelper()
scene.add( gridHelper );
const clock = new THREE.Clock();
//
const modelLoader = new THREE.GLTFLoader();
const loadGeometry = ( url ) => new Promise( ( resolve, reject ) => {
modelLoader.load( url, ( { scene } ) => resolve( scene.children[ 0 ].geometry ), undefined, reject );
} );
//
const loader = new ShadeNodeLoader();
const customNodeFactory = ( nodeDef ) => {
switch ( nodeDef.class ) {
case 'TimeNode':
return timerNode;
case 'MicNode':
// TODO(donmccurdy): Add some audio.
return micNode;
case 'PropNode':
const prop = new THREE.Mesh(
new THREE.SphereBufferGeometry( 1, 32, 32 ),
new THREE.MeshStandardMaterial( { color: 0x858585, roughness: 0.8, metalness: 1 } )
);
prop.name = nodeDef.options.userLabel || '';
prop.position.fromArray( nodeDef.options.position );
prop.scale.fromArray( nodeDef.options.scale );
// prop.material.envMap = envMap;
scene.add( prop );
otherProps.push( prop );
return new Vector3Node(
prop.position.x,
prop.position.y,
prop.position.z
);
}
throw new Error( `Unprovided node type: ${nodeDef.class}.` );
};
loader.setFactory( customNodeFactory );
const loadShader = ( url ) => new Promise( ( resolve, reject ) => {
loader.load( url, resolve, undefined, reject );
} );
//
Promise.all([
loadShader( selectedExample.shader ),
loadEnvironment( 'assets/environments/FootprintCourt/' ),
loadGeometry( 'assets/Suzanne.glb' ),
loadGeometry( 'assets/GrassBlade.glb' ),
loadGeometry( 'assets/RoundedCube.glb' ),
])
.then( ( [ _material, _envMap, suzanne, grass, roundedCube ] ) => {
material = _material;
envMap = _envMap;
props.suzanne = suzanne;
props.grass = grass;
props.roundedCube = roundedCube;
animate();
} )
.catch( ( e ) => {
console.error( e );
} );
//
function animate () {
geometry = props[selectedExample.prop];
// Instancing
if ( material.userData.instanceCount > 1 ) geometry = makeInstances( geometry, material );
// FWidthNode
if ( material.userData.needsDerivatives ) material = makeDerivatives( material );
// BarycentricNode
if ( material.userData.needsBarycentric ) geometry = makeBarycentric( geometry );
// TODO(donmccurdy): Environment map is not having a noticeable effect...
// material.envMap = new TextureNode( envMap );
mesh = new THREE.Mesh( geometry, material );
mesh.position.y = 1;
scene.add( mesh );
camera.position.fromArray( selectedExample.camera );
controls.update();
gridHelper.visible = selectedExample.grid === false ? false : true;
renderer.render( scene, camera );
renderer.setAnimationLoop( ( ) => {
timerNode.updateFrame( { delta: clock.getDelta(), time: clock.getElapsedTime() } );
renderer.render( scene, camera );
} );
// debug
Object.assign( window, { camera, scene, renderer, material, mesh } );
}
window.addEventListener( 'resize', () => {
const {clientHeight, clientWidth} = document.body;
camera.aspect = clientWidth / clientHeight;
camera.updateProjectionMatrix();
renderer.setSize( clientWidth, clientHeight );
} );
function makeDerivatives ( material ) {
const onBeforeCompile = material.onBeforeCompile;
material.onBeforeCompile = function () {
onBeforeCompile.apply(this, arguments);
material.extensions.derivatives = true;
};
return material;
}
function makeBarycentric ( geometry ) {
geometry = geometry.toNonIndexed();
const vertexCount = geometry.attributes.position.count;
const barycentric = new THREE.BufferAttribute( new Uint16Array( vertexCount * 3 ), 3 );
for ( let i = 0; i < geometry.attributes.position.count; i += 3 ) {
barycentric.setXYZ( i, 1, 0, 0 );
barycentric.setXYZ( i + 1, 0, 1, 0 );
barycentric.setXYZ( i + 2, 0, 0, 1 );
}
geometry.addAttribute( 'barycentric', barycentric );
return geometry;
}
function makeInstances ( geometry, material ) {
const instanceCount = material.userData.instanceCount;
const instanceID = new THREE.InstancedBufferAttribute(
new Float32Array( new Array( instanceCount ).fill( 0 ).map( ( _, index ) => index ) ),
1
);
geometry = new THREE.InstancedBufferGeometry().copy( geometry );
geometry.addAttribute( 'instanceID', instanceID );
geometry.maxInstancedCount = instanceCount;
return geometry;
}
function loadEnvironment ( path ) {
return Promise.resolve( null );
// const cubeMapURLs = [
// `${path}/posx.hdr`, `${path}/negx.hdr`,
// `${path}/posy.hdr`, `${path}/negy.hdr`,
// `${path}/posz.hdr`, `${path}/negz.hdr`
// ];
// return new Promise( ( resolve ) => {
// new THREE.HDRCubeTextureLoader().load( THREE.UnsignedByteType, cubeMapURLs, ( hdrCubeMap ) => {
// var pmremGenerator = new THREE.PMREMGenerator( hdrCubeMap );
// pmremGenerator.update( renderer );
// var pmremCubeUVPacker = new THREE.PMREMCubeUVPacker( pmremGenerator.cubeLods );
// pmremCubeUVPacker.update( renderer );
// resolve( pmremCubeUVPacker.CubeUVRenderTarget.texture );
// } );
// });
}
</script>
<div class="attribution">
<p>
three.js loader for <a href="https://shade.to/">Shade on iOS</a>
</p>
<p>•</p>
<p>
demo by Don McCurdy
(<a href="https://twitter.com/donrmccurdy">@donrmccurdy</a>,
<a href="https://github.com/donmccurdy/three-shadenodeloader">source code</a>)
</p>
</div>