Gear Mesh Generating Web Build

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.

Example setup of gear mesh generator in Unity’s editor.

DIGM-540 Gear Mesh Generating Resources

  1. Basic Mesh Scripting Tool Description for Unity
  2. Creating Basic Billboard Mesh Through Unity Scripting
    Shows how to create a vertex array, set these into triangles, and create a full mesh.

  3. More Help for Procedural Generation of Mesh

This shows that it is possible to create/modify meshes within Unity using scripting.

Project Direction – Create Script in Unity that Will Take User Inputs to Instantiate a Gear Model Based on Said Parameters

This may be possible through the general use of the Mesh class in Unity scripting. Something along the lines of creating a set of vertices for the central body (some smoothness level of cylinder), then creating a gear tooth mesh that can be multiplied and geometrically positioned around the central core body mesh created.

Bump Mapping

Normal Maps from Unity

“Normal Maps and Height Maps are both types of Bump Map. They both contain data for representing apparent detail on the surface of simpler polygonal meshes, but they each store that data in a different way. A height map is a simple black and white texture, where each pixel represents the amount that point on the surface should appear to be raised. The whiter the pixel colour, the higher the area appears to be raised.

A normal map is an RGB texture, where each pixel represents the difference in direction the surface should appear to be facing, relative to its un-modified surface normal. These textures tend to have a bluey-purple tinge, because of the way the vector is stored in the RGB values.”

Displacement Maps

Unity Displacment Maps and Tesselation

Tri Setup to Create Mesh for Gear Segments
Showing example breakdown of a gear segment in tris.

Randomly Selecting Points within a Given Polygon

References Used:

Stack Overflow – Random Points in a Quadrilateral

Wolfram Alpha Information on Points in a Triangle

I needed to be able to randomly but evenly distribute a number of objects within a 2D area so these are the sources I started with. The Stack Overflow question ended up being a very good starting point, but there were errors with the calculations that I needed to solve in order to actually use their logic.

Fixes from Stack Overflow

First calculation to fix was the originally randomly selected position of the object. It turned out that it was very close, but it actually only worked if v0 was the origin. It was simple enough to fix by adding v0 to the x equation.

The second equation to fix was the v3 equation. They were subtracting v0 twice which is incorrect (again something unnoticeable if v0 is that origin as you’re simply subtracting (0,0) twice). v0 should only be subtracted once to obtain the correct v3 value.

Finally, a correction that was found in the comments was that there are two rotation equations for x’ and the larger one is incorrect. In the text, it can be found that a rotation of pi is necessary. This can be mathematically applied by simply reversing (x – v3) to (v3 – x).

Work to do

Fixing these mathematical errors has appeared to give me the desired results with some further testing required after a final issue. I still need to determine how to let it know mathematically when to “flip” the x position into the x’ position. The original thought was to simply compare the distance from x to v3 and v0, and if it was closer to v3 (distance was less), then apply the x’ flip transformation. This however does not seem to be a geometrically sound solution as I am sometimes getting the flipped applied when it is within the proper bounds.