POVRay img2mesh

Converting greyscale PNG into POV, OBJ and STL 3D mesh

Pretty simple (well, it was initially) program that reads PNG image, and writes 3D mesh in POVRay format, using pixel X and Y coordinates for «east» and «south» position of 3D triangle mesh nodes, and, most important, pixel brightness as Z, that is, height of the node, thus converting image into sort of 3D landscape. After rendering POV scene thus obtained with POVRay (which is not just excellent, but also completely free 3D renderer) resulting image is obtained.

Source image (actual size) and resultng 3D object rendering (default texture). Due to unusual mesh building algorithm img2mesh provides smooth fine surface from just 64x64 px image.

E. g., example image below was started as simple black and white drawing, converted to PNG, then some Gaussian Blur and maps added, and PNG fed to img2mesh.

Surely POVRay can import PNG and use them as «heightfields» directly, but img2mesh uses rather unusual «pyramid» conversion algorithm, providing extra quality for small heightfields.

img2mesh is equipped with easy to use GUI for selecting input and output files.

Default export to POV file suggests camera autofocus on object, object scaling to fit 1, 1, 1 cube, and suitable scene structuring for easy editing later.

Update: In version 2.9, beside improvement of POVRay output, Autodesk DXF export added to previously provided Wavefront OBJ and stereolithography STL export.

Preview

Screenshot of Windows Explorer window with test source PNG map and resulting output files preview side by side.

There is a substantial difference between POV, OBJ, DXF and STL output:

Update: In version 2.8.2 mapping added. Maps are transfer functions z value is passed through. Result is similar to Photoshop or GIMP "Curves" applied to source heightfield PNG, except for that with img2mesh map is nondestructively applied to mesh within POVRay, so you can change mesh rendering without changing the mesh itself.

Since version 2.8.2.5, all predefined maps were replaced by generalized linear spline interpolation. Descriptive part in the beginning of POV file generated look like this:

    #declare Curve = function {
        spline { linear_spline
            0.0,   <0.0,   0>
            0.25,  <0.25,  0>
            0.5,   <0.5,   0>
            0.75,  <0.75,  0>
            1.0,   <1.0,   0>}
        }
    #declare map = function(c) {Curve(c).u}

First column contain input values, second column contain corresponding output values (third column currently is not used for anything good, just leave it alone). By default, input=output, that is, map does not change anything. However, you can edit it.

Left to right: source image, rendered result (default map), rendered result (map changed from 0.25, <0.25, 0> 0.75, <0.75, 0> to 0.75, <0.25, 0> 0.75, <0.25, 0>, i.e. the area between 1/4 and 3/4 of range was inverted (negative)).

Note that POVRay does not pay attention to the order of control points appearance, so you can add midpoints after endpoints in no particular order. Just remember to have endpoints 0 and 1 defined, since POVRay cannot use undefined functions; everything between 0 and 1 is up to you to edit. Beside that, you can change linear spline to other splines supported by POVRay. Finally, nothing can stop you from writing your own non-spline maps. For example, #declare map = function(c) {int(4*c)/4} will give you function similar to Photoshop "Posterize" with 4 colors, while #declare map = function(c) {4*c-int(4*c)} is 4-teeth saw function.

For Windows users, compiled Windows 64-bit exe file is available for download in Release section of GitHub. For other platforms, use Python sources - program is rather self-contained, you will find the only non-standard import (PyPNG) enclosed to download.


Step forward to img2mesh at GitHub for downloads.


or Move back to main page.