Browse Source

BufferGeometry.fromFlattenedGeometry()

Mr.doob 10 years ago
parent
commit
030c33c14f
3 changed files with 140 additions and 5 deletions
  1. 56 0
      src/core/BufferAttribute.js
  2. 35 2
      src/core/BufferGeometry.js
  3. 49 3
      src/core/Geometry.js

+ 56 - 0
src/core/BufferAttribute.js

@@ -44,6 +44,13 @@ THREE.BufferAttribute.prototype = {
 
 			var color = colors[ i ];
 
+			if ( vector === undefined ) {
+
+				console.warn( 'THREE.BufferAttribute.copyColorsArray(): color is undefined', i );
+				vector = new THREE.Color();
+
+			}
+
 			array[ offset ++ ] = color.r;
 			array[ offset ++ ] = color.g;
 			array[ offset ++ ] = color.b;
@@ -54,6 +61,48 @@ THREE.BufferAttribute.prototype = {
 
 	},
 
+	copyFacesArray: function ( faces ) {
+
+		var array = this.array, offset = 0;
+
+		for ( var i = 0, l = faces.length; i < l; i ++ ) {
+
+			var face = faces[ i ];
+
+			array[ offset ++ ] = face.a;
+			array[ offset ++ ] = face.b;
+			array[ offset ++ ] = face.c;
+
+		}
+
+		return this;
+
+	},
+
+	copyVector2sArray: function ( vectors ) {
+
+		var array = this.array, offset = 0;
+
+		for ( var i = 0, l = vectors.length; i < l; i ++ ) {
+
+			var vector = vectors[ i ];
+
+			if ( vector === undefined ) {
+
+				console.warn( 'THREE.BufferAttribute.copyVector2sArray(): vector is undefined', i );
+				vector = new THREE.Vector2();
+
+			}
+
+			array[ offset ++ ] = vector.x;
+			array[ offset ++ ] = vector.y;
+
+		}
+
+		return this;
+
+	},
+
 	copyVector3sArray: function ( vectors ) {
 
 		var array = this.array, offset = 0;
@@ -62,6 +111,13 @@ THREE.BufferAttribute.prototype = {
 
 			var vector = vectors[ i ];
 
+			if ( vector === undefined ) {
+
+				console.warn( 'THREE.BufferAttribute.copyVector3sArray(): vector is undefined', i );
+				vector = new THREE.Vector3();
+
+			}
+
 			array[ offset ++ ] = vector.x;
 			array[ offset ++ ] = vector.y;
 			array[ offset ++ ] = vector.z;

+ 35 - 2
src/core/BufferGeometry.js

@@ -128,7 +128,13 @@ THREE.BufferGeometry.prototype = {
 
 		} else if ( object instanceof THREE.Mesh ) {
 
-			this.fromGeometry( geometry, material );
+			if ( geometry.isFlattened === false ) {
+
+				geometry.flatten();
+
+			}
+
+			this.fromFlattenedGeometry( geometry );
 
 		}
 
@@ -146,7 +152,7 @@ THREE.BufferGeometry.prototype = {
 
 		var geometry = object.geometry;
 
-		if ( object instanceof THREE.PointCloud || object instanceof THREE.Line ) {
+		if ( object instanceof THREE.PointCloud || object instanceof THREE.Line || object instanceof THREE.Mesh ) {
 
 			if ( geometry.verticesNeedUpdate === true ) {
 
@@ -357,6 +363,33 @@ THREE.BufferGeometry.prototype = {
 
 	},
 
+	fromFlattenedGeometry: function ( geometry ) {
+
+		var indices = new Uint16Array( geometry.faces.length * 3 );
+		this.addAttribute( 'index', new THREE.BufferAttribute( indices, 1 ).copyFacesArray( geometry.faces ) );
+
+		var positions = new Float32Array( geometry.vertices.length * 3 );
+		this.addAttribute( 'position', new THREE.BufferAttribute( positions, 3 ).copyVector3sArray( geometry.vertices ) );
+
+		if ( geometry.normals.length > 0 ) {
+
+			var normals = new Float32Array( geometry.normals.length * 3 );
+			this.addAttribute( 'normal', new THREE.BufferAttribute( normals, 3 ).copyVector3sArray( geometry.normals ) );
+
+		}
+
+		var colors = new Float32Array( geometry.colors.length * 3 );
+		this.addAttribute( 'color', new THREE.BufferAttribute( colors, 3 ).copyVector3sArray( geometry.colors ) );
+
+		var uvs = new Float32Array( geometry.uvs.length * 2 );
+		this.addAttribute( 'uv', new THREE.BufferAttribute( uvs, 2 ).copyVector2sArray( geometry.uvs ) );
+
+		this.computeBoundingSphere();
+
+		return this;
+
+	},
+
 	computeBoundingBox: function () {
 
 		var vector = new THREE.Vector3();

+ 49 - 3
src/core/Geometry.js

@@ -16,11 +16,19 @@ THREE.Geometry = function () {
 	this.name = '';
 	this.type = 'Geometry';
 
-	this.vertices = [];
-	this.colors = [];  // one-to-one vertex colors, used in Points and Line
+	// FlattenedGeometry
+
+	this.isFlattened = false;
 
+	this.vertices = [];
+	this.colors = [];
+	this.normals = [];
+	this.colors = [];
+	this.uvs = [];
 	this.faces = [];
 
+	//
+
 	this.faceVertexUvs = [ [] ];
 
 	this.morphTargets = [];
@@ -37,7 +45,7 @@ THREE.Geometry = function () {
 
 	this.hasTangents = false;
 
-	this.dynamic = true; // the intermediate typed arrays will be deleted when set to false
+	this.dynamic = true;
 
 	// update flags
 
@@ -98,6 +106,44 @@ THREE.Geometry.prototype = {
 
 	},
 
+	flatten: function () {
+
+		var faces = this.faces;
+		var faceVertexUvs = this.faceVertexUvs[ 0 ];
+
+		for ( var i = 0, il = faces.length; i < il; i ++ ) {
+
+			var face = faces[ i ];
+			var indices = [ face.a, face.b, face.c ];
+
+			var vertexNormals = face.vertexNormals;
+			var vertexColors = face.vertexColors;
+			var vertexUvs = faceVertexUvs[ i ];
+
+			for ( var j = 0, jl = vertexNormals.length; j < jl; j ++ ) {
+
+				this.normals[ indices[ j ] ] = vertexNormals[ j ];
+
+			}
+
+			for ( var j = 0, jl = vertexColors.length; j < jl; j ++ ) {
+
+				this.colors[ indices[ j ] ] = vertexColors[ j ];
+
+			}
+
+			for ( var j = 0, jl = vertexUvs.length; j < jl; j ++ ) {
+
+				this.uvs[ indices[ j ] ] = vertexUvs[ j ];
+
+			}
+
+		}
+
+		this.isFlattened = true;
+
+	},
+
 	fromBufferGeometry: function ( geometry ) {
 
 		var scope = this;