2014-11-23 2 views
3

개체를 클릭하고 확대/축소 한 다음 마우스 오른쪽 단추를 누른 채 드래그하여 모든 각도에서 볼 수있는이 프로그램을 만들고 있습니다. 나는 카메라가 물체를 돌아 다닐 필요가있다. 물체를 바라 보는 카메라로 물체를 회전시키지 말아야한다. 나는 정직하게 그것을 어떻게 계산하는지 전혀 모른다! 테스트를 위해THREE.PerspectiveCamera를 개체 주위로 회전시키는 방법

그래서 카메라와 피사체 사이의 반경이 500이며, 선택 및 회전하는 동안, 카메라가 항상해야 이미 우리가 선택하고

var g = new GameObject(500, 0, 0);//The game object with xyz 
this.selected = g;//set selected to g 

//Create and set the camera 
this.camera = new THREE.PerspectiveCamera(45, w/h, 1, 10000); 
this.camera.position.x = 0; 
this.camera.position.y = 0; 
this.camera.position.z = 0; 

//set camera to look at the object which is 500 away in the x direction 
this.camera.lookAt(new THREE.Vector3(this.selected.x, this.selected.y, this.selected.z)); 

보고있다 XYZ와 게임 오브젝트가 500 시까 지.

여기 장면을 업데이트 :

Main.prototype.update = function(){ 

    this.renderer.render(this.scene, this.camera);//scene is just some ambient lighting 

    //what to do when mouse right is held down 
    if(this.rightMouseDown){ 

     //placeholder functionality, needs to rotate around object based on mouse movements 
     this.camera.position.x -= 5; 

    } 
} 

이 어떻게 (500)의 반경 g 주위에 카메라를 회전합니까?!?!

+0

왜 트랙볼 컨트롤을 사용하지 않습니까? http://threejs.org/examples/misc_controls_trackball.html – gaitat

답변

2

언급 한대로 트랙볼 컨트롤은 카메라 회전/회전을 쉽게하기 위해 구성 가능한 많은 매개 변수로 시작하는 가장 좋은 장소입니다. 이 방법의 잠재적 인 이점 중 하나는 (특히 프로젝트에 대한) 회전과 함께 작업 할 때 많은 좌절감의 원천 인 "짐벌 잠금"을 피하는 것입니다.

Rotate camera in Three.js with mouse

또 다른 옵션이 설정 될 카메라는 실제로 매우 간단 애니메이션 루프에서 자신을 좌표 : 여기에 트랙볼 컨트롤과 Orbitcontrols와 함께 당신을 도울 수있는 링크입니다

var angle = 0; 
var radius = 500; 

function animate() { 
... 
// Use Math.cos and Math.sin to set camera X and Z values based on angle. 
camera.position.x = radius * Math.cos(angle); 
camera.position.z = radius * Math.sin(angle); 
angle += 0.01; 
... 
} 

또 다른 옵션은 것 카메라를 피벗 개체에 연결하고 피벗을 회전하면됩니다.

var camera_pivot = new THREE.Object3D() 
var Y_AXIS = new THREE.Vector3(0, 1, 0); 

scene.add(camera_pivot); 
camera_pivot.add(camera); 
camera.position.set(500, 0, 0); 
camera.lookAt(camera_pivot.position); 
... 
camera_pivot.rotateOnAxis(Y_AXIS, 0.01); // radians 

이 옵션을 사용하면 카메라 객체가 "카메라 피벗 공간"에 있고 더 조작하기가 더 어려울 수 있습니다.

+0

감사합니다. 정말 도움이되었습니다. !! –

+0

이 "카메라 피벗 공간"개념을 자세히 설명 할 수 있습니까? 나는 윗 궤도와 아랫 줄을 꼬집지 않고 (궤도 제어가하는 것처럼 그리고 코드가하는 것처럼) 카메라를 객체 주위로 회전 시키려고 노력하고있다. 나는 중심을 중심으로 "더 깨끗한"회전이 필요합니다. 마치 물체를 잡고 자유롭게 회전시킬 때 (phi와 theta에서 회전하는 것뿐만 아니라). 도와 주시면 감사하겠습니다. – dylnmc

관련 문제