2026年4月19日日曜日

Three.jsのFlyControlsデモ blenderのフライモードに似たナビゲーション

 このカビ菌みたいなのはorbitと違う動きをするので、オリジナルコードかと思ったらjmsの中に入っていた。fly controlerというらしい。

解説もある。

FlyControlsは、BlenderのようなDCCツールのフライモードに似たナビゲーションを可能にします。3D空間のカメラを制限なく任意に変形させることができます(特定のターゲットにフォーカスするなど)。

似てるか、これ

キー操作で飛び回れるというのがポイントのようで、実際飛べました。便利だなこれ。で、ソースを見ながらカメラの勉強をすることにした。


	this.keydown = function ( event ) {
		if ( event.altKey ) {
			return;
		}
		//event.preventDefault();
		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;
			case 38: /*up*/ this.moveState.pitchUp = 1; break;
			case 40: /*down*/ this.moveState.pitchDown = 1; break;
			case 37: /*left*/ this.moveState.yawLeft = 1; break;
			case 39: /*right*/ this.moveState.yawRight = 1; break;
			case 81: /*Q*/ this.moveState.rollLeft = 1; break;
			case 69: /*E*/ this.moveState.rollRight = 1; break;
		}
		this.updateMovementVector();
		this.updateRotationVector();
	};

さっき自分で動かしてみたキー処理の部分で、動きのフラグをたてて移動と回転の更新をするようだ。この先に欲しいコードがあると思われる。

	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 ] );


controls = new FlyControls( camera, renderer.domElement );

こちらは、呼び出し側の処理で、コンストラクタの第一引数はやはりカメラなので、moveVectorやrotationVectorもカメラに属しているオブジェクトになるはずだ。

と思ったのだがうそで、これは単なるfly中で定義している三次元ベクターだった。

	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 Vector3( 0, 0, 0 );
	this.rotationVector = new Vector3( 0, 0, 0 );

なるほど、なるほど。あぶねー、

	this.update = function( delta ) {
		if ( this.enabled ) {
			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 );
			if (this.object.position.x < this.clamp.xmin) this.object.position.x = this.clamp.xmin;
			if (this.object.position.x > this.clamp.xmax) this.object.position.x = this.clamp.xmax;
			if (this.object.position.z < this.clamp.zmin) this.object.position.z = this.clamp.zmin;
			if (this.object.position.z > this.clamp.zmax) this.object.position.z = this.clamp.zmax;
			this.tmpQuaternion.set( this.rotationVector.x * rotMult, this.rotationVector.y * rotMult, this.rotationVector.z * rotMult, 1 ).normalize();
			this.object.quaternion.multiply( this.tmpQuaternion );
			// expose the rotation vector for convenience
			this.object.rotation.setFromQuaternion( this.object.quaternion, this.object.rotation.order );
		}
	};

実際はupdateという関数を呼び出しているようだった。objectはobject3Dの事だとすると合点がゆく

なんとなく上のコードで動きそうだが、クオータニオンというのが良く分からなかった。

Threeでの解説は淡白で、すぐwikipediaの解説に飛ぶ。

versors とも呼ばれる単位クォータニオンは、3 次元の物体の空間の向きや回転を表現するのに便利な数学的表記法です。オイラー角に比べて構成が簡単で、ジンバルロックの問題を回避することができます。

つーことは、クオータニオンでどっかで計算して、回転などしかけているということになりそうだ。サンプルを見れば使い方が分かるかもしれない。

const quaternion = new THREE.Quaternion();
quaternion.setFromAxisAngle( new THREE.Vector3( 0, 1, 0 ), Math.PI / 2 );
const vector = new THREE.Vector3( 1, 0, 0 );
vector.applyQuaternion( quaternion );

なんか計算が楽になるしくみっぽい。

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

flyの例ではsetとmultiplyを使っているようだ。scope=cameraと考えてよい模様。