2015-09-25 26 views
0

我不知道我在做什麼錯。我有多個網格,我試圖合併成一個網格,以便我可以保存繪製調用。 我的每個網格都有獨特的材質。在這個例子中,它只是具有不同的顏色,但實際上它們將具有映射的獨特紋理。ThreeJS合併多個網格與獨特的材料

這是我的代碼:

 materials = []; 
     blocks = []; 
     var tempMat; 
     var tempCube; 
     var tempGeo; 
     var tempvec; 


     // block 1 
     tempMat = new THREE.MeshLambertMaterial({ color: '0x0000ff' }); 
     materials.push(tempMat); 
     tempGeo = new THREE.CubeGeometry(1, 1, 1); 
     for (var ix=0; ix<tempGeo.faces.length; ix++) { 
       tempGeo.faces[ix].materialIndex = 0; 
     } 
     tempCube = new THREE.Mesh(tempGeo, tempMat); 
     tempCube.position.set(0, 3, -6); 
     blocks.push(tempCube); 


     // block 2 
     tempMat = new THREE.MeshLambertMaterial({ color: '0x00ff00' }); 
     materials.push(tempMat); 
     tempGeo = new THREE.CubeGeometry(1, 1, 1); 
     for (var ix=0; ix<tempGeo.faces.length; ix++) { 
       tempGeo.faces[ix].materialIndex = 1; 
     } 
     tempCube = new THREE.Mesh(tempGeo, tempMat); 
     tempCube.position.set(1, 3, -6); 
     blocks.push(tempCube); 


     // Merging them all into one 
     var geo = new THREE.Geometry(); 
     for (var i=0; i<blocks.length; i++) { 
      blocks[i].updateMatrix(); 
      geo.merge(blocks[i].geometry, blocks[i].matrix, i); 
     } 
     var newmesh = new THREE.Mesh(geo, new THREE.MeshFaceMaterial(materials)); 
     scene.add(newmesh); 

基本上,這給了我一個錯誤,指出: 遺漏的類型錯誤:無法讀取的不確定 財產「可見的」我的每一個功能的渲染時間被調用。

我哪裏錯了?

+0

你可以自己調試它。 (1)'Geometry.merge()'的第三個參數是一個偏移量,在你的情況下應該爲零。 (2)指定不帶引號的十六進制顏色:'THREE.MeshLambertMaterial({color:0x00ff00})' - 但不要做你正在做的事情。相反,請參閱答案。 – WestLangley

回答

2

您正在將幾何合併爲一個,並使用MeshFaceMaterial(在r.72中重命名爲MultiMaterial)。

合併具有不同材質索引的幾何圖形沒有任何意義。

WebGLRenderer需要根據材質對幾何進行分段來呈現它。

作爲一個經驗法則,只有合併幾何時,如果它們將使用單一材質進行渲染。

three.js r.72

+0

因此,如果我有10,000個立方體,並且每個立方體都有不同的紋理,那麼必須有10,000個繪製調用? – Captainlonate

+0

看,這就是我想要做的:http://www.sidequestsapps.com/sandbox_files/blocks/newgrid.html我需要能夠擁有超過100,000個立方體。有人建議分組分組。所以我打算用這種方法組合10x10x10塊組。但這聽起來像是你說我不能。那麼我有什麼選擇!?如果我不能有超過10,000個塊,並且沒有停止到3fps,我怎樣才能創建一個完整的Minecraft世界。 – Captainlonate

+0

對不起,我無法爲你設計你的代碼。我建議你看看three.js minecraft的兩個例子,每個例子都是用一次繪製調用來實現的。 – WestLangley