-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInstancedMesh.js
More file actions
214 lines (117 loc) · 4.25 KB
/
Copy pathInstancedMesh.js
File metadata and controls
214 lines (117 loc) · 4.25 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
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
import { InstancedBufferAttribute } from '../core/InstancedBufferAttribute.js';
import { Mesh } from './Mesh.js';
import { Box3 } from '../math/Box3.js';
import { Matrix4 } from '../math/Matrix4.js';
import { Sphere } from '../math/Sphere.js';
const _instanceLocalMatrix = /*@__PURE__*/ new Matrix4();
const _instanceWorldMatrix = /*@__PURE__*/ new Matrix4();
const _instanceIntersects = [];
const _box3 = /*@__PURE__*/ new Box3();
const _identity = /*@__PURE__*/ new Matrix4();
const _mesh = /*@__PURE__*/ new Mesh();
const _sphere = /*@__PURE__*/ new Sphere();
class InstancedMesh extends Mesh {
constructor( geometry, material, count ) {
super( geometry, material );
this.isInstancedMesh = true;
this.instanceMatrix = new InstancedBufferAttribute( new Float32Array( count * 16 ), 16 );
this.instanceColor = null;
this.count = count;
this.boundingBox = null;
this.boundingSphere = null;
for ( let i = 0; i < count; i ++ ) {
this.setMatrixAt( i, _identity );
}
}
computeBoundingBox() {
const geometry = this.geometry;
const count = this.count;
if ( this.boundingBox === null ) {
this.boundingBox = new Box3();
}
if ( geometry.boundingBox === null ) {
geometry.computeBoundingBox();
}
this.boundingBox.makeEmpty();
for ( let i = 0; i < count; i ++ ) {
this.getMatrixAt( i, _instanceLocalMatrix );
_box3.copy( geometry.boundingBox ).applyMatrix4( _instanceLocalMatrix );
this.boundingBox.union( _box3 );
}
}
computeBoundingSphere() {
const geometry = this.geometry;
const count = this.count;
if ( this.boundingSphere === null ) {
this.boundingSphere = new Sphere();
}
if ( geometry.boundingSphere === null ) {
geometry.computeBoundingSphere();
}
this.boundingSphere.makeEmpty();
for ( let i = 0; i < count; i ++ ) {
this.getMatrixAt( i, _instanceLocalMatrix );
_sphere.copy( geometry.boundingSphere ).applyMatrix4( _instanceLocalMatrix );
this.boundingSphere.union( _sphere );
}
}
copy( source, recursive ) {
super.copy( source, recursive );
this.instanceMatrix.copy( source.instanceMatrix );
if ( source.instanceColor !== null ) this.instanceColor = source.instanceColor.clone();
this.count = source.count;
if ( source.boundingBox !== null ) this.boundingBox = source.boundingBox.clone();
if ( source.boundingSphere !== null ) this.boundingSphere = source.boundingSphere.clone();
return this;
}
getColorAt( index, color ) {
color.fromArray( this.instanceColor.array, index * 3 );
}
getMatrixAt( index, matrix ) {
matrix.fromArray( this.instanceMatrix.array, index * 16 );
}
raycast( raycaster, intersects ) {
const matrixWorld = this.matrixWorld;
const raycastTimes = this.count;
_mesh.geometry = this.geometry;
_mesh.material = this.material;
if ( _mesh.material === undefined ) return;
// test with bounding sphere first
if ( this.boundingSphere === null ) this.computeBoundingSphere();
_sphere.copy( this.boundingSphere );
_sphere.applyMatrix4( matrixWorld );
if ( raycaster.ray.intersectsSphere( _sphere ) === false ) return;
// now test each instance
for ( let instanceId = 0; instanceId < raycastTimes; instanceId ++ ) {
// calculate the world matrix for each instance
this.getMatrixAt( instanceId, _instanceLocalMatrix );
_instanceWorldMatrix.multiplyMatrices( matrixWorld, _instanceLocalMatrix );
// the mesh represents this single instance
_mesh.matrixWorld = _instanceWorldMatrix;
_mesh.raycast( raycaster, _instanceIntersects );
// process the result of raycast
for ( let i = 0, l = _instanceIntersects.length; i < l; i ++ ) {
const intersect = _instanceIntersects[ i ];
intersect.instanceId = instanceId;
intersect.object = this;
intersects.push( intersect );
}
_instanceIntersects.length = 0;
}
}
setColorAt( index, color ) {
if ( this.instanceColor === null ) {
this.instanceColor = new InstancedBufferAttribute( new Float32Array( this.instanceMatrix.count * 3 ), 3 );
}
color.toArray( this.instanceColor.array, index * 3 );
}
setMatrixAt( index, matrix ) {
matrix.toArray( this.instanceMatrix.array, index * 16 );
}
updateMorphTargets() {
}
dispose() {
this.dispatchEvent( { type: 'dispose' } );
}
}
export { InstancedMesh };