Browse Source

Removed setGeometry() and setMaterial() from Mesh. Moved the boundSphere check to Frustum and Raycasting.

Mr.doob 12 years ago
parent
commit
e65bec241c

+ 1 - 1
examples/js/controls/TransformControls.js

@@ -934,7 +934,7 @@ THREE.TransformControls = function ( camera, domElement, doc ) {
 
 		var tempGeometry = new THREE.Geometry();
 		THREE.GeometryUtils.merge( tempGeometry, object );
-		object.setGeometry( tempGeometry );
+		object.geometry = tempGeometry;
 		object.position.set( 0, 0, 0 );
 		object.rotation.set( 0, 0, 0 );
 		object.scale.set( 1, 1, 1 );

+ 6 - 9
examples/js/loaders/VRMLLoader.js

@@ -112,8 +112,7 @@ THREE.VRMLLoader.prototype = {
 
 						if ( /appearance/.exec( data ) ) {
 
-							var material = defines[ /USE (\w+)/.exec( data )[ 1 ] ].clone();
-							parent.setMaterial( material );
+							parent.material = defines[ /USE (\w+)/.exec( data )[ 1 ] ].clone();
 
 						} else {
 
@@ -216,7 +215,7 @@ THREE.VRMLLoader.prototype = {
 
 						}
 
-						parent.setGeometry( new THREE.CubeGeometry( width, height, depth ) );
+						parent.geometry = new THREE.CubeGeometry( width, height, depth );
 
 					} else if ( /Cylinder/.exec( data.string ) ) {
 
@@ -238,7 +237,7 @@ THREE.VRMLLoader.prototype = {
 
 						}
 
-						parent.setGeometry( new THREE.CylinderGeometry( radius, radius, height ) );
+						parent.geometry = new THREE.CylinderGeometry( radius, radius, height );
 
 					} else if ( /Cone/.exec( data.string ) ) {
 
@@ -260,15 +259,13 @@ THREE.VRMLLoader.prototype = {
 
 						}
 
-						parent.setGeometry( new THREE.CylinderGeometry( topRadius, bottomRadius, height ) );
+						parent.geometry = new THREE.CylinderGeometry( topRadius, bottomRadius, height );
 
 					} else if ( /Sphere/.exec( data.string ) ) {
 
 						var result = /radius( +[\d|\.|\+|\-|e]+)/.exec( data.children[ 0 ] );
 
-						parent.setGeometry( new THREE.SphereGeometry(
-							parseFloat( result[ 1 ] )
-						) );
+						parent.geometry = new THREE.SphereGeometry( parseFloat( result[ 1 ] ) );
 
 					}
 
@@ -336,7 +333,7 @@ THREE.VRMLLoader.prototype = {
 
 							}
 
-							parent.setMaterial( material );
+							parent.material = material;
 
 						}
 

+ 13 - 4
src/core/Raycaster.js

@@ -66,9 +66,14 @@
 
 		} else if (object instanceof THREE.Mesh ) {
 
+			var geometry = object.geometry;
+
 			// Checking boundingSphere distance to ray
 			matrixPosition.getPositionFromMatrix( object.matrixWorld );
-			sphere.set( matrixPosition, object.geometry.boundingSphere.radius * object.matrixWorld.getMaxScaleOnAxis() );
+
+			if ( geometry.boundingSphere === null ) geometry.computeBoundingSphere();
+
+			sphere.set( matrixPosition, geometry.boundingSphere.radius * object.matrixWorld.getMaxScaleOnAxis() );
 
 			if ( raycaster.ray.isIntersectionSphere( sphere ) === false ) {
 
@@ -76,7 +81,6 @@
 
 			}
 
-			var geometry = object.geometry;
 			var vertices = geometry.vertices;
 
 			if ( geometry instanceof THREE.BufferGeometry ) {
@@ -279,9 +283,13 @@
 
 			var precisionSq = precision * precision;
 
+			var geometry = object.geometry;
+
+			if ( geometry.boundingSphere === null ) geometry.computeBoundingSphere();
+
 			// Checking boundingSphere distance to ray
 			matrixPosition.getPositionFromMatrix(object.matrixWorld);
-			sphere.set(matrixPosition, object.geometry.boundingSphere.radius * object.matrixWorld.getMaxScaleOnAxis());
+			sphere.set(matrixPosition, geometry.boundingSphere.radius * object.matrixWorld.getMaxScaleOnAxis());
 			
 			if(!raycaster.ray.isIntersectionSphere(sphere))
 				return intersects;
@@ -289,7 +297,8 @@
 			inverseMatrix.getInverse(object.matrixWorld);
 			localRay.copy(raycaster.ray).applyMatrix4(inverseMatrix);
 			localRay.direction.normalize(); // for scale matrix
-			var vertices = object.geometry.vertices;
+
+			var vertices = geometry.vertices;
 			var nbVertices = vertices.length;
 			var interSegment = new THREE.Vector3();
 			var interLine = new THREE.Vector3();

+ 1 - 0
src/loaders/JSONLoader.js

@@ -107,6 +107,7 @@ THREE.JSONLoader.prototype.parse = function ( json, texturePath ) {
 
 	geometry.computeCentroids();
 	geometry.computeFaceNormals();
+	geometry.computeBoundingSphere();
 
 	function parseModel( scale ) {
 

+ 7 - 2
src/math/Frustum.js

@@ -80,12 +80,17 @@ THREE.Frustum.prototype = {
 
 			// this method is expanded inlined for performance reasons.
 
+			var geometry = object.geometry;
 			var matrix = object.matrixWorld;
-			var planes = this.planes;
-			var negRadius = - object.geometry.boundingSphere.radius * matrix.getMaxScaleOnAxis();
+
+			if ( geometry.boundingSphere === null ) geometry.computeBoundingSphere();
+
+			var negRadius = - geometry.boundingSphere.radius * matrix.getMaxScaleOnAxis();
 
 			center.getPositionFromMatrix( matrix );
 
+			var planes = this.planes;
+
 			for ( var i = 0; i < 6; i ++ ) {
 
 				var distance = planes[ i ].distanceToPoint( center );

+ 3 - 12
src/objects/Line.js

@@ -6,19 +6,10 @@ THREE.Line = function ( geometry, material, type ) {
 
 	THREE.Object3D.call( this );
 
-	this.geometry = geometry;
-	this.material = ( material !== undefined ) ? material : new THREE.LineBasicMaterial( { color: Math.random() * 0xffffff } );
-	this.type = ( type !== undefined ) ? type : THREE.LineStrip;
-
-	if ( this.geometry ) {
-
-		if ( ! this.geometry.boundingSphere ) {
+	this.geometry = geometry !== undefined ? geometry : new THREE.Geometry();
+	this.material = material !== undefined ? material : new THREE.LineBasicMaterial( { color: Math.random() * 0xffffff } );
 
-			this.geometry.computeBoundingSphere();
-
-		}
-
-	}
+	this.type = ( type !== undefined ) ? type : THREE.LineStrip;
 
 };
 

+ 3 - 36
src/objects/Mesh.js

@@ -9,48 +9,15 @@ THREE.Mesh = function ( geometry, material ) {
 
 	THREE.Object3D.call( this );
 
-	this.geometry = null;
-	this.material = null;
+	this.geometry = geometry !== undefined ? geometry : new THREE.Geometry();
+	this.material = material !== undefined ? material : new THREE.MeshBasicMaterial( { color: Math.random() * 0xffffff } );
 
-	this.setGeometry( geometry );
-	this.setMaterial( material );
+	this.updateMorphTargets();
 
 };
 
 THREE.Mesh.prototype = Object.create( THREE.Object3D.prototype );
 
-THREE.Mesh.prototype.setGeometry = function ( geometry ) {
-
-	if ( geometry !== undefined ) {
-
-		this.geometry = geometry;
-
-		if ( this.geometry.boundingSphere === null ) {
-
-			this.geometry.computeBoundingSphere();
-
-		}
-
-		this.updateMorphTargets();
-
-	}
-
-};
-
-THREE.Mesh.prototype.setMaterial = function ( material ) {
-
-	if ( material !== undefined ) {
-
-		this.material = material;
-
-	} else {
-
-		this.material = new THREE.MeshBasicMaterial( { color: Math.random() * 0xffffff, wireframe: true } );
-
-	}
-
-};
-
 THREE.Mesh.prototype.updateMorphTargets = function () {
 
 	if ( this.geometry.morphTargets.length > 0 ) {

+ 3 - 15
src/objects/ParticleSystem.js

@@ -6,23 +6,10 @@ THREE.ParticleSystem = function ( geometry, material ) {
 
 	THREE.Object3D.call( this );
 
-	this.geometry = geometry;
-	this.material = ( material !== undefined ) ? material : new THREE.ParticleBasicMaterial( { color: Math.random() * 0xffffff } );
+	this.geometry = geometry !== undefined ? geometry : new THREE.Geometry();
+	this.material = material !== undefined ? material : new THREE.ParticleBasicMaterial( { color: Math.random() * 0xffffff } );
 
 	this.sortParticles = false;
-
-	if ( this.geometry ) {
-
-		// calc bound radius
-
-		if( this.geometry.boundingSphere === null ) {
-
-			this.geometry.computeBoundingSphere();
-
-		}
-
-	}
-
 	this.frustumCulled = false;
 
 };
@@ -32,6 +19,7 @@ THREE.ParticleSystem.prototype = Object.create( THREE.Object3D.prototype );
 THREE.ParticleSystem.prototype.clone = function ( object ) {
 
 	if ( object === undefined ) object = new THREE.ParticleSystem( this.geometry, this.material );
+
 	object.sortParticles = this.sortParticles;
 
 	THREE.Object3D.prototype.clone.call( this, object );