Browse Source

DirectGeometry now acts as intermediary between Geometry and BufferGeometry.

Mr.doob 10 năm trước cách đây
mục cha
commit
4d374abbdc

+ 6 - 6
src/core/BufferAttribute.js

@@ -76,17 +76,17 @@ THREE.BufferAttribute.prototype = {
 
 	},
 
-	copyFacesArray: function ( faces ) {
+	copyIndicesArray: function ( indices ) {
 
 		var array = this.array, offset = 0;
 
-		for ( var i = 0, l = faces.length; i < l; i ++ ) {
+		for ( var i = 0, l = indices.length; i < l; i ++ ) {
 
-			var face = faces[ i ];
+			var index = indices[ i ];
 
-			array[ offset ++ ] = face.a;
-			array[ offset ++ ] = face.b;
-			array[ offset ++ ] = face.c;
+			array[ offset ++ ] = index.a;
+			array[ offset ++ ] = index.b;
+			array[ offset ++ ] = index.c;
 
 		}
 

+ 47 - 219
src/core/BufferGeometry.js

@@ -155,26 +155,38 @@ THREE.BufferGeometry.prototype = {
 
 			this.addAttribute( 'position', positions.copyVector3sArray( geometry.vertices ) );
 			this.addAttribute( 'color', colors.copyColorsArray( geometry.colors ) );
-			this.computeBoundingSphere();
+
+			if ( geometry.boundingSphere !== null ) {
+
+				this.boundingSphere = geometry.boundingSphere.clone();
+
+			}
+
+			if ( geometry.boundingBox !== null ) {
+
+				this.boundingBox = geometry.boundingBox.clone();
+
+			}
 
 		} else if ( object instanceof THREE.Mesh ) {
 
-			// skinning
+			if ( geometry instanceof THREE.Geometry ) {
 
-			if ( object instanceof THREE.SkinnedMesh ) {
+				geometry = new THREE.DirectGeometry().fromGeometry( geometry, material );
 
-				if ( geometry instanceof THREE.Geometry ) {
+			}
 
-					console.log( 'THREE.BufferGeometry.setFromObject(): Converted THREE.Geometry to THREE.DirectGeometry as required for THREE.SkinnedMesh.', geometry );
-					geometry = new THREE.DirectGeometry().fromGeometry( geometry );
+			// skinning
 
-				}
+			if ( object instanceof THREE.SkinnedMesh ) {
 
+				/*
 				var skinIndices = new THREE.Float32Attribute( geometry.skinIndices.length * 4, 4 );
 				var skinWeights = new THREE.Float32Attribute( geometry.skinWeights.length * 4, 4 );
 
 				this.addAttribute( 'skinIndex', skinIndices.copyVector4sArray( geometry.skinIndices ) );
 				this.addAttribute( 'skinWeight', skinWeights.copyVector4sArray( geometry.skinWeights ) );
+				*/
 
 			}
 
@@ -182,15 +194,7 @@ THREE.BufferGeometry.prototype = {
 
 			if ( object.morphTargetInfluences !== undefined ) {
 
-				if ( geometry instanceof THREE.Geometry ) {
-
-					console.log( 'THREE.BufferGeometry.setFromObject(): Converted THREE.Geometry to THREE.DirectGeometry as required for MorphTargets.', geometry );
-					geometry = new THREE.DirectGeometry().fromGeometry( geometry );
-
-				}
-
-				// positions
-
+				/*
 				var morphTargets = geometry.morphTargets;
 
 				if ( morphTargets.length > 0 ) {
@@ -206,19 +210,14 @@ THREE.BufferGeometry.prototype = {
 					}
 
 				}
+				*/
 
-			}
-
-			if ( geometry instanceof THREE.DirectGeometry ) {
-
-				this.fromDirectGeometry( geometry );
-
-			} else if ( geometry instanceof THREE.Geometry ) {
-
-				this.fromGeometry( geometry, material );
+				// TODO normals, colors
 
 			}
 
+			this.fromDirectGeometry( geometry );
+
 		}
 
 		return this;
@@ -229,6 +228,7 @@ THREE.BufferGeometry.prototype = {
 
 		var geometry = object.geometry;
 
+		/*
 		if ( geometry.verticesNeedUpdate === true ) {
 
 			var attribute = this.attributes.position;
@@ -258,212 +258,30 @@ THREE.BufferGeometry.prototype = {
 			geometry.colorsNeedUpdate = false;
 
 		}
+		*/
+
+		return this;
 
 	},
 
 	fromGeometry: function ( geometry, material ) {
 
-		material = material || { 'vertexColors': THREE.NoColors };
-
-		var vertices = geometry.vertices;
-		var faces = geometry.faces;
-		var faceVertexUvs = geometry.faceVertexUvs;
-		var vertexColors = material.vertexColors;
-
-		var hasFaceVertexUv = faceVertexUvs[ 0 ] && faceVertexUvs[ 0 ].length > 0;
-		var hasFaceVertexUv2 = faceVertexUvs[ 1 ] && faceVertexUvs[ 1 ].length > 0;
-
-		var positions = new Float32Array( faces.length * 3 * 3 );
-		this.addAttribute( 'position', new THREE.BufferAttribute( positions, 3 ) );
-
-		var normals = new Float32Array( faces.length * 3 * 3 );
-		this.addAttribute( 'normal', new THREE.BufferAttribute( normals, 3 ) );
-
-		if ( vertexColors !== THREE.NoColors ) {
-
-			var colors = new Float32Array( faces.length * 3 * 3 );
-			this.addAttribute( 'color', new THREE.BufferAttribute( colors, 3 ) );
-
-		}
-
-		if ( hasFaceVertexUv === true ) {
-
-			var uvs = new Float32Array( faces.length * 3 * 2 );
-			this.addAttribute( 'uv', new THREE.BufferAttribute( uvs, 2 ) );
-
-		}
-
-		if ( hasFaceVertexUv2 === true ) {
-
-			var uvs2 = new Float32Array( faces.length * 3 * 2 );
-			this.addAttribute( 'uv2', new THREE.BufferAttribute( uvs2, 2 ) );
-
-		}
-
-		for ( var i = 0, i2 = 0, i3 = 0; i < faces.length; i ++, i2 += 6, i3 += 9 ) {
-
-			var face = faces[ i ];
-
-			var a = vertices[ face.a ];
-			var b = vertices[ face.b ];
-			var c = vertices[ face.c ];
-
-			positions[ i3     ] = a.x;
-			positions[ i3 + 1 ] = a.y;
-			positions[ i3 + 2 ] = a.z;
-
-			positions[ i3 + 3 ] = b.x;
-			positions[ i3 + 4 ] = b.y;
-			positions[ i3 + 5 ] = b.z;
-
-			positions[ i3 + 6 ] = c.x;
-			positions[ i3 + 7 ] = c.y;
-			positions[ i3 + 8 ] = c.z;
-
-			var vertexNormals = face.vertexNormals;
-
-			if ( vertexNormals.length === 3 ) {
-
-				var na = vertexNormals[ 0 ];
-				var nb = vertexNormals[ 1 ];
-				var nc = vertexNormals[ 2 ];
-
-				normals[ i3     ] = na.x;
-				normals[ i3 + 1 ] = na.y;
-				normals[ i3 + 2 ] = na.z;
-
-				normals[ i3 + 3 ] = nb.x;
-				normals[ i3 + 4 ] = nb.y;
-				normals[ i3 + 5 ] = nb.z;
-
-				normals[ i3 + 6 ] = nc.x;
-				normals[ i3 + 7 ] = nc.y;
-				normals[ i3 + 8 ] = nc.z;
-
-			} else {
+		return this.fromDirectGeometry( new THREE.DirectGeometry().fromGeometry( geometry, material ) );
 
-				var n = face.normal;
-
-				normals[ i3     ] = n.x;
-				normals[ i3 + 1 ] = n.y;
-				normals[ i3 + 2 ] = n.z;
-
-				normals[ i3 + 3 ] = n.x;
-				normals[ i3 + 4 ] = n.y;
-				normals[ i3 + 5 ] = n.z;
-
-				normals[ i3 + 6 ] = n.x;
-				normals[ i3 + 7 ] = n.y;
-				normals[ i3 + 8 ] = n.z;
-
-			}
-
-			if ( vertexColors === THREE.FaceColors ) {
-
-				var fc = face.color;
-
-				colors[ i3     ] = fc.r;
-				colors[ i3 + 1 ] = fc.g;
-				colors[ i3 + 2 ] = fc.b;
-
-				colors[ i3 + 3 ] = fc.r;
-				colors[ i3 + 4 ] = fc.g;
-				colors[ i3 + 5 ] = fc.b;
-
-				colors[ i3 + 6 ] = fc.r;
-				colors[ i3 + 7 ] = fc.g;
-				colors[ i3 + 8 ] = fc.b;
-
-			} else if ( vertexColors === THREE.VertexColors ) {
-
-				var vca = face.vertexColors[ 0 ];
-				var vcb = face.vertexColors[ 1 ];
-				var vcc = face.vertexColors[ 2 ];
-
-				colors[ i3     ] = vca.r;
-				colors[ i3 + 1 ] = vca.g;
-				colors[ i3 + 2 ] = vca.b;
-
-				colors[ i3 + 3 ] = vcb.r;
-				colors[ i3 + 4 ] = vcb.g;
-				colors[ i3 + 5 ] = vcb.b;
-
-				colors[ i3 + 6 ] = vcc.r;
-				colors[ i3 + 7 ] = vcc.g;
-				colors[ i3 + 8 ] = vcc.b;
-
-			}
-
-			if ( hasFaceVertexUv === true ) {
-
-				var vertexUvs = faceVertexUvs[ 0 ][ i ];
-
-				if ( vertexUvs !== undefined ) {
-
-					var uva = vertexUvs[ 0 ];
-					var uvb = vertexUvs[ 1 ];
-					var uvc = vertexUvs[ 2 ];
-
-					uvs[ i2     ] = uva.x;
-					uvs[ i2 + 1 ] = uva.y;
-
-					uvs[ i2 + 2 ] = uvb.x;
-					uvs[ i2 + 3 ] = uvb.y;
-
-					uvs[ i2 + 4 ] = uvc.x;
-					uvs[ i2 + 5 ] = uvc.y;
-
-				} else {
-
-					console.warn( 'THREE.BufferGeometry.fromGeometry(): Undefined vertexUv', i );
-
-				}
-
-			}
-
-			if ( hasFaceVertexUv2 === true ) {
-
-				var vertexUvs = faceVertexUvs[ 1 ][ i ];
-
-				if ( vertexUvs !== undefined ) {
-
-					var uva = vertexUvs[ 0 ];
-					var uvb = vertexUvs[ 1 ];
-					var uvc = vertexUvs[ 2 ];
-
-					uvs2[ i2     ] = uva.x;
-					uvs2[ i2 + 1 ] = uva.y;
-
-					uvs2[ i2 + 2 ] = uvb.x;
-					uvs2[ i2 + 3 ] = uvb.y;
-
-					uvs2[ i2 + 4 ] = uvc.x;
-					uvs2[ i2 + 5 ] = uvc.y;
+	},
 
-				} else {
+	fromDirectGeometry: function ( geometry ) {
 
-					console.warn( 'THREE.BufferGeometry.fromGeometry(): Undefined vertexUv2', i );
+		var indices = new Uint16Array( geometry.indices.length * 3 );
+		this.addAttribute( 'index', new THREE.BufferAttribute( indices, 1 ).copyIndicesArray( geometry.indices ) );
 
-				}
+		if ( geometry.vertices.length > 0 ) {
 
-			}
+			var positions = new Float32Array( geometry.vertices.length * 3 );
+			this.addAttribute( 'position', new THREE.BufferAttribute( positions, 3 ).copyVector3sArray( geometry.vertices ) );
 
 		}
 
-		this.computeBoundingSphere();
-
-		return this;
-
-	},
-
-	fromDirectGeometry: 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 );
@@ -485,7 +303,17 @@ THREE.BufferGeometry.prototype = {
 
 		}
 
-		this.computeBoundingSphere();
+		if ( geometry.boundingSphere !== null ) {
+
+			this.boundingSphere = geometry.boundingSphere.clone();
+
+		}
+
+		if ( geometry.boundingBox !== null ) {
+
+			this.boundingBox = geometry.boundingBox.clone();
+
+		}
 
 		return this;
 

+ 127 - 2
src/core/DirectGeometry.js

@@ -11,12 +11,13 @@ THREE.DirectGeometry = function () {
 	this.name = '';
 	this.type = 'DirectGeometry';
 
+	this.indices = [];
 	this.vertices = [];
 	this.colors = [];
 	this.normals = [];
 	this.colors = [];
 	this.uvs = [];
-	this.faces = [];
+	this.uvs2 = [];
 
 	// this.lineDistances = [];
 
@@ -53,8 +54,131 @@ THREE.DirectGeometry.prototype = {
 
 	},
 
-	fromGeometry: function ( geometry ) {
+	fromGeometry: function ( geometry, material ) {
 
+		material = material || { 'vertexColors': THREE.NoColors };
+
+		var vertices = geometry.vertices;
+		var faces = geometry.faces;
+		var faceVertexUvs = geometry.faceVertexUvs;
+		var materialVertexColors = material.vertexColors;
+
+		var hasFaceVertexUv = faceVertexUvs[ 0 ] && faceVertexUvs[ 0 ].length > 0;
+		var hasFaceVertexUv2 = faceVertexUvs[ 1 ] && faceVertexUvs[ 1 ].length > 0;
+
+		for ( var i = 0, j = 0; i < faces.length; i ++ ) {
+
+			this.indices.push( new THREE.Index( j ++, j ++, j ++ ) );
+
+			var face = faces[ i ];
+
+			var a = vertices[ face.a ];
+			var b = vertices[ face.b ];
+			var c = vertices[ face.c ];
+
+			this.vertices.push( a.clone(), b.clone(), c.clone() );
+
+			var vertexNormals = face.vertexNormals;
+
+			if ( vertexNormals.length === 3 ) {
+
+				this.normals.push(
+					vertexNormals[ 0 ].clone(),
+					vertexNormals[ 1 ].clone(),
+					vertexNormals[ 2 ].clone()
+				);
+
+			} else {
+
+				var normal = face.normal;
+
+				this.normals.push(
+					normal.clone(),
+					normal.clone(),
+					normal.clone()
+				);
+
+			}
+
+			var vertexColors = face.vertexColors;
+
+			if ( materialVertexColors === THREE.VertexColors ) {
+
+				this.colors.push(
+					vertexColors[ 0 ].clone(),
+					vertexColors[ 1 ].clone(),
+					vertexColors[ 2 ].clone()
+				);
+
+			} else if ( materialVertexColors === THREE.FaceColors ) {
+
+				var color = face.color;
+
+				this.colors.push(
+					color.clone(),
+					color.clone(),
+					color.clone()
+				);
+
+			}
+
+			if ( hasFaceVertexUv === true ) {
+
+				var vertexUvs = faceVertexUvs[ 0 ][ i ];
+
+				if ( vertexUvs !== undefined ) {
+
+					this.uvs.push(
+						vertexUvs[ 0 ].clone(),
+						vertexUvs[ 1 ].clone(),
+						vertexUvs[ 2 ].clone()
+					);
+
+				} else {
+
+					console.warn( 'THREE.BufferGeometry.fromGeometry(): Undefined vertexUv', i );
+
+					this.uvs.push(
+						new THREE.Vector2(),
+						new THREE.Vector2(),
+						new THREE.Vector2()
+					);
+
+				}
+
+			}
+
+			if ( hasFaceVertexUv2 === true ) {
+
+				var vertexUvs = faceVertexUvs[ 1 ][ i ];
+
+				if ( vertexUvs !== undefined ) {
+
+					this.uvs2.push(
+						vertexUvs[ 0 ].clone(),
+						vertexUvs[ 1 ].clone(),
+						vertexUvs[ 2 ].clone()
+					);
+
+				} else {
+
+					console.warn( 'THREE.BufferGeometry.fromGeometry(): Undefined vertexUv2', i );
+
+					this.uvs2.push(
+						new THREE.Vector2(),
+						new THREE.Vector2(),
+						new THREE.Vector2()
+					);
+
+				}
+
+			}
+
+		}
+
+		return this;
+
+		/*
 		this.vertices = geometry.vertices;
 		this.faces = geometry.faces;
 
@@ -105,6 +229,7 @@ THREE.DirectGeometry.prototype = {
 		if ( geometry.skinWeights ) this.skinWeights = geometry.skinWeights.slice( 0 );
 
 		return this;
+		*/
 
 	},
 

+ 11 - 0
src/core/Index.js

@@ -0,0 +1,11 @@
+/**
+ * @author mrdoob / http://mrdoob.com/
+ */
+
+THREE.Index = function ( a, b, c ) {
+
+	this.a = a;
+	this.b = b;
+	this.c = c;
+
+};

+ 29 - 33
src/renderers/webgl/WebGLObjects.js

@@ -119,7 +119,7 @@ THREE.WebGLObjects = function ( gl, info ) {
 
 		var geometry = geometries.get( object );
 
-		if ( object.geometry instanceof THREE.DirectGeometry ) {
+		if ( object.geometry.dynamic === true ) {
 
 			geometry.updateFromObject( object );
 
@@ -176,62 +176,58 @@ THREE.WebGLObjects = function ( gl, info ) {
 
 		//
 
-		if ( geometry instanceof THREE.BufferGeometry ) {
+		var attributes = geometry.attributes;
 
-			var attributes = geometry.attributes;
+		for ( var name in attributes ) {
 
-			for ( var name in attributes ) {
+			var attribute = attributes[ name ];
 
-				var attribute = attributes[ name ];
+			var bufferType = ( name === 'index' ) ? gl.ELEMENT_ARRAY_BUFFER : gl.ARRAY_BUFFER;
 
-				var bufferType = ( name === 'index' ) ? gl.ELEMENT_ARRAY_BUFFER : gl.ARRAY_BUFFER;
+			var data = ( attribute instanceof THREE.InterleavedBufferAttribute ) ? attribute.data : attribute;
 
-				var data = ( attribute instanceof THREE.InterleavedBufferAttribute ) ? attribute.data : attribute;
+			if ( data.buffer === undefined ) {
 
-				if ( data.buffer === undefined ) {
+				data.buffer = gl.createBuffer();
+				gl.bindBuffer( bufferType, data.buffer );
 
-					data.buffer = gl.createBuffer();
-					gl.bindBuffer( bufferType, data.buffer );
+				var usage = gl.STATIC_DRAW;
 
-					var usage = gl.STATIC_DRAW;
+				if ( data instanceof THREE.DynamicBufferAttribute
+						 || ( data instanceof THREE.InstancedBufferAttribute && data.dynamic === true )
+						 || ( data instanceof THREE.InterleavedBuffer && data.dynamic === true ) ) {
 
-					if ( data instanceof THREE.DynamicBufferAttribute
-							 || ( data instanceof THREE.InstancedBufferAttribute && data.dynamic === true )
-							 || ( data instanceof THREE.InterleavedBuffer && data.dynamic === true ) ) {
+					usage = gl.DYNAMIC_DRAW;
 
-						usage = gl.DYNAMIC_DRAW;
-
-					}
-
-					gl.bufferData( bufferType, data.array, usage );
-
-					data.needsUpdate = false;
+				}
 
-				} else if ( data.needsUpdate === true ) {
+				gl.bufferData( bufferType, data.array, usage );
 
-					gl.bindBuffer( bufferType, data.buffer );
+				data.needsUpdate = false;
 
-					if ( data.updateRange === undefined || data.updateRange.count === -1 ) { // Not using update ranges
+			} else if ( data.needsUpdate === true ) {
 
-						gl.bufferSubData( bufferType, 0, data.array );
+				gl.bindBuffer( bufferType, data.buffer );
 
-					} else if ( data.updateRange.count === 0 ) {
+				if ( data.updateRange === undefined || data.updateRange.count === -1 ) { // Not using update ranges
 
-						console.error( 'THREE.WebGLRenderer.updateObject: using updateRange for THREE.DynamicBufferAttribute and marked as needsUpdate but count is 0, ensure you are using set methods or updating manually.' );
+					gl.bufferSubData( bufferType, 0, data.array );
 
-					} else {
+				} else if ( data.updateRange.count === 0 ) {
 
-						gl.bufferSubData( bufferType, data.updateRange.offset * data.array.BYTES_PER_ELEMENT,
-										 data.array.subarray( data.updateRange.offset, data.updateRange.offset + data.updateRange.count ) );
+					console.error( 'THREE.WebGLRenderer.updateObject: using updateRange for THREE.DynamicBufferAttribute and marked as needsUpdate but count is 0, ensure you are using set methods or updating manually.' );
 
-						data.updateRange.count = 0; // reset range
+				} else {
 
-					}
+					gl.bufferSubData( bufferType, data.updateRange.offset * data.array.BYTES_PER_ELEMENT,
+									 data.array.subarray( data.updateRange.offset, data.updateRange.offset + data.updateRange.count ) );
 
-					data.needsUpdate = false;
+					data.updateRange.count = 0; // reset range
 
 				}
 
+				data.needsUpdate = false;
+
 			}
 
 		}

+ 1 - 0
utils/build/includes/common.json

@@ -20,6 +20,7 @@
 	"src/math/Triangle.js",
 	"src/core/Clock.js",
 	"src/core/EventDispatcher.js",
+	"src/core/Index.js",
 	"src/core/Raycaster.js",
 	"src/core/Object3D.js",
 	"src/core/Face3.js",