Imran's personal blog

March 22, 2017

Unity Mesh and Materials Notes

Filed under: Uncategorized — ipeerbhai @ 2:13 am

These are my notes on how to make a Mesh with Materials from pure C# code in Unity.
digested from:


Space — A coordinated system to define points.

UV — A 2d Space normalized based on the image’s size.  The definition is always UV = (0,0) is the origin and (1,1) is the top left in image space.  To convert an image point from pixels to UV, have U = (chosen pixel x position)/(Image Pixel Width) and V = (chosen pixel y position)/(Image Pixel Height).  A Loose idea is that the UV value is the “%” towards the top right of a picture.

Steps seem to be:

  1. Generate a GameObject( aka GO ).
  2. Generate Geometry.
    1. Geometry is stored in the mesh property of a MeshFilter component attached to a GameObject.
  3. Generate Texture.
    1. Texture is stored in the material property of a MeshRenderer component attached to a GameObject.

Step 1: Make a gameobject with the needed components:

Two different ways to do this:  method one:

Add this decoration to the class that will be your GO.

[RequireComponent(typeof(MeshFilter), typeof(MeshRenderer))

Method two — use the API to add at runtime.

		gameObject.AddComponent<MeshFilter>().mesh = mesh;
		gameObject.AddComponent<MeshRenderer>().material = material;


Step 2:  Generate Geometry.

Meshes are made of a handful of ideas.  This means you have a handful of things to figure out to make a mesh:

  1. Where are the vertices in XYZ.
  2. What are the right normals to use per vertex.
    1. Which vertices should be duplicated to handle different faces using them.
  3. What is each vertex’s UV.
  4. What is each vertex’s curvature.
  5. What subfacets should your create to maximize your texture.

The first is that meshes are made of triangles.  You add the points in Unity’s world space, then link them in triangles by using the indices of each vertex in an array of Vector3.

NOTE.  You can generate submeshes in a mesh.  You specify the array of vertexes as normal, but instead of directly adding trianges, instead you set Mesh.SubmeshCount to your count of submeshes. Now, add triangles to each submesh instead of the main mesh.  Example code:
Mesh output = new Mesh();
output.subMeshCount = 2;
output.SetTriangles(m_TriangleLinesA, 1);
output.SetTriangles(m_TriangleLinesB, 2);

// if you’re not using submeshes, you can just add triangles by:
output.triangles = m_TriangleArray;

One part of generating the geometry is generation of the UV axis per vertex.  Here’s a good URL for UV generation on sphere’s:

For cubes, it’s easier.  There’s yet another method for cylinders.  It seems cube, cylinder, and sphere are the three approximations people use for generating their UV axis information.  You’ll use the vertex and normals to figure out the right axis position for UV.

Step 3: Generate texture.








Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

Create a free website or blog at

%d bloggers like this: