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.
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.
Example result.

One way of displaying a three-dimensional object in Scratch is by slicing it into many 2D images at a series of depths. The images are imported as costumes and displayed as many sprites, clones, or stamps, each slightly offset on the screen to give the appearance of three dimensions.

In the simplest implementation, objects displayed with this method are using an oblique projection.

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 draw stack ::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.
draw stack ::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 draw stack
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 draw stack
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 draw stack
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

See Also

Cookies help us deliver our services. By using our services, you agree to our use of cookies.