gltf cursor-listener click event in A-frame

…衆ロ難τιáo~ 提交于 2019-12-03 22:22:41

问题


I am unable to figure out why cursor-listener works fine for all the entities except for my gltf model.

Here is my html

<div id="myEmbeddedScene">
  <a-scene embedded="">
    <a-assets>
          <a-asset-item id="ducks" src="../images/test.glb"></a-asset-item>
    </a-assets>

    <a-box cursor-listener color="#CCC" width="3" depth="3" height="0.1" position="0 0 -2"></a-box>

    <a-entity cursor-listener id="duck" gltf-model="#ducks" position="0 0.1 -2" rotation="0 -90 0"></a-entity>

    <a-camera>
      <a-cursor></a-cursor>
    </a-camera>

  </a-scene>
</div>

and here goes the cursor-listener component from a-frame

AFRAME.registerComponent('cursor-listener', {
    init: function () {
            this.el.addEventListener('click', function (evt) {
            console.log('I was clicked');               
        });
  }
});

The console log occurs just fine for the box entity but not for the gltf model. Please could someone offer their advise?


回答1:


You've run into the issue described here: After a model loads, the raycaster used to detect clicks needs to be refreshed, so that it knows about the model.

We have a more robust solution on the way for A-Frame 0.8.0, but in the meantime you can work around the problem with something like this:

AFRAME.registerComponent('raycaster-autorefresh', {
  init: function () {
    var el = this.el;
    this.el.addEventListener('model-loaded', function () {
      var cursorEl = el.querySelector('[raycaster]');
      cursorEl.components.raycaster.refreshObjects();
    });
  }
});

You would then need to add the raycaster-autorefresh to your scene element. Here is a Codepen showing the solution.



来源:https://stackoverflow.com/questions/47032056/gltf-cursor-listener-click-event-in-a-frame

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!