Learn creative coding writing simple programs

151. Convert an image into a 3D grid of boxes

This episode is... long! I hope you can stay awake for 18 minutes :)

In this episode I show you how you can create a grid of 3D cubes based on an input image. We use the brightness of each pixel in the image to set the size of each 3D cube in our grid.

scale() is used to scale the scene up. The default is scale(1). If you want to make everything half the size, use scale(0.5). And to double the size use scale(2).

I talk about rotating the camera, but what I meant was rotating our scene. It is indeed possible to rotate the camera, but we don't do that in this episode. To rotate the scene we just include calls to rotateX() and rotateY(), using the mouse coordinates as input. So when we move the mouse, everything spins around. You could use frameCount or millis() instead of mouseX or mouseY, if you wanted to have an automatic rotation.

lights() is used to enable the default 3D lights in Processing. Nothing fancy, but at least we see something. There are other kinds of lights, you can find them in the reference or in future episodes.

An idea to make the result in this episode more interesting: use not only brightness() but also hue() and saturation(). And map those to different properties of the box. You could for instance specify width, height and depth of the box (instead of making it a perfect cube). Or you could move the boxes up in the air depending on brightness, for example. And then rotate depending on hue. And on top of that, translate them again after rotating. That's what I did to generate this image: http://hamoid.tumblr.com/post/108687502754/

Ok, in the next episode I'll show you how to export your 3D object as an .obj file, and after that how to load an .obj file into Blender to render your object as an image with realistic lights and shadows!

Tags: 3D, grid, image, box, cube

Code editor

You can make changes to the code below. Then

Questions and comments

Try to stay close to the topic of this episode. Use the Processing forums for help with unrelated Processing projects (or hire me for help ;-)

To indicate that a word in your comment is code, use the `backtick`. Example
Do `float` and `int` smell similar?
To highlight code blocks, surround it with ``` code-fences ``` like this:
``` void setup() { size(600, 600); } ```