Advertisement
Guest User

Untitled

a guest
Feb 4th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.75 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 Quaternion[] m_rotation = new Quaternion[5] {
  13.         Quaternion.Euler(-90, 0, -90), Quaternion.Euler(0, 90, 90),
  14.         Quaternion.AngleAxis(180,Vector3.right), Quaternion.Euler(0, -90, -90), Quaternion.Euler(-90, 0, 90)};
  15.  
  16.     // Use this for initialization
  17.     void Start ()
  18.     {
  19.         m_spacing /= 4;
  20.         m_size = Mathf.FloorToInt(Mathf.Pow(8, (float)m_order));
  21.         m_hilbert = new float[m_size];
  22.        
  23.         for(int i = 0; i<m_size; i++)
  24.         {
  25.             m_hilbert[i] = i*255 / m_size;
  26.         }
  27.  
  28.         drawHilbert();
  29.     }
  30.  
  31.     // Update is called once per frame
  32.     void Update()
  33.     {
  34.  
  35.     }
  36.  
  37.     void drawHilbert()
  38.     {
  39.         writeCell(0, 0, this.transform.position, this.transform.right, this.transform.up, this.transform.forward, m_rotation);
  40.     }
  41.  
  42.     int writeCell(int order, int index, Vector3 position, Vector3 right, Vector3 up, Vector3 forward, Quaternion[]rotation)
  43.     {
  44.         if (order >= m_order)
  45.         {
  46.             if (index != 0)
  47.             {
  48.                 Debug.DrawLine(m_lastPoint, position, Color.HSVToRGB(m_hilbert[index - 1] / 256, 1, 1), 100, true);
  49.             }
  50.             m_lastPoint = position;
  51.             return index+1;
  52.         } else
  53.         {
  54.             index = writeCell(order + 1, index, position + (- forward - up - right) * (m_spacing / Mathf.Pow(2, (float)order)),
  55.                 m_rotation[0] * right, m_rotation[0] * up, m_rotation[0] * forward, rotatedQuats(rotation, m_rotation[0]));
  56.             index = writeCell(order + 1, index, position + (forward - up - right) * (m_spacing / Mathf.Pow(2, (float)order)),
  57.             m_rotation[1] * right, m_rotation[1] * up, m_rotation[1] * forward, rotatedQuats(rotation, m_rotation[1]));
  58.             index = writeCell(order + 1, index, position + (forward + up - right) * (m_spacing / Mathf.Pow(2, (float)order)),
  59.             m_rotation[1] * right, m_rotation[1] * up, m_rotation[1] * forward, rotatedQuats(rotation, m_rotation[1]));
  60.             index = writeCell(order + 1, index, position + (- forward + up - right) * (m_spacing / Mathf.Pow(2, (float)order)),
  61.             m_rotation[2] * right, m_rotation[2] * up, m_rotation[2] * forward, rotatedQuats(rotation, m_rotation[2]));
  62.             index = writeCell(order + 1, index, position + (-forward + up + right) * (m_spacing / Mathf.Pow(2, (float)order)),
  63.             m_rotation[2] * right, m_rotation[2] * up, m_rotation[2] * forward, rotatedQuats(rotation, m_rotation[2]));
  64.             index = writeCell(order + 1, index, position + (forward + up + right) * (m_spacing / Mathf.Pow(2, (float)order)),
  65.             m_rotation[3] * right, m_rotation[3] * up, m_rotation[3] * forward, rotatedQuats(rotation, m_rotation[3]));
  66.             index = writeCell(order + 1, index, position + (forward - up + right) * (m_spacing / Mathf.Pow(2, (float)order)),
  67.             m_rotation[3] * right, m_rotation[3] * up, m_rotation[3] * forward, rotatedQuats(rotation, m_rotation[3]));
  68.             index = writeCell(order + 1, index, position + (-forward - up + right) * (m_spacing / Mathf.Pow(2, (float)order)),
  69.             m_rotation[4] * right, m_rotation[4] * up, m_rotation[4] * forward, rotatedQuats(rotation, m_rotation[4]));
  70.             return index;
  71.         }
  72.     }
  73.  
  74.     Quaternion[] rotatedQuats(Quaternion[] input, Quaternion rotation)
  75.     {
  76.         for(int i = 0; i<input.Length; i++)
  77.         {
  78.             input[i] = rotation * input[i];
  79.         }
  80.         return input;
  81.     }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement