AFRAME 光线追踪获取相交实体的参考
AFRAME raytrace get reference of intersected entity
我有一个 raycaster 组件附加到 vr 控制器实体:
<a-entity id="righthand"
vive-controls="hand: right; "
oculus-touch-controls="hand: right;"
controls-ui
collider-check
>
<a-entity raycaster="objects: .collidable; showLine: true; far: 100; " line="color: blue; opacity: 0.5" ></a-entity>
</a-entity>
我在场景中得到了一个将接收光线追踪事件的实体:
<a-entity id='myCube' class="collidable" position="0 1.25 -6" obj-model="obj: #cube-obj; mtl: #cube-mtl" >
</a-entity>
如何获取 'raycaster-intersected' 事件中碰撞实体的 ID 或任何引用?
我尝试了以下代码,但似乎没有任何内容包含此数据:
AFRAME.registerComponent('collider-check', {
dependencies: ['raycaster'],
init: function () {
this.el.addEventListener('raycaster-intersected', function (evt) {
console.log(evt.detail.el); // not here
console.log(evt.detail.intersection); // not here
console.log(evt.detail);// not here
console.log('Player hit something!');
});
}
});
提前致谢。
------------更新------------
@Piotr Adam Milewski 回答正确。要监听的事件是 raycaster-intersection 而不是 raycaster-intersected。通过这种方式,您可以循环交叉实体的数组。
是否有可能从 raycaster-intersected 得到相同的结果??如果该事件是在相交的实体上发出的,那么应该可以获得它的 id 和其他 properties.I 认为每次发生相交事件时循环遍历数组是不理想的。
来自docs:
raycaster-intersected
在相交的实体上发射。它包含有关光线投射实体和交叉点详细信息的信息。
raycaster-intersection
在光线投射实体上发射,并包含相交实体的列表。
使用 raycaster-intersection
时,请尝试访问 evt.detail.els
以获取一组相交的实体。示例 here
由于 raycaster-intersected
是在相交的实体上发射的,您可以检测光线投射器是否接触到您的目标。
target.addEventListener('raycaster-intersected', (e)=> {
// intersected, e.target contains the element
// e.detail.getIntersection(e.target) contains info about the intersection
})
我有一个 raycaster 组件附加到 vr 控制器实体:
<a-entity id="righthand"
vive-controls="hand: right; "
oculus-touch-controls="hand: right;"
controls-ui
collider-check
>
<a-entity raycaster="objects: .collidable; showLine: true; far: 100; " line="color: blue; opacity: 0.5" ></a-entity>
</a-entity>
我在场景中得到了一个将接收光线追踪事件的实体:
<a-entity id='myCube' class="collidable" position="0 1.25 -6" obj-model="obj: #cube-obj; mtl: #cube-mtl" >
</a-entity>
如何获取 'raycaster-intersected' 事件中碰撞实体的 ID 或任何引用? 我尝试了以下代码,但似乎没有任何内容包含此数据:
AFRAME.registerComponent('collider-check', {
dependencies: ['raycaster'],
init: function () {
this.el.addEventListener('raycaster-intersected', function (evt) {
console.log(evt.detail.el); // not here
console.log(evt.detail.intersection); // not here
console.log(evt.detail);// not here
console.log('Player hit something!');
});
}
});
提前致谢。
------------更新------------
@Piotr Adam Milewski 回答正确。要监听的事件是 raycaster-intersection 而不是 raycaster-intersected。通过这种方式,您可以循环交叉实体的数组。
是否有可能从 raycaster-intersected 得到相同的结果??如果该事件是在相交的实体上发出的,那么应该可以获得它的 id 和其他 properties.I 认为每次发生相交事件时循环遍历数组是不理想的。
来自docs:
raycaster-intersected
在相交的实体上发射。它包含有关光线投射实体和交叉点详细信息的信息。raycaster-intersection
在光线投射实体上发射,并包含相交实体的列表。
使用 raycaster-intersection
时,请尝试访问 evt.detail.els
以获取一组相交的实体。示例 here
由于 raycaster-intersected
是在相交的实体上发射的,您可以检测光线投射器是否接触到您的目标。
target.addEventListener('raycaster-intersected', (e)=> {
// intersected, e.target contains the element
// e.detail.getIntersection(e.target) contains info about the intersection
})