Skip to content

So I decided to try creating a dice cube to help me understand textures in 3D. It was quite a long process, but the result is nice.

I’m just going to describe the process below, and only explain anything I haven’t covered in earlier posts. The full code is at the bottom.

I know how to draw a cube, from the previous post, so this post is all about the texture, adding the dots to the cube.

Creating the texture image

I am only allowed one texture image for the cube mesh, but I need six different faces, so I need to create an image with all six faces next to each other. I decided to stack them on top of each other, so I created a tall blank image to start with.

Then I downloaded a nice texture image from the Internet to use as the dice surface. I drew this to my blank image (remember, setContext lets you draw to an image instead of the screen). Later, when I viewed the result, the texture looked a bit dark, so I came back here, and drew a light blue and fairly transparent (a=85) rectangle over my whole image, which lightened it up. This may sound very professional, but to be honest, it was just a guess that this would work.

Then I needed a lot of spots for my dice. I wanted to make them look 3D, so I drew a black circle and then a whitish ellipse slightly off centre. I didn’t want to have to repeat this for every dot on the dice, so I actually created a special image just for a dot, and when it was done, I drew this dot image on my dice image.

I guessed how to place the dots on each face, and that took quite a lot of code.

Then I had to get my dots onto the cube. This means telling Codea – for each of 36 vertices – which part of the texture image to use. So I modified the cube class from last time, so it could accept a table of x,y vectors, and carefully set each one.

And the result is shown above. Full code here or here.

Where to here from here?

The result is very nice, but is it worth it? What can you do with this?

I had illusions of dropping a 3D dice on a surface, but Codea physics is limited to 2D, and the physics of 3D geometry are terrifying, so no go for anything involving gravity.

What about creating a walk through 3D world? This is also very difficult, because rendering objects in 3D is complex and requires a lot of processing power. There is a nice demo here:

It uses an internal map to figure out where you are and what is around you. Then it calculates the distance to each wall within your sight, and its size and angle of view. The program keeps things fairly simple by using plain wall colours, but if you run it, your frame refresh rate gets low quickly. On my iPad 3, it was down to 20 frames per second, which is jerky.

Other people have done some impressive 3D demos, such as waving grass or fluids. But they are just demos, like my dice.

Now, I can see why spaceships are such a popular choice for 3D games. No gravity, no walls, just things to shoot at.

For us ordinary mortals, it’s very difficult to see how we can do anything useful in 3D. So I think I’ll be sticking to 2D for the foreseeable future. Maybe I’ll learn about vertex shaders at some point, but not just yet.

So I’ll talk about something else next time.

Advertisement

From → 3D

Leave a Comment