3D - A Cube with Holes (Difficulty: 2)
-
Prerequisite
Key Topics Covered
- Initializing 3D scenes
- Using variables
- Using for loops
- Using boxes
- Parent-children relationship
- Naming objects
- Speeds of objects
- Export object as a GLB file
- Community models
Introduction
In this tutorial, we’ll build a cube with holes, and we can control where to put these holes using variables. Here is a preview:
Step 1 - Remix and Review
Please open this project below in a new browser tab, and remix it.
https://play.creaticode.com/projects/0068cbafa5ad0c465f966315
This project template is based on the Rubik’s Cube tutorial. It creates a matrix of boxes of 5 layers, 5 rows and 5 columns.
Since all the boxes are the same, it looks like one big cube. Also, the “add cubes” block is set to run without refresh, which makes it much faster.
Step 2 - A gap in the middle
Now we can start to make some holes in the cube. The key idea is to not add some boxes when some conditions are true.
To start with, let’s add an “if-else” block, and set its condition to be “x = 0”. Then we can put the blocks for adding boxes into the “else” branch. This way, whenever the variable x is 0, we will do nothing, which will leave a big gap in the center of the cube.
Step 3 - A Tunnel
Now let’s refine our condition so we make a tunnel instead of a gap.
Specifically, we will check if x and y are both 0, and only in such cases do we skip adding the boxes. It turns out only 5 boxes are skipped in this new program.
Step 4 - A Second Tunnel
Next, let’s add another tunnel from the front to the back. This time we would need to use a new condition: both x and z variables have to be 0. It is much easier to add another “if-else” block for it, so we would skip adding boxes if either condition is true.
Step 5 - A Third Tunnel
Now please try to add a third tunnel from left to right.
And here is the complete program with 3 “if-else” branches:
Step 6 - Bigger Space Inside
Suppose we want to carve out a bigger space inside the cube. We can set the condition to be that if all of x/y/z variables are 100 or smaller, we would not add any box there. Note that the “abs” block takes the absolute value of the input, so if x is -100, “abs(x)” would be 100.
Here is the result:
Step 7 - Add a Transformer Parent
So far all the boxes are independent of each other. To treat all of them as one object, we need to set another object as the parent of all boxes.
To do that, we first need to add a new transformer object, and give it a name like “p”. This object will not be visible, but we can still use it to control all its children objects.
Next, we need to set “p” as the parent of every box we add:
Step 8 - Make the Cube Spin
Although we still get the same cube as before, now we are capable of controlling all the boxes as one object through their parent.
For example, we can make the entire cube spin around the Z-axis. To do that, we first need to select the transformer object by its name “p”. This way, any block below will be applied to this parent object. Next, we can set the “z-rotation” speed to 100 for the parent, so all the child boxes will start rotating around it.
Step 9 - Exporting the Cube to a GLB file
If you would like others to use the cube you have just built, you can export it to a .GLB file. The GLB format is a format for 3D models, which is similar to the JPEG format for 2D images, and it is accepted by many 3D platforms.
To export an object, you just need to use the following block, and specify the name of the object “p” to be exported. All child objects will be exported as well, and that makes sure the entire cube is exported as one model file.
Step 10 - Upload and Share Your GLB Model File
You can upload and share any GLB model you have created to the entire CreatiCode community. They are called “community models”.
You just need to visit the “My Stuff” page, click “3D Models”, then follow the instructions to upload the GLB file. After that, all users can load your model into their scenes.
Creative Ideas
There are many ways you can adapt this project. Here are some ideas for your inspiration:
-