This one is definitely in the category “just for the fun of it”: I had recently purchased my first Lego model in two decades and was pleasantly surprised to find that it is as fun and relaxing to play with Lego as it was all those years ago. In particular I was drawn to the models of the Architecture Series which are quite densely build and contain a lot of detail in a small sized model.
While browsing the Lego website, I discovered that they offer a Mac application to construct models called the Lego Digital Designer. But the rendering quality was a bit bland and I started wondering what it would take to import such a model into the Shapeflow 3D / Core SDK.
Things actually turned out to be simpler than I thought. The native format of LDD is just a description of the positions of the pieces and does not contain the geometry itself. Considering the thousands of individual Lego pieces that exist, modeling them myself was out of the question. However, LDD can also export a scene in LDR format which is native to LDraw, a free Lego Editor. LDR is a rather simple text-based format which can contain links to other files as well as a primitive geometry description. So after a couple of hours I got this result running on my iPad Pro:
LDraw Format Details
The official spec for the format can be found on the LDraw.org website. Each line of text is identified by a number which describes the type of instruction:
- Zero: Comment or meta command (such as a separator to indicate the next building step in the instruction or a color definition)
- One: A reference to another LDraw file with a transformation matrix and an optional override material
- Two, three, four: Line, triangle and quad respectively, each containing a flat list of 3D coordinates.
- Five: Optional lines
So there is no topology, no normals, texture coordinates, etc. There is a basic material description as part of the meta command line syntax. The great part about LDraw is that the community has build a huge set of predefined Lego pieces. An end-user’s LDR file mostly contains a set of links to predefined pieces but could also contain links to smaller templates that are re-used multiple times. In the end its all the same because the predefined pieces are themselves defined in LDR format.
Rendering
The lack of topology of course means that one has to calculate normals oneself, merge duplicated vertices (each line/tri/quad has its one copy of vertex positions) and even the face orientation is not consistent. In addition, the coordinate system is a bit unusual in that it has an inverted Y axis. Luckily, all those things can easily be handled in Core SDK and therefore it was simply a matter of writing the LDR file format parser. Also the LDraw predefined pieces use a lot of indirection so to achieve a reasonable render speed, I merged all sub-meshes within a predefined piece into a single mesh.
The shot above uses image-based lighting and a simple cube-map reflection. Adding a bit of reflection does a lot for Lego because its shiny plastic after all. The screenshot below is the default rendering in the Shapeflow 3D iOS viewer using the default reflection cubemap. You can spot the reflective shine in the black pieces.
I’m actually quite happy how it turned out, especially considering the time. I guess it took about 4-5 hours to get the importer working, debug a few special cases (e.g. double back-slashes within relative path links while being on a unix-based system) and the basic material translation. As stated in the beginning, this has mostly been just for fun but drop a comment if you like it.
2 Comments
Bricker
Great. When’s the Windows version coming? LDraw is badly in need of a truly decent viewer application.
alex
Thanks. As written in the post, this was mostly a fun experiment. So nothing is planned but if there is enough interest I might spin of a simple viewer app and put it in the Windows/Apple app store or something…