【问题标题】:Dynamically created A-FRAME content set to embedded will not show A-FRAME inspector动态创建的 A-FRAME 内容设置为嵌入将不会显示 A-FRAME 检查器
【发布时间】:2019-09-25 03:13:35
【问题描述】:

我正在使用一个函数动态创建 A-FRAME 场景,该函数将图像路径作为参数并返回构建的 A-FRAME a-scene。

虽然尝试使用 ctrl + alt + i 初始化 A-FRAME 检查器不起作用,但返回的值正在工作并在 DOM 中正确显示,这主要是由于动态附加的内容。

A-FRAME 中是否有允许检查员处理动态附加内容或其他此类解决方法的标志或设置?

     function buildCelestialBody(a) {
    let aframe= `
                <!-- AFRAME CONTENT -->
                <!-- MAIN SCENE -->
                <a-scene embedded
                        loading-screen="dotsColor: #ffffff;  backgroundColor: black;"
                        vr-mode-ui="enabled: false fuseTimeout: 0">

                    <!-- ASSETS -->
                    <a-assets>
                        <img id="celestialbody" src="${a}"></img>
                    </a-assets>

                    <!-- CAMERA -->
                    <a-entity id='cameraWrapper' position="0 0 0" rotation="0 0 0">
                        <a-camera id="camera1" camera="active: true; zoom: 3.5;
                        spectator: false;"
                        look-controls="enabled:  false"></a-camera>
                    </a-entity>

                    <!-- CELESTIAL BODY -->
                    <a-sphere src="#celestialbody"
                    geometry="primitive: sphere;
                              segmentsHeight: 100;
                              segmentsWidth: 100"
                    scale="1 1 1"
                    position="0 0 -8.5;"
                    animation="property: rotation;
                                dur: 200000;
                                from: 400 360 0;
                                to: 400 -360 0;
                                dir: reverse;
                                loop: true;
                                easing: linear"
                    id="celestialbody" celestialbody>
                    </a-sphere>

                    <!-- LIGHT -->
                    <a-entity id="light"
                              light="type:directional; castShadow:true;"
                              position="2 1 1"
                              animation="property: position;
                                        dur: 2000;
                                        from: 2 1 1;
                                        to: -2 1 1;
                                        dir: reverse;
                                        loop: true;
                                        autoplay: false;
                                        easing: easeInQuad;">
                    </a-entity>

                </a-entity>

                <!-- LIGHT SOURCES -->
                <a-light id="light_1" type="ambient" color="#9ecdf9" intensity="0.460" position="2 4 4"></a-light>

                </a-scene>

                <!-- AFRAME CONTENT END -->`
        return aframe;

    } 

【问题讨论】:

    标签: aframe


    【解决方案1】:

    您需要将创建代码放在一个组件中,而不仅仅是一个全局范围的函数。将其放置在组件中可确保首先加载所有内容(包括检查器),然后运行您的代码,并且检查器将注册对象。

    
            AFRAME.registerComponent('treeman', {
                init: function(){
                    let el = this.el;
                    let sceneEl = document.querySelector('a-scene');
                    let trees = sceneEl.querySelector('#trees');
                    el.addEventListener("model-loaded", e =>{
                        //trees.emit('modelisloaded');
                        let tree3D = el.getObject3D('mesh');
                        if (!tree3D){return;}                   
                        tree3D.traverse(function(node){
                            if (node.isMesh){                           
                                node.renderOrder = 2;
                                console.log(node.name, node.renderOrder, node.material);
                                node.material.color=new THREE.Color(0xaa5522);
                                node.material.map = null;
                            }
                        });
    
                        let countX = 10;
                        let cubes = [];
                        let size = 0.125, spacing = 0.05, x;
                        for (let i=0; i<countX; i++){
                          cubes[i] = document.createElement('a-entity'); // create the element
                           // create components, id, geometry, position
                          cubes[i].setAttribute('id', 'tree_'+i.toString());
                          cubes[i].setAttribute('gltf-model', '#tree');
                          x = (size + spacing) * countX * (-0.5) + i * (size + spacing) ;
                          y = Math.random() * 0.25;
                          z = Math.random() * 1;
                          cubes[i].setAttribute('position', x.toString()+ ' '+y.toString()+' '+z.toString() );
    
    
                          // you can add event listeners here for interaction, such as mouse events.
                          sceneEl.appendChild(cubes[i]);// Append the element to the scene, so it becomes part of the DOM.
                        }           
                  });
                }
            });
    
    
           <a-entity id="treemodel" visible="false" gltf-model="#tree" scale="5 5 5" treeman></a-entity>
    
    
    

    我刚刚运行了这个,确认动态创建的对象确实出现在检查器中。

    glitch here

    【讨论】:

    • 我假设的 已经存在于 DOM 中?我已经尝试了上述方法,但是我的用例中的 是通过 JavaScript 创建的,然后动态附加到主体下方的 dom 中。即使使用已注册的组件,我也无法初始化 a-frame 检查器,这与您提到的有关,检查器可能正在加载,但元素附加到 dom 的时间导致它失败。
    • 您的代码结构非常不寻常,因为它在 javascript 中放置了 html 调用。我很惊讶这完全有效。您的 html 应该在 body 标签内,而 javascript 应该在 script 标签内。我建议您应该重新考虑“动态创建元素”的方式,并在 js 脚本中使用 THREEjs 命令和 Aframe 命令来创建实体。实体归根结底都是js,所以最好用js来创建。
    • Aframe 想出了一个巧妙的方法在 html 中嵌入组件,但它是语法糖(一种编码方便)。您可以在 html 中手动创建 3D 元素。但是你必须使用组件来修改它们,否则在aframe场景加载并准备好之前运行js会遇到错误。
    • 代码结构类似于codereadability.com/constructing-html-with-templates。按照您的建议注册组件很棒,因为可以在控制台日志中调用 AFRAME.components.foo 并继续,而上面的初始尝试仍然显示 a-scene,但如果在控制台中调用,则不会在 a-frame 中注册。但是,除非标记中已经有一个 并且没有通过 JS 附加到 DOM,否则检查器将不会加载、注册与否。可能是以非预期方式使用 AFRAME 的情况。
    • 您可以有一个简单的场景声明,其中包含一个包含组件的 emtpy 实体,然后生成所有其他实体。 &lt;a-scene&gt; &lt;a-entity component-manager &gt;&lt;/a-entity&gt;&lt;/a-scene&gt;这就是上面的故障对tree-manager的影响
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-11
    • 1970-01-01
    相关资源
    最近更新 更多