1

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
}

2 Answers 2

3

See https://github.com/mrdoob/three.js/issues/5483 for an explanation why you need to convert to THREE.Geometry or THREE.BufferGeometry if you want the raw data.


And a simpler way to do this is (from Export ThreeJS Geometry to JSON)

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

or

var rawGeometry = new THREE.Geometry();
rawGeometry.merge( geometry );
var expJson = rawGeometry.toJSON();
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.