forked from mrdoob/three.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebgl_buffergeometry_instancing_lambert.html
402 lines (280 loc) · 9.12 KB
/
webgl_buffergeometry_instancing_lambert.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
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
<!DOCTYPE html>
<html lang="en">
<head>
<title>three.js webgl - instancing - lambert shader</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<style>
body {
color: #ffffff;
font-family: Monospace;
font-size: 13px;
text-align: center;
font-weight: bold;
background-color: #000000;
margin: 0px;
overflow: hidden;
}
#info {
position: absolute;
top: 0px;
width: 100%;
padding: 5px;
}
a {
color: #ffffff;
}
#notSupported {
width: 50%;
margin: auto;
border: 2px red solid;
margin-top: 20px;
padding: 10px;
}
</style>
</head>
<body>
<div id="container"></div>
<div id="info">
<a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> - instancing - lambert shader
<div id="notSupported" style="display:none">Sorry your graphics card + browser does not support hardware instancing</div>
</div>
<script src="../build/three.js"></script>
<script src="js/WebGL.js"></script>
<script src="js/libs/stats.min.js"></script>
<script src="js/controls/OrbitControls.js"></script>
<script src="js/CurveExtras.js"></script>
<script>
if ( WEBGL.isWebGLAvailable() === false ) {
document.body.appendChild( WEBGL.getWebGLErrorMessage() );
}
THREE.ShaderLib.customDepthRGBA = { // this is a cut-and-paste of the depth shader -- modified to accommodate instancing for this app
uniforms: THREE.ShaderLib.depth.uniforms,
vertexShader:
`
// instanced
#ifdef INSTANCED
attribute vec3 instanceOffset;
attribute float instanceScale;
#endif
#include <common>
#include <uv_pars_vertex>
#include <displacementmap_pars_vertex>
#include <morphtarget_pars_vertex>
#include <skinning_pars_vertex>
#include <logdepthbuf_pars_vertex>
#include <clipping_planes_pars_vertex>
void main() {
#include <uv_vertex>
#include <skinbase_vertex>
#ifdef USE_DISPLACEMENTMAP
#include <beginnormal_vertex>
#include <morphnormal_vertex>
#include <skinnormal_vertex>
#endif
#include <begin_vertex>
// instanced
#ifdef INSTANCED
transformed *= instanceScale;
transformed = transformed + instanceOffset;
#endif
#include <morphtarget_vertex>
#include <skinning_vertex>
#include <displacementmap_vertex>
#include <project_vertex>
#include <logdepthbuf_vertex>
#include <clipping_planes_vertex>
}
`,
fragmentShader: THREE.ShaderChunk.depth_frag
};
THREE.ShaderLib.lambert = { // this is a cut-and-paste of the lambert shader -- modified to accommodate instancing for this app
uniforms: THREE.ShaderLib.lambert.uniforms,
vertexShader:
`
#define LAMBERT
#ifdef INSTANCED
attribute vec3 instanceOffset;
attribute vec3 instanceColor;
attribute float instanceScale;
#endif
varying vec3 vLightFront;
varying vec3 vIndirectFront;
#ifdef DOUBLE_SIDED
varying vec3 vLightBack;
varying vec3 vIndirectBack;
#endif
#include <common>
#include <uv_pars_vertex>
#include <uv2_pars_vertex>
#include <envmap_pars_vertex>
#include <bsdfs>
#include <lights_pars_begin>
#include <color_pars_vertex>
#include <fog_pars_vertex>
#include <morphtarget_pars_vertex>
#include <skinning_pars_vertex>
#include <shadowmap_pars_vertex>
#include <logdepthbuf_pars_vertex>
#include <clipping_planes_pars_vertex>
void main() {
#include <uv_vertex>
#include <uv2_vertex>
#include <color_vertex>
// vertex colors instanced
#ifdef INSTANCED
#ifdef USE_COLOR
vColor.xyz = instanceColor.xyz;
#endif
#endif
#include <beginnormal_vertex>
#include <morphnormal_vertex>
#include <skinbase_vertex>
#include <skinnormal_vertex>
#include <defaultnormal_vertex>
#include <begin_vertex>
// position instanced
#ifdef INSTANCED
transformed *= instanceScale;
transformed = transformed + instanceOffset;
#endif
#include <morphtarget_vertex>
#include <skinning_vertex>
#include <project_vertex>
#include <logdepthbuf_vertex>
#include <clipping_planes_vertex>
#include <worldpos_vertex>
#include <envmap_vertex>
#include <lights_lambert_vertex>
#include <shadowmap_vertex>
#include <fog_vertex>
}
`,
fragmentShader: THREE.ShaderLib.lambert.fragmentShader
};
//
var mesh, renderer, scene, camera, controls;
var stats;
init();
animate();
function init() {
renderer = new THREE.WebGLRenderer( { antialias: true } );
renderer.setSize( window.innerWidth, window.innerHeight );
renderer.shadowMap.enabled = true;
document.body.appendChild( renderer.domElement );
renderer.gammaOutput = true;
scene = new THREE.Scene();
scene.fog = new THREE.FogExp2( 0x000000, 0.004 );
renderer.setClearColor( scene.fog.color, 1 );
camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 1000 );
camera.position.set( 80, 40, 80 );
scene.add( camera );
controls = new THREE.OrbitControls( camera, renderer.domElement );
controls.enableZoom = false;
controls.maxPolarAngle = Math.PI / 2;
scene.add( new THREE.AmbientLight( 0xffffff, 0.7 ) );
var light = new THREE.DirectionalLight( 0xffffff, 0.4 );
light.position.set( 50, 40, 0 );
light.castShadow = true;
light.shadow.camera.left = - 40;
light.shadow.camera.right = 40;
light.shadow.camera.top = 40;
light.shadow.camera.bottom = - 40;
light.shadow.camera.near = 10;
light.shadow.camera.far = 180;
light.shadow.bias = - 0.001;
light.shadow.mapSize.width = 512;
light.shadow.mapSize.height = 512;
scene.add( light );
// light shadow camera helper
//light.shadowCameraHelper = new THREE.CameraHelper( light.shadow.camera );
//scene.add( light.shadowCameraHelper );
// instanced buffer geometry
var geometry = new THREE.InstancedBufferGeometry();
geometry.copy( new THREE.TorusBufferGeometry( 2, 0.5, 8, 128 ) );
const INSTANCES = 256;
var knot = new THREE.Curves.TorusKnot( 10 );
var positions = knot.getSpacedPoints( INSTANCES );
var offsets = new Float32Array( INSTANCES * 3 ); // xyz
var colors = new Float32Array( INSTANCES * 3 ); // rgb
var scales = new Float32Array( INSTANCES * 1 ); // s
for ( var i = 0, l = INSTANCES; i < l; i ++ ) {
var index = 3 * i;
// per-instance position offset
offsets[ index ] = positions[ i ].x;
offsets[ index + 1 ] = positions[ i ].y;
offsets[ index + 2 ] = positions[ i ].z;
// per-instance color tint - optional
colors[ index ] = 1;
colors[ index + 1 ] = 1;
colors[ index + 2 ] = 1;
// per-instance scale variation
scales[ i ] = 1 + 0.5 * Math.sin( 32 * Math.PI * i / INSTANCES );
}
geometry.addAttribute( 'instanceOffset', new THREE.InstancedBufferAttribute( offsets, 3 ) );
geometry.addAttribute( 'instanceColor', new THREE.InstancedBufferAttribute( colors, 3 ) );
geometry.addAttribute( 'instanceScale', new THREE.InstancedBufferAttribute( scales, 1 ) );
// material
var envMap = new THREE.TextureLoader().load( `textures/metal.jpg`, function ( texture ) {
texture.mapping = THREE.SphericalReflectionMapping;
texture.encoding = THREE.sRGBEncoding;
if ( mesh ) mesh.material.needsUpdate = true;
} );
var material = new THREE.MeshLambertMaterial( {
color: 0xffb54a,
envMap: envMap,
combine: THREE.MultiplyOperation,
reflectivity: 0.8,
vertexColors: THREE.VertexColors,
fog: true
} );
material.defines = material.defines || {};
material.defines[ 'INSTANCED' ] = "";
// custom depth material - required for instanced shadows
var shader = THREE.ShaderLib[ 'customDepthRGBA' ];
var uniforms = THREE.UniformsUtils.clone( shader.uniforms );
var customDepthMaterial = new THREE.ShaderMaterial( {
defines: {
'INSTANCED': "",
'DEPTH_PACKING': THREE.RGBADepthPacking
},
uniforms: uniforms,
vertexShader: shader.vertexShader,
fragmentShader: shader.fragmentShader
} );
//
mesh = new THREE.Mesh( geometry, material );
mesh.scale.set( 1, 1, 2 );
mesh.castShadow = true;
mesh.receiveShadow = true;
mesh.customDepthMaterial = customDepthMaterial;
mesh.frustumCulled = false;
scene.add( mesh );
//
var ground = new THREE.Mesh(
new THREE.PlaneBufferGeometry( 800, 800 ).rotateX( - Math.PI / 2 ),
new THREE.MeshPhongMaterial( { color: 0x888888 } )
);
ground.position.set( 0, - 40, 0 );
ground.receiveShadow = true;
scene.add( ground );
//
stats = new Stats();
document.body.appendChild( stats.dom );
//
window.addEventListener( 'resize', onWindowResize, false );
}
function onWindowResize() {
renderer.setSize( window.innerWidth, window.innerHeight );
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
}
function animate() {
requestAnimationFrame( animate );
mesh.rotation.y += 0.005;
stats.update();
renderer.render( scene, camera );
}
</script>
</body>
</html>