2
0
Эх сурвалжийг харах

Adapting Projector to new internal changes. Quite broken but getting there.

Mr.doob 14 жил өмнө
parent
commit
6514f67c12

+ 2 - 2
examples/canvas_sandbox.html

@@ -75,8 +75,9 @@
 		<script type="text/javascript" src="../src/renderers/WebGLShaders.js"></script>
 		<script type="text/javascript" src="../src/renderers/WebGLRenderer.js"></script>
 		<script type="text/javascript" src="../src/renderers/SoundRenderer.js"></script>
-		<script type="text/javascript" src="../src/renderers/renderables/RenderableObject.js"></script>
+		<script type="text/javascript" src="../src/renderers/renderables/RenderableVertex.js"></script>
 		<script type="text/javascript" src="../src/renderers/renderables/RenderableFace3.js"></script>
+		<script type="text/javascript" src="../src/renderers/renderables/RenderableObject.js"></script>
 		<script type="text/javascript" src="../src/renderers/renderables/RenderableParticle.js"></script>
 		<script type="text/javascript" src="../src/renderers/renderables/RenderableLine.js"></script>
 		<script type="text/javascript" src="../src/extras/GeometryUtils.js"></script>
@@ -95,7 +96,6 @@
 		<script type="text/javascript" src="../src/extras/geometries/TorusKnot.js"></script>
 		<script type="text/javascript" src="../src/extras/io/Loader.js"></script>
 		<script type="text/javascript" src="../src/extras/objects/MarchingCubes.js"></script>
-		
 
 		<script type="text/javascript" src="js/RequestAnimationFrame.js"></script>
 		<script type="text/javascript" src="js/Stats.js"></script>

+ 5 - 5
src/core/Face3.js

@@ -8,15 +8,15 @@ THREE.Face3 = function ( a, b, c, normal, color, materials ) {
 	this.a = a; 
 	this.b = b;
 	this.c = c;
-	
+
 	this.normal = normal instanceof THREE.Vector3 ? normal : new THREE.Vector3();
-	this.vertexNormals = normal instanceof Array ? normal : [];
-	
+	this.vertexNormals = normal instanceof Array ? normal : [ new THREE.Vector3(), new THREE.Vector3(), new THREE.Vector3() ];
+
 	this.color = color instanceof THREE.Color ? color : new THREE.Color();
 	this.vertexColors = color instanceof Array ? color : [];
-	
+
 	this.materials = materials instanceof Array ? materials : [ materials ];
-	
+
 	this.centroid = new THREE.Vector3();
 
 };

+ 5 - 5
src/core/Face4.js

@@ -9,15 +9,15 @@ THREE.Face4 = function ( a, b, c, d, normal, color, materials ) {
 	this.b = b;
 	this.c = c;
 	this.d = d;
-	
+
 	this.normal = normal instanceof THREE.Vector3 ? normal : new THREE.Vector3();
-	this.vertexNormals = normal instanceof Array ? normal : [];
-	
+	this.vertexNormals = normal instanceof Array ? normal : [ new THREE.Vector3(), new THREE.Vector3(), new THREE.Vector3(), new THREE.Vector3() ];
+
 	this.color = color instanceof THREE.Color ? color : new THREE.Color();
 	this.vertexColors = color instanceof Array ? color : [];
-	
+
 	this.materials = materials instanceof Array ? materials : [ materials ];
-	
+
 	this.centroid = new THREE.Vector3();
 
 };

+ 5 - 3
src/core/Geometry.js

@@ -11,12 +11,12 @@ THREE.Geometry = function () {
 
 	this.vertices = [];
 	this.colors = []; // one-to-one vertex colors, used in ParticleSystem, Line and Ribbon
-	
+
 	this.faces = [];
-	
+
 	this.faceUvs = [];
 	this.faceVertexUvs = [];
-	
+
 	this.morphTargets = [];
 
 	this.skinWeights = [];
@@ -66,12 +66,14 @@ THREE.Geometry.prototype = {
 		var n, nl, v, vl, vertex, f, fl, face, vA, vB, vC,
 		cb = new THREE.Vector3(), ab = new THREE.Vector3();
 
+		/*
 		for ( v = 0, vl = this.vertices.length; v < vl; v ++ ) {
 
 			vertex = this.vertices[ v ];
 			vertex.normal.set( 0, 0, 0 );
 
 		}
+		*/
 
 		for ( f = 0, fl = this.faces.length; f < fl; f ++ ) {
 

+ 4 - 1
src/core/Vertex.js

@@ -2,9 +2,11 @@
  * @author mr.doob / http://mrdoob.com/
  */
 
-THREE.Vertex = function ( position, normal ) {
+THREE.Vertex = function ( position /*, normal*/ ) {
 
 	this.position = position || new THREE.Vector3();
+
+	/*
 	this.positionWorld = new THREE.Vector3();
 	this.positionScreen = new THREE.Vector4();
 
@@ -15,5 +17,6 @@ THREE.Vertex = function ( position, normal ) {
 	this.tangent = new THREE.Vector4();
 
 	this.__visible = true;
+	*/
 
 };

+ 2 - 2
src/extras/geometries/Sphere.js

@@ -80,7 +80,7 @@ var Sphere = function ( radius, segmentsWidth, segmentsHeight ) {
 
 					this.faces.push( new THREE.Face3( aP1, aP2, aP3, [ new THREE.Vector3( n1.x, n1.y, n1.z ), new THREE.Vector3( n2.x, n2.y, n2.z ), new THREE.Vector3( n3.x, n3.y, n3.z ) ] ) );
 
-					this.uvs.push( [ aP1uv, aP2uv, aP3uv ] );
+					this.faceVertexUvs.push( [ aP1uv, aP2uv, aP3uv ] );
 
 				}
 
@@ -95,7 +95,7 @@ var Sphere = function ( radius, segmentsWidth, segmentsHeight ) {
 
 					this.faces.push( new THREE.Face3( aP1, aP3, aP4, [ new THREE.Vector3( n1.x, n1.y, n1.z ), new THREE.Vector3( n2.x, n2.y, n2.z ), new THREE.Vector3( n3.x, n3.y, n3.z ) ] ) );
 
-					this.uvs.push( [ aP1uv, aP3uv, aP4uv ] );
+					this.faceVertexUvs.push( [ aP1uv, aP3uv, aP4uv ] );
 
 				}
 

+ 115 - 66
src/renderers/Projector.js

@@ -7,6 +7,7 @@
 THREE.Projector = function() {
 
 	var _object, _objectCount, _objectPool = [],
+	_vertex, _vertexCount, _vertexPool = [],
 	_face3, _face32, _face3Count, _face3Pool = [],
 	_line, _lineCount, _linePool = [],
 	_particle, _particleCount, _particlePool = [],
@@ -30,6 +31,32 @@ THREE.Projector = function() {
 
 	_face3VertexNormals;
 
+
+	this.projectVector = function ( vector, camera ) {
+
+		_projScreenMatrix.multiply( camera.projectionMatrix, camera.matrixWorldInverse );
+		_projScreenMatrix.multiplyVector3( vector );
+
+		return vector;
+
+	};
+
+	this.unprojectVector = function ( vector, camera ) {
+
+		_projScreenMatrix.multiply( THREE.Matrix4.makeInvert( camera.projectionMatrix ), camera.matrixWorld );
+		_projScreenMatrix.multiplyVector3( vector );
+
+		/*
+		var matrix = camera.matrixWorld.clone();
+
+		matrix.multiplySelf( THREE.Matrix4.makeInvert( camera.projectionMatrix ) );
+		matrix.multiplyVector3( vector );
+		*/
+
+		return vector;
+
+	};
+
 	this.projectObjects = function ( scene, camera, sort ) {
 
 		var renderList = [],
@@ -45,7 +72,7 @@ THREE.Projector = function() {
 
 			if ( !object.visible || ( object instanceof THREE.Mesh && !isInFrustum( object ) ) ) continue;
 
-			_object = _objectPool[ _objectCount ] = _objectPool[ _objectCount ] || new THREE.RenderableObject();
+			_object = getNextObjectInPool();
 
 			_vector3.copy( object.position );
 			_projScreenMatrix.multiplyVector3( _vector3 );
@@ -55,8 +82,6 @@ THREE.Projector = function() {
 
 			renderList.push( _object );
 
-			_objectCount ++;
-
 		}
 
 		sort && renderList.sort( painterSort );
@@ -71,12 +96,14 @@ THREE.Projector = function() {
 
 		var renderList = [], near = camera.near, far = camera.far,
 		o, ol, v, vl, f, fl, n, nl, objects, object,
-		objectMatrix, objectMaterials, objectOverdraw,
+		objectMatrix, objectMaterials,
 		objectMatrixRotation,
 		geometry, vertices, vertex, vertexPositionScreen,
 		faces, face, faceVertexNormals, normal, v1, v2, v3, v4;
 
-		_face3Count = _lineCount = _particleCount = 0;
+		_face3Count = 0;
+		_lineCount = 0;
+		_particleCount = 0;
 
 		camera.matrixAutoUpdate && camera.updateMatrix();
 
@@ -85,7 +112,7 @@ THREE.Projector = function() {
 		_projScreenMatrix.multiply( camera.projectionMatrix, camera.matrixWorldInverse );
 		computeFrustum( _projScreenMatrix );
 
-		objects = this.projectObjects( scene, camera, true ); // scene.objects;
+		objects = this.projectObjects( scene, camera, true );
 
 		for ( o = 0, ol = objects.length; o < ol; o++ ) {
 
@@ -97,61 +124,53 @@ THREE.Projector = function() {
 			objectMatrixRotation = object.matrixRotationWorld;
 
 			objectMaterials = object.materials;
-			objectOverdraw = object.overdraw;
+
+			_vertexCount = 0;
 
 			if ( object instanceof THREE.Mesh ) {
 
 				geometry = object.geometry;
-
-				// vertices
-
 				vertices = geometry.vertices;
+				faces = geometry.faces;
 
 				for ( v = 0, vl = vertices.length; v < vl; v ++ ) {
 
-					vertex = vertices[ v ];
+					_vertex = getNextVertexInPool();
+					_vertex.positionWorld.copy( vertices[ v ].position );
 
-					vertex.positionWorld.copy( vertex.position );
-					objectMatrix.multiplyVector3( vertex.positionWorld );
+					objectMatrix.multiplyVector3( _vertex.positionWorld );
 
-					vertexPositionScreen = vertex.positionScreen;
-					vertexPositionScreen.copy( vertex.positionWorld );
-					_projScreenMatrix.multiplyVector4( vertexPositionScreen );
+					_vertex.positionScreen.copy( _vertex.positionWorld );
+					_projScreenMatrix.multiplyVector4( _vertex.positionScreen );
 
-					vertexPositionScreen.x /= vertexPositionScreen.w;
-					vertexPositionScreen.y /= vertexPositionScreen.w;
+					_vertex.positionScreen.x /= _vertex.positionScreen.w;
+					_vertex.positionScreen.y /= _vertex.positionScreen.w;
 
-					vertex.__visible = vertexPositionScreen.z > near && vertexPositionScreen.z < far;
+					_vertex.visible = _vertex.positionScreen.z > near && _vertex.positionScreen.z < far;
 
 				}
 
-				// faces
-
-				faces = geometry.faces;
-
 				for ( f = 0, fl = faces.length; f < fl; f ++ ) {
 
 					face = faces[ f ];
 
 					if ( face instanceof THREE.Face3 ) {
 
-						v1 = vertices[ face.a ]; v2 = vertices[ face.b ]; v3 = vertices[ face.c ];
+						v1 = _vertexPool[ face.a ];
+						v2 = _vertexPool[ face.b ];
+						v3 = _vertexPool[ face.c ];
 
-						if ( v1.__visible && v2.__visible && v3.__visible ) {
+						if ( v1.visible && v2.visible && v3.visible ) {
 
 							if ( ( object.doubleSided || ( object.flipSided !=
 							   ( v3.positionScreen.x - v1.positionScreen.x ) * ( v2.positionScreen.y - v1.positionScreen.y ) -
 							   ( v3.positionScreen.y - v1.positionScreen.y ) * ( v2.positionScreen.x - v1.positionScreen.x ) < 0 ) ) ) {
 
-								_face3 = _face3Pool[ _face3Count ] = _face3Pool[ _face3Count ] || new THREE.RenderableFace3();
+								_face3 = getNextFaceInPool();
 
-								_face3.v1.positionWorld.copy( v1.positionWorld );
-								_face3.v2.positionWorld.copy( v2.positionWorld );
-								_face3.v3.positionWorld.copy( v3.positionWorld );
-
-								_face3.v1.positionScreen.copy( v1.positionScreen );
-								_face3.v2.positionScreen.copy( v2.positionScreen );
-								_face3.v3.positionScreen.copy( v3.positionScreen );
+								_face3.v1.copy( v1 );
+								_face3.v2.copy( v2 );
+								_face3.v3.copy( v3 );
 
 								_face3.normalWorld.copy( face.normal );
 								objectMatrixRotation.multiplyVector3( _face3.normalWorld );
@@ -165,9 +184,9 @@ THREE.Projector = function() {
 								faceVertexNormals = face.vertexNormals;
 								_face3VertexNormals = _face3.vertexNormalsWorld;
 
-								for ( n = 0, nl = faceVertexNormals.length; n < nl; n ++ ) {
+								for ( n = 0; n < 3; n ++ ) {
 
-									normal = _face3VertexNormals[ n ] = _face3VertexNormals[ n ] || new THREE.Vector3();
+									normal = _face3VertexNormals[ n ];
 									normal.copy( faceVertexNormals[ n ] );
 									objectMatrixRotation.multiplyVector3( normal );
 
@@ -177,25 +196,22 @@ THREE.Projector = function() {
 
 								_face3.meshMaterials = objectMaterials;
 								_face3.faceMaterials = face.materials;
-								_face3.overdraw = objectOverdraw;
 
-								if ( object.geometry.uvs[ f ] ) {
+								if ( object.geometry.faceUvs[ f ] ) {
 
-									_face3.uvs[ 0 ] = object.geometry.uvs[ f ][ 0 ];
-									_face3.uvs[ 1 ] = object.geometry.uvs[ f ][ 1 ];
-									_face3.uvs[ 2 ] = object.geometry.uvs[ f ][ 2 ];
+									_face3.uvs[ 0 ] = object.geometry.faceUvs[ f ][ 0 ];
+									_face3.uvs[ 1 ] = object.geometry.faceUvs[ f ][ 1 ];
+									_face3.uvs[ 2 ] = object.geometry.faceUvs[ f ][ 2 ];
 
 								}
 
 								renderList.push( _face3 );
 
-								_face3Count ++;
-
 							}
 
 						}
 
-					} else if ( face instanceof THREE.Face4 ) {
+					} /* else if ( face instanceof THREE.Face4 ) {
 
 						v1 = vertices[ face.a ]; v2 = vertices[ face.b ]; v3 = vertices[ face.c ]; v4 = vertices[ face.d ];
 
@@ -232,7 +248,6 @@ THREE.Projector = function() {
 
 								_face3.meshMaterials = objectMaterials;
 								_face3.faceMaterials = face.materials;
-								_face3.overdraw = objectOverdraw;
 
 								if ( object.geometry.uvs[ f ] ) {
 
@@ -268,7 +283,6 @@ THREE.Projector = function() {
 
 								_face32.meshMaterials = objectMaterials;
 								_face32.faceMaterials = face.materials;
-								_face32.overdraw = objectOverdraw;
 
 								if ( object.geometry.uvs[ f ] ) {
 
@@ -286,7 +300,7 @@ THREE.Projector = function() {
 
 						}
 
-					}
+					} */
 
 				}
 
@@ -296,18 +310,17 @@ THREE.Projector = function() {
 
 				vertices = object.geometry.vertices;
 
-				vertex = vertices[ 0 ];
-				vertex.positionScreen.copy( vertex.position );
-				_projScreenObjectMatrix.multiplyVector4( vertex.positionScreen );
+				v1 = getNextVertexInPool();
+				v1.positionScreen.copy( vertices[ 0 ].position );
+				_projScreenObjectMatrix.multiplyVector4( v1.positionScreen );
 
 				for ( v = 1, vl = vertices.length; v < vl; v++ ) {
 
-					v1 = vertices[ v ];
-
-					v1.positionScreen.copy( v1.position );
+					v1 = getNextVertexInPool();
+					v1.positionScreen.copy( vertices[ v ].position );
 					_projScreenObjectMatrix.multiplyVector4( v1.positionScreen );
 
-					v2 = vertices[ v - 1 ];
+					v2 = _vertexPool[ _vertexCount - 2 ];
 
 					_clippedVertex1PositionScreen.copy( v1.positionScreen );
 					_clippedVertex2PositionScreen.copy( v2.positionScreen );
@@ -318,7 +331,7 @@ THREE.Projector = function() {
 						_clippedVertex1PositionScreen.multiplyScalar( 1 / _clippedVertex1PositionScreen.w );
 						_clippedVertex2PositionScreen.multiplyScalar( 1 / _clippedVertex2PositionScreen.w );
 
-						_line = _linePool[ _lineCount ] = _linePool[ _lineCount ] || new THREE.RenderableLine();
+						_line = getNextLineInPool();
 						_line.v1.positionScreen.copy( _clippedVertex1PositionScreen );
 						_line.v2.positionScreen.copy( _clippedVertex2PositionScreen );
 
@@ -328,22 +341,19 @@ THREE.Projector = function() {
 
 						renderList.push( _line );
 
-						_lineCount ++;
-
 					}
 				}
 
 			} else if ( object instanceof THREE.Particle ) {
 
 				_vector4.set( object.position.x, object.position.y, object.position.z, 1 );
-
 				_projScreenMatrix.multiplyVector4( _vector4 );
 
 				_vector4.z /= _vector4.w;
 
 				if ( _vector4.z > 0 && _vector4.z < 1 ) {
 
-					_particle = _particlePool[ _particleCount ] = _particlePool[ _particleCount ] || new THREE.RenderableParticle();
+					_particle = getNextParticleInPool();
 					_particle.x = _vector4.x / _vector4.w;
 					_particle.y = _vector4.y / _vector4.w;
 					_particle.z = _vector4.z;
@@ -357,8 +367,6 @@ THREE.Projector = function() {
 
 					renderList.push( _particle );
 
-					_particleCount ++;
-
 				}
 
 			}
@@ -371,16 +379,57 @@ THREE.Projector = function() {
 
 	};
 
-	this.unprojectVector = function ( vector, camera ) {
+	// Pools
 
-		var matrix = camera.matrixWorld.clone();
+	function getNextObjectInPool() {
 
-		matrix.multiplySelf( THREE.Matrix4.makeInvert( camera.projectionMatrix ) );
-		matrix.multiplyVector3( vector );
+		var object = _objectPool[ _objectCount ] = _objectPool[ _objectCount ] || new THREE.RenderableObject();
 
-		return vector;
+		_objectCount ++;
 
-	};
+		return object;
+
+	}
+
+	function getNextVertexInPool() {
+
+		var vertex = _vertexPool[ _vertexCount ] = _vertexPool[ _vertexCount ] || new THREE.RenderableVertex();
+
+		_vertexCount ++;
+
+		return vertex;
+
+	}
+
+	function getNextFaceInPool() {
+
+		var face = _face3Pool[ _face3Count ] = _face3Pool[ _face3Count ] || new THREE.RenderableFace3();
+
+		_face3Count ++;
+
+		return face;
+
+	}
+
+	function getNextLineInPool() {
+
+		var line = _linePool[ _lineCount ] = _linePool[ _lineCount ] || new THREE.RenderableLine();
+
+		_lineCount ++;
+
+		return line;
+
+	}
+
+	function getNextParticleInPool() {
+
+		var particle = _particlePool[ _particleCount ] = _particlePool[ _particleCount ] || new THREE.RenderableParticle();
+		_particleCount ++;
+		return partile;
+
+	}
+
+	//
 
 	function painterSort( a, b ) {
 

+ 7 - 8
src/renderers/renderables/RenderableFace3.js

@@ -4,21 +4,20 @@
 
 THREE.RenderableFace3 = function () {
 
-	this.z = null;
-
-	this.v1 = new THREE.Vertex();
-	this.v2 = new THREE.Vertex();
-	this.v3 = new THREE.Vertex();
+	this.v1 = new THREE.RenderableVertex();
+	this.v2 = new THREE.RenderableVertex();
+	this.v3 = new THREE.RenderableVertex();
 
 	this.centroidWorld = new THREE.Vector3();
 	this.centroidScreen = new THREE.Vector3();
 
 	this.normalWorld = new THREE.Vector3();
-	this.vertexNormalsWorld = [];
+	this.vertexNormalsWorld = [ new THREE.Vector3(), new THREE.Vector3(), new THREE.Vector3() ];
 
 	this.meshMaterials = null;
 	this.faceMaterials = null;
-	this.overdraw = false;
-	this.uvs = [ null, null, null ];
+	this.uvs = [];
+
+	this.z = null;
 
 };

+ 2 - 2
src/renderers/renderables/RenderableLine.js

@@ -6,8 +6,8 @@ THREE.RenderableLine = function () {
 
 	this.z = null;
 
-	this.v1 = new THREE.Vertex();
-	this.v2 = new THREE.Vertex();
+	this.v1 = new THREE.RenderableVertex();
+	this.v2 = new THREE.RenderableVertex();
 
 	this.materials = null;
 

+ 19 - 0
src/renderers/renderables/RenderableVertex.js

@@ -0,0 +1,19 @@
+/**
+ * @author mr.doob / http://mrdoob.com/
+ */
+
+THREE.RenderableVertex = function () {
+
+	this.positionWorld = new THREE.Vector3();
+	this.positionScreen = new THREE.Vector4();
+
+	this.visible = true;
+
+};
+
+THREE.RenderableVertex.prototype.copy = function ( vertex ) {
+
+	this.positionWorld.copy( vertex.positionWorld );
+	this.positionScreen.copy( vertex.positionScreen );
+
+}

+ 2 - 1
utils/build.py

@@ -71,8 +71,9 @@ COMMON_FILES = [
 'renderers/WebGLShaders.js',
 'renderers/WebGLRenderer.js',
 'renderers/SoundRenderer.js',
-'renderers/renderables/RenderableObject.js',
+'renderers/renderables/RenderableVertex.js',
 'renderers/renderables/RenderableFace3.js',
+'renderers/renderables/RenderableObject.js',
 'renderers/renderables/RenderableParticle.js',
 'renderers/renderables/RenderableLine.js'
 ]