1

I am trying to convert .svg file to 3d (.obj file) using javafx.

I can able to convert primitives like Shape - Cylinder, Box etc to Mesh. Is it possible to convert SVGPath to convert to any particular Mesh.

Sharan
  • 15
  • 6

1 Answers1

1

The open source library FXyz has exactly what you are looking for: a SVG3DMesh class that given a 2D SVGPath (or a string with its content) will return a 3D TriangleMesh, extruding the 2D shape to a certain height.

Later on you can export that mesh to a obj file.

This is a code snippet of how you can use it:

SVG3DMesh svg3DMesh = new SVG3DMesh("M40,60 C42,48 44,30 25,32", 10);

SVG3DMesh

You can show the mesh:

svg3DMesh.setDrawMode(DrawMode.LINE);
svg3DMesh.setCullFace(CullFace.NONE);

or show a solid 3D object with the color you want:

svg3DMesh.setTextureModeNone(Color.RED);

For exporting the mesh to obj:

OBJWriter writer=new OBJWriter((TriangleMesh) ((TexturedMesh) svg3DMesh.getMeshFromLetter("")).getMesh(), "svg");
writer.setMaterialColor(Color.RED);
writer.exportMesh();

it will generate svg.obj and svg.mtl.

jeyko
  • 3,093
  • 3
  • 13
  • 16
José Pereda
  • 44,311
  • 7
  • 104
  • 132
  • Pereda, Thank you so much for your comments. This will really help me to understand the process behind the conversion. – Sharan Nov 09 '16 at 17:22
  • @José Is it also possible to create a flat (non-extruded) mesh that way? – mipa Nov 10 '16 at 09:22
  • @mipa Yes, indeed, that's the first part of how the 3D mesh is generated: The 2D closed path uses Poly2Tri to generate a 2D mesh. You'll need to add some API to get that mesh out of SVG3DMesh... – José Pereda Nov 10 '16 at 09:27
  • @José Wouldn't that be a reasonable default behaviour for the cases where you do not explicitly specify a height or, if that is not possible due to backwards compatibility issues, if you specify a negative height? – mipa Nov 10 '16 at 10:17
  • @mipa it is a 3D library... so it generates full 3D objects. But it can be extended to return 2D meshes as well, it just needs the API to get them once they are generated. – José Pereda Nov 10 '16 at 10:31