提交 1e5fc985 编写于 作者: M Mr.doob

Reworking Scene Graph setup.

上级 9a6a20b5
......@@ -53,11 +53,12 @@
info.innerHTML = 'Drag to spin the cube';
container.appendChild( info );
scene = new THREE.Scene();
camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 1000 );
camera.position.y = 150;
camera.position.z = 500;
scene = new THREE.Scene();
scene.add( camera );
// Cube
......
......@@ -44,10 +44,12 @@
container = document.createElement( 'div' );
document.body.appendChild( container );
scene = new THREE.Scene();
camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 1, 10000 );
camera.position.z = 500;
scene = new THREE.Scene();
camera.target = new THREE.Vector3();
scene.add( camera );
var geometry = new THREE.CubeGeometry( 100, 100, 100 );
var material = new THREE.MeshNormalMaterial();
......@@ -65,7 +67,6 @@
mesh.rotation.y = Math.random() * 360 * ( Math.PI / 180 );
mesh.matrixAutoUpdate = false;
mesh.updateMatrix();
group.add( mesh );
}
......
......@@ -133,7 +133,7 @@
// find intersections
camera.update();
camera.updateMatrixWorld();
var vector = new THREE.Vector3( mouse.x, mouse.y, 0.5 );
projector.unprojectVector( vector, camera );
......
......@@ -26,7 +26,7 @@
var camera, scene, renderer;
var cube, plane, target = new THREE.Vector3();
var cube, plane, objects = [];
var targetRotation = 0;
var targetRotationOnMouseDown = 0;
......@@ -37,19 +37,15 @@
var windowHalfX = window.innerWidth / 2;
var windowHalfY = window.innerHeight / 2;
var moveForward = false,
moveBackwards = false,
moveUp = false,
moveDown = false,
moveLeft = false,
moveRight = false,
var moveForward = false;
var moveBackwards = false;
var moveLeft = false;
var moveRight = false;
var moveUp = false;
var moveDown = false;
yawLeft = false,
yawRight = false,
pitchUp = false,
pitchDown = false,
rollLeft = false,
rollRight = false;
var targetMoveLeft = false;
var targetMoveRight = false;
var debugContext;
......@@ -65,6 +61,7 @@
camera.position.x = 1000;
camera.position.y = 1000;
camera.position.z = 1000;
camera.target = new THREE.Vector3( 0, 150, 0 );
scene = new THREE.Scene();
......@@ -86,7 +83,7 @@
geometry = new THREE.CubeGeometry( 100, 100, 100 );
material = new THREE.MeshDepthMaterial( { near: 1, far: 2000 } );
for (var i = 0; i < 20; i ++ ) {
for ( var i = 0; i < 20; i ++ ) {
cube = new THREE.Mesh( geometry, material );
cube.overdraw = true;
......@@ -98,7 +95,9 @@
cube.rotation.y = Math.random() * 200 - 100;
cube.rotation.z = Math.random() * 200 - 100;
scene.add(cube);
scene.add( cube );
objects.push( cube );
}
......@@ -146,18 +145,16 @@
function onDocumentKeyDown( event ) {
switch( event.keyCode ) {
switch ( event.keyCode ) {
case 38: moveForward = true; break; // up
case 40: moveBackwards = true; break; // down
case 37: moveLeft = true; break; // left
case 39: moveRight = true; break; // right
case 65: yawLeft = true; break; // a
case 68: yawRight = true; break; // d
case 87: moveUp/*pitchUp*/ = true; break; // w
case 83: moveDown/*pitchDown*/ = true; break; // s
case 90: rollLeft = true; break; // z
case 67: rollRight = true; break; // c
case 87: moveUp = true; break; // w
case 83: moveDown = true; break; // s
case 65: targetMoveLeft = true; break; // a
case 68: targetMoveRight = true; break; // d
}
......@@ -165,18 +162,16 @@
function onDocumentKeyUp( event ) {
switch( event.keyCode ) {
switch ( event.keyCode ) {
case 38: moveForward = false; break; // up
case 40: moveBackwards = false; break; // down
case 37: moveLeft = false; break; // left
case 39: moveRight = false; break; // right
case 65: yawLeft = false; break; // a
case 68: yawRight = false; break; // d
case 87: moveUp/*pitchUp*/ = false; break; // w
case 83: moveDown/*pitchDown*/ = false; break; // s
case 90: rollLeft = false; break; // z
case 67: rollRight = false; break; // c
case 87: moveUp = false; break; // w
case 83: moveDown = false; break; // s
case 65: targetMoveLeft = false; break; // a
case 68: targetMoveRight = false; break; // d
}
......@@ -195,25 +190,19 @@
function render() {
if ( moveForward ) camera.position.z -= 10; // camera.moveZ( 10 );
if ( moveBackwards ) camera.position.z += 10; // camera.moveZ( - 10 );
if ( moveUp ) camera.position.y += 10; // camera.moveZ( 10 );
if ( moveDown ) camera.position.y -= 10; // camera.moveZ( - 10 );
if ( moveLeft ) camera.position.x -= 10; // camera.moveX( - 10 );
if ( moveRight ) camera.position.x += 10; // camera.moveX( 10 );
if ( moveForward ) camera.position.z -= 10;
if ( moveBackwards ) camera.position.z += 10;
if ( pitchUp ) camera.rotation.x += 0.01; // camera.rotateX( 1 );
if ( pitchDown ) camera.rotation.x -= 0.01; // camera.rotateX( - 1 );
if ( moveLeft ) camera.position.x -= 10;
if ( moveRight ) camera.position.x += 10;
if ( yawLeft ) target.x -= 10; // camera.rotation.y += 0.01; // camera.rotateY( 1 );
if ( yawRight ) target.x += 10; // camera.rotation.y -= 0.01; // camera.rotateY( - 1 );
if ( moveUp ) camera.position.y += 10;
if ( moveDown ) camera.position.y -= 10;
if ( rollLeft ) camera.rotation.z += 0.01; // camera.rotateZ( 1 );
if ( rollRight ) camera.rotation.z -= 0.01; // camera.rotateZ( - 1 );
if ( targetMoveLeft ) camera.target.x -= 10;
if ( targetMoveRight ) camera.target.x += 10;
camera.lookAt( target );
camera.lookAt( camera.target );
debugContext.clearRect( - 256, - 256, 512, 512 );
......@@ -228,14 +217,14 @@
// camera
debugContext.moveTo( camera.position.x * 0.1, camera.position.z * 0.1 );
debugContext.lineTo( target.x * 0.1, target.z * 0.1 );
debugContext.lineTo( camera.target.x * 0.1, camera.target.z * 0.1 );
debugContext.rect( camera.position.x * 0.1 - 5, camera.position.z * 0.1 - 5, 10, 10 );
debugContext.rect( target.x * 0.1 - 5, target.z * 0.1 - 5, 10, 10 );
debugContext.rect( camera.target.x * 0.1 - 5, camera.target.z * 0.1 - 5, 10, 10 );
debugContext.rect( - 50, - 50, 100, 100 );
for ( var i = 1; i < scene.objects.length; i++ ) {
for ( var i = 0; i < objects.length; i++ ) {
var object = scene.objects[i];
var object = objects[ i ];
object.rotation.x += 0.01;
object.rotation.y += 0.005;
......
......@@ -38,15 +38,15 @@
var windowHalfX = window.innerWidth / 2;
var windowHalfY = window.innerHeight / 2;
var moveForward = false, moveBackwards = false,
moveUp = false, moveDown = false,
moveLeft = false, moveRight = false,
var moveForward = false;
var moveBackwards = false;
var moveLeft = false;
var moveRight = false;
var moveUp = false;
var moveDown = false;
yawLeft = false, yawRight = false,
pitchUp = false, pitchDown = false,
rollLeft = false, rollRight = false;
var target = new THREE.Vector3( 0, 150, 0 );
var targetMoveLeft = false;
var targetMoveRight = false;
var debugContext;
......@@ -61,6 +61,7 @@
camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 10000 );
camera.position.y = 150;
camera.position.z = 400;
camera.target = new THREE.Vector3( 0, 150, 0 );
scene = new THREE.Scene();
......@@ -157,18 +158,16 @@
function onDocumentKeyDown( event ) {
switch( event.keyCode ) {
switch ( event.keyCode ) {
case 38: moveForward = true; break; // up
case 40: moveBackwards = true; break; // down
case 37: moveLeft = true; break; // left
case 39: moveRight = true; break; // right
case 65: yawLeft = true; break; // a
case 68: yawRight = true; break; // d
case 87: moveUp/*pitchUp*/ = true; break; // w
case 83: moveDown/*pitchDown*/ = true; break; // s
case 90: rollLeft = true; break; // z
case 67: rollRight = true; break; // c
case 87: moveUp = true; break; // w
case 83: moveDown = true; break; // s
case 65: targetMoveLeft = true; break; // a
case 68: targetMoveRight = true; break; // d
}
......@@ -176,18 +175,16 @@
function onDocumentKeyUp( event ) {
switch( event.keyCode ) {
switch ( event.keyCode ) {
case 38: moveForward = false; break; // up
case 40: moveBackwards = false; break; // down
case 37: moveLeft = false; break; // left
case 39: moveRight = false; break; // right
case 65: yawLeft = false; break; // a
case 68: yawRight = false; break; // d
case 87: moveUp/*pitchUp*/ = false; break; // w
case 83: moveDown/*pitchDown*/ = false; break; // s
case 90: rollLeft = false; break; // z
case 67: rollRight = false; break; // c
case 87: moveUp = false; break; // w
case 83: moveDown = false; break; // s
case 65: targetMoveLeft = false; break; // a
case 68: targetMoveRight = false; break; // d
}
......@@ -206,25 +203,19 @@
function render() {
if ( moveForward ) camera.position.z -= 5;
if ( moveBackwards ) camera.position.z += 5;
if ( moveUp ) camera.position.y += 5;
if ( moveDown ) camera.position.y -= 5;
if ( moveLeft ) camera.position.x -= 5;
if ( moveRight ) camera.position.x += 5;
if ( moveForward ) camera.position.z -= 10;
if ( moveBackwards ) camera.position.z += 10;
if ( pitchUp ) camera.rotation.x += 0.01;
if ( pitchDown ) camera.rotation.x -= 0.01;
if ( moveLeft ) camera.position.x -= 10;
if ( moveRight ) camera.position.x += 10;
if ( yawLeft ) target.x -= 5;
if ( yawRight ) target.x += 5;
if ( moveUp ) camera.position.y += 10;
if ( moveDown ) camera.position.y -= 10;
if ( rollLeft ) camera.rotation.z += 0.01;
if ( rollRight ) camera.rotation.z -= 0.01;
if ( targetMoveLeft ) camera.target.x -= 10;
if ( targetMoveRight ) camera.target.x += 10;
camera.lookAt( target );
camera.lookAt( camera.target );
debugContext.clearRect( -256, -256, 512, 512 );
......@@ -239,9 +230,9 @@
// camera
debugContext.moveTo( camera.position.x * 0.1, camera.position.z * 0.1 );
debugContext.lineTo( target.x * 0.1, target.z * 0.1 );
debugContext.lineTo( camera.target.x * 0.1, camera.target.z * 0.1 );
debugContext.rect( camera.position.x * 0.1 - 5, camera.position.z * 0.1 - 5, 10, 10 );
debugContext.rect( target.x * 0.1 - 5, target.z * 0.1 - 5, 10, 10 );
debugContext.rect( camera.target.x * 0.1 - 5, camera.target.z * 0.1 - 5, 10, 10 );
debugContext.rect( - 50, - 50, 100, 100 );
for ( var i = 0, l = objects.length; i < l; i++ ) {
......
......@@ -102,60 +102,6 @@ THREE.Matrix4.prototype = {
},
multiplyVector3: function ( v ) {
var vx = v.x, vy = v.y, vz = v.z,
d = 1 / ( this.n41 * vx + this.n42 * vy + this.n43 * vz + this.n44 );
v.x = ( this.n11 * vx + this.n12 * vy + this.n13 * vz + this.n14 ) * d;
v.y = ( this.n21 * vx + this.n22 * vy + this.n23 * vz + this.n24 ) * d;
v.z = ( this.n31 * vx + this.n32 * vy + this.n33 * vz + this.n34 ) * d;
return v;
},
multiplyVector4: function ( v ) {
var vx = v.x, vy = v.y, vz = v.z, vw = v.w;
v.x = this.n11 * vx + this.n12 * vy + this.n13 * vz + this.n14 * vw;
v.y = this.n21 * vx + this.n22 * vy + this.n23 * vz + this.n24 * vw;
v.z = this.n31 * vx + this.n32 * vy + this.n33 * vz + this.n34 * vw;
v.w = this.n41 * vx + this.n42 * vy + this.n43 * vz + this.n44 * vw;
return v;
},
rotateAxis: function ( v ) {
var vx = v.x, vy = v.y, vz = v.z;
v.x = vx * this.n11 + vy * this.n12 + vz * this.n13;
v.y = vx * this.n21 + vy * this.n22 + vz * this.n23;
v.z = vx * this.n31 + vy * this.n32 + vz * this.n33;
v.normalize();
return v;
},
crossVector: function ( a ) {
var v = new THREE.Vector4();
v.x = this.n11 * a.x + this.n12 * a.y + this.n13 * a.z + this.n14 * a.w;
v.y = this.n21 * a.x + this.n22 * a.y + this.n23 * a.z + this.n24 * a.w;
v.z = this.n31 * a.x + this.n32 * a.y + this.n33 * a.z + this.n34 * a.w;
v.w = ( a.w ) ? this.n41 * a.x + this.n42 * a.y + this.n43 * a.z + this.n44 * a.w : 1;
return v;
},
multiply: function ( a, b ) {
var a11 = a.n11, a12 = a.n12, a13 = a.n13, a14 = a.n14,
......@@ -192,6 +138,12 @@ THREE.Matrix4.prototype = {
},
multiplySelf: function ( m ) {
return this.multiply( this, m );
},
multiplyToArray: function ( a, b, r ) {
this.multiply( a, b );
......@@ -205,14 +157,6 @@ THREE.Matrix4.prototype = {
},
multiplySelf: function ( m ) {
this.multiply( this, m );
return this;
},
multiplyScalar: function ( s ) {
this.n11 *= s; this.n12 *= s; this.n13 *= s; this.n14 *= s;
......@@ -224,6 +168,60 @@ THREE.Matrix4.prototype = {
},
multiplyVector3: function ( v ) {
var vx = v.x, vy = v.y, vz = v.z,
d = 1 / ( this.n41 * vx + this.n42 * vy + this.n43 * vz + this.n44 );
v.x = ( this.n11 * vx + this.n12 * vy + this.n13 * vz + this.n14 ) * d;
v.y = ( this.n21 * vx + this.n22 * vy + this.n23 * vz + this.n24 ) * d;
v.z = ( this.n31 * vx + this.n32 * vy + this.n33 * vz + this.n34 ) * d;
return v;
},
multiplyVector4: function ( v ) {
var vx = v.x, vy = v.y, vz = v.z, vw = v.w;
v.x = this.n11 * vx + this.n12 * vy + this.n13 * vz + this.n14 * vw;
v.y = this.n21 * vx + this.n22 * vy + this.n23 * vz + this.n24 * vw;
v.z = this.n31 * vx + this.n32 * vy + this.n33 * vz + this.n34 * vw;
v.w = this.n41 * vx + this.n42 * vy + this.n43 * vz + this.n44 * vw;
return v;
},
rotateAxis: function ( v ) {
var vx = v.x, vy = v.y, vz = v.z;
v.x = vx * this.n11 + vy * this.n12 + vz * this.n13;
v.y = vx * this.n21 + vy * this.n22 + vz * this.n23;
v.z = vx * this.n31 + vy * this.n32 + vz * this.n33;
v.normalize();
return v;
},
crossVector: function ( a ) {
var v = new THREE.Vector4();
v.x = this.n11 * a.x + this.n12 * a.y + this.n13 * a.z + this.n14 * a.w;
v.y = this.n21 * a.x + this.n22 * a.y + this.n23 * a.z + this.n24 * a.w;
v.z = this.n31 * a.x + this.n32 * a.y + this.n33 * a.z + this.n34 * a.w;
v.w = ( a.w ) ? this.n41 * a.x + this.n42 * a.y + this.n43 * a.z + this.n44 * a.w : 1;
return v;
},
determinant: function () {
var n11 = this.n11, n12 = this.n12, n13 = this.n13, n14 = this.n14,
......@@ -742,23 +740,31 @@ THREE.Matrix4.prototype = {
this.n24 = m.n24;
this.n34 = m.n34;
return this;
},
extractRotation: function ( m, s ) {
extractRotation: function ( m ) {
var invScaleX = 1 / s.x, invScaleY = 1 / s.y, invScaleZ = 1 / s.z;
var vector = THREE.Matrix4.__v1;
this.n11 = m.n11 * invScaleX;
this.n21 = m.n21 * invScaleX;
this.n31 = m.n31 * invScaleX;
var scaleX = vector.set( m.n11, m.n21, m.n31 ).length();
var scaleY = vector.set( m.n12, m.n22, m.n32 ).length();
var scaleZ = vector.set( m.n13, m.n23, m.n33 ).length();
this.n12 = m.n12 * invScaleY;
this.n22 = m.n22 * invScaleY;
this.n32 = m.n32 * invScaleY;
this.n11 = m.n11 / scaleX;
this.n21 = m.n21 / scaleX;
this.n31 = m.n31 / scaleX;
this.n13 = m.n13 * invScaleZ;
this.n23 = m.n23 * invScaleZ;
this.n33 = m.n33 * invScaleZ;
this.n12 = m.n12 / scaleY;
this.n22 = m.n22 / scaleY;
this.n32 = m.n32 / scaleY;
this.n13 = m.n13 / scaleZ;
this.n23 = m.n23 / scaleZ;
this.n33 = m.n33 / scaleZ;
return this;
}
......
......@@ -4,7 +4,7 @@
* @author alteredq / http://alteredqualia.com/
*/
THREE.Object3D = function() {
THREE.Object3D = function () {
this.name = '';
......@@ -101,7 +101,7 @@ THREE.Object3D.prototype = {
if ( this.children.indexOf( object ) === - 1 ) {
if( object.parent !== undefined ) {
if ( object.parent !== undefined ) {
object.parent.remove( object );
......@@ -110,50 +110,18 @@ THREE.Object3D.prototype = {
object.parent = this;
this.children.push( object );
// add to scene
var scene = this;
while ( scene.parent !== undefined ) {
scene = scene.parent;
}
if ( scene !== undefined && scene instanceof THREE.Scene ) {
scene.addChildRecurse( object );
}
}
},
remove: function ( object ) {
var scene = this;
var childIndex = this.children.indexOf( object );
var index = this.children.indexOf( object );
if ( childIndex !== - 1 ) {
if ( index !== - 1 ) {
object.parent = undefined;
this.children.splice( childIndex, 1 );
// remove from scene
while ( scene.parent !== undefined ) {
scene = scene.parent;
}
if ( scene !== undefined && scene instanceof THREE.Scene ) {
scene.removeChildRecurse( object );
}
this.children.splice( index, 1 );
}
......@@ -216,17 +184,17 @@ THREE.Object3D.prototype = {
},
update: function ( parentMatrixWorld, forceUpdate, camera ) {
updateMatrixWorld: function ( force ) {
this.matrixAutoUpdate && this.updateMatrix();
// update matrixWorld
if ( this.matrixWorldNeedsUpdate || forceUpdate ) {
if ( this.matrixWorldNeedsUpdate || force ) {
if ( parentMatrixWorld ) {
if ( this.parent ) {
this.matrixWorld.multiply( parentMatrixWorld, this.matrix );
this.matrixWorld.multiply( this.parent.matrixWorld, this.matrix );
} else {
......@@ -234,11 +202,9 @@ THREE.Object3D.prototype = {
}
this.matrixRotationWorld.extractRotation( this.matrixWorld, this.scale );
this.matrixWorldNeedsUpdate = false;
forceUpdate = true;
force = true;
}
......@@ -246,7 +212,7 @@ THREE.Object3D.prototype = {
for ( var i = 0, l = this.children.length; i < l; i ++ ) {
this.children[ i ].update( this.matrixWorld, forceUpdate, camera );
this.children[ i ].updateMatrixWorld( force );
}
......
......@@ -79,32 +79,39 @@ THREE.Projector = function() {
};
this.projectObjects = function ( scene, camera, sort ) {
var o, ol, objects, object, matrix;
this.projectObjects = function ( object, sort ) {
_objectList.length = 0;
_objectCount = 0;
objects = scene.objects;
var projectObject = function ( object ) {
if ( object.visible == false ) return;
for ( o = 0, ol = objects.length; o < ol; o ++ ) {
if ( object instanceof THREE.Particle || object instanceof THREE.Line ||
( object instanceof THREE.Mesh && ( !object.frustumCulled || isInFrustum( object ) ) ) ) {
object = objects[ o ];
_object = getNextObjectInPool();
if ( !object.visible || ( object instanceof THREE.Mesh && ( object.frustumCulled && !isInFrustum( object ) ) ) ) continue;
_vector3.copy( object.position );
_projScreenMatrix.multiplyVector3( _vector3 );
_object = getNextObjectInPool();
_object.object = object;
_object.z = _vector3.z;
_vector3.copy( object.position );
_projScreenMatrix.multiplyVector3( _vector3 );
_objectList.push( _object );
_object.object = object;
_object.z = _vector3.z;
}
_objectList.push( _object );
for ( var c = 0, cl = object.children.length; c < cl; c ++ ) {
}
projectObject( object.children[ c ] );
}
};
projectObject( scene );
sort && _objectList.sort( painterSort );
......@@ -112,8 +119,6 @@ THREE.Projector = function() {
};
// TODO: Rename to projectElements?
this.projectScene = function ( scene, camera, sort ) {
var near = camera.near, far = camera.far,
......@@ -130,9 +135,14 @@ THREE.Projector = function() {
_lineCount = 0;
_particleCount = 0;
camera.matrixAutoUpdate && camera.update( undefined, true );
if ( camera.parent == null ) {
console.warn( "Camera is not on the Scene. Adding it..." );
scene.add( camera );
scene.update( undefined, false, camera );
}
scene.updateMatrixWorld();
THREE.Matrix4.makeInvert( camera.matrixWorld, camera.matrixWorldInverse );
......@@ -140,7 +150,7 @@ THREE.Projector = function() {
computeFrustum( _projScreenMatrix );
objects = this.projectObjects( scene, camera, true );
objects = this.projectObjects( scene, true );
for ( o = 0, ol = objects.length; o < ol; o++ ) {
......@@ -149,7 +159,6 @@ THREE.Projector = function() {
if ( !object.visible ) continue;
objectMatrix = object.matrixWorld;
objectMatrixRotation = object.matrixRotationWorld;
objectMaterials = object.materials;
objectOverdraw = object.overdraw;
......@@ -163,6 +172,8 @@ THREE.Projector = function() {
faces = geometry.faces;
faceVertexUvs = geometry.faceVertexUvs;
objectMatrixRotation = object.matrixRotationWorld.extractRotation( object.matrixWorld );
for ( v = 0, vl = vertices.length; v < vl; v ++ ) {
_vertex = getNextVertexInPool();
......
......@@ -15,7 +15,7 @@ THREE.Ray.prototype = {
intersectScene: function ( scene ) {
return this.intersectObjects( scene.objects );
return this.intersectObjects( scene.children );
},
......@@ -90,6 +90,8 @@ THREE.Ray.prototype = {
objMatrix,
intersectPoint;
object.matrixRotationWorld.extractRotation( object.matrixWorld );
for ( f = 0, fl = geometry.faces.length; f < fl; f ++ ) {
face = geometry.faces[ f ];
......
......@@ -188,7 +188,7 @@ THREE.CanvasRenderer = function ( parameters ) {
_context.fillRect( _clipRect.getX(), _clipRect.getY(), _clipRect.getWidth(), _clipRect.getHeight() );
*/
_enableLighting = scene.lights.length > 0;
// _enableLighting = scene.lights.length > 0;
if ( _enableLighting ) {
......
/**
* @author mr.doob / http://mrdoob.com/
* @author mikael emtinger / http://gomo.se/
*/
THREE.Scene = function () {
......@@ -8,119 +7,14 @@ THREE.Scene = function () {
THREE.Object3D.call( this );
this.fog = null;
this.matrixAutoUpdate = false;
this.overrideMaterial = null;
this.collisions = null;
this.objects = [];
this.lights = [];
this.__objectsAdded = [];
this.__objectsRemoved = [];
this.matrixAutoUpdate = false;
};
THREE.Scene.prototype = new THREE.Object3D();
THREE.Scene.prototype.constructor = THREE.Scene;
THREE.Scene.prototype.supr = THREE.Object3D.prototype;
THREE.Scene.prototype.add = function ( object ) {
this.supr.add.call( this, object );
this.addChildRecurse( object );
}
THREE.Scene.prototype.addChildRecurse = function ( child ) {
if ( child instanceof THREE.Light ) {
if ( this.lights.indexOf( child ) === - 1 ) {
this.lights.push( child );
}
} else if ( !( child instanceof THREE.Camera || child instanceof THREE.Bone ) ) {
if ( this.objects.indexOf( child ) === - 1 ) {
this.objects.push( child );
this.__objectsAdded.push( child );
// check if previously removed
var i = this.__objectsRemoved.indexOf( child );
if ( i !== -1 ) {
this.__objectsRemoved.splice( i, 1 );
}
}
}
for ( var c = 0; c < child.children.length; c ++ ) {
this.addChildRecurse( child.children[ c ] );
}
}
THREE.Scene.prototype.remove = function ( object ) {
this.supr.remove.call( this, object );
this.removeChildRecurse( object );
}
THREE.Scene.prototype.removeChildRecurse = function ( child ) {
if ( child instanceof THREE.Light ) {
var i = this.lights.indexOf( child );
if ( i !== -1 ) {
this.lights.splice( i, 1 );
}
} else if ( !( child instanceof THREE.Camera ) ) {
var i = this.objects.indexOf( child );
if( i !== -1 ) {
this.objects.splice( i, 1 );
this.__objectsRemoved.push( child );
// check if previously added
var ai = this.__objectsAdded.indexOf( child );
if ( ai !== -1 ) {
this.__objectsAdded.splice( ai, 1 );
}
}
}
for ( var c = 0; c < child.children.length; c ++ ) {
this.removeChildRecurse( child.children[ c ] );
}
}
// DEPRECATED
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册