2016-01-20 44 views
2

我想要使用three.js的STLLoader函數加載一個心臟模型的橫截面。我目前正在嘗試使用ThreeCSG包裝器作爲csg.js庫,與this stack overflow post中的相同。使用three.js STL對象的橫截面

這裏是我的CSG減法

function modelLoadedCallBack(geometry) { 

    material = new THREE.MeshPhongMaterial({ color: model.color }); 
    mesh = new THREE.Mesh(geometry, material); 
    mesh.rotation.set(model.rotationX, model.rotationY, model.rotationZ); 
    mesh.scale.set(model.scale, model.scale, model.scale); 
    var originalBSP = new ThreeBSP(mesh); 

    var xSectionBSP = new ThreeBSP(xSection); 
    var subtractedBSP = originalBSP.subtract(xSectionBSP); 

    var result = subtractedBSP.toMesh(material); 
    result.geometry.computeVertexNormals(); 

    scene.add(result); 

}; 

代碼我加載STL模型,然後在裝載機的回調函數,我嘗試減去網格。我得到的錯誤是在ThreeCSG包裝文件的第34行,說

ThreeCSG.js:34 Uncaught TypeError: Cannot read property 'length' of undefined

我猜這是因爲(a)我不使用ThreeCSG proprerly,(B)我需要做的減去代碼中的其他位置,或者(c)不支持STL格式模型。

在任何情況下,我完全難住,並會感謝有人使用three.js更有經驗的人的建議。

+0

我在腳本中有同樣的錯誤。我想這裏是對我們的問題unswer https://stackoverflow.com/questions/20602045/csg-operation-with-stlloader/20724436#20724436 – user2075609

+0

可能會使用obj loader在我的情況 – user2075609

+0

由於我只需要橫斷面在我寫了我自己的着色器程序。在片段着色器中,我只是檢查給定的頂點是否超出了切割平面,如果是,則丟棄該頂點。 if(view == 1 && vPosition.z> 0.0)discard; - 視圖描述選定的切割平面。 [Here](http://gamedevelopment.tutsplus.com/tutorials/a-beginners-guide-to-coding-graphics-shaders--cms-23313)是着色器教程。 –

回答

0

看起來正確。如果STL幾何體爲空或者您忘記初始化xSection,則會發生此錯誤。

+0

我跨過調試器,發現STL網格的幾何體沒有面部區域。在三個CSG中,調用'geometry.faces.length',這就是拋出錯誤的原因。是否有另一種方法來截取橫截面,或者有什麼我可以對STL幾何進行修復的方法? –

+0

更新:我已經嘗試將bufferGeometry轉換爲常規幾何體,但這會顯着減慢加載時間,並且在嘗試將網格轉換爲ThreeBSP對象時也會使網頁崩潰。 –

+0

ThreeCSG不支持BufferGeometry。如果它在幾何體上崩潰,那聽起來像是ThreeCSG或csg.js中的一個錯誤。注意:那些不被認爲是穩定的庫。 – kintel