Skip to content

Commit 85a366c

Browse files
authored
Examples: Convert modifiers to ES6. (#21604)
1 parent 7b0d2e9 commit 85a366c

File tree

6 files changed

+1178
-1148
lines changed

6 files changed

+1178
-1148
lines changed

examples/js/modifiers/EdgeSplitModifier.js

Lines changed: 94 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1,137 +1,144 @@
11
( function () {
22

3-
var EdgeSplitModifier = function () {
3+
const _A = new THREE.Vector3();
44

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();
126

13-
function computeNormals() {
7+
const _C = new THREE.Vector3();
148

15-
normals = new Float32Array( indexes.length * 3 );
9+
class EdgeSplitModifier {
1610

17-
for ( var i = 0; i < indexes.length; i += 3 ) {
11+
modify( geometry, cutOffAngle, tryKeepNormals = true ) {
1812

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() {
2814

29-
for ( var j = 0; j < 3; j ++ ) {
15+
normals = new Float32Array( indexes.length * 3 );
3016

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+
}
3444

3545
}
3646

3747
}
3848

39-
}
49+
function mapPositionsToIndexes() {
50+
51+
pointToIndexMap = Array( positions.length / 3 );
4052

41-
function mapPositionsToIndexes() {
53+
for ( let i = 0; i < indexes.length; i ++ ) {
4254

43-
pointToIndexMap = Array( positions.length / 3 );
55+
const index = indexes[ i ];
4456

45-
for ( var i = 0; i < indexes.length; i ++ ) {
57+
if ( pointToIndexMap[ index ] == null ) {
4658

47-
var index = indexes[ i ];
59+
pointToIndexMap[ index ] = [];
4860

49-
if ( pointToIndexMap[ index ] == null ) {
61+
}
5062

51-
pointToIndexMap[ index ] = [];
63+
pointToIndexMap[ index ].push( i );
5264

5365
}
5466

55-
pointToIndexMap[ index ].push( i );
56-
5767
}
5868

59-
}
69+
function edgeSplitToGroups( indexes, cutOff, firstIndex ) {
70+
71+
_A.set( normals[ 3 * firstIndex ], normals[ 3 * firstIndex + 1 ], normals[ 3 * firstIndex + 2 ] ).normalize();
6072

61-
function edgeSplitToGroups( indexes, cutOff, firstIndex ) {
73+
const result = {
74+
splitGroup: [],
75+
currentGroup: [ firstIndex ]
76+
};
6277

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 ) {
6879

69-
for ( var j of indexes ) {
80+
if ( j !== firstIndex ) {
7081

71-
if ( j !== firstIndex ) {
82+
_B.set( normals[ 3 * j ], normals[ 3 * j + 1 ], normals[ 3 * j + 2 ] ).normalize();
7283

73-
B.set( normals[ 3 * j ], normals[ 3 * j + 1 ], normals[ 3 * j + 2 ] ).normalize();
84+
if ( _B.dot( _A ) < cutOff ) {
7485

75-
if ( B.dot( A ) < cutOff ) {
86+
result.splitGroup.push( j );
7687

77-
result.splitGroup.push( j );
88+
} else {
7889

79-
} else {
90+
result.currentGroup.push( j );
8091

81-
result.currentGroup.push( j );
92+
}
8293

8394
}
8495

8596
}
8697

98+
return result;
99+
87100
}
88101

89-
return result;
102+
function edgeSplit( indexes, cutOff, original = null ) {
90103

91-
}
104+
if ( indexes.length === 0 ) return;
105+
const groupResults = [];
92106

93-
function edgeSplit( indexes, cutOff, original = null ) {
107+
for ( const index of indexes ) {
94108

95-
if ( indexes.length === 0 ) return;
96-
var groupResults = [];
109+
groupResults.push( edgeSplitToGroups( indexes, cutOff, index ) );
97110

98-
for ( var index of indexes ) {
99-
100-
groupResults.push( edgeSplitToGroups( indexes, cutOff, index ) );
111+
}
101112

102-
}
113+
let result = groupResults[ 0 ];
103114

104-
var result = groupResults[ 0 ];
115+
for ( const groupResult of groupResults ) {
105116

106-
for ( var groupResult of groupResults ) {
117+
if ( groupResult.currentGroup.length > result.currentGroup.length ) {
107118

108-
if ( groupResult.currentGroup.length > result.currentGroup.length ) {
119+
result = groupResult;
109120

110-
result = groupResult;
121+
}
111122

112123
}
113124

114-
}
125+
if ( original != null ) {
115126

116-
if ( original != null ) {
127+
splitIndexes.push( {
128+
original: original,
129+
indexes: result.currentGroup
130+
} );
117131

118-
splitIndexes.push( {
119-
original: original,
120-
indexes: result.currentGroup
121-
} );
132+
}
122133

123-
}
134+
if ( result.splitGroup.length ) {
124135

125-
if ( result.splitGroup.length ) {
136+
edgeSplit( result.splitGroup, cutOff, original || result.currentGroup[ 0 ] );
126137

127-
edgeSplit( result.splitGroup, cutOff, original || result.currentGroup[ 0 ] );
138+
}
128139

129140
}
130141

131-
}
132-
133-
this.modify = function ( geometry, cutOffAngle, tryKeepNormals = true ) {
134-
135142
if ( geometry.isGeometry === true ) {
136143

137144
console.error( 'THREE.EdgeSplitModifier no longer supports THREE.Geometry. Use THREE.BufferGeometry instead.' );
@@ -140,7 +147,7 @@
140147
}
141148

142149
let hadNormals = false;
143-
oldNormals = null;
150+
let oldNormals = null;
144151

145152
if ( geometry.attributes.normal ) {
146153

@@ -169,13 +176,15 @@
169176

170177
}
171178

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;
174183
computeNormals();
175184
mapPositionsToIndexes();
176-
splitIndexes = [];
185+
const splitIndexes = [];
177186

178-
for ( var vertexIndexes of pointToIndexMap ) {
187+
for ( const vertexIndexes of pointToIndexMap ) {
179188

180189
edgeSplit( vertexIndexes, Math.cos( cutOffAngle ) - 0.001 );
181190

@@ -192,13 +201,13 @@
192201

193202
}
194203

195-
var newIndexes = new Uint32Array( indexes.length );
204+
const newIndexes = new Uint32Array( indexes.length );
196205
newIndexes.set( indexes );
197206

198-
for ( var i = 0; i < splitIndexes.length; i ++ ) {
207+
for ( let i = 0; i < splitIndexes.length; i ++ ) {
199208

200-
var split = splitIndexes[ i ];
201-
var index = indexes[ split.original ];
209+
const split = splitIndexes[ i ];
210+
const index = indexes[ split.original ];
202211

203212
for ( const attribute of Object.values( newAttributes ) ) {
204213

@@ -210,7 +219,7 @@
210219

211220
}
212221

213-
for ( var j of split.indexes ) {
222+
for ( const j of split.indexes ) {
214223

215224
newIndexes[ j ] = indexes.length + i;
216225

@@ -253,9 +262,9 @@
253262

254263
return geometry;
255264

256-
};
265+
}
257266

258-
};
267+
}
259268

260269
THREE.EdgeSplitModifier = EdgeSplitModifier;
261270

0 commit comments

Comments
 (0)