2016-09-16 26 views
0

我正在註冊由其他基元組成的框架中的基元。但我想要獨立地縮放和定位這些內部原語。但是由於位置本身就是一個組成部分,所以這些內部基元沒有位置,因此我無法移動它們。如何設置元素的組成部分的值,這些元素是我在aframe中註冊的基元的一部分?

如何設置原始組件的值,這是我想要註冊的基元的一部分?

AFRAME.registerPrimitive('a-svrbutton', { 
 
    defaultComponents: { 
 
    geometry: { 
 
     primitive: 'box', 
 
     height: 1.5, 
 
     depth: 0.2, 
 
     width: 3.5 
 
    }, 
 
    material: { 
 
     opacity: 1.0, 
 
     transparent: true, 
 
     side: 'Double', 
 
     color: '#8450ff' 
 
    }, 
 
    position: { 
 
     x: 0, 
 
     y: 0, 
 
     z: -5 
 
    }, 
 
    'bmfont-text': { 
 
     text: 'hola', 
 
     color: '#ffffff' 
 
    }, 
 

 
    }, 
 
    mappings: { 
 
    height: 'geometry.height', 
 
    width: 'geometry.width', 
 
    depth: 'geometry.depth', 
 
    color: 'material.color', 
 
    opacity: 'material.opacity', 
 
    position: 'position' 
 
    } 
 
});
<script src="https://aframe.io/releases/0.5.0/aframe.min.js"></script> 
 

 
<a-scene> 
 
    <a-svrbutton id="mybutton" position="0 0 -5"></a-svrbutton> 
 
</a-scene>

確保您的瀏覽器支持WebVR爲運行上面的代碼片段。

我想在默認的「components」bmfont-text中將默認位置定義爲任意的xy和z。但我不能,因爲這個'原始'本身沒有位置組件。我怎樣才能做到這一點?

+0

您可以包含迄今爲止嘗試過的示例代碼嗎?有不同的方法來設置bmfont,並且我不知道還有哪些其他原語(可能是指組件?)。你可以在任何元素上放置位置,即使它是一個不會影響位置的基元。 –

+0

是的,我想知道這是與模板相關,還是與組件+基元相關。 – ngokevin

回答

1

一個primitive通常是一個或多個組件,沒有其他原語的簡寫,所以我可能會誤會你在做什麼 - 如果這樣你能示例代碼添加到你的問題?

您可以添加通常不相關的組件爲原始就好了,即使原始不已經是組件:

<a-text text="Hello World;" position="0 0 -5"></a-text> 

如果不工作,我建議使用bmfont作爲一個組件,而比原始版本<a-text />

<a-entity bmfont-text="text: Hello World;" position="0 0 -5"></a-entity> 

作爲A型架v0.3.0的有一些bugs with primitives,因此部件和混入可能更可靠。

0

一種方法是讓它們成爲兩個獨立的實體。

<a-entity> 
    <a-srvbutton></a-srvbutton> 
    <a-text></a-text> 
</a-entity> 

或者可能使一個包裝組件包裝bmfont文本?

AFRAME.registerComponent('bmfont-text-wrapper', { 
    schema: { 
    textPosition: {type: 'vec3'}, 
    text: {type: 'string'} 
    }, 

    init: function() { 
    var text = document.createElement('a-entity'); 
    text.setAttribute('bmfont-text', this.data.text); 
    text.setAttribute('position', this.data.textPosition); 
    this.el.appendChild(text); 
    } 
}); 

然後添加映射。