简体   繁体   English

A-Frame + AR.js:点击屏幕时如何在 Arjs 中动态添加对象?

[英]A-Frame + AR.js: How to add objects dynamically in Arjs when tapping on screen?

I'm currently working on an A-Frame AR app by using ar.js (I have to use ar.js).我目前正在使用 ar.js 开发 A-Frame AR 应用程序(我必须使用 ar.js)。 The goal is to dynamically add objects to the scene when tapping on the scene.目标是在点击场景时将对象动态添加到场景中。 Unfortunately nothing seems to work out.不幸的是,似乎什么都没有解决。 I managed to change the colors of an existing object when tapping on the screen but not adding a new one.在点击屏幕但没有添加新的时,我设法更改了现有 object 的 colors。 Could someone please help me out here?有人可以帮我吗? I would be really grateful!我将不胜感激!

AFRAME.registerComponent('cursor-listener', {
  init: function () {
  var currentEl = this.el;
  currentEl.addEventListener("click", function (evt) {
  console.log("I was clicked!");
  currentEl.setAttribute('material', {"color": "blue"});
  var el = document.createElement("a-entity");
  el.setAttribute('geometry', { "primitive": "sphere", "radius": "1" });
 
  
  
  el.setAttribute('position', this.el.object3D.position)
  const randomYRotation = Math.random() * 360
  el.setAttribute('rotation', `0 ${randomYRotation} 0`)
  el.setAttribute('visible', 'true')
  el.setAttribute('shadow', {receive: false})
  this.el.sceneEl.appendChild(el);
  });
  },
  });
  

this.el.sceneEl.appendChild(el); this.el.sceneEl.appendChild(el);

You should append the element to the marker node, and treat it as your "root" element - as it's the one being tracked when detected by camera:您应该 append 元素到标记节点,并将其视为您的“根”元素 - 因为它是被相机检测到的元素:

<script>
  AFRAME.registerComponent('cursor-listener', {
     init:function () {
       const marker = document.querySelector("a-marker")
       var el = document.createElement("a-sphere");
       marker.appendChild(el);
     }
  })
</script>
<!-- scene and stuff -->
<a-marker preset="hiro>
</a-marker>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM