简体   繁体   English

如何将TrackballControl应用于移动目标?

[英]How can I apply TrackballControls to a moving target?

I would like to apply the three.js script TrackballControls to a moving object in a way that preserves the ability to zoom and rotate the camera while the object moves through the scene. 我想将三个.js脚本TrackballControls应用于移动对象,以保持在对象移动通过场景时缩放和旋转相机的能力。 For example, I would like to be able to have the camera "follow" a moving planet, while the user retains the ability to zoom and rotate around the planet. 例如,我希望能够让摄像机“跟随”一个移动的行星,同时用户保留在地球周围进行缩放和旋转的能力。 Here is a basic jsfiddle: 这是一个基本的jsfiddle:

http://jsfiddle.net/mareid/8egUM/3/ http://jsfiddle.net/mareid/8egUM/3/

(The mouse control doesn't actually work on jsfiddle for some reason, but it does on my machine). (出于某种原因,鼠标控件实际上并不适用于jsfiddle,但它确实在我的机器上运行)。

I would like to be able to attach the camera to the red sphere so that it moves along with it, but without losing the ability to zoom and rotate . 我希望能够将相机连接到红色球体,以便它随之移动,但不会失去变焦和旋转的能力 I can get the camera to follow the sphere easily enough by adding lines like these to the render() function: 通过在render()函数中添加这样的行,我可以让相机足够容易地跟踪球体:

mouseControls.target = new THREE.Vector3(object.position);
camera.position.set(object.position.x,object.position.y,object.position.z+20);

But if I do that, the fixed camera.position line overrides the ability of TrackballControls to zoom, rotate, etc. Mathematically, I feel like it should be possible to shift the origin of all of the TrackballControls calculations to the centre of the red sphere, but I can't figure out how to do this. 但是如果我这样做,固定的camera.position行会覆盖TrackballControls缩放,旋转等的能力。在数学上,我觉得应该可以将所有TrackballControls计算的原点移动到红色球体的中心。 ,但我无法弄清楚如何做到这一点。 I've tried all sorts of vector additions of the position of the red sphere to the _this.target and _eye vectors in TrackballControls.js, to no avail. 我已经尝试将各种向量的红色球体添加到TrackballControls.js中的_this.target和_eye向量,但无济于事。

Thanks for your help! 谢谢你的帮助!

I'd recommend OrbitControls rather than TrackballControls . 我建议使用OrbitControls而不是TrackballControls My answer here applies primarily to OrbitControls ; 我的答案主要适用于OrbitControls ; the same principle might also work with TrackballControls (I have not looked at its code in a while). 同样的原则也可能适用于TrackballControls (我有一段时间没有看过它的代码)。

If you look at how OrbitControls works you should notice it uses this.object for the camera, and this.target for where the camera is looking at, and it uses that vector (the difference between them) for some of its calculations. 如果你看看OrbitControls是如何工作的,你应该注意到它使用this.object用于摄像机,而this.target用于摄像机所在的位置,并且它使用该向量(它们之间的差异)进行一些计算。 Movement is applied to both positions when panning; 平移时,移动适用于两个位置; only the camera is moved when rotating. 旋转时只移动相机。 this.pan is set to shift the camera, but out of the box it only deals with a panning perpendicular to the this.object to this.target vector because of the way it sets the panning vector. this.pan设置为移动摄像机,但开箱即用它只处理垂直于this.objectthis.target向量的平移,因为它设置平移向量的方式。

Anyway, if you subtract the vector from object.position to controls.target and set controls.pan to that vector, that should make it jump to that object: 无论如何,如果你将object.position的向量减去controls.target并将controls.pan设置为该向量,那么它应该跳转到该对象:

Fixed example code: 修复示例代码:

Add a helper to OrbitControls.js which has access to its local pan variable: OrbitControls.js添加一个帮助器,它可以访问其本地pan变量:

function goTo( vector ) {
    // Cloning given vector since it would be modified otherwise
    pan.add( vector.clone().sub( this.target ) );
}

Your own code: 你自己的代码:

function animate() {

    requestAnimationFrame(animate);
    mouseControls.goTo( object.position ); // Call the new helper
    mouseControls.update();
    render();

}

You'll also probably want to set mouseControls.noPan to true. 您可能还想将mouseControls.noPan设置为true。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM