【发布时间】:2021-01-21 09:47:40
【问题描述】:
我将 Collada 和相关图像数据存储在从应用程序输出的多部分文件中。我需要能够通过 three.js 加载 Collada 对象和图像以在 Web 中显示。 three.js 可以解释多部分文件或文件中的数据,还是需要将其解析为实际文件,因为 Collada 加载程序示例显示它链接到 .dae 文件(例如 loader.load('models/ monster.dae') 而不是实际数据。
无论如何,我将如何实现这一目标?最终目标是加载并查看文件中多部分数据所代表的“盒子”。
这是我从应用程序收到的多部分文件的示例结构:
MIME-Version:1.0
Content-Type:multipart/mixed;
boundary="----=_Part_4_153315749.1440434094461"
------=_Part_4_153315749.1440434094461
Content-Type: application/octet-stream; name=Texture_1.png
Content-ID: response-1
Content-Disposition: attachment; filename=Texture_1.png
‰PNG
"blob data here"
------=_Part_4_153315749.1440434094461
Content-Type: application/octet-stream; name=manifest.xml
Content-ID: response-2
Content-Disposition: attachment; filename=manifest.xml
<?xml version="1.0"?>
<dae_root>blank_3D.dae</dae_root>
------=_Part_4_153315749.1440434094461
Content-Type: application/octet-stream; name=texture_3D.dae
Content-ID: response-3
Content-Disposition: attachment; filename=texture_3D.dae
<xml data here... lots of xml data>
------=_Part_4_153315749.1440434094461
Content-Type: application/octet-stream; name=Texture_0.png
Content-ID: response-4
Content-Disposition: attachment; filename=Texture_0.png
‰PNG
"blob image data"
更新:
通过让供应商拆分文件,我可以将 collada 文件直接加载到查看器中......但我不知道如何将图像连同它一起加载到内存中或以其他方式加载。关于如何包含属于 collada 文件的图像/纹理的任何想法?这是我当前的加载代码:
// instantiate a loader
var loader = new THREE.ColladaLoader();
loader.options.convertUpAxis = true;
loader.load('http://applicationvendorapi.com/dae', function (collada) {
box = collada.scene;
box.traverse(function (child) {
if (child instanceof THREE.SkinnedMesh) {
var animation = new THREE.Animation(child, child.geometry.animation);
animation.play();
}
});
box.scale.x = box.scale.y = box.scale.z = .2;
box.updateMatrix();
init();
animate();
});
纹理的位置往往与 Collada 文件位于同一目录中。这是 Collada 的 XML。问题是我可以通过 Web 服务(例如 /endpoint/texture_0 和 /endpoint/texture_1 )拉入其他图像来引用这些图像
<library_images>
<image id="Texture_0_png">
<init_from>./Texture_0.png</init_from>
</image>
<image id="Texture_1_png">
<init_from>./Texture_1.png</init_from>
</image>
更新 2:
使用@gaitat 的建议,我尝试首先通过 ImageUtils 加载纹理。但是,它永远不会调用纹理的 URL/加载纹理。但是,它仍然会调用对象。这是包装在纹理调用中的当前对象:
// Load texture before loading and initializing 3D object
var texture0 = THREE.ImageUtils.loadTexture('http://vendorwebservice/texture_0', {}, function loaded() {
// Instantiate a Collada loader
var loader = new THREE.ColladaLoader();
loader.options.convertUpAxis = true;
loader.load('http://vendorwebservice/dae', function (collada) {
box = collada.scene;
box.traverse(function (child) {
if (child instanceof THREE.SkinnedMesh) {
var animation = new THREE.Animation(child, child.geometry.animation);
animation.play();
}
});
box.scale.x = box.scale.y = box.scale.z = .2;
box.updateMatrix();
init();
animate();
});
});
这就是有趣的地方。由于我拉入的 DAE 文件具有对其纹理的以下 xml 引用,它实际上也尝试“获取”这些,但不存在具有 .png 扩展名的端点,因此它失败(例如http://vendorwebservice/texture_0.png):
<library_images>
<image id="Texture_0_png">
<init_from>./Texture_0.png</init_from>
</image>
<image id="Texture_1_png">
<init_from>./Texture_1.png</init_from>
</image>
【问题讨论】:
-
评论不用于扩展讨论;这个对话是moved to chat。
标签: javascript three.js