【发布时间】:2018-03-26 16:49:53
【问题描述】:
我正在尝试将图像作为资产属性类型传递给其他一些组件(以便可以传递 #selector 或 url(url)),但它似乎包含整个 html 组件,而不仅仅是网址。
<!DOCTYPE html>
<html>
<head>
<title>Hello, WebVR! - A-Frame</title>
<meta name='description' content='Hello, WebVR! - A-Frame'>
<script src='../../global/js/aframe-v0.8.0.min.js'></script>
<script>
AFRAME.registerComponent('some-component', {
schema: {
image: {type:'asset', default:''},
model: {type:'asset', default:''}
},
init: function() {
console.log(this.data.image); //prints out <img id="SomeImage" src="../../someDir/someFile.jpg">
console.log(this.data.model); //prints out '../../someDir/someModel.gltf'
}
});
</script>
</head>
<body>
<a-scene >
<a-assets timeout='3000'>
<!-- this works as an asset no problem -->
<a-asset-item id='SomeModel' src='../../global/assets/models/gltf/UserHead/UserHead.gltf'></a-asset-item>
<!-- this does not pass as an asset but rather an html element -->
<img id='SomeImage' src='../../global/assets/textures/equirectangular/CloudySky.jpg'>
</a-assets>
<a-entity some-component='image:#SomeImage; model:#SomeModel;'></a-entity>
</a-scene>
</body>
</html>
我想我可能会看看 A-Frame 如何在材质组件中处理这个问题,但看不到材质上的“src”属性来自哪里?
<a-entity id='skyBox'
geometry='primitive:sphere; radius:50; segments-height:6; segments-width:6;'
material='shader:flat; src:#skyMap; side:back; height:2048; width:2048'>
</a-entity>
材质组件(看不到src):https://github.com/aframevr/aframe/blob/master/src/components/material.js
谢谢!
编辑:
根据 Piotr 在下面的发现,看起来图像是作为一种特殊情况处理的,就像在 src 代码 here with frame 0.8.0 中一样,图像源是这样处理的:
hash: function (data) {
if (data.src.tagName) {
// Since `data.src` can be an element, parse out the string if necessary for the hash.
data = utils.extendDeep({}, data);
data.src = data.src.src;
}
return JSON.stringify(data);
},
所以基本上,如果我们的资产属性将无法正确处理图像的图像,并通过额外的步骤从图像中获取 url
data.src.src
或
data.src.getAttribute('src');
【问题讨论】:
标签: aframe