teleias

A Quick Example of Gouraud Shading

Oct 30th, 2015
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.53 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4.  
  5.  
  6. public class PixelVertices : MonoBehaviour {
  7.     private List<PixelVertex> vertices = new List<PixelVertex>();
  8.     public void Start()
  9.     {
  10.         foreach(Transform t in transform)
  11.         {
  12.             vertices.Add(t.GetComponent<PixelVertex>());
  13.         }
  14.         Gouraud2D();
  15.     }
  16.     public void Gouraud2D()
  17.     {
  18.         const float EPSILON = 0.1f;
  19.         PixelVertex v1 = vertices[0];
  20.         PixelVertex v2 = vertices[1];
  21.         PixelVertex v3 = vertices[2];
  22.         Dictionary<Coordinate, GameObject> gos = new Dictionary<Coordinate, GameObject>();
  23.         for (int ix = 0; ix < 100; ix++)
  24.         {
  25.             for (int iz = 0; iz < 100; iz++)
  26.             {
  27.                 GameObject go = GameObject.CreatePrimitive(PrimitiveType.Sphere);
  28.                 go.transform.position = new Vector3(ix, 0, iz);
  29.                 go.GetComponent<Renderer>().enabled = false;
  30.                 gos.Add(new Coordinate(ix, iz), go);
  31.             }
  32.         }
  33.         {
  34.             Vector3 cur = v1.location;
  35.             Vector3 dir = (v2.location - v1.location).normalized;
  36.             float x, z;
  37.             x = v1.location.x;
  38.             z = v1.location.z;
  39.             int counter = 0;
  40.             do
  41.             {
  42.                 Coordinate c = new Coordinate(x, z);
  43.                 if (gos.ContainsKey(c))
  44.                 {
  45.                     GameObject go = gos[c];
  46.                     go.GetComponent<Renderer>().material.color = Color.Lerp(v1.color, v2.color, 1 - (v2.location - cur).magnitude / (v2.location - v1.location).magnitude);
  47.                     go.GetComponent<Renderer>().enabled = true;
  48.                 }
  49.                 x += dir.x;
  50.                 z += dir.z;
  51.                 cur += dir;
  52.             } while (counter++ < 10000 && Mathf.Abs((v2.location - cur).normalized.x - dir.x) < EPSILON && Mathf.Abs((v2.location - cur).normalized.z - dir.z) < EPSILON);
  53.         }
  54.         {
  55.             Vector3 cur = v2.location;
  56.             Vector3 dir = (v3.location - v2.location).normalized;
  57.             float x, z;
  58.             x = v2.location.x;
  59.             z = v2.location.z;
  60.             int counter = 0;
  61.             do
  62.             {
  63.                 Coordinate c = new Coordinate(x, z);
  64.                 if (gos.ContainsKey(c))
  65.                 {
  66.                     GameObject go = gos[c];
  67.                     go.GetComponent<Renderer>().material.color = Color.Lerp(v2.color, v3.color, 1 - (v3.location - cur).magnitude / (v3.location - v2.location).magnitude);
  68.                     go.GetComponent<Renderer>().enabled = true;
  69.                 }
  70.                 x += dir.x;
  71.                 z += dir.z;
  72.                 cur += dir;
  73.                 Debug.Log(counter);
  74.                 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)));
  75.             } while (counter++ < 10000 && Mathf.Abs((v3.location - cur).normalized.x - dir.x) < EPSILON && Mathf.Abs((v3.location - cur).normalized.z - dir.z) < EPSILON);
  76.         }
  77.         {
  78.             Vector3 cur = v3.location;
  79.             Vector3 dir = (v1.location - v3.location).normalized;
  80.             float x, z;
  81.             x = v3.location.x;
  82.             z = v3.location.z;
  83.             int counter = 0;
  84.             do
  85.             {
  86.                 Coordinate c = new Coordinate(x, z);
  87.                 if (gos.ContainsKey(c))
  88.                 {
  89.                     GameObject go = gos[c];
  90.                     go.GetComponent<Renderer>().material.color = Color.Lerp(v3.color, v1.color, 1 - (v1.location - cur).magnitude / (v1.location - v3.location).magnitude);
  91.                     go.GetComponent<Renderer>().enabled = true;
  92.                 }
  93.                 x += dir.x;
  94.                 z += dir.z;
  95.                 cur += dir;
  96.             } while (counter++ < 10000 && Mathf.Abs((v1.location - cur).normalized.x - dir.x) < EPSILON && Mathf.Abs((v1.location - cur).normalized.z - dir.z) < EPSILON);
  97.         }
  98.  
  99.  
  100.  
  101.         for (int iz = 0; iz < 100; iz++)
  102.         {
  103.             int start = -1, end = -1;
  104.             Color? colorStart = null, colorEnd = null;
  105.             Coordinate c;
  106.             //find start
  107.             {
  108.                 int ix = 0;
  109.                 while (true)
  110.                 {
  111.                     if (ix >= 100)
  112.                         break;
  113.  
  114.                     c = new Coordinate(ix, iz);
  115.                     if (gos.ContainsKey(c) && gos[c].GetComponent<Renderer>().enabled)
  116.                     {
  117.                         start = ix;
  118.                         colorStart = gos[c].GetComponent<Renderer>().material.color;
  119.                         break;
  120.                     }
  121.                     ix++;
  122.                 }
  123.                 //find end
  124.                 ix = 100 - 1;
  125.                 while (true)
  126.                 {
  127.                     if (ix <= 0)
  128.                         break;
  129.  
  130.                     c = new Coordinate(ix, iz);
  131.                     if (gos.ContainsKey(c) && gos[c].GetComponent<Renderer>().enabled)
  132.                     {
  133.                         end = ix;
  134.                         colorEnd = gos[c].GetComponent<Renderer>().material.color;
  135.                         break;
  136.                     }
  137.                     ix--;
  138.                 }
  139.             }
  140.             if (start == -1 || end == -1 || colorStart == null || colorEnd == null)
  141.                 continue;
  142.             //fill in betwixt
  143.             for(int ix = start; ix <= end; ix++)
  144.             {
  145.                 c = new Coordinate(ix, iz);
  146.                 if(gos.ContainsKey(c))
  147.                 {
  148.                     gos[c].GetComponent<Renderer>().material.color = Color.Lerp((Color)colorStart, (Color)colorEnd, (end - ix)/(float)(end - start));
  149.                     gos[c].GetComponent<Renderer>().enabled = true;
  150.                 }
  151.             }
  152.         }
  153.     }
  154.     class Coordinate
  155.     {
  156.         public int x, z;
  157.         public Coordinate(float x, float z) : this((int)x, (int)z) { }
  158.         public Coordinate(int x, int z)
  159.         {
  160.             this.x = x;
  161.             this.z = z;
  162.         }
  163.         public override bool Equals(object obj)
  164.         {
  165.             return (obj.GetType() == typeof(Coordinate)) && Equals((Coordinate)obj);
  166.         }
  167.         public bool Equals(Coordinate obj)
  168.         {
  169.             return obj.x == x && obj.z == z;
  170.         }
  171.         public override int GetHashCode()
  172.         {
  173.             return 0;
  174.         }
  175.     }
  176. }
Advertisement
Add Comment
Please, Sign In to add comment