This article is about animating a 3D object. For making a 3D simulation, see How to Make a Three-Dimensional Project. For making a 3D maze, see Raycaster.

This is a tutorial on animating a three-dimensional object using the "stack method".

The Stack Method

A diagram of stacking layers on top of each other to make a 3D object.
A diagram of stacking layers on top of each other to make a 3D object.

The "stack method" of creating a 3D object involves slicing the object up into 2D layers. Draw each layer in a separate costume of the sprite being used for the 3D object. Then, use code to stack the layers on top of each other and to rotate the sprite in 3D. In theory, you could make any 3D object you wanted like this, but in this tutorial, for simplicity's sake, a cube will be used.

Creating the Costumes

A cube has three layers. The top is a square with an outline, the middle layers are all squares without outlines but with dots on the corners, and the bottom layer is a square with an outline (see the diagram on the right). All the squares should be the same size. Name the top layer "top", the middle layer "middle", and the bottom layer "bottom".

Writing the Code

To draw the layers on top of each other, stamp each layer on the stage with the stamp block, moving the sprite upwards each time. All the layers should be stamped very quickly, and the best way to do this in Scratch is to use a custom block with run without screen refresh selected.

Setup

The dialog box for creating a custom block.
The dialog box for creating a custom block.

Create a custom block. You can name it whatever you want. In this tutorial, it will be named 3D ::custom.

Note Note: Make sure "run without screen refresh" is selected.

Then add this code to your project to make the custom block run over and over again until the project stops.

when gf clicked
forever
turn cw (1) degrees //Makes the sprite slowly rotate in a 3D circle.
3D ::custom
end

Resetting

Add an erase all block under the custom block definition to clear the canvas every time the 3D object is drawn and a go to x: (0) y: (0) block below that to reset the sprite's position.

The Layers

In this tutorial, layers will be drawn from the bottom of the object up, so the code will start with the bottom layer. Build this code:

define 3D
erase all
go to x: (0) y: (0)
switch costume to (bottom v)//switch to the bottom layer
stamp //stamp the layer
change y by (1) //move up to draw the next layer

This code will draw the top layer. Next, draw the middle:

define 3D
erase all
go to x: (0) y: (0)
switch costume to (bottom v)
stamp
change y by (1)
switch costume to (middle v) //switch to the middle layer
repeat (20) //the middle layer should be drawn more than once
stamp //stamp the layer
change y by (1) //move up
end

Then, the top layer:

define 3D
erase all
go to x: (0) y: (0)
switch costume to (bottom v)
stamp
change y by (1)
switch costume to (middle v)
repeat (20)
stamp
change y by (1)
end
switch costume to (top v) //the top layer
stamp //stamp it
change y by (1) //move up

If you run the code, it should show a slowly rotating 3D cube. You can add as many layers as you want to this. Use the repeat [] block if the layer needs to be drawn more than once.

Examples

Note that one of these examples uses the old method of repeating the same code over and over again instead of using the repeat [] block and not using custom blocks. The newer method is easier to code, but the results are the same.
The "3D Train" example creates clones instead of stamping. This is yet another method of accomplishing the same thing.