2016-02-19 34 views
0

在使用JSON加載器加載模型時,我在Chrome和Safari中看到了不同的結果。在Safari中,以下代碼一次加載1000個模型,而在Chrome中它們在大約20秒內逐一加載。我想讓他們在Chrome中同時出現,有什麼想法?在ThreeJS JSON中加載模型

// Add Models 
      for(var i = 0; i < noOfMeshesToBuild;i++){ 

       var object; 
       var loaderJ = new THREE.JSONLoader(); 
       loaderJ.load(meshNames[Math.floor(Math.random() * meshNames.length)], function (geometry, materials) { 
        // Workaround to make faces flat shaded with lambert material 
        geometry.computeFaceNormals(); 
        for (var i = 0; i < geometry.faces.length; i ++) { 
         geometry.faces[ i ].vertexNormals = []; 
        } 
        geometry = new THREE.BufferGeometry().fromGeometry(geometry); 

        // Add objects to scene, array 
        object = new THREE.Mesh(geometry, new THREE.MeshLambertMaterial()); // this makes a new material for each object 
        theLoadedMeshes.push(object); 
        scene.add(object); 

        // Randomise stuff 
        RandomisePosition(object); 
        RandomiseRotation(object); 
        RandomiseScale(object); 
        RandomiseColors(object); 

        // Add edges 
        edge = new THREE.EdgesHelper(object, edgeColor, 5.0); 
        edge.material.linewidth = 1; 
        scene.add(edge); 

        //Add direction, distance to translate the objects 
        var transAmount = 1.0/(object.scale.x + Math.random()) * masterSpeed; 
        var posMin = [-1,1]; 
        var direction = Math.floor(Math.random()+ 0.5); 
        var posMinChoice = posMin[Math.floor(Math.random() * posMin.length)]; 
        transArray.push(transAmount); 
        directionArray.push(direction); 
        posMinArray.push(posMinChoice); 

       }); 
      } 
+0

如何在瀏覽器控制檯查看網絡選項卡?您可以通過返回JSON中的多個模型來避免與同一主機並行執行一千個請求嗎? – Kenney

+0

謝謝你指點我正確的方向! – wxxhrt

回答

1

我只有3種型號所以只加載它們的3然後克隆工作一種享受,感謝我指出了正確的方向肯尼

// Load up Models 
      for(var n = 0; n < meshNames.length; n++){ 
       var loaderJ = new THREE.JSONLoader(); 
       loaderJ.load(meshNames[n], function (geometry, materials) { 
        // Workaround to make faces flat shaded with lambert material 
        geometry.computeFaceNormals(); 
        for (var i = 0; i < geometry.faces.length; i ++) { 
         geometry.faces[ i ].vertexNormals = []; 
        } 
        geometry = new THREE.BufferGeometry().fromGeometry(geometry); 

        // Add objects to jsonObject array 
        var object = new THREE.Mesh(geometry, new THREE.MeshLambertMaterial()); // this makes a new material for each object 
        jsonObjects.push(object); 

        // Once all objects have been added 
        if (jsonObjects.length == meshNames.length){ 
         Clone(); 
        } 
       }); 
      } 

      // Clone Models 
      function Clone(){ 
       for(var i = 0; i < noOfMeshesToBuild;i++){ 

        var randomObject = jsonObjects[Math.floor(Math.random() * jsonObjects.length)]; 
        object = randomObject.clone(); 
        // this makes each object have its own material 
        object.material = new THREE.MeshLambertMaterial(); 
        scene.add(object); 
        theLoadedMeshes.push(object); 

        RandomisePosition(object); 
        RandomiseRotation(object); 
        RandomiseScale(object); 
        RandomiseColors(object); 

        // Add edges 
        edge = new THREE.EdgesHelper(object, edgeColor, 5.0); 
        edge.material.linewidth = 1; 
        scene.add(edge); 

        //Add direction, distance to translate the objects 
        var transAmount = 1.0/(object.scale.x + Math.random()) * masterSpeed; 
        var posMin = [-1,1]; 
        var direction = Math.floor(Math.random()+ 0.5); 
        var posMinChoice = posMin[Math.floor(Math.random() * posMin.length)]; 
        transArray.push(transAmount); 
        directionArray.push(direction); 
        posMinArray.push(posMinChoice); 

       } 
      } 
     } 
+0

不錯的解決方案。我知道1000有什麼可疑的,但我沒有想到這一點:-) – Kenney