【问题标题】:Three JS OBJloader - obj not importing properly三个 JS OBJloader - obj 没有正确导入
【发布时间】:2019-01-28 15:45:40
【问题描述】:

我正在尝试使用 OBJLoader 导入一个 obj,但它没有正确导入

obj 是这样的—— 对象img

它正在导入这个 - 三个js中的obj

发生的情况是整个 obj 没有很好地导入。

我该怎么办?

我正在做的代码是

var objLoader = new THREE.OBJLoader();
        var mtlLoader = new THREE.MTLLoader();
            mtlLoader.setTexturePath("obj2/");
            mtlLoader.setPath(  "obj2/"  );
            mtlLoader.load( "Mules/Base_10.mtl", function( materials ) {
                materials.preload();
                objLoader.setMaterials( materials );
                objLoader.load( 'obj2/Mules/Base_10.obj', function ( object ) {

                        object.traverse( function ( child )
                        {
                            if ( child instanceof THREE.Mesh )
                            {
                                meshes.push(child);
                            }
                        });
                        var object = meshes[meshes.length-1];
                        object.position.y = -0.05;
                        object.position.x = 0;
                        object.position.z = 0;

                        object.name = "salto";
                        scene.add(object);
                    }, onProgress, onError );
            }); 

谢谢。

【问题讨论】:

  • 可以看到图一和图二的区别。在图像中,我正在预览 obj 文件,在图像二中它在浏览器中。抱歉英语不好,谢谢你
  • 查看您的代码,当加载一个 OBJ 文件时,您会从中收集所有 THREE.Mesh 对象,然后您只将最后一个网格添加到场景中。这是故意的吗?
  • 好吧,我从 link 改编了这段代码。我评论了那部分,现在看起来不错:)谢谢:D

标签: javascript three.js objloader mtl-file


【解决方案1】:

问题是:

                object.traverse(...);
                var object = meshes[meshes.length-1]; //<-- you overriding the object, with the last mesh. 
                object.position.y = -0.05;
                object.position.x = 0;
                object.position.z = 0;

                object.name = "salto";
                scene.add(object); //<-- than you add the object to your scene.

不要覆盖对象。此外,您不需要遍历对象,因为您会将整个对象添加到场景中。无论如何你对你的网格什么都不做:)

所以试试这个:

                object.position.y = -0.05;
                object.position.x = 0;
                object.position.z = 0;

                object.name = "salto";
                scene.add(object);

【讨论】:

  • 已解决。谢谢你:)
猜你喜欢
  • 2023-03-27
  • 1970-01-01
  • 1970-01-01
  • 2023-03-14
  • 1970-01-01
  • 1970-01-01
  • 2017-02-12
  • 2019-03-14
  • 2021-03-08
相关资源
最近更新 更多