Aframe Dynamic导入GLTF文件

发布于 2025-01-28 16:11:50 字数 1706 浏览 4 评论 0原文

因此,我有一个简单的GLTF文件,我想在运行时用A型框架导入它。

我已经尝试将GLTF字符串转换为URL并加载它,但是这给出了组件:GLTF模型:WARN无法读取未定义的属性(读取'0')警告。这是我尝试的代码:

<a-scene>
  <a-entity id="entity" position="0 2 -5"></a-entity>
</a-scene>

<script>
  const gltfString = `{"cacessors":[{"bufferView":0,"byteOffset":0,"count":3,"componentType":5126,"extras":{},"type":"VEC3","min":[-0.5,-0.5,0.0],"max":[0.5,0.5,0.0]},{"bufferView":0,"byteOffset":12,"count":3,"componentType":5126,"extras":{},"type":"VEC3"}],"asset":{"extras":{},"version":"2.0"},"buffers":[{"byteLength":72,"uri":"data:application/octet-stream;base64,AAAAAAAAAD8AAAAAAACAPwAAAAAAAAAAAAAAvwAAAL8AAAAAAAAAAAAAgD8AAAAAAAAAPwAAAL8AAAAAAAAAAAAAAAAAAIA/","extras":{}}],"bufferViews":[{"buffer":0,"byteLength":72,"byteStride":24,"target":34962,"extras":{}}],"extras":{},"meshes":[{"extras":{},"primitives":[{"attributes":{"POSITION":0,"COLOR_0":1},"extras":{}}]}],"nodes":[{"extras":{},"mesh":0}],"scenes":[{"extras":{},"nodes":[0]}]}`;
  const gltfBlob = new Blob([gltfString]); // convert string to blob
  const gltfUrl = URL.createObjectURL(gltfBlob, {type: "text/plain"}); // blob to URL

  document.addEventListener('DOMContentLoaded', function (event) {
    const entity = document.querySelector('#entity');
        
    console.log(entity) // blob:http://localhost:5500/4efe2c0d-5568-4928-8820-2e686b5b0c2a

    bruhEntity.setAttribute('gltf-model', `url(${gltfUrl})`);
  });
</script>

实现这一目标的方法是什么?

这是我的小glitch项目

So I have a simply gltf file and i would like to import it with a-frame during runtime.

I already tried to convert the gltf string to a URL and load it, however this gives a components:gltf-model:warn Cannot read properties of undefined (reading '0') warning. This is the code i tried:

<a-scene>
  <a-entity id="entity" position="0 2 -5"></a-entity>
</a-scene>

<script>
  const gltfString = `{"cacessors":[{"bufferView":0,"byteOffset":0,"count":3,"componentType":5126,"extras":{},"type":"VEC3","min":[-0.5,-0.5,0.0],"max":[0.5,0.5,0.0]},{"bufferView":0,"byteOffset":12,"count":3,"componentType":5126,"extras":{},"type":"VEC3"}],"asset":{"extras":{},"version":"2.0"},"buffers":[{"byteLength":72,"uri":"data:application/octet-stream;base64,AAAAAAAAAD8AAAAAAACAPwAAAAAAAAAAAAAAvwAAAL8AAAAAAAAAAAAAgD8AAAAAAAAAPwAAAL8AAAAAAAAAAAAAAAAAAIA/","extras":{}}],"bufferViews":[{"buffer":0,"byteLength":72,"byteStride":24,"target":34962,"extras":{}}],"extras":{},"meshes":[{"extras":{},"primitives":[{"attributes":{"POSITION":0,"COLOR_0":1},"extras":{}}]}],"nodes":[{"extras":{},"mesh":0}],"scenes":[{"extras":{},"nodes":[0]}]}`;
  const gltfBlob = new Blob([gltfString]); // convert string to blob
  const gltfUrl = URL.createObjectURL(gltfBlob, {type: "text/plain"}); // blob to URL

  document.addEventListener('DOMContentLoaded', function (event) {
    const entity = document.querySelector('#entity');
        
    console.log(entity) // blob:http://localhost:5500/4efe2c0d-5568-4928-8820-2e686b5b0c2a

    bruhEntity.setAttribute('gltf-model', `url(${gltfUrl})`);
  });
</script>

What would be the way to achieve this?

This is my glitch project

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

青衫负雪 2025-02-04 16:11:50

字符串中有一个错别字,这是找出问题的简单方法:

  • 将缓冲区保存在文件test.gltf中。
  • 将其扔进 gltf-viewer
  • 找出有误(从错误从这个问题
const gltfString = `{"accessors":[{"bufferView":0,"byteOffset":0,"count":3,"componentType":5126,"extras":{},"type":"VEC3","min":[-0.5,-0.5,0.0],"max":[0.5,0.5,0.0]},{"bufferView":0,"byteOffset":12,"count":3,"componentType":5126,"extras":{},"type":"VEC3"}],"asset":{"extras":{},"version":"2.0"},"buffers":[{"byteLength":72,"uri":"data:application/octet-stream;base64,AAAAAAAAAD8AAAAAAACAPwAAAAAAAAAAAAAAvwAAAL8AAAAAAAAAAAAAgD8AAAAAAAAAPwAAAL8AAAAAAAAAAAAAAAAAAIA/","extras":{}}],"bufferViews":[{"buffer":0,"byteLength":72,"byteStride":24,"target":34962,"extras":{}}],"extras":{},"meshes":[{"extras":{},"primitives":[{"attributes":{"POSITION":0,"COLOR_0":1},"extras":{}}]}],"nodes":[{"extras":{},"mesh":0}],"scenes":[{"extras":{},"nodes":[0]}]}`;

const gltfBlob = new Blob([gltfString], {
  type: "text/plain"
}); // convert string to blob
const gltfUrl = URL.createObjectURL(gltfBlob, ); // blob to URL

const entity = document.querySelector('#entity');
entity.setAttribute('gltf-model', `url(${gltfUrl})`);
<script src="https://aframe.io/releases/1.3.0/aframe.min.js"></script>
<a-scene renderer="colorManagement: true">
  <a-light position="0 2 -1.9" intensity=10 type="point"></a-light>
  <a-entity id="entity" position="0 2 -2"></a-entity>
</a-scene>

There was a typo in the string, here's a simple way to find out whats wrong:

  • Save the buffer in a file test.gltf.
  • Throw it into the gltf-viewer
  • Find out there is a typo (from the error this.json.accessors is undefined)
  • change cacessors to accessors
  • voila:

const gltfString = `{"accessors":[{"bufferView":0,"byteOffset":0,"count":3,"componentType":5126,"extras":{},"type":"VEC3","min":[-0.5,-0.5,0.0],"max":[0.5,0.5,0.0]},{"bufferView":0,"byteOffset":12,"count":3,"componentType":5126,"extras":{},"type":"VEC3"}],"asset":{"extras":{},"version":"2.0"},"buffers":[{"byteLength":72,"uri":"data:application/octet-stream;base64,AAAAAAAAAD8AAAAAAACAPwAAAAAAAAAAAAAAvwAAAL8AAAAAAAAAAAAAgD8AAAAAAAAAPwAAAL8AAAAAAAAAAAAAAAAAAIA/","extras":{}}],"bufferViews":[{"buffer":0,"byteLength":72,"byteStride":24,"target":34962,"extras":{}}],"extras":{},"meshes":[{"extras":{},"primitives":[{"attributes":{"POSITION":0,"COLOR_0":1},"extras":{}}]}],"nodes":[{"extras":{},"mesh":0}],"scenes":[{"extras":{},"nodes":[0]}]}`;

const gltfBlob = new Blob([gltfString], {
  type: "text/plain"
}); // convert string to blob
const gltfUrl = URL.createObjectURL(gltfBlob, ); // blob to URL

const entity = document.querySelector('#entity');
entity.setAttribute('gltf-model', `url(${gltfUrl})`);
<script src="https://aframe.io/releases/1.3.0/aframe.min.js"></script>
<a-scene renderer="colorManagement: true">
  <a-light position="0 2 -1.9" intensity=10 type="point"></a-light>
  <a-entity id="entity" position="0 2 -2"></a-entity>
</a-scene>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文