Continuing to work my way through the so-far-so-good tutorials at catlikecoding.com, today’s exercise is interesting. The project takes mathematical functions and applies them in code, using them to instantiate GameObjects as a graph rendered in 3D space. This is building up to the use of mathematical concepts in larger ways in upcoming exercises.
As someone who has never enjoyed math, and has always appreciated allowing the computer to do the math for me, jumping into mathematical concepts is a little daunting. But I can definitely see the usefullness of using these concepts in things like procedural generation, so here we go!
Project Goal: Explore the use of mathematical functions to programmatically render cubes in Unity
Tutorial Link: Building a Graph at catlikecoding.com
Concepts Covered: Creating and updating prefabs, programmatically instantiate GameObjects using mathematical functions, create basic custom shaders, and make the generated graph move
Building the Graph
There were definitely some initial concepts I felt pretty comfortable with, especially around creating prefabs and how they are used. However, working with the Instantiate() method was interesting, as I haven’t done a lot of instantiation of GameObjects in code.
Essentially, we build a script that takes in a reference to a prefab (our cube) and on Awake() the script uses a simple formula to instantiate and arrange cubes on screen.
The most interesting part of the tutorial was the creation of a custom shader that automatically applies a color to cubes based on their position in the graph.
Shaders have, to be honest, been a bit of a mystery to me so far, so getting to create one from scratch and see how they’re coded was super interesting. Definitely a different syntax, and something to look into later.
This tutorial showed me some cool ideas on using mathematical functions to procedurally generate objects in 3D space, which I can totally see being useful in a wide variety of situations. Procedural generation is huge, and gives infinite variety.
Also, getting my feet wet with coding a custom shader was really interesting! Dynamic shaders help add some of my favorite effects to games, so I can see these being super useful and powerful.
While the animated graph below isn’t necessarily the most interesting, it felt good to knock out another project so quickly!
So, without further ado… A graph!
Note: This project is embedded using Unity’s WebGL player, which is not supported on mobile.
View the source for this finished project on GitHub