From befc98a01e44efe7380bf27e2d290cfa97118348 Mon Sep 17 00:00:00 2001 From: alteredq Date: Fri, 25 Nov 2011 03:01:00 +0100 Subject: [PATCH] Refactored bounding box to use 2x Vector3 instead of custom object. --- build/Three.js | 11 ++--- build/custom/ThreeCanvas.js | 14 +++--- build/custom/ThreeExtras.js | 14 +++--- build/custom/ThreeSVG.js | 14 +++--- build/custom/ThreeWebGL.js | 7 ++- examples/canvas_geometry_text.html | 2 +- examples/canvas_particles_shapes.html | 12 ++--- examples/js/Car.js | 29 +++++------- examples/webgl_geometry_text.html | 2 +- examples/webgl_materials_cubemap_dynamic.html | 27 ++++++----- examples/webgl_particles_dynamic.html | 5 +- examples/webgl_particles_shapes.html | 2 +- examples/webgl_shadowmap.html | 2 +- src/core/Geometry.js | 46 +++++++++++-------- src/extras/GeometryUtils.js | 7 +-- 15 files changed, 100 insertions(+), 94 deletions(-) diff --git a/build/Three.js b/build/Three.js index aca4b2a4..2b0ea388 100644 --- a/build/Three.js +++ b/build/Three.js @@ -89,10 +89,9 @@ this.faces[a],c instanceof THREE.Face3)c.vertexNormals=[new THREE.Vector3,new TH d[c.b].addSelf(c.normal),d[c.c].addSelf(c.normal),d[c.d].addSelf(c.normal));a=0;for(b=this.vertices.length;a0){this.boundingBox={x:[this.vertices[0].position.x,this.vertices[0].position.x],y:[this.vertices[0].position.y,this.vertices[0].position.y],z:[this.vertices[0].position.z,this.vertices[0].position.z]};for(var b=1,c=this.vertices.length;bthis.boundingBox.x[1])this.boundingBox.x[1]=a.position.x;if(a.position.ythis.boundingBox.y[1])this.boundingBox.y[1]=a.position.y;if(a.position.zthis.boundingBox.z[1])this.boundingBox.z[1]=a.position.z}}},computeBoundingSphere:function(){for(var a= -0,b=0,c=this.vertices.length;b0){var a;a=this.vertices[0].position;this.boundingBox?(this.boundingBox.min.copy(a),this.boundingBox.max.copy(a)):this.boundingBox={min:a.clone(),max:a.clone()};for(var b=this.boundingBox.min,c=this.boundingBox.max,d=1,e=this.vertices.length;dc.x)c.x=a.x;if(a.yc.y)c.y=a.y;if(a.zc.z)c.z=a.z}}},computeBoundingSphere:function(){for(var a=0,b=0,c=this.vertices.length;bthis.points.length-2?f:f+1;c[3]=f>this.points.length-3?f:f+2;k=this.points[c[0]];j=this.points[c[1]]; o=this.points[c[2]];p=this.points[c[3]];h=g*g;i=g*h;d.x=b(k.x,j.x,o.x,p.x,g,h,i);d.y=b(k.y,j.y,o.y,p.y,g,h,i);d.z=b(k.z,j.z,o.z,p.z,g,h,i);return d};this.getControlPointsArray=function(){var a,b,c=this.points.length,d=[];for(a=0;aa?b(c,e-1):k[e]0){this.boundingBox={x:[this.vertices[0].position.x,this.vertices[0].position.x],y:[this.vertices[0].position.y,this.vertices[0].position.y],z:[this.vertices[0].position.z,this.vertices[0].position.z]};for(var b=1,c=this.vertices.length;bthis.boundingBox.x[1])this.boundingBox.x[1]=a.position.x;if(a.position.ythis.boundingBox.y[1])this.boundingBox.y[1]=a.position.y;if(a.position.zthis.boundingBox.z[1])this.boundingBox.z[1]=a.position.z}}},computeBoundingSphere:function(){for(var a= -0,b=0,c=this.vertices.length;b0){var a;a=this.vertices[0].position;this.boundingBox?(this.boundingBox.min.copy(a),this.boundingBox.max.copy(a)):this.boundingBox={min:a.clone(),max:a.clone()};for(var b=this.boundingBox.min,c=this.boundingBox.max,d=1,e=this.vertices.length;dc.x)c.x=a.x;if(a.yc.y)c.y=a.y;if(a.zc.z)c.z=a.z}}},computeBoundingSphere:function(){for(var a=0,b=0,c=this.vertices.length;b1&&(d=1-d,e=1-e);g=1-d-e;f.copy(a);f.multiplyScalar(d);h.copy(b);h.multiplyScalar(e);f.addSelf(h);h.copy(c);h.multiplyScalar(g);f.addSelf(h);return f},randomPointInFace:function(a,b,c){var d,e,g;if(a instanceof THREE.Face3)return d=b.vertices[a.a].position,e=b.vertices[a.b].position, g=b.vertices[a.c].position,THREE.GeometryUtils.randomPointInTriangle(d,e,g);else if(a instanceof THREE.Face4){d=b.vertices[a.a].position;e=b.vertices[a.b].position;g=b.vertices[a.c].position;var b=b.vertices[a.d].position,f;c?a._area1&&a._area2?(c=a._area1,f=a._area2):(c=THREE.GeometryUtils.triangleArea(d,e,b),f=THREE.GeometryUtils.triangleArea(e,g,b),a._area1=c,a._area2=f):(c=THREE.GeometryUtils.triangleArea(d,e,b),f=THREE.GeometryUtils.triangleArea(e,g,b));return THREE.GeometryUtils.random()*(c+ -f)a?b(d,e-1):k[e]a?b(c,e-1):k[e]0){this.boundingBox={x:[this.vertices[0].position.x,this.vertices[0].position.x],y:[this.vertices[0].position.y,this.vertices[0].position.y],z:[this.vertices[0].position.z,this.vertices[0].position.z]};for(var b=1,c=this.vertices.length;bthis.boundingBox.x[1])this.boundingBox.x[1]=a.position.x;if(a.position.ythis.boundingBox.y[1])this.boundingBox.y[1]=a.position.y;if(a.position.zthis.boundingBox.z[1])this.boundingBox.z[1]=a.position.z}}},computeBoundingSphere:function(){for(var a= -0,b=0,c=this.vertices.length;b0){var a;a=this.vertices[0].position;this.boundingBox?(this.boundingBox.min.copy(a),this.boundingBox.max.copy(a)):this.boundingBox={min:a.clone(),max:a.clone()};for(var b=this.boundingBox.min,c=this.boundingBox.max,d=1,e=this.vertices.length;dc.x)c.x=a.x;if(a.yc.y)c.y=a.y;if(a.zc.z)c.z=a.z}}},computeBoundingSphere:function(){for(var a=0,b=0,c=this.vertices.length;b0){this.boundingBox={x:[this.vertices[0].position.x,this.vertices[0].position.x],y:[this.vertices[0].position.y,this.vertices[0].position.y],z:[this.vertices[0].position.z,this.vertices[0].position.z]};for(var b=1,d=this.vertices.length;b< -d;b++){a=this.vertices[b];if(a.position.xthis.boundingBox.x[1])this.boundingBox.x[1]=a.position.x;if(a.position.ythis.boundingBox.y[1])this.boundingBox.y[1]=a.position.y;if(a.position.zthis.boundingBox.z[1])this.boundingBox.z[1]=a.position.z}}},computeBoundingSphere:function(){for(var a= -0,b=0,d=this.vertices.length;b0){var a;a=this.vertices[0].position;this.boundingBox?(this.boundingBox.min.copy(a),this.boundingBox.max.copy(a)):this.boundingBox={min:a.clone(),max:a.clone()};for(var b=this.boundingBox.min,d=this.boundingBox.max,c=1,f=this.vertices.length;c< +f;c++){a=this.vertices[c].position;if(a.xd.x)d.x=a.x;if(a.yd.y)d.y=a.y;if(a.zd.z)d.z=a.z}}},computeBoundingSphere:function(){for(var a=0,b=0,d=this.vertices.length;bthis.points.length-2?g:g+1;d[3]=g>this.points.length-3?g:g+2;k=this.points[d[0]];j=this.points[d[1]]; p=this.points[d[2]];s=this.points[d[3]];i=h*h;l=h*i;c.x=b(k.x,j.x,p.x,s.x,h,i,l);c.y=b(k.y,j.y,p.y,s.y,h,i,l);c.z=b(k.z,j.z,p.z,s.z,h,i,l);return c};this.getControlPointsArray=function(){var a,b,d=this.points.length,c=[];for(a=0;a1) hue-=1; particle = new THREE.Particle( material ); particle.scale.x = particle.scale.y = Math.random() * 20 +20; - particleCloud.add( particle ); + particleCloud.add( particle ); return particle; }; var onParticleCreated = function(p) { var position = p.position; - p.target.position = position; + p.target.position = position; }; var onParticleDead = function(particle) { particle.target.visible = false; - particleCloud.remove(particle.target); + particleCloud.remove(particle.target); }; sparksEmitter = new SPARKS.Emitter(new SPARKS.SteadyCounter(160)); @@ -210,7 +210,7 @@ sparksEmitter.addAction(new SPARKS.Age()); //sparksEmitter.addAction(new SPARKS.Accelerate(0.2)); - sparksEmitter.addAction(new SPARKS.Move()); + sparksEmitter.addAction(new SPARKS.Move()); sparksEmitter.addAction(new SPARKS.RandomDrift(50,50,2000)); sparksEmitter.addCallback("created", onParticleCreated); diff --git a/examples/js/Car.js b/examples/js/Car.js index fd8ac2fb..18532102 100644 --- a/examples/js/Car.js +++ b/examples/js/Car.js @@ -20,9 +20,7 @@ THREE.Car = function () { // - other wheels are mirrored against car root // - if necessary back wheels can be offset manually - this.wheelOffsetX = 0; - this.wheelOffsetY = 0; - this.wheelOffsetZ = 0; + this.wheelOffset = new THREE.Vector3(); this.wheelDiameter = 1; @@ -266,15 +264,10 @@ THREE.Car = function () { var bb = scope.wheelGeometry.boundingBox; - var dx = 0.5 * ( bb.x[ 1 ] + bb.x[ 0 ] ); - var dy = 0.5 * ( bb.y[ 1 ] + bb.y[ 0 ] ); - var dz = 0.5 * ( bb.z[ 1 ] + bb.z[ 0 ] ); + scope.wheelOffset.add( bb.min, bb.max ); + scope.wheelOffset.multiplyScalar( 0.5 ); - scope.wheelOffsetX = dx; - scope.wheelOffsetY = dy; - scope.wheelOffsetZ = dz; - - scope.wheelDiameter = bb.y[ 1 ] - bb.y[ 0 ]; + scope.wheelDiameter = bb.max.y - bb.min.y; THREE.GeometryUtils.center( scope.wheelGeometry ); @@ -282,8 +275,8 @@ THREE.Car = function () { // rig the car - var delta, - s = scope.modelScale, + var s = scope.modelScale, + delta = new THREE.Vector3(), faceMaterial = new THREE.MeshFaceMaterial(); // body @@ -295,7 +288,7 @@ THREE.Car = function () { // front left wheel - delta = new THREE.Vector3( s * scope.wheelOffsetX, s * scope.wheelOffsetY, s * scope.wheelOffsetZ ); + delta.multiply( scope.wheelOffset, new THREE.Vector3( s, s, s ) ); scope.frontLeftWheelRoot.position.addSelf( delta ); @@ -307,7 +300,7 @@ THREE.Car = function () { // front right wheel - delta = new THREE.Vector3( -s * scope.wheelOffsetX, s * scope.wheelOffsetY, s * scope.wheelOffsetZ ); + delta.multiply( scope.wheelOffset, new THREE.Vector3( -s, s, s ) ); scope.frontRightWheelRoot.position.addSelf( delta ); @@ -321,7 +314,8 @@ THREE.Car = function () { // back left wheel - delta = new THREE.Vector3( s * scope.wheelOffsetX, s * scope.wheelOffsetY, - s * scope.wheelOffsetZ - scope.backWheelOffset ); + delta.multiply( scope.wheelOffset, new THREE.Vector3( s, s, -s ) ); + delta.z -= scope.backWheelOffset; scope.backLeftWheelMesh = new THREE.Mesh( scope.wheelGeometry, faceMaterial ); @@ -332,7 +326,8 @@ THREE.Car = function () { // back right wheel - delta = new THREE.Vector3( -s * scope.wheelOffsetX, s * scope.wheelOffsetY, - s * scope.wheelOffsetZ - scope.backWheelOffset ) + delta.multiply( scope.wheelOffset, new THREE.Vector3( -s, s, -s ) ); + delta.z -= scope.backWheelOffset; scope.backRightWheelMesh = new THREE.Mesh( scope.wheelGeometry, faceMaterial ); diff --git a/examples/webgl_geometry_text.html b/examples/webgl_geometry_text.html index 99aacece..fedb9583 100644 --- a/examples/webgl_geometry_text.html +++ b/examples/webgl_geometry_text.html @@ -493,7 +493,7 @@ } - var centerOffset = -0.5 * ( textGeo.boundingBox.x[ 1 ] - textGeo.boundingBox.x[ 0 ] ); + var centerOffset = -0.5 * ( textGeo.boundingBox.max.x - textGeo.boundingBox.min.x ); textMesh1 = new THREE.Mesh( textGeo, faceMaterial ); diff --git a/examples/webgl_materials_cubemap_dynamic.html b/examples/webgl_materials_cubemap_dynamic.html index f00d6cac..59bfe9d0 100644 --- a/examples/webgl_materials_cubemap_dynamic.html +++ b/examples/webgl_materials_cubemap_dynamic.html @@ -592,23 +592,26 @@ var bb = shadowObject.geometry.boundingBox; + var dimensions = new THREE.Vector3(); + dimensions.sub( bb.max, bb.min ); + var margin = 0.15, - width = bb.z[ 1 ] - bb.z[ 0 ], - height = bb.x[ 1 ] - bb.x[ 0 ], - depth = bb.y[ 1 ] - bb.y[ 0 ], + width = dimensions.z, + height = dimensions.x, + depth = dimensions.y, - left = bb.z[ 0 ] - margin * width, - right = bb.z[ 1 ] + margin * width, + left = bb.min.z - margin * width, + right = bb.max.z + margin * width, - top = bb.x[ 1 ] + margin * height, - bottom = bb.x[ 0 ] - margin * height, + top = bb.max.x + margin * height, + bottom = bb.min.x - margin * height, - near = bb.y[ 1 ] + margin * depth, - far = bb.y[ 0 ] - margin * depth; + near = bb.max.y + margin * depth, + far = bb.min.y - margin * depth; var topCamera = new THREE.OrthographicCamera( left, right, top, bottom, near, far ); - topCamera.position.y = bb.y[ 1 ]; + topCamera.position.y = bb.max.y; topCamera.lookAt( shadowScene.position ); var renderShadow = new THREE.RenderPass( shadowScene, topCamera ); @@ -669,8 +672,8 @@ var bb = object.bodyMesh.geometry.boundingBox; var ss = object.modelScale * 1.0; - var shadowWidth = ss * ( bb.z[ 1 ] - bb.z[ 0 ] ); - var shadowHeight = 1.25 * ss * ( bb.x[ 1 ] - bb.x[ 0 ] ); + var shadowWidth = ss * ( bb.max.z - bb.min.z ); + var shadowHeight = 1.25 * ss * ( bb.max.x - bb.min.x ); var shadowPlane = new THREE.PlaneGeometry( shadowWidth, shadowHeight ); var shadowMaterial = new THREE.MeshBasicMaterial( { color: 0xffffff, opacity: 0.5, transparent: true, diff --git a/examples/webgl_particles_dynamic.html b/examples/webgl_particles_dynamic.html index 9d798a7a..5b895121 100644 --- a/examples/webgl_particles_dynamic.html +++ b/examples/webgl_particles_dynamic.html @@ -280,8 +280,7 @@ mesh: mesh, vertices: geometry.vertices, vertices_tmp: vertices_tmp, vl: vl, down: 0, up: 0, direction: 0, speed: 35, delay: Math.floor( 200 + 200 * Math.random() ), started: false, start: Math.floor( 100 + 200 * Math.random() ), - dynamic: dynamic, - bb: geometry.boundingBox + dynamic: dynamic } ); } @@ -298,7 +297,7 @@ function render () { - delta = 10 * clock.getDelta();; + delta = 10 * clock.getDelta(); delta = delta < 2 ? delta : 2; diff --git a/examples/webgl_particles_shapes.html b/examples/webgl_particles_shapes.html index ab6acfd6..48b6a575 100644 --- a/examples/webgl_particles_shapes.html +++ b/examples/webgl_particles_shapes.html @@ -173,7 +173,7 @@ text3d.computeVertexNormals(); text3d.computeBoundingBox(); - var centerOffset = -0.5 * ( text3d.boundingBox.x[ 1 ] - text3d.boundingBox.x[ 0 ] ); + var centerOffset = -0.5 * ( text3d.boundingBox.max.x - text3d.boundingBox.min.x ); text = new THREE.Mesh( text3d, new THREE.MeshFaceMaterial() ); diff --git a/examples/webgl_shadowmap.html b/examples/webgl_shadowmap.html index 8aade07b..33273e20 100644 --- a/examples/webgl_shadowmap.html +++ b/examples/webgl_shadowmap.html @@ -206,7 +206,7 @@ }); textGeo.computeBoundingBox(); - var centerOffset = -0.5 * ( textGeo.boundingBox.x[ 1 ] - textGeo.boundingBox.x[ 0 ] ); + var centerOffset = -0.5 * ( textGeo.boundingBox.max.x - textGeo.boundingBox.min.x ); var textMaterial = new THREE.MeshPhongMaterial( { color: 0xff0000, specular: 0xffffff, ambient: 0xaa0000 } ); diff --git a/src/core/Geometry.js b/src/core/Geometry.js index 75a6bbd1..393b0012 100644 --- a/src/core/Geometry.js +++ b/src/core/Geometry.js @@ -341,45 +341,55 @@ THREE.Geometry.prototype = { computeBoundingBox: function () { - var vertex; - if ( this.vertices.length > 0 ) { - this.boundingBox = { 'x': [ this.vertices[ 0 ].position.x, this.vertices[ 0 ].position.x ], - 'y': [ this.vertices[ 0 ].position.y, this.vertices[ 0 ].position.y ], - 'z': [ this.vertices[ 0 ].position.z, this.vertices[ 0 ].position.z ] }; + var position, firstPosition = this.vertices[ 0 ].position; + + if ( ! this.boundingBox ) { + + this.boundingBox = { min: firstPosition.clone(), max: firstPosition.clone() }; + + } else { + + this.boundingBox.min.copy( firstPosition ); + this.boundingBox.max.copy( firstPosition ); + + } + + var min = this.boundingBox.min, + max = this.boundingBox.max; for ( var v = 1, vl = this.vertices.length; v < vl; v ++ ) { - vertex = this.vertices[ v ]; + position = this.vertices[ v ].position; - if ( vertex.position.x < this.boundingBox.x[ 0 ] ) { + if ( position.x < min.x ) { - this.boundingBox.x[ 0 ] = vertex.position.x; + min.x = position.x; - } else if ( vertex.position.x > this.boundingBox.x[ 1 ] ) { + } else if ( position.x > max.x ) { - this.boundingBox.x[ 1 ] = vertex.position.x; + max.x = position.x; } - if ( vertex.position.y < this.boundingBox.y[ 0 ] ) { + if ( position.y < min.y ) { - this.boundingBox.y[ 0 ] = vertex.position.y; + min.y = position.y; - } else if ( vertex.position.y > this.boundingBox.y[ 1 ] ) { + } else if ( position.y > max.y ) { - this.boundingBox.y[ 1 ] = vertex.position.y; + max.y = position.y; } - if ( vertex.position.z < this.boundingBox.z[ 0 ] ) { + if ( position.z < min.z ) { - this.boundingBox.z[ 0 ] = vertex.position.z; + min.z = position.z; - } else if ( vertex.position.z > this.boundingBox.z[ 1 ] ) { + } else if ( position.z > max.z ) { - this.boundingBox.z[ 1 ] = vertex.position.z; + max.z = position.z; } diff --git a/src/extras/GeometryUtils.js b/src/extras/GeometryUtils.js index 06a2673e..d0e6e7bd 100644 --- a/src/extras/GeometryUtils.js +++ b/src/extras/GeometryUtils.js @@ -482,11 +482,12 @@ THREE.GeometryUtils = { geometry.computeBoundingBox(); + var bb = geometry.boundingBox; + var offset = new THREE.Vector3(); - offset.x = - 0.5 * ( geometry.boundingBox.x[ 1 ] + geometry.boundingBox.x[ 0 ] ); - offset.y = - 0.5 * ( geometry.boundingBox.y[ 1 ] + geometry.boundingBox.y[ 0 ] ); - offset.z = - 0.5 * ( geometry.boundingBox.z[ 1 ] + geometry.boundingBox.z[ 0 ] ); + offset.add( bb.min, bb.max ); + offset.multiplyScalar( -0.5 ); geometry.applyMatrix( new THREE.Matrix4().setTranslation( offset.x, offset.y, offset.z ) ); geometry.computeBoundingBox();