arskrald/AR-2/Assets/TriangleMesh.cs

166 lines
4.3 KiB
C#
Raw Permalink Normal View History

2019-02-18 21:22:15 +00:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[ExecuteInEditMode]
[RequireComponent(typeof(MeshRenderer))]
[RequireComponent(typeof(MeshFilter))]
public class TriangleMesh : MonoBehaviour {
private MeshFilter meshFilter;
private Mesh mesh = null;
private Vector3[] vertices = new Vector3[15];
private Color[] colors = new Color[15];
private Vector3[] normals = new Vector3[15];
private Vector2[] uv = new Vector2[15];
private int[] triangles = new int[39];
private float blinkRate = 0.5f;
public float alpha = 0.8f;
void Start () {
}
void Update () {
meshFilter = GetComponent<MeshFilter>();
if(mesh==null)
{
mesh = new Mesh();
}
// top
vertices[0] = new Vector3(1.5f, 6);
vertices[1] = new Vector3(1, 5);
vertices[2] = new Vector3(2, 5);
vertices[3] = new Vector3(1, 4);
vertices[4] = new Vector3(2, 4);
vertices[5] = new Vector3(1, 3);
vertices[6] = new Vector3(2, 3);
vertices[7] = new Vector3(1, 2);
vertices[8] = new Vector3(2, 2);
vertices[9] = new Vector3(1, 1);
vertices[10] = new Vector3(2, 1);
vertices[11] = new Vector3(3, 1);
// bottom right point
vertices[12] = new Vector3(3, 0);
vertices[13] = new Vector3(0, 1);
// bottom left point
vertices[14] = new Vector3(0, 0);
/*
uvs[0] = new Vector2(0, 0);
uvs[1] = new Vector2(1, 0.5f);
uvs[2] = new Vector2(0.5f, 0.15f);
*/
normals[0] = new Vector3(0,1,0);
normals[1] = new Vector3(0,1,0);
normals[2] = new Vector3(0,1,0);
normals[3] = new Vector3(0,1,0);
normals[4] = new Vector3(0,1,0);
normals[5] = new Vector3(0,1,0);
normals[6] = new Vector3(0,1,0);
normals[7] = new Vector3(0,1,0);
normals[8] = new Vector3(0,1,0);
normals[9] = new Vector3(0,1,0);
normals[10] = new Vector3(0,1,0);
normals[11] = new Vector3(0,1,0);
normals[12] = new Vector3(0,0,1);
normals[13] = new Vector3(0,1,0);
normals[14] = new Vector3(0,0,1);
float col = Mathf.Cos(Time.time);
colors[0] = Color.blue;
colors[1] = Color.blue;
colors[2] = Color.blue;
colors[3] = Color.blue;
colors[4] = Color.blue;
colors[5] = Color.blue;
colors[6] = Color.blue;
colors[7] = Color.blue;
colors[8] = Color.blue;
colors[9] = Color.blue;
colors[10] = Color.blue;
colors[11] = new Color(0, col, 1 - col);
colors[12] = Color.blue;
colors[13] = new Color(col, 0, 1 - col);
colors[14] = Color.blue;
colors[0].a = alpha;
// top
triangles[0] = 1;
triangles[1] = 2;
triangles[2] = 0;
triangles[3] = 1;
triangles[4] = 4;
triangles[5] = 2;
triangles[6] = 1;
triangles[7] = 3;
triangles[8] = 4;
triangles[9] = 3;
triangles[10] = 5;
triangles[11] = 4;
triangles[12] = 5;
triangles[13] = 6;
triangles[14] = 4;
triangles[15] = 5;
triangles[16] = 8;
triangles[17] = 6;
triangles[18] = 5;
triangles[19] = 7;
triangles[20] = 8;
triangles[21] = 7;
triangles[22] = 9;
triangles[23] = 8;
triangles[24] = 9;
triangles[25] = 10;
triangles[26] = 8;
triangles[27] = 13;
triangles[28] = 9;
triangles[29] = 7;
triangles[30] = 13;
triangles[31] = 14;
triangles[32] = 9;
triangles[33] = 8;
triangles[34] = 10;
triangles[35] = 11;
triangles[36] = 10;
triangles[37] = 12;
triangles[38] = 11;
vertices[12] += normals[12] * Mathf.Sin(Time.time);
vertices[14] += normals[14] * Mathf.Sin(Time.time);
mesh.vertices = vertices;
mesh.colors = colors;
mesh.normals = normals;
mesh.uv = uv;
mesh.triangles = triangles;
meshFilter.sharedMesh = mesh;
}
}