Advertisement
Guest User

Untitled

a guest
Feb 4th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.53 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class Hilbert : MonoBehaviour {
  6.  
  7.     public int m_order = 3;
  8.     public float m_spacing = 1;
  9.     private int m_size;
  10.     private float[] m_hilbert;
  11.     private Vector3 m_lastPoint = new Vector3(0, 0, 0);
  12.     private Vector3[] m_rotation = new Vector3[5] { new Vector3(-90, 0, -90), new Vector3(0, 90, 90),
  13.         new Vector3(180, 0, 0), new Vector3(0, -90, -90), new Vector3(-90, 0, 90)};
  14.  
  15.     // Use this for initialization
  16.     void Start ()
  17.     {
  18.         m_size = Mathf.FloorToInt(Mathf.Pow(8, (float)m_order));
  19.         m_hilbert = new float[m_size];
  20.        
  21.         for(int i = 0; i<m_size; i++)
  22.         {
  23.             m_hilbert[i] = i*255 / m_size;
  24.         }
  25.  
  26.         drawHilbert();
  27.     }
  28.  
  29.     // Update is called once per frame
  30.     void Update()
  31.     {
  32.  
  33.     }
  34.  
  35.     void drawHilbert()
  36.     {
  37.         writeCell(0, 0, this.transform.position, this.transform.right, this.transform.up, this.transform.forward);
  38.     }
  39.  
  40.     int writeCell(int order, int index, Vector3 position, Vector3 right, Vector3 up, Vector3 forward)
  41.     {
  42.         if (order >= m_order)
  43.         {
  44.             if (index != 0)
  45.             {
  46.                 Debug.DrawLine(m_lastPoint, position, Color.HSVToRGB(m_hilbert[index - 1] / 256, 1, 1), 100);
  47.             }
  48.             m_lastPoint = position;
  49.             return index+1;
  50.         } else
  51.         {
  52.             index = writeCell(order + 1, index, position + (- forward - up - right) * (m_spacing / Mathf.Pow(2, (float)order)),
  53.                 Quaternion.Euler(m_rotation[0]) * right, Quaternion.Euler(m_rotation[0]) * up, Quaternion.Euler(m_rotation[0]) * forward);
  54.             index = writeCell(order + 1, index, position + (forward - up - right) * (m_spacing / Mathf.Pow(2, (float)order)),
  55.             Quaternion.Euler(m_rotation[1]) * right, Quaternion.Euler(m_rotation[1]) * up, Quaternion.Euler(m_rotation[1]) * forward);
  56.             index = writeCell(order + 1, index, position + (forward + up - right) * (m_spacing / Mathf.Pow(2, (float)order)),
  57.             Quaternion.Euler(m_rotation[1]) * right, Quaternion.Euler(m_rotation[1]) * up, Quaternion.Euler(m_rotation[1]) * forward);
  58.             index = writeCell(order + 1, index, position + (- forward + up - right) * (m_spacing / Mathf.Pow(2, (float)order)),
  59.             Quaternion.Euler(m_rotation[2]) * right, Quaternion.Euler(m_rotation[2]) * up, Quaternion.Euler(m_rotation[2]) * forward);
  60.             index = writeCell(order + 1, index, position + (-forward + up + right) * (m_spacing / Mathf.Pow(2, (float)order)),
  61.             Quaternion.Euler(m_rotation[2]) * right, Quaternion.Euler(m_rotation[2]) * up, Quaternion.Euler(m_rotation[2]) * forward);
  62.             index = writeCell(order + 1, index, position + (forward + up + right) * (m_spacing / Mathf.Pow(2, (float)order)),
  63.             Quaternion.Euler(m_rotation[3]) * right, Quaternion.Euler(m_rotation[3]) * up, Quaternion.Euler(m_rotation[3]) * forward);
  64.             index = writeCell(order + 1, index, position + (forward - up + right) * (m_spacing / Mathf.Pow(2, (float)order)),
  65.             Quaternion.Euler(m_rotation[3]) * right, Quaternion.Euler(m_rotation[3]) * up, Quaternion.Euler(m_rotation[3]) * forward);
  66.             index = writeCell(order + 1, index, position + (-forward - up + right) * (m_spacing / Mathf.Pow(2, (float)order)),
  67.             Quaternion.Euler(m_rotation[4]) * right, Quaternion.Euler(m_rotation[4]) * up, Quaternion.Euler(m_rotation[4]) * forward);
  68.             return index;
  69.         }
  70.     }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement