【问题标题】:How can create a-scene programmatically using aframe如何使用 aframe 以编程方式创建场景
【发布时间】:2018-04-12 13:07:57
【问题描述】:

我是 js 和 html 的新手,我想进入 Aframe。 我想从声明式创建场景到使用 js 来创建场景的编程方式:

<!DOCTYPE html>
<html>
  <head>
    <title>JavaScript - A-Frame School</title>
    <meta name="description" content="JavaScript - A-Frame School">
    <script src="https://aframe.io/releases/0.8.0/aframe.min.js"></script>

  </head>
  <body>
    <a-scene school-playground>
      <a-box  position="-1 0 -4.25" rotation="0 45 0"  color="red" ></a-box>

      <a-sky color="#ECECEC"></a-sky>
    </a-scene>
  </body>
</html>

类似的事情:

<!DOCTYPE html>
<html>
  <head>
    <title>JavaScript - A-Frame School</title>
    <meta name="description" content="JavaScript - A-Frame School">
    <script src="https://aframe.io/releases/0.8.0/aframe.min.js"></script>
    <script>
AFRAME.registerComponent('school-playground', {

init: function () {

 var body = document.querySelector('body');
 var sceneEl = document.createElement("a-scene");
 var body = document.querySelector('body');
 sceneEl.setAttribute("embedded", true);
 sceneEl.style.height="700px";
 sceneEl.style.width="100%";
 sceneEl.setAttribute("school-playground", "");

 var myBox = document.createElement('a-box');
 myBox.setAttribute('position', {x:-1, y:0, z:-4})
 myBox.setAttribute('rotation', {x:0,y:45, z:0}
 myBox.setAttribute('color', "red");
 sceneEl.appendChild(myBox);
body.appendChild(sceneEl);
//I also tried document.body.appendChild(sceneEl);
}
});
        </script>
  </head>
  <body>

  </body>
</html>

似乎无法正确地做到这一点。我需要保持静态定义的场景吗?

感谢您的帮助。

【问题讨论】:

    标签: javascript html aframe virtual-reality programmatically


    【解决方案1】:

    组件在附加到实体时初始化 (https://aframe.io/docs/0.8.0/introduction/writing-a-component.html#using-property-data-from-a-lifecycle-handler)。您的组件刚刚注册但未与任何实体关联,因此 init 方法不会运行。您可以像在 JavaScript 中的任何其他常规 DOM 组件一样以编程方式创建场景,与您所做的类似,但请记住在组件之外执行此操作并将场景附加到文档中:

     var sceneEl = document.createElement("a-scene");
     ...
     document.body.appendChild(sceneEl);
    

    您还可以静态定义&lt;a-scene&gt; 标签,然后填充场景:

    sceneEl = document.querySelector("a-scene");
    ... create and append scene entities ...
    sceneEl.appendChild(yourEntity);
    

    我还建议您将 A-Frame 版本升级到 0.8.0

    关于 Glitch 的完整可运行示例:https://glitch.com/edit/#!/conscious-way

    <!DOCTYPE html>
    <html>
      <head>
        <title>Hello, WebVR! - A-Frame</title>
        <meta name="description" content="Hello, WebVR! - A-Frame">
        <script src="https://aframe.io/releases/0.8.0/aframe.min.js"></script>
      </head>
      <body>
      </body>
      <script>
       var sceneEl = document.createElement('a-scene');
       sceneEl.setAttribute('background', {color: 'red'});
       var cubeEl = document.createElement('a-box');
       cubeEl.setAttribute('color', 'blue');
       cubeEl.setAttribute('position', '0 1.5 -2');
       sceneEl.appendChild(cubeEl);
       document.body.appendChild(sceneEl);
      </script>
    </html>
    

    【讨论】:

    • 第二种方法效果很好,但这不是我真正想要的。第一种方法是我想要的,但不起作用。我忘了添加它,但我做了: **** var sceneEl = document.createElement("a-scene"); body.appendChild(sceneEl);在控制台中,我看到身体是空的。感谢您的建议:我升级了 aframe 版本
    【解决方案2】:

    这是我暂时写的(场景没有出现):

    <!DOCTYPE html>
    <html>
      <head>
        <title>JavaScript - A-Frame School</title>
        <meta name="description" content="JavaScript - A-Frame School">
    <script src="https://aframe.io/releases/0.8.0/aframe.min.js"></script>
    
    <script>
    AFRAME.registerComponent('school-playground', {
        /**
         * Code within this function will be called when everything in <a-scene> is ready and loaded.
         */
      init: function () {
          //var body = document.body;    
    
        //  var sceneEl = document.querySelector('a-scene');
         var sceneEl = document.createElement("a-scene");
          var body = document.querySelector('body');
          sceneEl.setAttribute("embedded", true);
         //sceneEl.setAttribute("class", "fullscreen");
          sceneEl.style.height="700px";
          sceneEl.style.width="100%";
    
    
    
          var camera = document.createElement("a-entity");
          camera.setAttribute("camera", "userHeight: 1.6");
          camera.setAttribute("look-controls", {enabled: true});
          camera.setAttribute("wasd-controls", "");
          camera.setAttribute("active", true);
    
          sceneEl.appendChild(camera)
    
    
           //cylinder creation using the necessary attributes
          var cylinder = document.createElement('a-cylinder');
          cylinder.setAttribute('color', '#FF9500');
          cylinder.setAttribute('height', '2');
          cylinder.setAttribute('radius', '0.75');
          cylinder.setAttribute('position', '3 1 -4');
          sceneEl.appendChild(cylinder);
    
          //box creation using the necessary attributes
          for (var i =0; i < 50; i++){
            var myBox = document.createElement('a-box');
            myBox.setAttribute('position', {x:Math.random()* 5-2.5 , y: Math.random()* 5-2.5 ,z : Math.random()* 5-7})
            myBox.setAttribute('scale', {x: Math.random() / 1.25, y: Math.random() / 1.25, z: Math.random() / 1.25});
            myBox.setAttribute( 'material', {color: '#00bfff'});
            myBox.setAttribute('material', {visible: true});
            myBox.setAttribute('rotation', {x: 0, y: 0, z: 0});
            sceneEl.appendChild(myBox);
          }
          document.body.appendChild(sceneEl);
      }
      });
    </script>
    
    
    
     </head>
      <body>
    
      </body>
    </html>
    

    【讨论】:

    • 我更新了我的原始答案,在底部有一个完整的可运行示例。如上所述,如果组件没有附加到实体,它们就不会运行。这就是为什么你的school-playground 的逻辑没有执行
    • 好的,它工作正常!我认为我的学校操场在 java 中用作类名。谢谢:D
    猜你喜欢
    • 2018-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-19
    • 2021-03-09
    • 2021-09-14
    • 2010-11-19
    相关资源
    最近更新 更多