【THREE源码解析篇之其他】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 );

		}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值