2016-11-23 60 views
0

我修改平面幾何體的頂點以創建新幾何體。 但是,當我輸出幾何圖形時,它仍然是一個平面幾何圖形,我的修改過的頂點不包含在內。使用Three.js將修改後的平面幾何體導出爲JSON

如何將我的新幾何體導出爲包含每個更改的JSON?

這裏是我的代碼:

var seg = 5; 
var dim = 70; 
var zScale = 20; 
var geometry = new THREE.PlaneGeometry(dim, dim, seg, seg); 
var index = 0; 

for (var gridY = 0; gridY < seg; gridY++) { 
    for (var gridX = 0; gridX < seg; gridX++) { 
     var z = Math.random() * zScale; 
     geometry.vertices[index].z = z; 
     index++; 
    } 
} 

geometry.elementsNeedUpdate = true; 
geometry.verticesNeedUpdate = true; 
var expJson = geometry.toJSON(); 
console.log(expJson); 

我導出的數據是這樣的:

{ 
    height: 70 
    heightSegments: 5 
    metadata: { 
     generator: "Geometry.toJSON" 
     type: "PlaneGeometry" 
    }, 
    version: 4.4 
    type: "PlaneGeometry" 
    uuid: "5F071B03-15EA-43CE-B4B3-4944E13D781C" 
    width: 70 
    widthSegments: 5 
} 

回答

2

爲什麼你需要,如果你想將原始數據轉換爲THREE.GeometryTHREE.BufferGeometry的說明,請參見https://github.com/mrdoob/three.js/issues/5483


和更簡單的方式來做到這一點(Export ThreeJS Geometry to JSON

var rawGeometry = new THREE.BufferGeometry().fromGeometry(geometry); 
var expJson = rawGeometry.toJSON(); 

var rawGeometry = new THREE.Geometry(); 
rawGeometry.merge(geometry); 
var expJson = rawGeometry.toJSON(); 
0

這是我使用的解決方案。如果有人有更好的一個請發佈。但這對我很有用,而且很整潔。

我只是添加一個新的幾何對象,並從平面幾何體複製verticies和麪。

var seg = 5; 
var dim = 70; 
var zScale = 20; 
var geometry = new THREE.PlaneGeometry(dim, dim, seg, seg); 
var index = 0; 

for (var gridY = 0; gridY < seg; gridY++) { 
    for (var gridX = 0; gridX < seg; gridX++) { 
     var z = Math.random() * zScale; 
     geometry.vertices[index].z = z; 
     index++; 
    } 
} 

geometry.elementsNeedUpdate = true; 
geometry.verticesNeedUpdate = true; 

// The fix start --- 
var neoGeo = new THREE.Geometry(); 
neoGeo.vertices = geometry.vertices; 
neoGeo.faces = geometry.faces; 
neoGeo.faceVertexUvs = geometry.faceVertexUvs; 
// The fix end --- 

var expJson = neoGeo.toJSON(); //Export json from the new geometry object 
console.log(expJson); 
相關問題