-
Notifications
You must be signed in to change notification settings - Fork 14
/
index.js
394 lines (253 loc) · 8.18 KB
/
index.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
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
/**************************
* Dusan Bosnjak @pailhead
**************************/
module.exports = function( THREE ){
const differentSignature = parseInt(THREE.REVISION) >= 96
//monkeypatch shaders
require('./monkey-patch.js')(THREE);
//depth mat
var DEPTH_MATERIAL = new THREE.MeshDepthMaterial();
DEPTH_MATERIAL.depthPacking = THREE.RGBADepthPacking;
DEPTH_MATERIAL.clipping = true;
DEPTH_MATERIAL.defines = {
INSTANCE_TRANSFORM: ''
};
//distance mat
var
DISTANCE_SHADER = THREE.ShaderLib[ "distanceRGBA" ],
DISTANCE_UNIFORMS = THREE.UniformsUtils.clone( DISTANCE_SHADER.uniforms ),
DISTANCE_DEFINES = {
'USE_SHADOWMAP': '',
'INSTANCE_TRANSFORM': ''
},
DISTANCE_MATERIAL = new THREE.ShaderMaterial( {
defines: DISTANCE_DEFINES,
uniforms: DISTANCE_UNIFORMS,
vertexShader: DISTANCE_SHADER.vertexShader,
fragmentShader: DISTANCE_SHADER.fragmentShader,
clipping: true
})
;
//main class
THREE.InstancedMesh = function (
bufferGeometry,
material,
numInstances,
dynamic,
colors,
uniformScale
) {
THREE.Mesh.call( this , (new THREE.InstancedBufferGeometry()).copy( bufferGeometry ) ); //hacky for now
this._dynamic = !!dynamic; //TODO: set a bit mask for different attributes?
this._uniformScale = !!uniformScale;
this._colors = !!colors;
this.numInstances = numInstances;
this._setAttributes();
/**
* use the setter to decorate this material
* this is in lieu of changing the renderer
* WebGLRenderer injects stuff like this
*/
this.material = material.clone();
this.frustumCulled = false; //you can uncheck this if you generate your own bounding info
//make it work with depth effects
this.customDepthMaterial = DEPTH_MATERIAL;
this.customDistanceMaterial = DISTANCE_MATERIAL;
}
THREE.InstancedMesh.prototype = Object.create( THREE.Mesh.prototype );
THREE.InstancedMesh.constructor = THREE.InstancedMesh;
//this is kinda gnarly, done in order to avoid setting these defines in the WebGLRenderer (it manages most if not all of the define flags)
Object.defineProperties( THREE.InstancedMesh.prototype , {
'material': {
set: function( m ){
/**
* whenever a material is set, decorate it,
* if a material used with regular geometry is passed,
* it will mutate it which is bad mkay
*
* either flag Material with these instance properties:
*
* "i want to create a RED PLASTIC material that will
* be INSTANCED and i know it will be used on clones
* that are known to be UNIFORMly scaled"
* (also figure out where dynamic fits here)
*
* or check here if the material has INSTANCE_TRANSFORM
* define set, if not, clone, document that it breaks reference
* or do a shallow copy or something
*
* or something else?
*/
m = m.clone();
if ( m.defines ) {
m.defines.INSTANCE_TRANSFORM = '';
if ( this._uniformScale ) m.defines.INSTANCE_UNIFORM = ''; //an optimization, should avoid doing an expensive matrix inverse in the shader
else delete m.defines['INSTANCE_UNIFORM'];
if ( this._colors ) m.defines.INSTANCE_COLOR = '';
else delete m.defines['INSTANCE_COLOR'];
}
else{
m.defines = { INSTANCE_TRANSFORM: '' };
if ( this._uniformScale ) m.defines.INSTANCE_UNIFORM = '';
if ( this._colors ) m.defines.INSTANCE_COLOR = '';
}
this._material = m;
},
get: function(){ return this._material; }
},
//force new attributes to be created when set?
'numInstances': {
set: function( v ){
this._numInstances = v;
//reset buffers
this._setAttributes();
},
get: function(){ return this._numInstances; }
},
//do some auto-magic when BufferGeometry is set
//TODO: account for Geometry, or change this approach completely
'geometry':{
set: function( g ){
//if its not already instanced attach buffers
if ( !!g.attributes.instancePosition ) {
this._geometry = new THREE.InstancedBufferGeometry();
this._setAttributes();
}
else
this._geometry = g;
},
get: function(){ return this._geometry; }
}
});
THREE.InstancedMesh.prototype.setPositionAt = function( index , position ){
this.geometry.attributes.instancePosition.setXYZ( index , position.x , position.y , position.z );
};
THREE.InstancedMesh.prototype.setQuaternionAt = function ( index , quat ) {
this.geometry.attributes.instanceQuaternion.setXYZW( index , quat.x , quat.y , quat.z , quat.w );
};
THREE.InstancedMesh.prototype.setScaleAt = function ( index , scale ) {
this.geometry.attributes.instanceScale.setXYZ( index , scale.x , scale.y , scale.z );
};
THREE.InstancedMesh.prototype.setColorAt = function ( index , color ) {
if( !this._colors ) {
console.warn( 'THREE.InstancedMesh: color not enabled');
return;
}
this.geometry.attributes.instanceColor.setXYZ(
index ,
Math.floor( color.r * 255 ),
Math.floor( color.g * 255 ),
Math.floor( color.b * 255 )
);
};
THREE.InstancedMesh.prototype.getPositionAt = function( index , position ){
var arr = this.geometry.attributes.instancePosition.array;
index *= 3;
return position ?
position.set( arr[index++], arr[index++], arr[index] ) :
new THREE.Vector3( arr[index++], arr[index++], arr[index] )
;
};
THREE.InstancedMesh.prototype.getQuaternionAt = function ( index , quat ) {
var arr = this.geometry.attributes.instanceQuaternion.array;
index = index << 2;
return quat ?
quat.set( arr[index++], arr[index++], arr[index++], arr[index] ) :
new THREE.Quaternion( arr[index++], arr[index++], arr[index++], arr[index] )
;
};
THREE.InstancedMesh.prototype.getScaleAt = function ( index , scale ) {
var arr = this.geometry.attributes.instanceScale.array;
index *= 3;
return scale ?
scale.set( arr[index++], arr[index++], arr[index] ) :
new THREE.Vector3( arr[index++], arr[index++], arr[index] )
;
};
THREE.InstancedMesh.prototype.getColorAt = (function(){
var inv255 = 1/255;
return function ( index , color ) {
if( !this._colors ) {
console.warn( 'THREE.InstancedMesh: color not enabled');
return false;
}
var arr = this.geometry.attributes.instanceColor.array;
index *= 3;
return color ?
color.setRGB( arr[index++] * inv255, arr[index++] * inv255, arr[index] * inv255 ) :
new THREE.Vector3( arr[index++], arr[index++], arr[index] ).multiplyScalar( inv255 )
;
};
})()
THREE.InstancedMesh.prototype.needsUpdate = function( attribute ){
switch ( attribute ){
case 'position' :
this.geometry.attributes.instancePosition.needsUpdate = true;
break;
case 'quaternion' :
this.geometry.attributes.instanceQuaternion.needsUpdate = true;
break;
case 'scale' :
this.geometry.attributes.instanceScale.needsUpdate = true;
break;
case 'colors' :
this.geometry.attributes.instanceColor.needsUpdate = true;
break;
default:
this.geometry.attributes.instancePosition.needsUpdate = true;
this.geometry.attributes.instanceQuaternion.needsUpdate = true;
this.geometry.attributes.instanceScale.needsUpdate = true;
if(this._colors){
this.geometry.attributes.instanceColor.needsUpdate = true;
}
break;
}
};
THREE.InstancedMesh.prototype._setAttributes = function(){
var normalized = true
var meshPerAttribute = 1
var vec4Size = 4
var vec3Size = 3
var attributes = {
instancePosition: [
new Float32Array( this.numInstances * vec3Size ),
vec3Size,
!normalized,
meshPerAttribute,
],
instanceQuaternion: [
new Float32Array( this.numInstances * vec4Size ),
vec4Size,
!normalized,
meshPerAttribute,
],
instanceScale: [
new Float32Array( this.numInstances * vec3Size ),
vec3Size,
!normalized,
meshPerAttribute,
]
}
if ( this._colors ){
attributes.instanceColor = [
new Uint8Array( this.numInstances * vec3Size ),
vec3Size,
normalized,
meshPerAttribute,
]
}
Object.keys(attributes).forEach(name=>{
const a = attributes[name]
let attribute
if(differentSignature){
attribute = new THREE.InstancedBufferAttribute(...a)
} else {
attribute = new THREE.InstancedBufferAttribute(a[0],a[1],a[3])
attribute.normalized = a[2]
}
attribute.dynamic = this._dynamic
this.geometry.addAttribute(name, attribute)
})
};
return THREE.InstancedMesh;
};