簡體   English   中英

A-Frame + AR.js:點擊屏幕時如何在 Arjs 中動態添加對象?

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

我目前正在使用 ar.js 開發 A-Frame AR 應用程序(我必須使用 ar.js)。 目標是在點擊場景時將對象動態添加到場景中。 不幸的是,似乎什么都沒有解決。 在點擊屏幕但沒有添加新的時,我設法更改了現有 object 的 colors。 有人可以幫我嗎? 我將不勝感激!

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);

您應該 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