|
1 | 1 | ( function () { |
2 | 2 |
|
3 | | - var EdgeSplitModifier = function () { |
| 3 | + const _A = new THREE.Vector3(); |
4 | 4 |
|
5 | | - var A = new THREE.Vector3(); |
6 | | - var B = new THREE.Vector3(); |
7 | | - var C = new THREE.Vector3(); |
8 | | - var positions, normals; |
9 | | - var indexes; |
10 | | - var pointToIndexMap, splitIndexes; |
11 | | - let oldNormals; |
| 5 | + const _B = new THREE.Vector3(); |
12 | 6 |
|
13 | | - function computeNormals() { |
| 7 | + const _C = new THREE.Vector3(); |
14 | 8 |
|
15 | | - normals = new Float32Array( indexes.length * 3 ); |
| 9 | + class EdgeSplitModifier { |
16 | 10 |
|
17 | | - for ( var i = 0; i < indexes.length; i += 3 ) { |
| 11 | + modify( geometry, cutOffAngle, tryKeepNormals = true ) { |
18 | 12 |
|
19 | | - var index = indexes[ i ]; |
20 | | - A.set( positions[ 3 * index ], positions[ 3 * index + 1 ], positions[ 3 * index + 2 ] ); |
21 | | - index = indexes[ i + 1 ]; |
22 | | - B.set( positions[ 3 * index ], positions[ 3 * index + 1 ], positions[ 3 * index + 2 ] ); |
23 | | - index = indexes[ i + 2 ]; |
24 | | - C.set( positions[ 3 * index ], positions[ 3 * index + 1 ], positions[ 3 * index + 2 ] ); |
25 | | - C.sub( B ); |
26 | | - A.sub( B ); |
27 | | - var normal = C.cross( A ).normalize(); |
| 13 | + function computeNormals() { |
28 | 14 |
|
29 | | - for ( var j = 0; j < 3; j ++ ) { |
| 15 | + normals = new Float32Array( indexes.length * 3 ); |
30 | 16 |
|
31 | | - normals[ 3 * ( i + j ) ] = normal.x; |
32 | | - normals[ 3 * ( i + j ) + 1 ] = normal.y; |
33 | | - normals[ 3 * ( i + j ) + 2 ] = normal.z; |
| 17 | + for ( let i = 0; i < indexes.length; i += 3 ) { |
| 18 | + |
| 19 | + let index = indexes[ i ]; |
| 20 | + |
| 21 | + _A.set( positions[ 3 * index ], positions[ 3 * index + 1 ], positions[ 3 * index + 2 ] ); |
| 22 | + |
| 23 | + index = indexes[ i + 1 ]; |
| 24 | + |
| 25 | + _B.set( positions[ 3 * index ], positions[ 3 * index + 1 ], positions[ 3 * index + 2 ] ); |
| 26 | + |
| 27 | + index = indexes[ i + 2 ]; |
| 28 | + |
| 29 | + _C.set( positions[ 3 * index ], positions[ 3 * index + 1 ], positions[ 3 * index + 2 ] ); |
| 30 | + |
| 31 | + _C.sub( _B ); |
| 32 | + |
| 33 | + _A.sub( _B ); |
| 34 | + |
| 35 | + const normal = _C.cross( _A ).normalize(); |
| 36 | + |
| 37 | + for ( let j = 0; j < 3; j ++ ) { |
| 38 | + |
| 39 | + normals[ 3 * ( i + j ) ] = normal.x; |
| 40 | + normals[ 3 * ( i + j ) + 1 ] = normal.y; |
| 41 | + normals[ 3 * ( i + j ) + 2 ] = normal.z; |
| 42 | + |
| 43 | + } |
34 | 44 |
|
35 | 45 | } |
36 | 46 |
|
37 | 47 | } |
38 | 48 |
|
39 | | - } |
| 49 | + function mapPositionsToIndexes() { |
| 50 | + |
| 51 | + pointToIndexMap = Array( positions.length / 3 ); |
40 | 52 |
|
41 | | - function mapPositionsToIndexes() { |
| 53 | + for ( let i = 0; i < indexes.length; i ++ ) { |
42 | 54 |
|
43 | | - pointToIndexMap = Array( positions.length / 3 ); |
| 55 | + const index = indexes[ i ]; |
44 | 56 |
|
45 | | - for ( var i = 0; i < indexes.length; i ++ ) { |
| 57 | + if ( pointToIndexMap[ index ] == null ) { |
46 | 58 |
|
47 | | - var index = indexes[ i ]; |
| 59 | + pointToIndexMap[ index ] = []; |
48 | 60 |
|
49 | | - if ( pointToIndexMap[ index ] == null ) { |
| 61 | + } |
50 | 62 |
|
51 | | - pointToIndexMap[ index ] = []; |
| 63 | + pointToIndexMap[ index ].push( i ); |
52 | 64 |
|
53 | 65 | } |
54 | 66 |
|
55 | | - pointToIndexMap[ index ].push( i ); |
56 | | - |
57 | 67 | } |
58 | 68 |
|
59 | | - } |
| 69 | + function edgeSplitToGroups( indexes, cutOff, firstIndex ) { |
| 70 | + |
| 71 | + _A.set( normals[ 3 * firstIndex ], normals[ 3 * firstIndex + 1 ], normals[ 3 * firstIndex + 2 ] ).normalize(); |
60 | 72 |
|
61 | | - function edgeSplitToGroups( indexes, cutOff, firstIndex ) { |
| 73 | + const result = { |
| 74 | + splitGroup: [], |
| 75 | + currentGroup: [ firstIndex ] |
| 76 | + }; |
62 | 77 |
|
63 | | - A.set( normals[ 3 * firstIndex ], normals[ 3 * firstIndex + 1 ], normals[ 3 * firstIndex + 2 ] ).normalize(); |
64 | | - var result = { |
65 | | - splitGroup: [], |
66 | | - currentGroup: [ firstIndex ] |
67 | | - }; |
| 78 | + for ( const j of indexes ) { |
68 | 79 |
|
69 | | - for ( var j of indexes ) { |
| 80 | + if ( j !== firstIndex ) { |
70 | 81 |
|
71 | | - if ( j !== firstIndex ) { |
| 82 | + _B.set( normals[ 3 * j ], normals[ 3 * j + 1 ], normals[ 3 * j + 2 ] ).normalize(); |
72 | 83 |
|
73 | | - B.set( normals[ 3 * j ], normals[ 3 * j + 1 ], normals[ 3 * j + 2 ] ).normalize(); |
| 84 | + if ( _B.dot( _A ) < cutOff ) { |
74 | 85 |
|
75 | | - if ( B.dot( A ) < cutOff ) { |
| 86 | + result.splitGroup.push( j ); |
76 | 87 |
|
77 | | - result.splitGroup.push( j ); |
| 88 | + } else { |
78 | 89 |
|
79 | | - } else { |
| 90 | + result.currentGroup.push( j ); |
80 | 91 |
|
81 | | - result.currentGroup.push( j ); |
| 92 | + } |
82 | 93 |
|
83 | 94 | } |
84 | 95 |
|
85 | 96 | } |
86 | 97 |
|
| 98 | + return result; |
| 99 | + |
87 | 100 | } |
88 | 101 |
|
89 | | - return result; |
| 102 | + function edgeSplit( indexes, cutOff, original = null ) { |
90 | 103 |
|
91 | | - } |
| 104 | + if ( indexes.length === 0 ) return; |
| 105 | + const groupResults = []; |
92 | 106 |
|
93 | | - function edgeSplit( indexes, cutOff, original = null ) { |
| 107 | + for ( const index of indexes ) { |
94 | 108 |
|
95 | | - if ( indexes.length === 0 ) return; |
96 | | - var groupResults = []; |
| 109 | + groupResults.push( edgeSplitToGroups( indexes, cutOff, index ) ); |
97 | 110 |
|
98 | | - for ( var index of indexes ) { |
99 | | - |
100 | | - groupResults.push( edgeSplitToGroups( indexes, cutOff, index ) ); |
| 111 | + } |
101 | 112 |
|
102 | | - } |
| 113 | + let result = groupResults[ 0 ]; |
103 | 114 |
|
104 | | - var result = groupResults[ 0 ]; |
| 115 | + for ( const groupResult of groupResults ) { |
105 | 116 |
|
106 | | - for ( var groupResult of groupResults ) { |
| 117 | + if ( groupResult.currentGroup.length > result.currentGroup.length ) { |
107 | 118 |
|
108 | | - if ( groupResult.currentGroup.length > result.currentGroup.length ) { |
| 119 | + result = groupResult; |
109 | 120 |
|
110 | | - result = groupResult; |
| 121 | + } |
111 | 122 |
|
112 | 123 | } |
113 | 124 |
|
114 | | - } |
| 125 | + if ( original != null ) { |
115 | 126 |
|
116 | | - if ( original != null ) { |
| 127 | + splitIndexes.push( { |
| 128 | + original: original, |
| 129 | + indexes: result.currentGroup |
| 130 | + } ); |
117 | 131 |
|
118 | | - splitIndexes.push( { |
119 | | - original: original, |
120 | | - indexes: result.currentGroup |
121 | | - } ); |
| 132 | + } |
122 | 133 |
|
123 | | - } |
| 134 | + if ( result.splitGroup.length ) { |
124 | 135 |
|
125 | | - if ( result.splitGroup.length ) { |
| 136 | + edgeSplit( result.splitGroup, cutOff, original || result.currentGroup[ 0 ] ); |
126 | 137 |
|
127 | | - edgeSplit( result.splitGroup, cutOff, original || result.currentGroup[ 0 ] ); |
| 138 | + } |
128 | 139 |
|
129 | 140 | } |
130 | 141 |
|
131 | | - } |
132 | | - |
133 | | - this.modify = function ( geometry, cutOffAngle, tryKeepNormals = true ) { |
134 | | - |
135 | 142 | if ( geometry.isGeometry === true ) { |
136 | 143 |
|
137 | 144 | console.error( 'THREE.EdgeSplitModifier no longer supports THREE.Geometry. Use THREE.BufferGeometry instead.' ); |
|
140 | 147 | } |
141 | 148 |
|
142 | 149 | let hadNormals = false; |
143 | | - oldNormals = null; |
| 150 | + let oldNormals = null; |
144 | 151 |
|
145 | 152 | if ( geometry.attributes.normal ) { |
146 | 153 |
|
|
169 | 176 |
|
170 | 177 | } |
171 | 178 |
|
172 | | - indexes = geometry.index.array; |
173 | | - positions = geometry.getAttribute( 'position' ).array; |
| 179 | + const indexes = geometry.index.array; |
| 180 | + const positions = geometry.getAttribute( 'position' ).array; |
| 181 | + let normals; |
| 182 | + let pointToIndexMap; |
174 | 183 | computeNormals(); |
175 | 184 | mapPositionsToIndexes(); |
176 | | - splitIndexes = []; |
| 185 | + const splitIndexes = []; |
177 | 186 |
|
178 | | - for ( var vertexIndexes of pointToIndexMap ) { |
| 187 | + for ( const vertexIndexes of pointToIndexMap ) { |
179 | 188 |
|
180 | 189 | edgeSplit( vertexIndexes, Math.cos( cutOffAngle ) - 0.001 ); |
181 | 190 |
|
|
192 | 201 |
|
193 | 202 | } |
194 | 203 |
|
195 | | - var newIndexes = new Uint32Array( indexes.length ); |
| 204 | + const newIndexes = new Uint32Array( indexes.length ); |
196 | 205 | newIndexes.set( indexes ); |
197 | 206 |
|
198 | | - for ( var i = 0; i < splitIndexes.length; i ++ ) { |
| 207 | + for ( let i = 0; i < splitIndexes.length; i ++ ) { |
199 | 208 |
|
200 | | - var split = splitIndexes[ i ]; |
201 | | - var index = indexes[ split.original ]; |
| 209 | + const split = splitIndexes[ i ]; |
| 210 | + const index = indexes[ split.original ]; |
202 | 211 |
|
203 | 212 | for ( const attribute of Object.values( newAttributes ) ) { |
204 | 213 |
|
|
210 | 219 |
|
211 | 220 | } |
212 | 221 |
|
213 | | - for ( var j of split.indexes ) { |
| 222 | + for ( const j of split.indexes ) { |
214 | 223 |
|
215 | 224 | newIndexes[ j ] = indexes.length + i; |
216 | 225 |
|
|
253 | 262 |
|
254 | 263 | return geometry; |
255 | 264 |
|
256 | | - }; |
| 265 | + } |
257 | 266 |
|
258 | | - }; |
| 267 | + } |
259 | 268 |
|
260 | 269 | THREE.EdgeSplitModifier = EdgeSplitModifier; |
261 | 270 |
|
|
0 commit comments