
course11

using UnityEngine;
using System.Collections;
using ProceduralToolkit;
using UnityEngine.UI;
public class Cosahedra : MonoBehaviour
{
public Text NumTrisText;
public float radius = 1.5f;
MeshDraft mD_CosaHedra = new MeshDraft();
MeshDraft mD_CosaHedra_Sphere = new MeshDraft();
MeshDraft mD_Tri = new MeshDraft();
// Use this for initialization
void Start ()
{
mD_CosaHedra = new MeshDraft();
mD_CosaHedra = MeshDraft.Icosahedron(radius);
}
float accumeTime = 0f;
float accTrisTime = 0f;
int numtogen = 0;
int triNo=0;
bool done = false;
int depth = 5;
void Update ()
{
accumeTime += Time.deltaTime;
if(accumeTime> 3f)
{
accTrisTime += Time.deltaTime;
if(accTrisTime>0.05f && !done)
{
for (int i = 0; i < 10; i++)
{
numtogen++;
NumTrisText.text = "Number of Triangles = " + numtogen.ToString() + " Depth = " + depth.ToString();
triNo = 0;
//mD_CosaHedra_Sphere.Clear();
Create_GP(depth);
if (numtogen > triNo + 1) done = true;
}
GetComponent<MeshFilter>().mesh = mD_CosaHedra_Sphere.ToMesh();
accTrisTime = 0;
}
}
}
void Create_GP(int depth)
{
for(int i = 0; i<20; i++)
{
Vector3 v0 = mD_CosaHedra.vertices[mD_CosaHedra.triangles[i*3]];
Vector3 v1 = mD_CosaHedra.vertices[mD_CosaHedra.triangles[i*3 +1]];
Vector3 v2 = mD_CosaHedra.vertices[mD_CosaHedra.triangles[i*3 + 2]];
Color col = getIDXolor(i);
subdivide(v0, v1, v2, depth, col);
}
}
void addTriangle(Vector3 v0, Vector3 v1, Vector3 v2, Color col)
{
if (++triNo > numtogen) return;
if (triNo < numtogen) return;
mD_Tri.Clear();
mD_Tri = MeshDraft.Triangle(v0, v1, v2);
mD_Tri.Paint(col);
mD_CosaHedra_Sphere.Add(mD_Tri);
}
void subdivide(Vector3 v1, Vector3 v2, Vector3 v3, int depth, Color col)
{
Vector3 v12, v23, v31;
Vector3 v12_n, v23_n, v31_n;
int i;
if (depth == 0)
{
addTriangle(v1, v2, v3, col);
return;
}
v12 = (v1 + v2) / 2.0f;
v23 = (v2 + v3) / 2.0f;
v31 = (v3 + v1) / 2.0f;
/* extrude midpoints to lie on sphere */
v12_n = v12.normalized * radius;
v23_n = v23.normalized * radius;
v31_n = v31.normalized * radius;
/* recursively subdivide new triangles */
subdivide(v1, v12_n, v31_n, depth - 1, col);
subdivide(v2, v23_n, v12_n, depth - 1, col);
subdivide(v3, v31_n, v23_n, depth - 1, col);
subdivide(v12_n, v23_n, v31_n, depth - 1, col);
}
Color getIDXolor(int i)
{
Color col;
col.r = 0.2f + (float)(i) * 0.8f / 20f ;
col.g = 0.5f + (float)(i) * 0.5f / 20f;
col.b = 1f - (float)(i) * 0.8f / 20f;
col.a = 1f;
return col;
}
Color getINColor(int i)
{
Color col;
col.r = (float)(i) / 1024f;
col.g = 1f - (float)(i) / 1024f;
col.b = 0.5f - (float)(i) / 1024f;
if(col.b>0) col.b = 1f- (float)(i) / 1024f;
col.a = 1f;
return col;
}
}



