This week in Scratch advanced we took on the ambitious task of writing a general-purpose 3d wireframe engine. The goal here was to build an engine that we could use to view any structure in 3-dimensions as though we had x-ray vision. We didn’t really know how it might turn out but were delighted with the results! If you just want the code, skip to the bottom!
We didn’t quite know how to start this so we Googled 3D and Wireframe, and we found this article on wikipedia that really helped us understand what it was:https://en.wikipedia.org/wiki/Wire-frame_model
This showed us some models and also gave us a clue on how we might do it – they suggested creating two lists – one of “vertexes” or points and another of “edges” or lines. They gave an example of the points for a cube so we took their example for our first model and expanded to one of size 50 (so we could see it onscreen).
Edge table specifies the start and end vertices for each edge. This table is all about joining points from the points table.
|Edge||Start Vertex||End Vertex|
Step 1: First write the code to build the points and draw the cube:
- We created 3 lists to store the points: points_x, points_y, points_z
- We created two lists to store the edges: edges_from, edges_to
- We first added some custom blocks to add values to the lists, making :
- add_point (x, y, z) which adds a point to the points lists
- add_edge(from, to) which adds an edge to the edge lists
- A clear_shape which clears all of the lists with the shape data
- build_cube which called the above blocks to add all the items needed for the table above.
- We tested these blocks by running the and making sure that when we started the game we had 8 points in each list and 12 edges in the edge lists.
The code for these blocks is below:
Step 2: Write the code to draw the shape
- We created a custom block to go to a given point go_to_point(point_number)
- initially this just went to the x,y of the point_number in the list
- We created a custom block to draw the points. We made sure that the draw_points had “do not refresh screen” checked so that it would run nice and smooth (we did this on all the “draw blocks”)
- We also added a very similar draw_lines block which drew the edges
- The main look looked like the following at this stage. It was pretty simple, just drawing the points and the lines.
- At this stage, when we ran the program, it showed us a square:
- The reason it looked like a square is because we didn’t have perspective built in. We looked at perspective for a few minutes and figured out why eyes that work like ours make things look smaller as they get more distant. We learned out that how quickly things get smaller as they get more distant is called the “perspective“
Step 3: Write the code to add perspective
- Adding perspective was easy – we revisited our go_to_point block and changed it to the following:
- We realize that things don’t get get smaller quite slowly ass they move away so we set the perspective to a very small number – we played around with it a bit till the cube looked like we expected – the value 0.004 looks quite good:
- Now our cube was starting to take shape!
Step 4: Rotation!
- We again visited the internet to look for how to rotate points about an axis – x, y, or z. We googled and came to this page: https://www.siggraph.org/education/materials/HyperGraph/modeling/mod_tran/3drota.htm Which showed how it was done.
- A point rotates around the X axis by an angle q, it’s Y and Z values change – the webpage showed us the formulas which could tell you how much:
newY = y*cos q – z*sin q
newZ = y*sin q + z*cos qSimilarly it showed the formula for rotating around the Y axis:newZ = z*cos q – x*sin q
newX = z*sin q + x*cos q
- We took those and put them in two custom blocks (actually only got one of them finished in the class, but showing both here. The web page also shows how to do z-axis rotation so feel free to add that into your version if you like!
- The final version of the main loop now looks like:
- I also added some simple blocks to let you control the rotation with the arrow keys:
That’s IT – a fully functional wireframe model viewer that lets you model, view and rotate any structure in 3D with full perspective!! All in scratch!
Feel free to play with it and build other structures – houses, spaceships, random structures, etc. It is pretty smooth with thousands of points on my old pc. For example, I added a cloud to the center of the cube with this code at the end of the cube block:
This makes the cube look full of stars – see what you can come up with!
If this opens up in a webpage, click on “view raw” to download it.