Skip to content

Instantly share code, notes, and snippets.

@davepape davepape/triangles3.cs
Last active Oct 18, 2018

Embed
What would you like to do?
Create a mesh of 2 triangles, with texture coordinates
// Creating Mesh data in Unity
// This script should be attached to a GameObject that has a MeshFilter and MeshRenderer.
// The script will replace the MeshFilter's geometry by 2 triangles with texture coordinates.
// The vertex coords & tex coords are public variables so they can be manipulated from the Unity inspector.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class triangles3 : MonoBehaviour {
public Vector3 v0 = new Vector3(-1f,0f,0f);
public Vector3 v1 = new Vector3(0f,1f,0f);
public Vector3 v2 = new Vector3(1f,0f,0f);
public Vector3 v3 = new Vector3(0f,-1f,0f);
public Vector2 uv0 = new Vector2(0f,0f);
public Vector2 uv1 = new Vector2(0f,1f);
public Vector2 uv2 = new Vector2(1f,1f);
public Vector2 uv3 = new Vector2(1f,0f);
void Start ()
{
Vector3[] newVertices = { v0, v1, v2, v3 };
Color[] newColors = { Color.red, Color.green, Color.blue, Color.yellow };
Vector2[] newUV = { uv0, uv1, uv2, uv3 };
int[] newTriangles = { 0,1,2, 0,2,3 };
Mesh m = GetComponent<MeshFilter>().mesh;
m.vertices = newVertices;
m.colors = newColors;
m.uv = newUV;
m.triangles = newTriangles;
m.RecalculateNormals();
}
void Update ()
{
Mesh m = GetComponent<MeshFilter>().mesh;
Vector3[] verts = { v0, v1, v2, v3 };
Vector2[] newUV = { uv0, uv1, uv2, uv3 };
m.vertices = verts;
m.uv = newUV;
m.RecalculateNormals();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.