camera.lookAt()的逆 [英] Inverse of camera.lookAt()

查看:907
本文介绍了camera.lookAt()的逆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我googled很远,但我没有找到解决方案,我认为实际上是一个很常见的情况。假设我有一个 THREE.PerspectiveCamera 初始化为查看空间中的某一点:

I googled far and wide but I haven't found the solution to what I think to actually be a pretty common situation. Say I have a THREE.PerspectiveCamera initialized to look at a certain point in space:

var camera = new THREE.PerspectiveCamera(45, 2, 0.1, 100);
var target = new THREE.Vector3(1, 2, 3);
camera.lookAt(target);

现在,稍后在代码中我想找到坐标<只需查询相机即可。
我尝试了此问题,根据我自己的情况进行调整:

Now, later on in the code I'd like to be able to find out the coordinates of target by simply querying camera.
I tried what suggested in this question, adapting it to my own scenario:

var vector = new THREE.Vector3();
vector.applyQuaternion(camera.quaternion);
console.log(vector);

但它记录一个坐标向量(0,0,0) / code>而不是正确的坐标(在我的例子中,应该是(1,2,3))。

见解? Cheers。

But it logs a vector of coordinates (0, 0, 0) instead of the correct coordinates (which, in my example, should be (1, 2, 3)).
Any insights? Cheers.

EDIT:

好​​,所以我将尝试在这里进行语境化, MrTrustworthy的解决方案不幸地不适用于我的情况。我试图调整 THREE.OrbitControls 库为我的目的,因为我注意到,当使用它,它覆盖照相机最初看到的位置。这也已在此处报告。基本上,在的第36行OrbitControls.js (我使用的版本,可以找到这里 this.target 初始化为 new THREE.Vector3 ;我发现如果我手动设置它等于相同的向量,我使用作为 camera.lookAt()的参数一切工作很好:我可以开始平移,轨道和缩放从相同的POV场景我会看到的场景从如果我没有应用的控件。当然,我不能硬编码这个信息到 OrbitControls.js ,因为它会要求我改变它每次我想改变我的相机的初始lookAt如果我遵循MrTrustworthy的建议,我必须改变 OrbitControls.js 的第36行,如下所示: this.target = object.targetRef (或 this.target = object.targetRef || new THREE.Vecotr3()),这也太需要 object 有一个 targetRef 属性,而我试图坚持只使用three.js的现有对象属性和方法。希望这有助于更好地了解我的情况。干杯。


Ok so I'm going to try to contextualize here, so as to justify why MrTrustworthy's solution is unfortunately not applicable in my scenario. I'm trying to tweak the THREE.OrbitControls library for my purposes, since I noticed that when using it, it overrides whichever position the camera was looking at originally. This has also been reported here. Basically, on line 36 of OrbitControls.js (I'm using the version which can be found here) this.target is initialized to a new THREE.Vector3(); I found out that if I manually set it to equal the same vector I use as argument of camera.lookAt() everything works just fine: I can start panning, orbiting and zooming the scene from the same POV I would see the scene from if I didn't apply the controls. Of course, I cannot hard-code this information into OrbitControls.js because it would require me to change it every time I want to change the initial "lookAt" of my camera; and if I were to follow MrTrustworthy's suggestion I would have to change line 36 of OrbitControls.js to read like this: this.target = object.targetRef (or this.target = object.targetRef || new THREE.Vecotr3()), which is also too "opinionated" (it would always require object to have a targetRef property, whereas I'm trying to stick to using only three.js's existing object properties and methods). Hope this helps get a better understanding of my case. Cheers.

推荐答案

我想出来并写了我的解决方案这里。由于此问题同时影响 THREE.TrackballControls THREE.OrbitControls ,解决方案涉及对这两个文件应用一个轻微的更改。我不知道它是否可以被认为是一个有效的变化,并使其方式rev。 70;我将在github上发布一个公共关系,只是为了它:)

感谢所有投入的人。

I figured it out and wrote my solution here. Since the issue affects both THREE.TrackballControls and THREE.OrbitControls, the solution involves applying a slight change to both those files. I wonder if it can be considered a valid change and make its way to rev. 70; I will issue a PR on github just for the sake of it :)
Thanks to all those who pitched in.

这篇关于camera.lookAt()的逆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆