At the beginning of this summer i had enough free time to work on a little personal project. Since i’m already forgetting everything i know about it, i thought that writing a post could be a good idea to refresh my memory.

So, before it’s too late: introducing Legolize!

Legolize! is a tool that automatically transforms an image into a group of 3D Lego bricks. All the output bricks have real dimensions and are correctly snapped to each other; that means that you can process an image, buy the bricks online and assemble the real structure by yourself! ( at least in theory :D)



Here are a sample input and its output, rendered in Vray (click for higher res!) :



In more detail:

the process is quite straightforward.

These are the basic 3d blocks. In a moment of sheer inspiration i called them A B C D and E, left to right.

First of all, the image is loaded and resized to a given size. After that, every pixel’s color is compared with a user given palette, from which the most similar color is selected. At this point, we have a matrix of small “A” bricks.

This matrix is then analyzed in multiple passes, checking if there are contiguous cells of the same color that can be swapped with a bigger brick. For example, 3 vertical A bricks can become a C brick; 2 horizontal C bricks can become a D brick, and so on.

After this process is complete we are basically done; the main draw() loop reads the final matrix, draws the bricks, and that’s it.

This is the very basic process. In reality, the code has a couple of fancier features. For example:

a background layer of “interlocked” bricks is automatically added (i think it looks cool!)





an additional flag triggers a fancier resizing code, after which every pixel is perfectly matched to 1 brick of type C. In this case, the basic unit of the grid becomes bigger, giving a more pleasant, yet less detailed output. If we set resolution and color palette precisely, this mode works great with 8 bit images





another interesting feature is the possibility of loading a grayscale image to set different depths for different areas of the image:

(click for higher res! base image by Paul Robertson)

This whole process, of course, can be done on multiple images. Ta da, we have animations!

(click here if you can’t see the embedded video!)

And now, a little bit of nerdtalk. The tool is written in Processing and consecutive frames are loaded and processed one after each other. The tool considers the alpha channel of .pngs and .tiffs.

The computation, on my i7, is acceptably fast.

I didn’t try, but I suspect that with a little bit of profiling and some low resolution inputs it could almost run in realtime. Drawing the 3d bricks is by far the heavier task, so i inserted a flag that forces the use of very low resolution meshes for the bricks.

During the draw() loop the tool exports to a series of .txt files the computed matrix of every frame (with coordinates, color codes and brick types)..

Since i wanted to render the output, i also wrote a 3dsmax interpreter in Maxscript. Of course, this phase is inexplicably slower than the whole Processing phase. F you, maxscript, you and your atavic slowness. Never trust a non case-sensitive language.

A final thought: even if the project is very simple, the whole thing is as a horrendous blob in an uncommented, pre alpha state. Almost every variable is defined by hardcoding and… well, it’s nothing more than a personal experiment.



For now, hope you enjoyed anyway! :)