Skip to content

Instantly share code, notes, and snippets.

@prucha
Last active March 31, 2024 15:45
Show Gist options
  • Save prucha/866b9535d525adc984c4fe883e73a6c7 to your computer and use it in GitHub Desktop.
Save prucha/866b9535d525adc984c4fe883e73a6c7 to your computer and use it in GitHub Desktop.
Unity: Creating a Mesh in code
using UnityEngine;
using System.Collections;
// This Unity script demonstrates how to create a Mesh (in this case a Cube) purely through code.
// Simply, create a new Scene, add this script to the Main Camera, and run.
public class UnityMeshCreator : MonoBehaviour {
GameObject _cube;
void Start () {
//1) Create an empty GameObject with the required Components
_cube = new GameObject("Cube");
_cube.AddComponent<MeshRenderer>();
MeshFilter meshFilter = _cube.AddComponent<MeshFilter>();
Mesh mesh = meshFilter.mesh;
//Create a 'Cube' mesh...
//2) Define the cube's dimensions
float length = 1f;
float width = 1f;
float height = 1f;
//3) Define the co-ordinates of each Corner of the cube
Vector3[] c = new Vector3[8];
c[0] = new Vector3(-length * .5f, -width * .5f, height * .5f);
c[1] = new Vector3(length * .5f, -width * .5f, height * .5f);
c[2] = new Vector3(length * .5f, -width * .5f, -height * .5f);
c[3] = new Vector3(-length * .5f, -width * .5f, -height * .5f);
c[4] = new Vector3(-length * .5f, width * .5f, height * .5f);
c[5] = new Vector3(length * .5f, width * .5f, height * .5f);
c[6] = new Vector3(length * .5f, width * .5f, -height * .5f);
c[7] = new Vector3(-length * .5f, width * .5f, -height * .5f);
//4) Define the vertices that the cube is composed of:
//I have used 16 vertices (4 vertices per side).
//This is because I want the vertices of each side to have separate normals.
//(so the object renders light/shade correctly)
Vector3[] vertices = new Vector3[]
{
c[0], c[1], c[2], c[3], // Bottom
c[7], c[4], c[0], c[3], // Left
c[4], c[5], c[1], c[0], // Front
c[6], c[7], c[3], c[2], // Back
c[5], c[6], c[2], c[1], // Right
c[7], c[6], c[5], c[4] // Top
};
//5) Define each vertex's Normal
Vector3 up = Vector3.up;
Vector3 down = Vector3.down;
Vector3 forward = Vector3.forward;
Vector3 back = Vector3.back;
Vector3 left = Vector3.left;
Vector3 right = Vector3.right;
Vector3[] normals = new Vector3[]
{
down, down, down, down, // Bottom
left, left, left, left, // Left
forward, forward, forward, forward, // Front
back, back, back, back, // Back
right, right, right, right, // Right
up, up, up, up // Top
};
//6) Define each vertex's UV co-ordinates
Vector2 uv00 = new Vector2(0f, 0f);
Vector2 uv10 = new Vector2(1f, 0f);
Vector2 uv01 = new Vector2(0f, 1f);
Vector2 uv11 = new Vector2(1f, 1f);
Vector2[] uvs = new Vector2[]
{
uv11, uv01, uv00, uv10, // Bottom
uv11, uv01, uv00, uv10, // Left
uv11, uv01, uv00, uv10, // Front
uv11, uv01, uv00, uv10, // Back
uv11, uv01, uv00, uv10, // Right
uv11, uv01, uv00, uv10 // Top
};
//7) Define the Polygons (triangles) that make up the our Mesh (cube)
//IMPORTANT: Unity uses a 'Clockwise Winding Order' for determining front-facing polygons.
//This means that a polygon's vertices must be defined in
//a clockwise order (relative to the camera) in order to be rendered/visible.
int[] triangles = new int[]
{
3, 1, 0, 3, 2, 1, // Bottom
7, 5, 4, 7, 6, 5, // Left
11, 9, 8, 11, 10, 9, // Front
15, 13, 12, 15, 14, 13, // Back
19, 17, 16, 19, 18, 17, // Right
23, 21, 20, 23, 22, 21, // Top
};
//8) Build the Mesh
mesh.Clear();
mesh.vertices = vertices;
mesh.triangles = triangles;
mesh.normals = normals;
mesh.uv = uvs;
mesh.Optimize();
//mesh.RecalculateNormals();
_cube.transform.Translate(0f, 1f, -8f);
//9) Give it a Material
Material cubeMaterial = new Material(Shader.Find("Standard"));
cubeMaterial.SetColor("_Color", new Color(0f, 0.7f, 0f)); //green main color
_cube.GetComponent<Renderer>().material = cubeMaterial;
}
// Update is called once per frame
void Update () {
//Rotate the cube
_cube.transform.Rotate(5f * Time.deltaTime, 15f * Time.deltaTime, 3f * Time.deltaTime);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment