【问题标题】:A-Frame: Cannot set world position of scene child object successfullyA-Frame:无法成功设置场景子对象的世界位置
【发布时间】:2019-04-10 23:34:55
【问题描述】:

基本上在我的场景中,我有一个带有激光控制和光线投射器的实体,结构如下:

   <a-scene>
        <a-entity laser-controls raycaster ...>
        <!--<a-entity id="inventory" ...> Could be inserted here-->
        </a-entity>
        <!--<a-entity id="inventory" ...> Could be inserted here-->
    </a-scene>

我的目标是在激光线的当前 x,y 位置召唤库存,我已经可以访问对应于激光线末端的点。我不希望库存再次从那个位置移动。如果我将库存设置为 raycaster 的子级,它总是随线移动。如果我将库存设置为场景的子节点,同时将它的位置设置为它应该在的点的世界坐标,它就是行不通的。

我尝试过但失败的方法:

  • 将库存作为 raycaster 子级启动并将父级更改为场景并应用 raycaster 的世界矩阵
  • 将库存保持为 raycaster 子项,捕获其初始世界矩阵并在每个刻度上设置相同的世界矩阵

最后,我目前的方法(使用代码)仍然失败

1.将线端的局部坐标转换为世界坐标

2.将库存附加到场景

3.将线端的世界坐标转换为场景的局部坐标

4.将3中的位置应用到库存

let v = this.emptyVec
v.copy(this.raycaster.components.line.data.end)
this.raycaster.object3D.localToWorld(v);
this.el.appendChild(inventoryNode) //this.el is the scene
this.el.object3D.worldToLocal(v);
const {x,y} = v
inventoryNode.object3D.position.set(x,y,inventoryZDistance)

TLDR:如何在将实体添加到场景的时间点将实体设置到光线投射器线端的位置并让它永远保持在该位置

【问题讨论】:

    标签: three.js aframe coordinate-transformation webvr


    【解决方案1】:

    最终找到了解决方案。这是在控制器单击事件的事件侦听器上运行的(点是它不需要在每个滴答声上运行,它只运行一次)

    1. 创建了一个虚拟节点,它是光线投射器的子节点,并将其位置设置为线端的 x、y 坐标以及我想要的任何 z 坐标
    2. 将适当的库存节点附加到场景中
    3. 使用 setFromMatrixPosition() 方法从虚拟节点的世界矩阵中设置库存的位置

      let dummyNode = document.createElement("a-entity")
      dummyNode.setAttribute("id", "dummyinventory")
      dummyNode.setAttribute("visible", false) //maybe unnecessary
      const {x,y} = this.raycaster.components.line.data.end
      dummyNode.object3D.position.set(x,y,inventoryZDistance)
      this.raycaster.appendChild(dummyNode)
      
      inventoryNode.setAttribute('look-at', "[camera]") //if you want inventory to face the camera
      this.el.appendChild(inventoryNode)
      setTimeout(()=>{
          let newMatrix = dummyNode.object3D.matrixWorld
          inventoryNode.object3D.position.setFromMatrixPosition(newMatrix)
      },50) //give the world matrix of dummyNode some time to update, with no timeout sometimes fails
      

    如果想要通过让虚拟节点成为相机的子节点而不是光线投射器来生成用户正在查看的某个实体,则可以应用相同的思考过程

    【讨论】:

      猜你喜欢
      • 2017-12-29
      • 2016-12-21
      • 2018-08-02
      • 2013-02-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-16
      • 1970-01-01
      相关资源
      最近更新 更多