问题
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