基本上在我的场景中,我有一个带有激光控制的实体和一个以下结构的raycaster:
<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的子库,它总是随着线移动。如果我将库存设置为场景的孩子,同时将其位置设置为它应该是的点的世界坐标,则它不起作用。
我尝试过但失败的方法:
最后,我目前的方法(使用代码)仍然失败
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:如何将实体设置为raycaster line end的位置,并将其添加到场景中,并将其永久保留在该位置
结束找到解决方案。这是在控制器点击事件的事件监听器上运行(点是它不需要在每个tick上运行,它只运行一次)
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
如果想要通过让虚拟节点成为相机的子节点而不是raycaster来生成用户正在查看的某个实体,则可以应用相同的思考过程。