[英]Threejs rotate camera around Y
I am little bit confused with this one :S我对这个有点困惑:S
I created a code where my camera does his job "halfly".我创建了一个代码,我的相机可以“一半”地完成他的工作。 I mean I can rotate the camera on a half-circle with my mousemove events but the aim would be the whole circle :)
我的意思是我可以用我的 mousemove 事件在半圆上旋转相机,但目标是整个圆:)
onRotateCamera(event){
if(this.cameraDragged){
let radius:number = Math.round(Math.sqrt(this.camera.position.x * this.camera.position.x + this.camera.position.z * this.camera.position.z)*1000)/1000;
let delta:number = (this.cameraDragged.clientX - event.clientX) / 1000;
let angle:number = Math.round(Math.asin(this.camera.position.z/radius)*1000)/1000;
angle += delta;
this.camera.position.x = Math.round(radius * Math.cos( angle )*1000)/1000;
this.camera.position.z = Math.round(radius * Math.sin( angle )*1000)/1000;
this.camera.lookAt(this.scene.position);
}
}
Camera movement stops when this.camera.position.z/radius is -1 or 1 :S Could someone help me with this please?:)当 this.camera.position.z/radius 为 -1 或 1 时,相机移动停止:S 有人可以帮我吗?:)
If you want to rotate around an axis, why not try using a Quaternion
:如果要绕轴旋转,为什么不尝试使用
Quaternion
:
camera.position.applyQuaternion( new THREE.Quaternion().setFromAxisAngle
new THREE.Vector3( 0, 1, 0 ), // The positive y-axis
RADIAN / 1000 * delta // The amount of rotation to apply this time
));
camera.lookAt( scene.position );
The above will create a rotation
(defined as a quaternion
) around the Y-axis and a full rotation will occur every 1000 delta
(which could be milliseconds, or pixels, either way) of drag.以上将创建围绕 Y 轴的
rotation
(定义为quaternion
),并且每拖动 1000 delta
(可能是毫秒或像素,无论哪种方式)都会发生一次完整旋转。 Heres a sample implementation:这是一个示例实现:
var renderer = new THREE.WebGLRenderer; var scene = new THREE.Scene; var camera = new THREE.PerspectiveCamera; var last = false; var quaternion = new THREE.Quaternion; var axis = new THREE.Vector3( 0, 1, 0 ); renderer.domElement.addEventListener( 'mousedown', event => { last = new THREE.Vector2( event.clientX, event.clientY ); }); renderer.domElement.addEventListener( 'mousemove', event => { if( last ){ let delta = event.clientX - last.x; camera.position.applyQuaternion( quaternion.setFromAxisAngle( axis, Math.PI * 2 * (delta / innerWidth) )); camera.lookAt( scene.position ); last.set( event.clientX, event.clientY ); } renderer.render( scene, camera ); }); renderer.domElement.addEventListener( 'mouseup', event => { last = false; }); scene.add( new THREE.Mesh( new THREE.BoxGeometry, new THREE.MeshBasicMaterial ) ); scene.add( new THREE.AxesHelper ); renderer.setSize( 512, 512 ); camera.position.set( 0, 0, 5 ); renderer.render( scene, camera ); document.body.appendChild( renderer.domElement );
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/92/three.min.js"></script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.