【问题标题】:Capture Event 'raycaster-intersected' AFRAME捕获事件 'raycaster-intersected' AFRAME
【发布时间】:2018-08-31 09:08:42
【问题描述】:

我需要在事件raycaster-intersected 每次发生时捕获它。

我需要到检测到的第一个实体的距离来检查碰撞,但该事件仅在光线投射器第一次击中实体时触发。

如果光线投射器仍然接触到实体,它不会触发事件。

我有下一个代码:

AFRAME.registerComponent("follow-body", {
  'schema': {
    entityId: {
      type:'string',
      default:''
    }
  },
  init: function(){
    this.pibot = document.querySelector("#" + this.data.entityId);
  },
  tick: function(){
    let pibotPos = this.pibot.object3D.position;
    let pibotRotation = this.pibot.object3D.rotation;
    let el = this.el;
    pibotPos.y += 0.2;

    el.object3D.position.set(pibotPos.x, pibotPos.y, pibotPos.z);
    el.object3D.rotation.set(pibotRotation.x, pibotRotation.y, pibotRotation.z);
    el.addEventListener('raycaster-intersection', function(evt){
      var e = new CustomEvent('intersection-detected', {detail: evt.detail});
      this.dispatchEvent(e);
    });
  }
});

【问题讨论】:

  • 代码 sn-p 不运行。错误:{“消息”:“脚本错误。”,“文件名”:“”,“lineno”:0,“colno”:0 }

标签: javascript events three.js aframe


【解决方案1】:

这样的自定义组件怎么样:
- raycaster-intersected 事件触发 - 你存储光线投射器
- raycaster-intersected-cleared - 你取消了 raycaster 参考
- 在tick 上,如果存在光线投射器参考,则获取交点并进行计算。

AFRAME.registerComponent("foo", {
  init: function() {
    this.el.addEventListener("raycaster-intersected", evt => {
      this.intersectingRaycaster = evt.detail.el.components.raycaster;
    });
    this.el.addEventListener("raycaster-intersected-cleared", () => {
      this.intersectingRaycaster = null;
    });
  },
  tick: function() {
     if (!this.intersectingRaycaster) {
        return;
     }

     const intersection = this.intersectingRaycaster.getIntersection(this.el);
     if (intersection) {
        let point = intersection.point;
        // your calculations
     }
  }
});

在fiddle 中查看。

【讨论】:

  • 我昨天发布了这个,我在 AFRAME 的源代码上看到的是“raycaster-intersection”只在新的交叉点上触发,所以我需要手动计算位置。
  • 是的,这就是为什么您需要检测交叉路口何时发生以及何时停止的原因。顺便说一句,交叉点的位置在我的代码中的 point 引用中。我的例子不起作用吗?
  • 是的,它看起来与我编码的相似,我在 raycaster-intersection 触发时注册了一个变量 boolean true 并存储了 raycaster 指向的对象的位置。接下来我计算光线投射器和物体之间的距离。并在 raycaster-intersection-cleared 时全部清除。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-09-08
  • 2018-11-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-24
  • 2018-10-04
相关资源
最近更新 更多