Skip to content

Instantly share code, notes, and snippets.

@corbanbrook
Created January 14, 2019 18:29
Show Gist options
  • Save corbanbrook/27b4e2d13d08651112b32d49a741b083 to your computer and use it in GitHub Desktop.
Save corbanbrook/27b4e2d13d08651112b32d49a741b083 to your computer and use it in GitHub Desktop.
Old Geometry Merge
merge: function ( geometry1, object2 /* mesh | geometry */ ) {
var matrix, matrixRotation,
vertexOffset = geometry1.vertices.length,
uvPosition = geometry1.faceVertexUvs[ 0 ].length,
geometry2 = object2 instanceof THREE.Mesh ? object2.geometry : object2,
vertices1 = geometry1.vertices,
vertices2 = geometry2.vertices,
faces1 = geometry1.faces,
faces2 = geometry2.faces,
uvs1 = geometry1.faceVertexUvs[ 0 ],
uvs2 = geometry2.faceVertexUvs[ 0 ];
var geo1MaterialsMap = {};
for ( var i = 0; i < geometry1.materials.length; i ++ ) {
var id = geometry1.materials[ i ].id;
geo1MaterialsMap[ id ] = i;
}
if ( object2 instanceof THREE.Mesh ) {
object2.matrixAutoUpdate && object2.updateMatrix();
matrix = object2.matrix;
matrixRotation = new THREE.Matrix4();
matrixRotation.extractRotation( matrix, object2.scale );
}
// vertices
for ( var i = 0, il = vertices2.length; i < il; i ++ ) {
var vertex = vertices2[ i ];
var vertexCopy = vertex.clone();
if ( matrix ) matrix.multiplyVector3( vertexCopy.position );
vertices1.push( vertexCopy );
}
// faces
for ( i = 0, il = faces2.length; i < il; i ++ ) {
var face = faces2[ i ], faceCopy, normal, color,
faceVertexNormals = face.vertexNormals,
faceVertexColors = face.vertexColors;
if ( face instanceof THREE.Face3 ) {
faceCopy = new THREE.Face3( face.a + vertexOffset, face.b + vertexOffset, face.c + vertexOffset );
} else if ( face instanceof THREE.Face4 ) {
faceCopy = new THREE.Face4( face.a + vertexOffset, face.b + vertexOffset, face.c + vertexOffset, face.d + vertexOffset );
}
faceCopy.normal.copy( face.normal );
if ( matrixRotation ) matrixRotation.multiplyVector3( faceCopy.normal );
for ( var j = 0, jl = faceVertexNormals.length; j < jl; j ++ ) {
normal = faceVertexNormals[ j ].clone();
if ( matrixRotation ) matrixRotation.multiplyVector3( normal );
faceCopy.vertexNormals.push( normal );
}
faceCopy.color.copy( face.color );
for ( var j = 0, jl = faceVertexColors.length; j < jl; j ++ ) {
color = faceVertexColors[ j ];
faceCopy.vertexColors.push( color.clone() );
}
if ( face.materialIndex !== undefined ) {
var material2 = geometry2.materials[ face.materialIndex ];
var materialId2 = material2.id;
var materialIndex = geo1MaterialsMap[ materialId2 ];
if ( materialIndex === undefined ) {
materialIndex = geometry1.materials.length;
geo1MaterialsMap[ materialId2 ] = materialIndex;
geometry1.materials.push( material2 );
}
faceCopy.materialIndex = materialIndex;
}
faceCopy.centroid.copy( face.centroid );
if ( matrix ) matrix.multiplyVector3( faceCopy.centroid );
faces1.push( faceCopy );
}
// uvs
for ( i = 0, il = uvs2.length; i < il; i ++ ) {
var uv = uvs2[ i ], uvCopy = [];
for ( var j = 0, jl = uv.length; j < jl; j ++ ) {
uvCopy.push( new THREE.UV( uv[ j ].u, uv[ j ].v ) );
}
uvs1.push( uvCopy );
}
},
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment