Skip to content

Commit 94e47d4

Browse files
authored
Merge pull request #14547 from erasta/dev_geomfrombuff
Reduce memory allocation on Geometry.fromBufferGeometry
2 parents ceeeba4 + 2c7393a commit 94e47d4

File tree

1 file changed

+22
-28
lines changed

1 file changed

+22
-28
lines changed

src/core/Geometry.js

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -228,58 +228,52 @@ Geometry.prototype = Object.assign( Object.create( EventDispatcher.prototype ),
228228

229229
if ( uvs2 !== undefined ) this.faceVertexUvs[ 1 ] = [];
230230

231-
var tempNormals = [];
232-
var tempUVs = [];
233-
var tempUVs2 = [];
234-
235231
for ( var i = 0, j = 0; i < positions.length; i += 3, j += 2 ) {
236232

237-
scope.vertices.push( new Vector3( positions[ i ], positions[ i + 1 ], positions[ i + 2 ] ) );
238-
239-
if ( normals !== undefined ) {
240-
241-
tempNormals.push( new Vector3( normals[ i ], normals[ i + 1 ], normals[ i + 2 ] ) );
242-
243-
}
233+
scope.vertices.push( new Vector3().fromArray( positions, i ) );
244234

245235
if ( colors !== undefined ) {
246236

247-
scope.colors.push( new Color( colors[ i ], colors[ i + 1 ], colors[ i + 2 ] ) );
248-
249-
}
250-
251-
if ( uvs !== undefined ) {
252-
253-
tempUVs.push( new Vector2( uvs[ j ], uvs[ j + 1 ] ) );
254-
255-
}
256-
257-
if ( uvs2 !== undefined ) {
258-
259-
tempUVs2.push( new Vector2( uvs2[ j ], uvs2[ j + 1 ] ) );
237+
scope.colors.push( new Color().fromArray( colors, i ) );
260238

261239
}
262240

263241
}
264242

265243
function addFace( a, b, c, materialIndex ) {
266244

267-
var vertexNormals = normals !== undefined ? [ tempNormals[ a ].clone(), tempNormals[ b ].clone(), tempNormals[ c ].clone() ] : [];
268-
var vertexColors = colors !== undefined ? [ scope.colors[ a ].clone(), scope.colors[ b ].clone(), scope.colors[ c ].clone() ] : [];
245+
var vertexColors = ( colors === undefined ) ? [] : [
246+
scope.colors[ a ].clone(),
247+
scope.colors[ b ].clone(),
248+
scope.colors[ c ].clone() ];
249+
250+
var vertexNormals = ( normals === undefined ) ? [] : [
251+
new Vector3().fromArray( normals, a * 3 ),
252+
new Vector3().fromArray( normals, b * 3 ),
253+
new Vector3().fromArray( normals, c * 3 )
254+
];
269255

270256
var face = new Face3( a, b, c, vertexNormals, vertexColors, materialIndex );
271257

272258
scope.faces.push( face );
273259

274260
if ( uvs !== undefined ) {
275261

276-
scope.faceVertexUvs[ 0 ].push( [ tempUVs[ a ].clone(), tempUVs[ b ].clone(), tempUVs[ c ].clone() ] );
262+
scope.faceVertexUvs[ 0 ].push( [
263+
new Vector2().fromArray( uvs, a * 2 ),
264+
new Vector2().fromArray( uvs, b * 2 ),
265+
new Vector2().fromArray( uvs, c * 2 )
266+
] );
277267

278268
}
279269

280270
if ( uvs2 !== undefined ) {
281271

282-
scope.faceVertexUvs[ 1 ].push( [ tempUVs2[ a ].clone(), tempUVs2[ b ].clone(), tempUVs2[ c ].clone() ] );
272+
scope.faceVertexUvs[ 1 ].push( [
273+
new Vector2().fromArray( uvs2, a * 2 ),
274+
new Vector2().fromArray( uvs2, b * 2 ),
275+
new Vector2().fromArray( uvs2, c * 2 )
276+
] );
283277

284278
}
285279

0 commit comments

Comments
 (0)