Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- public class PixelVertices : MonoBehaviour {
- private List<PixelVertex> vertices = new List<PixelVertex>();
- public void Start()
- {
- foreach(Transform t in transform)
- {
- vertices.Add(t.GetComponent<PixelVertex>());
- }
- Gouraud2D();
- }
- public void Gouraud2D()
- {
- const float EPSILON = 0.1f;
- PixelVertex v1 = vertices[0];
- PixelVertex v2 = vertices[1];
- PixelVertex v3 = vertices[2];
- Dictionary<Coordinate, GameObject> gos = new Dictionary<Coordinate, GameObject>();
- for (int ix = 0; ix < 100; ix++)
- {
- for (int iz = 0; iz < 100; iz++)
- {
- GameObject go = GameObject.CreatePrimitive(PrimitiveType.Sphere);
- go.transform.position = new Vector3(ix, 0, iz);
- go.GetComponent<Renderer>().enabled = false;
- gos.Add(new Coordinate(ix, iz), go);
- }
- }
- {
- Vector3 cur = v1.location;
- Vector3 dir = (v2.location - v1.location).normalized;
- float x, z;
- x = v1.location.x;
- z = v1.location.z;
- int counter = 0;
- do
- {
- Coordinate c = new Coordinate(x, z);
- if (gos.ContainsKey(c))
- {
- GameObject go = gos[c];
- go.GetComponent<Renderer>().material.color = Color.Lerp(v1.color, v2.color, 1 - (v2.location - cur).magnitude / (v2.location - v1.location).magnitude);
- go.GetComponent<Renderer>().enabled = true;
- }
- x += dir.x;
- z += dir.z;
- cur += dir;
- } while (counter++ < 10000 && Mathf.Abs((v2.location - cur).normalized.x - dir.x) < EPSILON && Mathf.Abs((v2.location - cur).normalized.z - dir.z) < EPSILON);
- }
- {
- Vector3 cur = v2.location;
- Vector3 dir = (v3.location - v2.location).normalized;
- float x, z;
- x = v2.location.x;
- z = v2.location.z;
- int counter = 0;
- do
- {
- Coordinate c = new Coordinate(x, z);
- if (gos.ContainsKey(c))
- {
- GameObject go = gos[c];
- go.GetComponent<Renderer>().material.color = Color.Lerp(v2.color, v3.color, 1 - (v3.location - cur).magnitude / (v3.location - v2.location).magnitude);
- go.GetComponent<Renderer>().enabled = true;
- }
- x += dir.x;
- z += dir.z;
- cur += dir;
- Debug.Log(counter);
- Debug.Log((v3.location - cur).normalized + " " + dir + (Mathf.Approximately((v3.location - cur).normalized.x, dir.x) && Mathf.Approximately((v3.location - cur).normalized.z, dir.z)));
- } while (counter++ < 10000 && Mathf.Abs((v3.location - cur).normalized.x - dir.x) < EPSILON && Mathf.Abs((v3.location - cur).normalized.z - dir.z) < EPSILON);
- }
- {
- Vector3 cur = v3.location;
- Vector3 dir = (v1.location - v3.location).normalized;
- float x, z;
- x = v3.location.x;
- z = v3.location.z;
- int counter = 0;
- do
- {
- Coordinate c = new Coordinate(x, z);
- if (gos.ContainsKey(c))
- {
- GameObject go = gos[c];
- go.GetComponent<Renderer>().material.color = Color.Lerp(v3.color, v1.color, 1 - (v1.location - cur).magnitude / (v1.location - v3.location).magnitude);
- go.GetComponent<Renderer>().enabled = true;
- }
- x += dir.x;
- z += dir.z;
- cur += dir;
- } while (counter++ < 10000 && Mathf.Abs((v1.location - cur).normalized.x - dir.x) < EPSILON && Mathf.Abs((v1.location - cur).normalized.z - dir.z) < EPSILON);
- }
- for (int iz = 0; iz < 100; iz++)
- {
- int start = -1, end = -1;
- Color? colorStart = null, colorEnd = null;
- Coordinate c;
- //find start
- {
- int ix = 0;
- while (true)
- {
- if (ix >= 100)
- break;
- c = new Coordinate(ix, iz);
- if (gos.ContainsKey(c) && gos[c].GetComponent<Renderer>().enabled)
- {
- start = ix;
- colorStart = gos[c].GetComponent<Renderer>().material.color;
- break;
- }
- ix++;
- }
- //find end
- ix = 100 - 1;
- while (true)
- {
- if (ix <= 0)
- break;
- c = new Coordinate(ix, iz);
- if (gos.ContainsKey(c) && gos[c].GetComponent<Renderer>().enabled)
- {
- end = ix;
- colorEnd = gos[c].GetComponent<Renderer>().material.color;
- break;
- }
- ix--;
- }
- }
- if (start == -1 || end == -1 || colorStart == null || colorEnd == null)
- continue;
- //fill in betwixt
- for(int ix = start; ix <= end; ix++)
- {
- c = new Coordinate(ix, iz);
- if(gos.ContainsKey(c))
- {
- gos[c].GetComponent<Renderer>().material.color = Color.Lerp((Color)colorStart, (Color)colorEnd, (end - ix)/(float)(end - start));
- gos[c].GetComponent<Renderer>().enabled = true;
- }
- }
- }
- }
- class Coordinate
- {
- public int x, z;
- public Coordinate(float x, float z) : this((int)x, (int)z) { }
- public Coordinate(int x, int z)
- {
- this.x = x;
- this.z = z;
- }
- public override bool Equals(object obj)
- {
- return (obj.GetType() == typeof(Coordinate)) && Equals((Coordinate)obj);
- }
- public bool Equals(Coordinate obj)
- {
- return obj.x == x && obj.z == z;
- }
- public override int GetHashCode()
- {
- return 0;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment