2015-11-05 51 views
0

如何加載一次JSON模型並將其多次添加到場景中?Three.js - 多次調用JSON模型

目前,我稱這個模型爲兩次。也許有人可以給我一個我想要完成的工作的例子。

var loader = new THREE.JSONLoader(); 

    loader.load("models/model1.js", meshloader1("models/model1.js")); 
    loader.load("models/model1.js", meshloader2("models/model1.js")); 

     function meshloader1(fileName){ 
      return function(geometry){ 
       mesh1 = new THREE.Mesh(geometry, material); 
       mesh1 .position.set(0, 0, 0); 
       mesh1 .scale.set(1, 1, 1); 
       scene.add(mesh1); 
      } 
     } 

     function meshloader2(fileName){ 
      return function(geometry){ 
       mesh2 = new THREE.Mesh(geometry, material); 
       mesh2 .position.set(0, 0, 0); 
       mesh2 .scale.set(1, 1, 1); 
       scene.add(mesh2); 
      } 
     } 

回答

1

你可以定義一個geometry對象爲您的裝載機以外的這樣的:

var loader = new THREE.JSONLoader(); 
var genericGeometry; 

loader.load("models/model1.js", meshloader("models/model1.js")); 

function meshloader(fileName){ 
    return function(geometry){ 
     genericGeometry = geometry; 
     createMeshs(); 
    } 
} 

// Since `loader.load()` method is async, 
// you need to wrap this inside a function and call it when the geometry is actually loaded 
function createMeshs(){ 
    var mesh1 = new THREE.Mesh(genericGeometry, material); 
    mesh1.position.set(0, 0, 0); 
    mesh1.scale.set(1, 1, 1); 
    scene.add(mesh1); 

    var mesh2 = new THREE.Mesh(genericGeometry, material); 
    mesh2.position.set(1, 1, 0); 
    mesh2.scale.set(1, 1, 1); 
    scene.add(mesh2); 
}