Skip to content

Instantly share code, notes, and snippets.

@davepape
Created September 29, 2015 04:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davepape/6759de4f9ab9020b532e to your computer and use it in GitHub Desktop.
Save davepape/6759de4f9ab9020b532e to your computer and use it in GitHub Desktop.
create a mesh with 2 triangles in Unity
#pragma strict
// Script to create a mesh with 2 triangles.
// This should be attached to an empty GameObject.
// It assumes the existence of a custom shader to use the vertex colors
// without texture or lighting.
function Start ()
{
gameObject.AddComponent.<MeshFilter>();
gameObject.AddComponent.<MeshRenderer>();
var mat = new Material(Shader.Find("Custom/Vertex Colored"));
GetComponent.<Renderer>().material = mat;
var p0 : Vector3 = Vector3(-2,0,-1);
var p1 : Vector3 = Vector3(0,1,-1);
var p2 : Vector3 = Vector3(1,0,-1);
var p3 : Vector3 = Vector3(0,-1,-1);
var newVertices = [ p0, p1, p2, p3 ];
var newColors = [ Color(1, 0, 0), Color(0,1,0), Color(0,0,1), Color(1,1,0) ];
var newTriangles = [ 0, 1, 2, 0, 2, 3 ];
var m = GetComponent.<MeshFilter>().mesh;
m.vertices = newVertices;
m.colors = newColors;
m.triangles = newTriangles;
}
function Update()
{
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment