Three.js 中的 Raycast 只有一个投影矩阵

dan*_*nvk 14 three.js raycasting mapbox-gl-js

我正在按照此示例在 Mapbox GL JS 页面中使用 Three.js 渲染一些自定义图层。我想添加光线投射来确定用户点击了哪个对象。

问题是我只从 Mapbox 获得了一个投影矩阵,我用它来渲染场​​景:

class CustomLayer {
  type = 'custom';
  renderingMode = '3d';

  onAdd(map, gl) {
    this.map = map;
    this.camera = new THREE.Camera();
    this.renderer = new THREE.WebGLRenderer({
      canvas: map.getCanvas(),
      context: gl,
      antialias: true,
    });
    this.scene = new THREE.Scene();
    // ...
  }

  render(gl, matrix) {
    this.camera.projectionMatrix = new THREE.Matrix4()
      .fromArray(matrix)
      .multiply(this.cameraTransform);
    this.renderer.state.reset();
    this.renderer.render(this.scene, this.camera);
  }
}
Run Code Online (Sandbox Code Playgroud)

这渲染得很好,并在我平移/旋转/缩放地图时跟踪视图中的变化。

自由岛上的立方体

不幸的是,当我尝试添加光线投射时,出现错误:

  raycast(point) {
    var mouse = new THREE.Vector2();
    mouse.x = ( point.x / this.map.transform.width ) * 2 - 1;
    mouse.y = 1 - ( point.y / this.map.transform.height ) * 2;
    const raycaster = new THREE.Raycaster();
    raycaster.setFromCamera(mouse, this.camera);
    console.log(raycaster.intersectObjects(this.scene.children, true));
  }
Run Code Online (Sandbox Code Playgroud)

这给了我一个例外:

THREE.Raycaster: Unsupported camera type.
Run Code Online (Sandbox Code Playgroud)

我可以THREE.CameraTHREE.PerspectiveCamera不影响场景渲染的情况下从通用更改为通用:

THREE.Raycaster: Unsupported camera type.
Run Code Online (Sandbox Code Playgroud)

这修复了异常,但也不会导致记录任何对象。稍微挖掘一下就会发现相机projectionMatrixInverse都是NaNs,我们可以通过计算它来修复它:

this.camera = new THREE.PerspectiveCamera(28, window.innerWidth / window.innerHeight, 0.1, 1e6);
Run Code Online (Sandbox Code Playgroud)

现在无论我点击哪里,我都会得到两个交叉点,立方体的两个面。它们的距离为 0:

[
  { distance: 0, faceIndex: 10, point: Vector3 { x: 0, y: 0, z: 0 }, uv: Vector2 {x: 0.5, y: 0.5}, ... },
  { distance: 0, faceIndex: 11, point: Vector3 { x: 0, y: 0, z: 0 }, uv: Vector2 {x: 0.5, y: 0.5}, ... },
]
Run Code Online (Sandbox Code Playgroud)

所以很明显有些东西在这里不起作用。查看 的代码setCamera,它同时涉及projectionMatrixmatrixWorld。有没有一种方法可以设置matrixWorld,或仅使用投影矩阵直接构建光线投射器的光线?似乎我只需要投影矩阵来渲染场景,所以我希望它也是我投射光线所需的全部。

此 codepen 中的完整示例。

Iso*_*lin 7

调试困难,但解决了!

TL;DR:此工作Fiddle 中的完整代码。

  1. 我认为没有 MapBox 相机的世界矩阵不是主要问题,而是坐标空间的不兼容。Mapbox 提供了一个z指向上的左手系统。三使用右手系统,y指向上方。

  2. 在调试过程中,我创建了 raycaster 设置函数的简化副本,以控制一切,并且得到了回报。

  3. 立方体不是调试的最佳对象,它太对称了。最好的是非对称基元或复合对象。我更喜欢使用 3D 坐标交叉来直接查看轴方向。

坐标系

  • 有没有必要改变DefaultUpBoxCustomLayer构造,因为目标是拥有一切在三个默认的坐标对齐。
  • 您在onAdd()方法中翻转 y 轴,但随后在初始化组时也会缩放所有对象。这样,您在反转投影后操作的坐标raycast()不再以米为单位。因此,让我们将缩放部分与this.cameraTransform. 左手到右手的转换也应该在这里完成。我决定翻转 z 轴并沿 x 轴旋转 90 度以获得 y 指向上的右手系:
const centerLngLat = map.getCenter();
this.center = MercatorCoordinate.fromLngLat(centerLngLat, 0);
const {x, y, z} = this.center;

const s = this.center.meterInMercatorCoordinateUnits();

const scale = new THREE.Matrix4().makeScale(s, s, -s);
const rotation = new THREE.Matrix4().multiplyMatrices(
        new THREE.Matrix4().makeRotationX(-0.5 * Math.PI),
        new THREE.Matrix4().makeRotationY(Math.PI)); //optional Y rotation

this.cameraTransform = new THREE.Matrix4()
        .multiplyMatrices(scale, rotation)
        .setPosition(x, y, z);
Run Code Online (Sandbox Code Playgroud)
  1. 确保从 中的组中删除缩放makeScene,实际上您不再需要该组。

  2. 无需触摸该render功能。

光线投射

在这里它变得有点棘手,基本上它Raycaster会做什么,但我遗漏了一些不必要的函数调用,例如相机世界矩阵是标识 => 不需要与它相乘。

  1. 获取投影矩阵的逆。它在Object3D.projectionMatrix分配时不会更新,所以让我们手动计算它。
  2. 使用它来获取 3D 中的相机和鼠标位置。这相当于Vector3.unproject.
  3. viewDirection只是从cameraPosition到 的归一化向量mousePosition
const camInverseProjection = 
        new THREE.Matrix4().getInverse(this.camera.projectionMatrix);
const cameraPosition =
        new THREE.Vector3().applyMatrix4(camInverseProjection);
const mousePosition =
        new THREE.Vector3(mouse.x, mouse.y, 1)
        .applyMatrix4(camInverseProjection);
const viewDirection = mousePosition.clone()
        .sub(cameraPosition).normalize();
Run Code Online (Sandbox Code Playgroud)
  1. 设置 raycaster 光线
this.raycaster.set(cameraPosition, viewDirection);
Run Code Online (Sandbox Code Playgroud)
  1. 其余的可以保持不变。

完整代码

用于演示的小提琴

  • 我添加mousemove了使用jQuery显示实时调试信息。
  • 单击后,有关命中的完整信息将记录到控制台。