简体   繁体   English

如何在three.js中加载3D .obj模型?

[英]How to load 3D .obj model in three.js?

I am the three.js beginner. 我是three.js初学者。 I created 3D obj model file in three.js. 我在three.js中创建了3D obj模型文件。 How to the object(3D .obj) return to another function? 如何将对象(3D .obj)返回到另一个函数? and How to original colour visible .obj 3D? 以及如何使原始颜色可见的.obj 3D?

Javascript Java脚本

this.draw3DObj = function (startX, startY, endX, endY, objType) {
    switch (objType) {
     case "window":
      var loader = new THREE.OBJLoader();
      var material = new THREE.MeshNormalMaterial({
       color: 0xbcb9b1,
       side: THREE.DoubleSide
      });
      loader.load('model/window.obj', function(object) {
        object.traverse(function(child) {

         if (child instanceof THREE.Mesh) {
          child.material = material;
         }

        });
        scene1.add(object);
        object.position.x = width;
        object.position.y = depth;
        object.position.z = height;
       },

       function(xhr) {
        console.log((xhr.loaded / xhr.total * 100) + '% loaded');
       },

       function(error) {
        console.log('An error happened');
       }
      );
      break;
    }
    return object; // How to return?
};

You cannot directly return the object, because the loading as asynchronous. 您不能直接返回对象,因为加载是异步的。 The first argument passed to loader.load is a callback that gets called when the object is loaded. 传递给loader.load的第一个参数是在加载对象时调用的回调。 At the end of that function you can call another function and pass the object. 在该函数的末尾,您可以调用另一个函数并传递该对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM