【问题标题】:threejs - raycasting in AR with controller after repositioningthreejs - 重新定位后使用控制器在 AR 中进行光线投射
【发布时间】:2021-05-16 12:53:57
【问题描述】:

我对threejs比较陌生,所以我正在做的可能不是最有效的方法。

我在移动设备上的 AR 中有一个对象,我想知道我在触摸屏幕时是否与它相交。
我使用以下代码生成光线投射,并且它最初可以工作。

    const tempMatrix = new THREE.Matrix4();
    tempMatrix.identity().extractRotation(this.controller.matrixWorld);
    this.raycaster.ray.origin.setFromMatrixPosition(this.controller.matrixWorld);
    this.raycaster.ray.direction.set(0, 0, -1).applyMatrix4(tempMatrix);

但是,我可以通过移动和旋转整个场景来重新定位对象(即重置位置,使对象在前面,相对于当前的相机方向和位置)。

重新定位后,光线投射完全偏移,不会在我触摸屏幕附近的任何地方投射光线。

重新定位是这样完成的(虽然它有效,但如果有更好的方法,请告诉我!):

public handleReposition(): void {
    const xRotation = Math.abs(this.camera.rotation.x) > Math.PI / 2 ? -Math.PI : 0;
    const yRotation = this.camera.rotation.y;

    this.scene.rotation.set(xRotation, yRotation, xRotation);
    this.scene.position.set(this.camera.position.x, this.camera.position.y, this.camera.position.z);
  }

如何实现将光线投射到正确的新位置?

谢谢!

【问题讨论】:

    标签: three.js augmented-reality webxr


    【解决方案1】:

    假设this.scene实际上是主要的threejs Scene,改变它的旋转或位置通常是个坏主意,因为它会影响场景中的所有东西,包括控制器。我建议改为移动您的对象,或者将您的对象添加到 Group 并移动它。

    【讨论】:

      猜你喜欢
      • 2018-07-24
      • 2022-10-15
      • 1970-01-01
      • 1970-01-01
      • 2015-10-17
      • 2016-03-15
      • 2019-03-10
      • 2017-02-21
      • 1970-01-01
      相关资源
      最近更新 更多