arpo - 8 months ago 55

Javascript Question

I modify a plane geometry's vertices to create new geometry.

But when I export the geometry it's still a plane geometry and my modified vertices is not included.

How do I export my new geometry as JSON including every changes?

Here's my code:

`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);

My exported data looks like this:

`{`

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

}

Answer Source

This is the solution I use. If someone has a better one please post it. But this works for me and is quite neat.

I just add a new geometry object and copies the verticies and faces from the plane geometry.

```
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);
```