UNPKG

4.92 kBJavaScriptView Raw
1( function () {
2
3 const _box = new THREE.Box3();
4
5 const _vector = new THREE.Vector3();
6
7 class LineSegmentsGeometry extends THREE.InstancedBufferGeometry {
8
9 constructor() {
10
11 super();
12 this.type = 'LineSegmentsGeometry';
13 const positions = [ - 1, 2, 0, 1, 2, 0, - 1, 1, 0, 1, 1, 0, - 1, 0, 0, 1, 0, 0, - 1, - 1, 0, 1, - 1, 0 ];
14 const uvs = [ - 1, 2, 1, 2, - 1, 1, 1, 1, - 1, - 1, 1, - 1, - 1, - 2, 1, - 2 ];
15 const index = [ 0, 2, 1, 2, 3, 1, 2, 4, 3, 4, 5, 3, 4, 6, 5, 6, 7, 5 ];
16 this.setIndex( index );
17 this.setAttribute( 'position', new THREE.Float32BufferAttribute( positions, 3 ) );
18 this.setAttribute( 'uv', new THREE.Float32BufferAttribute( uvs, 2 ) );
19
20 }
21
22 applyMatrix4( matrix ) {
23
24 const start = this.attributes.instanceStart;
25 const end = this.attributes.instanceEnd;
26
27 if ( start !== undefined ) {
28
29 start.applyMatrix4( matrix );
30 end.applyMatrix4( matrix );
31 start.needsUpdate = true;
32
33 }
34
35 if ( this.boundingBox !== null ) {
36
37 this.computeBoundingBox();
38
39 }
40
41 if ( this.boundingSphere !== null ) {
42
43 this.computeBoundingSphere();
44
45 }
46
47 return this;
48
49 }
50
51 setPositions( array ) {
52
53 let lineSegments;
54
55 if ( array instanceof Float32Array ) {
56
57 lineSegments = array;
58
59 } else if ( Array.isArray( array ) ) {
60
61 lineSegments = new Float32Array( array );
62
63 }
64
65 const instanceBuffer = new THREE.InstancedInterleavedBuffer( lineSegments, 6, 1 ); // xyz, xyz
66
67 this.setAttribute( 'instanceStart', new THREE.InterleavedBufferAttribute( instanceBuffer, 3, 0 ) ); // xyz
68
69 this.setAttribute( 'instanceEnd', new THREE.InterleavedBufferAttribute( instanceBuffer, 3, 3 ) ); // xyz
70 //
71
72 this.computeBoundingBox();
73 this.computeBoundingSphere();
74 return this;
75
76 }
77
78 setColors( array ) {
79
80 let colors;
81
82 if ( array instanceof Float32Array ) {
83
84 colors = array;
85
86 } else if ( Array.isArray( array ) ) {
87
88 colors = new Float32Array( array );
89
90 }
91
92 const instanceColorBuffer = new THREE.InstancedInterleavedBuffer( colors, 6, 1 ); // rgb, rgb
93
94 this.setAttribute( 'instanceColorStart', new THREE.InterleavedBufferAttribute( instanceColorBuffer, 3, 0 ) ); // rgb
95
96 this.setAttribute( 'instanceColorEnd', new THREE.InterleavedBufferAttribute( instanceColorBuffer, 3, 3 ) ); // rgb
97
98 return this;
99
100 }
101
102 fromWireframeGeometry( geometry ) {
103
104 this.setPositions( geometry.attributes.position.array );
105 return this;
106
107 }
108
109 fromEdgesGeometry( geometry ) {
110
111 this.setPositions( geometry.attributes.position.array );
112 return this;
113
114 }
115
116 fromMesh( mesh ) {
117
118 this.fromWireframeGeometry( new THREE.WireframeGeometry( mesh.geometry ) ); // set colors, maybe
119
120 return this;
121
122 }
123
124 fromLineSegments( lineSegments ) {
125
126 const geometry = lineSegments.geometry;
127
128 if ( geometry.isGeometry ) {
129
130 console.error( 'THREE.LineSegmentsGeometry no longer supports Geometry. Use THREE.BufferGeometry instead.' );
131 return;
132
133 } else if ( geometry.isBufferGeometry ) {
134
135 this.setPositions( geometry.attributes.position.array ); // assumes non-indexed
136
137 } // set colors, maybe
138
139
140 return this;
141
142 }
143
144 computeBoundingBox() {
145
146 if ( this.boundingBox === null ) {
147
148 this.boundingBox = new THREE.Box3();
149
150 }
151
152 const start = this.attributes.instanceStart;
153 const end = this.attributes.instanceEnd;
154
155 if ( start !== undefined && end !== undefined ) {
156
157 this.boundingBox.setFromBufferAttribute( start );
158
159 _box.setFromBufferAttribute( end );
160
161 this.boundingBox.union( _box );
162
163 }
164
165 }
166
167 computeBoundingSphere() {
168
169 if ( this.boundingSphere === null ) {
170
171 this.boundingSphere = new THREE.Sphere();
172
173 }
174
175 if ( this.boundingBox === null ) {
176
177 this.computeBoundingBox();
178
179 }
180
181 const start = this.attributes.instanceStart;
182 const end = this.attributes.instanceEnd;
183
184 if ( start !== undefined && end !== undefined ) {
185
186 const center = this.boundingSphere.center;
187 this.boundingBox.getCenter( center );
188 let maxRadiusSq = 0;
189
190 for ( let i = 0, il = start.count; i < il; i ++ ) {
191
192 _vector.fromBufferAttribute( start, i );
193
194 maxRadiusSq = Math.max( maxRadiusSq, center.distanceToSquared( _vector ) );
195
196 _vector.fromBufferAttribute( end, i );
197
198 maxRadiusSq = Math.max( maxRadiusSq, center.distanceToSquared( _vector ) );
199
200 }
201
202 this.boundingSphere.radius = Math.sqrt( maxRadiusSq );
203
204 if ( isNaN( this.boundingSphere.radius ) ) {
205
206 console.error( 'THREE.LineSegmentsGeometry.computeBoundingSphere(): Computed radius is NaN. The instanced position data is likely to have NaN values.', this );
207
208 }
209
210 }
211
212 }
213
214 toJSON() { // todo
215 }
216
217 applyMatrix( matrix ) {
218
219 console.warn( 'THREE.LineSegmentsGeometry: applyMatrix() has been renamed to applyMatrix4().' );
220 return this.applyMatrix4( matrix );
221
222 }
223
224 }
225
226 LineSegmentsGeometry.prototype.isLineSegmentsGeometry = true;
227
228 THREE.LineSegmentsGeometry = LineSegmentsGeometry;
229
230} )();