Tuesday, 30 October 2018

Converting JSCAD / JS output to JSON (for three.js)

I'm looking for a way to convert the native output (parametric CAD?) of OpenJSCAD (jscad / js file) i.e.

function model() {
return polyhedron({points:[
[41.4780, -44.9832, 0.0000],
[41.8432, -43.0168, 0.0000],
[41.4780, -44.9832, 1.0579],
[41.8432, -43.0168, 1.0579],
...........................
[-41.7700, 35.8856, 1.2659]
],
triangles:[
[0, 2, 3],
[3, 1, 0],
[2, 6, 3],
[3, 6, 7],
[3, 7, 5],
..........
[298, 296, 297]
]});
}

function main() {
return model();
}

into json which can then be loaded into a three.js scene using the json object loader.

Is this possible?

EDIT: As digitial-pollution pointed out, this output is already a JSON string - and points and triangles are in seperate arrays.

The three.js json loader appears to accept json files that have been formatted with "Vertices" and "Faces" in seperate arrays. I've tried tweaking the output to match a working example - https://threejs.org/examples/webgl_loader_json_claraio.html replacing the values in the points array with "Vertices" and the faces with values in "triangles", also stripping out the brackets where neccessary - the three.js loader still fails to load the object.

Here's an example openjscad JS model: https://pastebin.com/BuSxNgg2

And here's an example working three.js JSON model: https://threejs.org/examples/models/json/teapot-claraio.json



from Converting JSCAD / JS output to JSON (for three.js)

No comments:

Post a Comment