3

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

Huskie69
  • 795
  • 3
  • 11
  • 31
  • 1
    the argument shown in you polyhedron method is json, but I didn't understand the question as the example already has already been converted. – digital-pollution Oct 29 '18 at 16:35
  • Thanks for pointing that out digital-pollution - that could explain the downvote! I've tried manipulating the openjscad output in a way that can be parsed into the three.js json loader but it just fails - presumably the json loader takes specific parameters that aren't present in the jscad output. – Huskie69 Oct 30 '18 at 09:17
  • the only other thing I can think of is that for the json to validate the keys need double "quotes" around them (JSON expects a string, javascript object is not so strict), but it should be fine as a JS object shown in the example. – digital-pollution Oct 30 '18 at 09:41

0 Answers0