Last active
October 18, 2018 19:12
-
-
Save davepape/84673c93b9f3aaed7eade0bc70d99d00 to your computer and use it in GitHub Desktop.
Create a mesh of 2 triangles, with texture coordinates
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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