THREE.Object3D源码解析
applyMatrix4
applyMatrix4: function ( matrix ) {
if ( this.matrixAutoUpdate ) this.updateMatrix();
this.matrix.premultiply( matrix );
this.matrix.decompose( this.position, this.quaternion, this.scale );
}
rotateOnWorldAxis(世界坐标系下旋转)
rotateOnWorldAxis: function ( axis, angle ) {
// rotate object on axis in world space
// axis is assumed to be normalized
// method assumes no rotated parent
_q1.setFromAxisAngle( axis, angle );
this.quaternion.premultiply( _q1 );
return this;
},
rotateOnAxis(本地坐标系下旋转工具函数)、rotate(旋转)
rotateOnAxis: function ( axis, angle ) {
// rotate object on axis in object space
// axis is assumed to be normalized
_q1.setFromAxisAngle( axis, angle );
this.quaternion.multiply( _q1 );
return this;
},
rotateX: function ( angle ) {
return this.rotateOnAxis( _xAxis, angle );
},
rotateY: function ( angle ) {
return this.rotateOnAxis( _yAxis, angle );
},
rotateZ: function ( angle ) {
return this.rotateOnAxis( _zAxis, angle );
},
translateOnAxis(平移的工具函数)、 translate
translateOnAxis: function ( axis, distance ) {
// translate object by distance along axis in object space
// axis is assumed to be normalized
_v1.copy( axis ).applyQuaternion( this.quaternion );
this.position.add( _v1.multiplyScalar( distance ) );
return this;
}
translateX: function ( distance ) {
return this.translateOnAxis( _xAxis, distance );
},
translateY: function ( distance ) {
return this.translateOnAxis( _yAxis, distance );
},
translateZ: function ( distance ) {
return this.translateOnAxis( _zAxis, distance );
},
localToWorld(本地坐标转世界坐标)
localToWorld: function ( vector ) {
return vector.applyMatrix4( this.matrixWorld );
},
worldToLocal(世界转本地)
worldToLocal: function ( vector ) {
return vector.applyMatrix4( _m1.getInverse( this.matrixWorld ) );
},
traverse(遍历子)
traverse: function ( callback ) {
callback( this );
var children = this.children;
for ( var i = 0, l = children.length; i < l; i ++ ) {
children[ i ].traverse( callback );
}
}
updateMatrix
updateMatrix: function () {
this.matrix.compose( this.position, this.quaternion, this.scale );
this.matrixWorldNeedsUpdate = true;
}
updateMatrixWorld(重要)(scene来使用遍历更新自身及所有子对象的矩阵)
用于WebGLRenderer.js中全局遍历scene中的对象更新矩阵信息
// WebGLRenderer.js源码
this.render=function(){
...
// WebGL渲染器中执行场景对象的updateMatrixWorld()方法,更新场景和场景所有子孙对象的本地矩阵
if (scene.autoUpdate === true) scene.updateMatrixWorld();
...
}
updateMatrixWorld: function ( force ) {
if ( this.matrixAutoUpdate ) this.updateMatrix();// 更新对象的本地矩阵属性
if ( this.matrixWorldNeedsUpdate || force ) {
if ( this.parent === null ) {
// 如果一个对象没有父对象,也就是树结构对象的根节点对象,世界矩阵就等于本地矩阵
this.matrixWorld.copy( this.matrix );
} else {
// 更新对象的世界矩阵,父对象的世界矩阵和对象本地矩阵的乘积
this.matrixWorld.multiplyMatrices( this.parent.matrixWorld, this.matrix );
}
this.matrixWorldNeedsUpdate = false;
force = true;
}
// update children
// 通过递归算法遍历一个对象的所有子对象,执行与根对象一样的操作更新本地和世界矩阵属性
var children = this.children;
for ( var i = 0, l = children.length; i < l; i ++ ) {
children[ i ].updateMatrixWorld( force );
}
}