Brackeys Tutorials: Terrain Generation and Vertex Colors

September 11, 2019

Graphics Tutorials

Mesh Generation/Alteration and Vertex Colors

Youtube – MESH GENERATION in Unity – Basics

Tutorial #1

By: Brackeys


Youtube – PROCEDURAL TERRAIN in Unity! – Mesh Generation

Tutorial #2

By: Brackeys


Youtube – MESH COLOR in Unity – Terrain Generation

Tutorial #3

By: Brackeys


Tutorial #1

This tutorial just covers the basics of creating meshes in Unity. This covers the basics of vertices and tris in Unity, as well as the back-face culling done. This just means when creating tris for the triangle array that they must be input in a clockwise manner to display in the proper direction.

Tutorial #2

This tutorial gets into using the basics of Tutorial #1 to create an entire terrain. This process simply starts by creating a grid of a bunch of vertices, filling them in with tris, and then modifying the positions of some of those vertices.

This started with a process I have used before in mesh generation, where you use several for loops to generate all the vertices in an array of some given size, then fill the gaps between those vertices with tris using more for loops and some basic math. They did add a nice twist where they made the CreateShape method into a coroutine, so we could use WaitForSeconds and see the mesh be filled out. While this was done for a neat aesthetic purpose, this could possibly help in debugging meshes to see where the tris start to be created incorrectly.

The very simple for loop setup for going through all the vertices and filling in the tris did have one flaw that was addressed in the tutorial. When going from the end of a row to the next row, we were creating an extra tri which extended from the end of the row all the way back to the beginning of the next row. Weird errors like this have gotten me before in mesh generation, so I just wanted to point it out.

The setup in this tutorial did the whole quad for each vert, so basically each point was given its own square to cover as we went through the for loops. To avoid the issue of creating extra tris between rows, they simply “skipped” the final vert in each row by adding 1 to the vert index an extra time once a row was completed.

Example of tri generation snippet: for (int z = 0; z < zSize; z++) { for (int x = 0; x < xSize; x++) { triangles[tris + 0] = vert + 0; triangles[tris + 1] = vert + xSize + 1; triangles[tris + 2] = vert + 1; triangles[tris + 3] = vert + 1; triangles[tris + 4] = vert + xSize + 1; triangles[tris + 5] = vert + xSize + 2; vert++; tris += 6; yield return new WaitForSeconds(0.1f); } vert++; // This is the extra vert index added to ensure proper transition from one row to next }

Finally, to make it seem more like the terrain we wanted, we added some noise to the y position of our verts when creating them. Perlin noise was the choice used in the tutorial. Perlin noise is Unity takes in two coordinate parameters, then outputs a noise value between 0 and 1. You can further multiply this by another factor to create more significant noise effects.

There was an interesting issue with using perlin noise. They multiplied the input parameters by 0.3f, which looked very arbitrary. They mentioned that there was a reason for this covered in another video on perlin noise so I checked that and apparently perlin noise repeats on whole numbers. Since we were fedding in parameters based on our vertex indices, these were all whole numbers. Sure enough, when I removed the 0.3f multiplier, the entire terrain was flat again. Something about being “not whole numbers” allows the noise to work.

Tutorial #3

I logged this tutorial earlier, and just wanted to include it here since it went with the other tutorials. I’ll be looking to use this as my Next Step for this post, and hopefully get some more vertex color tutorials to go along with it. I would like to look into some more shader code focused ones if I can since it should be pretty straight forward/simple shader language to get some more practice.

NEXT STEP

Do Tutorial #3 and find more vertex color tutorials (preferably with focus on using shader language).

General Graphics in Unity – Vertex Colors, Shaders, Meshes, UVs

September 11, 2019

General Graphics Information

Meshes, Shaders, Vertices, UVs

Youtube – MESH COLOR in Unity – Terrain Generation

Tutorial #1

Youtube – Learning Shaders in Unity – Everything about Meshes in under 5 Minutes

Info #1

Youtube – Pico Tanks: Vertex shader in Unity

Info #2

Tutorial #1

I was interested at looking into using vertex colors for objects, which led me to a few interesting videos on various aspects of 3D modeling in general. Tutorial #1 was a nice example of using vertex colors in Unity, as well as giving me a simple tutorial to look into on mesh deformation as well. It uses Unity’s shader graph to apply vertex colors, and just simply sets colors with a gradient onto the vertices themselves based on their height (y position).

Info #1

Info #1 was a very brief but general overview of topics on meshes.

  • UVs: It explained how UVs are used to map textures onto faces of objects, which was the biggest topic I had not really covered up to this point. UVs also belong directly to a vertex.
  • Vertices: It also went into how their can be more vertices on an object than you’d expect (since they have a normal that goes with individual faces).

This video was specifically aimed at Unity, so some of the information was most likely Unity specific. For instance, the fact that the Mesh class contains Vertices, Normals, UVs, Tangents, Triangles, and Colors. It finally got into vertex colors at the end, which also showed an example of it being used that just made a very cool colored cube I thought.

From: Info #1

Info #2

Finally, Info #2 was just a very quick video of a game project showcasing their use of vertex shaders to actually move trees in their environment when objects are moving through them. Moving objects affect the colors of an underlying texture map, which use this “color information” to apply affects, such as rotating the trees.

May 19th, 2018

Math for Game Programmers – Talks from GDC – Notes

GDC 2015 – Math for Game Programmers: Fast and Funky 1D Nonlinear Transformations

Presenter: Squirrel Eiserloh

Youtube link to presentation

Notes:

  • Other names used for these concepts in other fields:
    • Easing functions
    • Filter functions
    • Lerping functions
    • Tweening functions
  • Implicit vs. Parametric Equations
    • Implicit: Ex. x^2 + y^2 = R^2
    • Parametric: Ex. Px = R * cos(2*pi*t); Py = R * sin(2*pi*t)
  • Parametric Equations use a single variable (usually a float) as input
  • Opportunities to Use Parametric Transformations
    • Anywhere you have a single float to change
    • Anywhere that could be expressed as a single float
    • Anytime you use time
  • The two most important number ranges in mathematics and computer science are:
    • 0 to 1: Good for fractions and percentages
    • -1 to 1: Good for deviations from some original value
  • These functions are normalized
    • Input of 0 has output of 0, and input of 1 has output of 1
    • The in-between values are those that vary function to function
  • Types of functions
    • Smooth Start: exponential functions with various orders
    • Ex: t^2
    • Smooth Stop: gives opposite feel of smooth start
    • Ex: 1 – (1-t)^2
    • Mix functions
    • Crossfade: mix but “blend weight” is the t parameter itself
    • Scale: multiply something by t; can also multiply functions together
    • Bezier Curves
Other suggested talks:

Juice it or Lose it – by: Martin Jonasson and Petri Purho
The art of screen shake – by: jan willem Nijman vlambeer

Math for Game Programmers – Talks from GDC – Notes

GDC 2013 – Math for Game Programmers: Interaction With 3D Geometry

Presenter: Stan Melax

Youtube link to presentation

Notes:

  • Basic Vector Math
    • Dot product
    • Cross product
    • Outer product
    • Jacobian
    • Determinants
  • Geometry Building Blocks
    • Traingles and planes
    • In games, triangles and planes need to know above and below
    • Triangles use normals
    • Planes use Ax + By + Cz + D == 0
  • Ray-Triangle Intersections
  • Ray-Mesh Intersection
    • Could check against all triangles, but there are ways to remove some for efficiency
    • Need to check if you’ve hit the nearest triangle
    • Mesh must be intact, can have issues if there are holes
  • Convex Mesh
    • Neighboring face normals tilt away
    • Volume bounded by number of planes
    • Every vertex lies at/below every other face
    • Convex used because it makes a lot of tests/calculations simpler
    • If point lies under every plane, it is inside
    • Can move rays as they intersect triangles
  • Convex Hulls
    • Techniques for converting meshes into convex meshes
    • There are two main techniques:
    • Expand Outward – start with small mesh and expand out until all vertices are accounted for
    • Reduce Inward – start with a large mesh and shrink it down to fit all vertices
  • Convex Decomposition: breaking down complex shapes into separate convex meshes
  • Objects in Motion – Spatial Properties
    • Find the center of triangles, then the center of tetrahedrons
    • Find the area of triangles and the volumes of tetrahedrons
    • These properties can be used for basic accurate physics simulations of objects
  • Time Integration without Numerical Drift
    • Forward Euler update versus Runge Kutta update
    • Forward Euler applies derivative at every step, which can give inaccurate results in too large of time steps
    • Runge Kutta looks at multiple time steps and their derivatives and takes a weighted average to make steps more consistent
  • Soft Body Meshes
    • Connect points/vertices of mesh with spring-like connections
    • Two main techniques:
    • Kinematic: can have issues with stressed state oscillating as it will not come to rest, forces continue to act
    • Dynamic: has better resting stressed states that will settle down
  • How to learn more
    • Practice tinkering with your own physics engine code
    • Write gjk algorithm
    • Help understand physics engines and their limitations
May 13th, 2018

Procedural Generation

Unite 2016 – The Power of Procedural Meshes

Unity Talk by Alexander Birke

Youtube Link to Talk

Suggested Reading:

“Essential Mathematics for Games – A Programmers Guide” by James M. Van Verth & Lars M. Bishop

Links to examples to learn about procedural generation of meshes
Notes from talk:
    Why use procedural meshes?

  • Player Made Content (Ex. Spore)
  • Unique Mechanics (Ex. Gish)
  • Procedural Generation (Ex. Sir You’re Being Hunted)
Unity Procedural Meshes
  • Mesh starts by creating vertices, and then making triangles from those vertices
  • Components Needed:
    • MeshFilter – stores the mesh
    • MeshRenderer – shows the mesh
    • Script – to generate the mesh
  • Normal vectors used to determine surface orientation
  • Colors
    • Used to be determined by assigning values to vertices
    • Can be used to provide more information to vertices still
    • Can be done as bytes or floats
    • Bytes generally better
  • UVs
    • 2d coordinates for each vertex
    • Used to map textures
    • Create vector2d array to assign to mesh object
  • Setting Trangle Indices
    • Most difficult part
    • Find common features of topology that repeats
    • Draw it out to help find these patterns!
  • Delaunay Triangulation
    • Maximizes angle of all angles of triangles in triangulation
    • Good for GPU rendering since skinny triangles can lead to visual artifacts
    • Port of triangle.net can be found in an above link
  • Debugging Procedural Meshes
    • Very important to do
    • Use Gizmos and Debug classes
    • Rotate camera to check if tris are facing correct direction
    • Turn on wireframe rendering
  • Optimizing Meshes
    • StaticBatchingUtility: batches multiple game objects together into fewer drawcalls but where each can still be culled
    • Mesh.CombineMeshes: Puts many meshes into one, good if individual meshes not likely to cull, but can break things if not done carefully
  • Optimizing Dynamic Meshes
    • Mesh.MarkDynamic: allocates a memory buffer of sorts to designated mesh to let graphics API know mesh data will change often
    • Base data structure to store data in is flat arrays when possible
    • Lists can be used too, especially if you are unsure how many vertices you will need before creating mesh
    • Frustrum Culling
    • Skinned Meshes
  • Multithreaded Mesh Generation: Unity API not thread safe
  • Compute Shaders
    • GPU great for running parallel tasks
    • Only available on modern platforms: PS4, Xbone, DirectX 11, OpenGL 4.3, OpenGL ES 3.1
    • Unity can cross compile with these options
Summary

Procedurally generating a mesh starts with creating vertices, and then triangles from these vertices. Normals and UVs can be added to this data for more informed meshes. In Unity, your mesh will need a MeshFilter and MeshRenderer component. Debugging your mesh is important, and some tools in Unity to help with that are the gizmo and debug classes, moving the camera around, and turning on wireframe rendering. There are also several options to look into to either optimize your existing meshes how they are, or compute them more efficiently.

More to Research
  • Bezier Curves: parametric mathematical curves used commonly in generating computer graphics
  • Use of GPU to do parallel calculations, and how to connect that with Unity scripting
  • Thread and Multithread Mesh Generation: Not sure what this actually meant
  • Computing shaders
  • Talk through Unity by “Sir You’re Being Hunted” on 3D Mesh Generation
March 25th, 2018

Update on Gear Mesh Generating Toy

Link to Web Build of Prototype Currently

Current Controls
  • Left Mouse Button – Press: Select a gear to make it active
  • Let Mouse Button – Drag: Move selected gear around
  • Left Mouse Button – Drag + Spacebar: Destroy selected gear
  • w : makes the gear spawning object the active object (instead of any gears)
  • Typing in input fields: Changing the number in this input field will alter the corresponding parameter of the currently active (selected) object.

Selecting a gear and changing the value in the input field will immediately change that corresponding parameter on that gear. For example, if you left click a gear, then type the value “1” into the “Body Radius” input field, that gear will have its body radius value immediately change to 1, and its mesh and colliders will update along with it.

There is an invisible gear spawning object (“gear spawner”) that creates a gear when the “Create Gear” button is pressed. The gear created will have values based on whatever the spawner’s current values are. To change the spawner’s values, the spawner must be the active object. This is where the “w” key control comes in. Once the player presses “w”, as long as they don’t left click another gear in the mean time, changes to the input field will become the values of the spawner. Then the next time the “Create Gear” button is pressed, it will use those updated spawner values to create the new gear. This gear can then be altered as any other gear.

Current Issues to Avoid

The input fields do not show the values of an object when it is selected. This can be a bit confusing since it makes it hard to tell what the currently selected object’s values are, unless the player just remembers them. This is something that would be crucial to update as there is currently no way to actually see the current values.

The spawner does initialize at all values of “0”. Just be aware that if you try to create gears before changing the spawner’s parameters, you will be creating invisible empty objects.

When attempting to update the spawner, make sure to press “w” before changing any values in the input fields. This was something I found myself messing up a lot, and would be a feature to improve in the future. Again, make sure to press “w”, and not click any other gears, before changing the input fields to update the spawner’s values.

March 19th, 2018

Real Time Updating Gear Mesh – PROTOTYPE

Prototype – Real Time Gear Mesh Editor

This example build shows the real time updating capabilities of the current scripting behind the gear mesh creator. This also has some of the functionality of the original build, but that part is buggy when trying to tie it in to this current build. This build is solely to show that the real time editing of the mesh does work with the current script structure.

There is still significant work needed to combine this real time updating feature with the effective gear generation coding to create a single, cohesive build of all the elements.

March 12th, 2018

Prototype of Gear Mesh Creator in Unity

Gear Mesh Creator Protoype

This is a very bare bones version of the gear mesh generator in Unity. Enter values into all of the parameters on the left side of the screen, then press “Create Gear” to generate a gear with those parameters. Clicking and dragging a gear allows you to move it around on the xy-plane. While dragging a gear, press space bar to delete it.

The scripting allows for the functionality of real time updates to the mesh and colliders of the gears, but I was unable to implement a player based way to use that that. While editing the values in the Unity editor, it will change the shape in real time. This action just needs to be translated to the build side. I will be looking at something where clicking a gear selects it, then the parameters of that gear will fill the boxes on the side, and when the player edits them, they will see the changes immediately on that gear. Then there will be a separate way to access the parameters when needing to create an entirely new gear.

March 10th, 2018

Working with Meshes Via Scripting in Unity

Creating a Game Object and Mesh at Runtime
Tutorial Series – Moving Mesh Vertices at Runtime

This short video shows a very basic way to manipulate existing vertices. It simply moves a few vertices along their normals in a sine wave type pattern.

Writing to .CSV Files with Unity Scripting

How to write data to CSV file in UNITY

Printing out arrays of data to .CSV files can be useful when creating large arrays of data for things such as vertices and tris as it can give you a better way to figure out where errors are occurring.

Using InputFields with C# Unity 5.4 (Basic Tutorial)

This video shows the most basic use of input fields in Unity. This may be a direction to take to connect the player to the variables in the gear mesh editor.

Basic UI Functionality – Interacting Directly with Game Objects Unity

Unity 5 UI Tutorial – Input field and event handlers

This video is in the middle of a series by this author on some basic UI functions within Unity. These UI elements focus on types which the player can interact with, and they will cause some change in the game (as opposed to more static UI elements that just show values for things such as score or lives). This can prove useful as a simple way to give users access to the parameters of the gear mesh objects.