SQLの窓 イラストAC フリー素材

2012年05月19日

Three.js の個別ソースを使った部分カスタマイズ

Three.js は多くのソースがビルドされて統合されているので、直接変更する事はできませんが、個別のソースコードを変更して、上書きしてカスタマイズする事ができます( Three.js というより、JavaScript 全般ですが )

<script  type="text/javascript" src="/three/Three_49.js"></script>
<script  type="text/javascript" src="/three/FlyControls.js"></script>
以下は、FlyControls クラスをカスタマイズして、キーボードの機能を拡張して対象物のまわりをカメラが周回できるようにしたものです。
/**
 * @author James Baicoianu / http://www.baicoianu.com/
 */

THREE.FlyControls = function ( object, domElement ) {

	this.object = object;

	this.domElement = ( domElement !== undefined ) ? domElement : document;
	if ( domElement ) this.domElement.setAttribute( 'tabindex', -1 );

	// API

	this.movementSpeed = 1.0;
	this.rollSpeed = 0.005;

	this.dragToLook = false;
	this.autoForward = false;

	// disable default target object behavior

	this.object.useQuaternion = true;

	// internals

	this.tmpQuaternion = new THREE.Quaternion();

	this.mouseStatus = 0;

	this.moveState = { up: 0, down: 0, left: 0, right: 0, forward: 0, back: 0, pitchUp: 0, pitchDown: 0, yawLeft: 0, yawRight: 0, rollLeft: 0, rollRight: 0 };
	this.moveVector = new THREE.Vector3( 0, 0, 0 );
	this.rotationVector = new THREE.Vector3( 0, 0, 0 );

	this.handleEvent = function ( event ) {

		if ( typeof this[ event.type ] == 'function' ) {

			this[ event.type ]( event );

		}

	};

	this.keydown = function( event ) {

		if ( event.altKey ) {

			return;

		}

		switch( event.keyCode ) {


			case 16: /* shift */ this.movementSpeedMultiplier = .1; break;

			// 前進・後退
			case 87: /*W*/ this.moveState.forward = 1; break;
			case 83: /*S*/ this.moveState.back = 1; break;

			// 左右移動
			case 65: /*A*/ this.moveState.left = 1; break;
			case 68: /*D*/ this.moveState.right = 1; break;

			// 上下移動
			case 82: /*R*/ this.moveState.up = 1; break;
			case 70: /*F*/ this.moveState.down = 1; break;

			// Z 軸回転
			case 81: /*Q*/ this.moveState.rollLeft = 1; break;
			case 69: /*E*/ this.moveState.rollRight = 1; break;

			// 対象周辺円移動
			case 79: /*O*/ 
				this.moveState.pitchUp = 1; 
				this.moveState.down = 1;
				break;
			case 76: /*L*/ 
				this.moveState.pitchDown = 1;
				this.moveState.up = 1; 
				break;

			case 75: /*K*/
				this.moveState.yawLeft = 1; 
				this.moveState.right = 1
				break;
			case 187: /*;*/
				this.moveState.yawRight = 1; 
				this.moveState.left = 1
				break;

			// 同一視点移動
			case 89: /*Y*/ 
				this.moveState.pitchUp = 1; 
				break;
			case 72: /*H*/ 
				this.moveState.pitchDown = 1;
				break;

			case 71: /*G*/ 
				this.moveState.yawLeft = 1; 
				break;
			case 74: /*J*/ 
				this.moveState.yawRight = 1; 
				break;

		}

		this.updateMovementVector();
		this.updateRotationVector();

	};

	this.keyup = function( event ) {

		switch( event.keyCode ) {

			case 16: /* shift */ this.movementSpeedMultiplier = 1; break;

			// 前進・後退
			case 87: /*W*/ this.moveState.forward = 0; break;
			case 83: /*S*/ this.moveState.back = 0; break;

			// 左右移動
			case 65: /*A*/ this.moveState.left = 0; break;
			case 68: /*D*/ this.moveState.right = 0; break;

			// 上下移動
			case 82: /*R*/ this.moveState.up = 0; break;
			case 70: /*F*/ this.moveState.down = 0; break;

			// Z 軸回転
			case 81: /*Q*/ this.moveState.rollLeft = 0; break;
			case 69: /*E*/ this.moveState.rollRight = 0; break;

			// 対象周辺円移動
			case 79: /*O*/ 
				this.moveState.pitchUp = 0; 
				this.moveState.down = 0;
				break;
			case 76: /*L*/ 
				this.moveState.pitchDown = 0;
				this.moveState.up = 0; 
				break;

			case 75: /*K*/
				this.moveState.yawLeft = 0; 
				this.moveState.right = 0
				break;
			case 187: /*;*/
				this.moveState.yawRight = 0; 
				this.moveState.left = 0
				break;

			// 同一視点移動
			case 89: /*Y*/ 
				this.moveState.pitchUp = 0; 
				break;
			case 72: /*H*/ 
				this.moveState.pitchDown = 0;
				break;

			case 71: /*G*/ 
				this.moveState.yawLeft = 0; 
				break;
			case 74: /*J*/ 
				this.moveState.yawRight = 0; 
				break;

		}

		this.updateMovementVector();
		this.updateRotationVector();

	};

	this.mousedown = function( event ) {

		if ( this.domElement !== document ) {

			this.domElement.focus();

		}

		event.preventDefault();
		event.stopPropagation();

		if ( this.dragToLook ) {

			this.mouseStatus ++;

		} else {

			switch ( event.button ) {

				case 0: this.object.moveForward = true; break;
				case 2: this.object.moveBackward = true; break;

			}

		}

	};

	this.mousemove = function( event ) {

		if ( !this.dragToLook || this.mouseStatus > 0 ) {

			var container = this.getContainerDimensions();
			var halfWidth  = container.size[ 0 ] / 2;
			var halfHeight = container.size[ 1 ] / 2;

			this.moveState.yawLeft   = - ( ( event.pageX - container.offset[ 0 ] ) - halfWidth  ) / halfWidth;
			this.moveState.pitchDown =   ( ( event.pageY - container.offset[ 1 ] ) - halfHeight ) / halfHeight;

			this.updateRotationVector();

		}

	};

	this.mouseup = function( event ) {

		event.preventDefault();
		event.stopPropagation();

		if ( this.dragToLook ) {

			this.mouseStatus --;

			this.moveState.yawLeft = this.moveState.pitchDown = 0;

		} else {

			switch ( event.button ) {

				case 0: this.moveForward = false; break;
				case 2: this.moveBackward = false; break;

			}

		}

		this.updateRotationVector();

	};

	this.update = function( delta ) {

		var moveMult = delta * this.movementSpeed;
		var rotMult = delta * this.rollSpeed;

		this.object.translateX( this.moveVector.x * moveMult );
		this.object.translateY( this.moveVector.y * moveMult );
		this.object.translateZ( this.moveVector.z * moveMult );

		this.tmpQuaternion.set( this.rotationVector.x * rotMult, this.rotationVector.y * rotMult, this.rotationVector.z * rotMult, 1 ).normalize();
		this.object.quaternion.multiplySelf( this.tmpQuaternion );

		this.object.matrix.setPosition( this.object.position );
		this.object.matrix.setRotationFromQuaternion( this.object.quaternion );
		this.object.matrixWorldNeedsUpdate = true;


	};

	this.updateMovementVector = function() {

		var forward = ( this.moveState.forward || ( this.autoForward && !this.moveState.back ) ) ? 1 : 0;

		this.moveVector.x = ( -this.moveState.left    + this.moveState.right );
		this.moveVector.y = ( -this.moveState.down    + this.moveState.up );
		this.moveVector.z = ( -forward + this.moveState.back );

		//console.log( 'move:', [ this.moveVector.x, this.moveVector.y, this.moveVector.z ] );

	};

	this.updateRotationVector = function() {

		this.rotationVector.x = ( -this.moveState.pitchDown + this.moveState.pitchUp );
		this.rotationVector.y = ( -this.moveState.yawRight  + this.moveState.yawLeft );
		this.rotationVector.z = ( -this.moveState.rollRight + this.moveState.rollLeft );

		//console.log( 'rotate:', [ this.rotationVector.x, this.rotationVector.y, this.rotationVector.z ] );

	};

	this.getContainerDimensions = function() {

		if ( this.domElement != document ) {

			return {
				size	: [ this.domElement.offsetWidth, this.domElement.offsetHeight ],
				offset	: [ this.domElement.offsetLeft,  this.domElement.offsetTop ]
			};

		} else {

			return {
				size	: [ window.innerWidth, window.innerHeight ],
				offset	: [ 0, 0 ]
			};

		}

	};

	function bind( scope, fn ) {

		return function () {

			fn.apply( scope, arguments );

		};

	};

	this.domElement.addEventListener( 'mousemove', bind( this, this.mousemove ), false );
	this.domElement.addEventListener( 'mousedown', bind( this, this.mousedown ), false );
	this.domElement.addEventListener( 'mouseup',   bind( this, this.mouseup ), false );

	this.domElement.addEventListener( 'keydown', bind( this, this.keydown ), false );
	this.domElement.addEventListener( 'keyup',   bind( this, this.keyup ), false );

	this.updateMovementVector();
	this.updateRotationVector();

};



以下から実際のテストページへ移動します。

※ データが 12メガあるので、最初のロードはしばらく時間がかかります。

専門的な事は解りませんが、事象として 『 同一視点移動』は、カメラを垂直方向へ移動して、その際視点の移動が発生するので、それを元に戻す為に回転運動を付加しているようでした。そこで、その移動部分を相殺して回転運動だけを残しています。


タグ:Three.js 3D
【Three.jsの最新記事】
posted by at 2012-05-19 01:27 | Three.js | このブログの読者になる | 更新情報をチェックする


Android SDK ポケットリファレンス
改訂版 Webデザイナーのための jQuery入門
今すぐ使えるかんたん ホームページ HTML&CSS入門
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX