2016-07-27 2 views
0

私はgroupChairという名前の空のTHREE.Object3D()を作成しました。そして、3 objファイルをロードし、コールバック関数でgroupChairに追加します。 groupChairをシーンに追加します。できます。なぜこのグループをコピーできないのですか?以下 ロードされた.objモデルを含むグループをクローンする方法は?

はコードです:すべてのオブジェクトがロードされる前に、コード例で

 var groupChair = new THREE.Object3D(); 
     var cloneChair; 
     var cushion; 
     var backrest; 
     var frame; 

     var loader = new THREE.OBJLoader(manager); 

     //load the 1st obj file 
     loader.load('model-stuff/chair/obj/cushion.obj', function(object) { 
      object.traverse(function (child){ 
       if (child instanceof THREE.Mesh) { 
        child.material = new THREE.MeshStandardMaterial({ 
         map: textureColorForCushion, 
         roughness: 3, 
         metalness:0.6, 
         blending: THREE.NormalBlending, 
         shading: THREE.SmoothShading, 
         envMap: textureCube, 
        }); 
        child.receiveShadow = true; 
        child.castShadow = true; 
       } 
      }); 
      cushion = object; 
      groupChair.add(cushion); 
     }, onProgress, onError); 

     //load the 2nd obj file 
     loader.load('model-stuff/chair/obj/backrest.obj', function(object) { 
      object.traverse(function (child){ 
       if (child instanceof THREE.Mesh) { 
        child.material = new THREE.MeshStandardMaterial({ 
         map: textureColorForCushion, 
         roughness: 3, 
         metalness:0.6, 
         blending: THREE.NormalBlending, 
         shading: THREE.SmoothShading, 
         envMap: textureCube, 
        }); 
        child.receiveShadow = true; 
        child.castShadow = true; 
       } 
      }); 
      backrest = object; 
      groupChair.add(backrest); 
     }, onProgress, onError); 

     //load the 3th obj file 
     loader.load('model-stuff/chair/obj/frame.obj', function(object) { 
      object.traverse(function (child){ 
       if (child instanceof THREE.Mesh) { 
        child.material = new THREE.MeshStandardMaterial({ 
         map: textureColorForFrame, 
         roughness: 0.9, 
         metalness:0.4, 
         blending: THREE.NormalBlending, 
         shading: THREE.SmoothShading, 
         envMap: textureCube, 
        }); 
        child.receiveShadow = true; 
        child.castShadow = true; 
       } 
      }); 
      frame = object; 
      groupChair.add(frame); 
     }, onProgress, onError); 

     scene.add(groupChair); 

     //clone groupChair object, but it doesn't work 
     cloneChair = groupChair.clone(); 
     cloneChair.position.set(30,0,0); 
     scene.add(cloneChair); 

答えて

0

、あなたはgroupChairオブジェクトのクローンを作成しています。すべてのオブジェクトがロードされるまで待機するには、THREE.LoadingManagerを使用できます。また、ロードするオブジェクトごとに、別々のローダーを追加する必要があります。

var groupChair = new THREE.Object3D(); 
    var cloneChair; 
    var cushion; 
    var backrest; 
    var frame; 

    var loader1 = new THREE.OBJLoader(manager); 

    //load the 1st obj file 
    loader1.load('model-stuff/chair/obj/cushion.obj', function(object) { 
     object.traverse(function (child){ 
      if (child instanceof THREE.Mesh) { 
       child.material = new THREE.MeshStandardMaterial({ 
        map: textureColorForCushion, 
        roughness: 3, 
        metalness:0.6, 
        blending: THREE.NormalBlending, 
        shading: THREE.SmoothShading, 
        envMap: textureCube, 
       }); 
       child.receiveShadow = true; 
       child.castShadow = true; 
      } 
     }); 
     cushion = object; 
     groupChair.add(cushion); 
    }, onProgress, onError); 

    var loader2 = new THREE.OBJLoader(manager); 
    //load the 2nd obj file 
    loader2.load('model-stuff/chair/obj/backrest.obj', function(object) { 
     object.traverse(function (child){ 
      if (child instanceof THREE.Mesh) { 
       child.material = new THREE.MeshStandardMaterial({ 
        map: textureColorForCushion, 
        roughness: 3, 
        metalness:0.6, 
        blending: THREE.NormalBlending, 
        shading: THREE.SmoothShading, 
        envMap: textureCube, 
       }); 
       child.receiveShadow = true; 
       child.castShadow = true; 
      } 
     }); 
     backrest = object; 
     groupChair.add(backrest); 
    }, onProgress, onError); 

    var loader3 = new THREE.OBJLoader(manager); 
    //load the 3th obj file 
    loader3.load('model-stuff/chair/obj/frame.obj', function(object) { 
     object.traverse(function (child){ 
      if (child instanceof THREE.Mesh) { 
       child.material = new THREE.MeshStandardMaterial({ 
        map: textureColorForFrame, 
        roughness: 0.9, 
        metalness:0.4, 
        blending: THREE.NormalBlending, 
        shading: THREE.SmoothShading, 
        envMap: textureCube, 
       }); 
       child.receiveShadow = true; 
       child.castShadow = true; 
      } 
     }); 
     frame = object; 
     groupChair.add(frame); 
    }, onProgress, onError); 

    scene.add(groupChair); 

    manager.onLoad = function(){ 

     //clone groupChair object, but it doesn't work 
     cloneChair = groupChair.clone(); 
     cloneChair.position.set(30,0,0); 
     scene.add(cloneChair); 

    } 
+0

本当にありがとうございます。できます。 –

関連する問題