2015-05-21 121 views
0

我試圖使用three.js加載一個dae文件,但我無法獲取要顯示的材質。我無法找到任何使用dae文件加載文件本身中的頂點顏色的示例。我可以通過將材質設置爲MeshBasicMaterial並將頂點顏色屬性設置爲頂點顏色來將它加載到threejs.org/editor。我試圖在代碼中複製它,但我不認爲我很懂。這是我的裝載機功能...使用three.js加載頂點顏色的collada文件

loader.load('objects/chair.dae', function (collada) { 
//dummy1.dae 
    var dae = collada.scene; 
    var skin = collada.skins[ 0 ]; 
    console.log(collada.scene); 
    //collada.dae.materials[0] = collada.scene.children[0].children[0].material; 
    //dae.material = collada.scene.children[0].children[0].material; 
    //var material = collada.scene.children[0].children[0].material; 
    var material = new THREE.MeshBasicMaterial({ 
    vertexColors: THREE.VertexColors 
    }); 
    collada.scene.position.set(0,0,0);//x,z,y- if you think in blender  dimensions ;) 
    collada.scene.scale.set(15.0, 15.0, 15.0); 
    var mesh = new THREE.Mesh(collada, material); 
    scene.add(mesh); 
    //scene.add(collada.scene); 
    var axes = new THREE.AxisHelper(50); 
    axes.position = dae.position; 
    scene.add(axes); 
    var gridXZ = new THREE.GridHelper(100, 10); 
    gridXZ.setColors(new THREE.Color(0x8f8f8f), new THREE.Color(0x8f8f8f)); 
    gridXZ.position.set(0,0,0); 
    scene.add(gridXZ); 
}); 

這是產生誤差。

+0

這是我想加載的dae文件... https://www.dropbox.com/s/6ghvsq3uqaehsex/chair.dae?dl=0 –

回答

0

至於代碼,基本上你必須改變Collada對象中網格的材質。就像:

 loader.load(
      // resource URL 
      'models/example.dae', 
      // Function when resource is loaded 
      function (collada) { 
       var dae = collada.scene; 

       // CORRECT FOR IMPORTER 
       // collada object with only vertex color are not correctly loaded 
       dae.traverse(function(child) { 
        if (child instanceof THREE.Mesh) { 
         child.material = new THREE.MeshBasicMaterial({ vertexColors: true }); 
        } 
       }); 

       dae.position.set(0,0,0); 
       dae.scale.set(10, 10, 10); 

       scene.add(dae); 
       animate(); 
      }, 
      // Function called when download progresses 
      function (xhr) { 
       console.log((xhr.loaded/xhr.total * 100) + '% loaded'); 
      } 
     );