gpt4 book ai didi

javascript - 当要旋转的对象的父级有旋转时如何使用 SparkAR 的 LookAt

转载 作者:行者123 更新时间:2023-12-04 13:29:26 24 4
gpt4 key购买 nike

在 SparkAR 中,我想要一个元素(称为 toRotate),它总是看着相机,类似于已经问过的 here (但接受的解决方案对我来说并不令人满意,我没有足够的声誉来回答它的评论)。
问题在于该元素位于无法修改的层次结构中:订单必须绝对root->planeTracker->parent->toRotatethis screenshot所示(似乎我没有足够的声誉来嵌入图像)。
通过使用 lookAt函数(感谢 this github repo 我知道我必须创建一个 toRotateDuplicate 同级到 toRotate 并且具有相同的本地位置,然后改用它的转换)我可以让它在编辑器中正常工作,但是一旦我在我的设备上试一下 toRotate相反,有一个奇怪的旋转..
经过一些测试,我可以看出问题来自于 planeTracker 的事实。旋转等于 Quaternion.Identity 仅在编辑器上,而在设备上该值会有所不同。
如果更换planeTracker也证明了这一点。通过一个空节点并对其应用非身份旋转,即使在编辑器上我们也有“奇怪的旋转”问题。
这是我的代码:

const Scene = require("Scene");
const Reactive = require("Reactive");

const find = e => Scene.root.findFirst(e);

(async function () {
const toRotate = await find('toRotate');
const toRotateDuplicate = await find('toRotateDuplicate');
const camera = await find('Camera');
toRotate.worldTransform.rotation = toRotateDuplicate.worldTransform.lookAt(camera.worldTransform.position).rotation;
})();
因为我使用的是世界变换,所以我不应该有关于旋转的问题,对吗?
那我该怎么办?
请注意,该解决方案已接受 here对我不起作用:如果相机正在查看对象的中心,则该对象仅查看相机,另外,如果我移动相机而不旋转它,我可以看到该对象没有更新其旋转以继续看看相机。

最佳答案

我找到了一个“hacky”解决方案:
为我们的 toRotate 创建另一个 sibling 叫 cameraDuplicate ,这反射(reflect)了 Camera 的世界位置对象.. 由于所有使用的变换都在 lookAt 中逻辑现在处于相同的层次结构级别,我可以使用本地转换而不是世界转换 ^^'
这是更新后的代码:

const Scene = require("Scene");
const Reactive = require("Reactive");

const find = e => Scene.root.findFirst(e);

(async function () {
const toRotate = await find('toRotate');
const toRotateDuplicate = await find('toRotateDuplicate');
const camera = await find('Camera');

const cameraDuplicate = await find('cameraDuplicate');
cameraDuplicate.worldTransform.position = camera.worldTransform.position;
toRotate.transform.rotation = toRotateDuplicate.transform.lookAt(cameraDuplicate.transform.position).rotation;
})();
我不是它的忠实粉丝,因为:它意味着创建第二个 sibling (好像一个还不够)并且没有澄清我的轮换问题,只是避免了它..所以请提供您的解决方案,我会接受它如果没有我的 hacky :)

关于javascript - 当要旋转的对象的父级有旋转时如何使用 SparkAR 的 LookAt,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65992708/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com