Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class Hilbert : MonoBehaviour {
- public int m_order = 3;
- public float m_spacing = 1;
- private int m_size;
- private float[] m_hilbert;
- private Vector3 m_lastPoint = new Vector3(0, 0, 0);
- private Quaternion[] m_rotation = new Quaternion[5] {
- Quaternion.Euler(-90, 0, -90), Quaternion.Euler(0, 90, 90),
- Quaternion.AngleAxis(180,Vector3.right), Quaternion.Euler(0, -90, -90), Quaternion.Euler(-90, 0, 90)};
- // Use this for initialization
- void Start ()
- {
- m_spacing /= 4;
- m_size = Mathf.FloorToInt(Mathf.Pow(8, (float)m_order));
- m_hilbert = new float[m_size];
- for(int i = 0; i<m_size; i++)
- {
- m_hilbert[i] = i*255 / m_size;
- }
- drawHilbert();
- }
- // Update is called once per frame
- void Update()
- {
- }
- void drawHilbert()
- {
- writeCell(0, 0, this.transform.position, this.transform.right, this.transform.up, this.transform.forward, m_rotation);
- }
- int writeCell(int order, int index, Vector3 position, Vector3 right, Vector3 up, Vector3 forward, Quaternion[]rotation)
- {
- if (order >= m_order)
- {
- if (index != 0)
- {
- Debug.DrawLine(m_lastPoint, position, Color.HSVToRGB(m_hilbert[index - 1] / 256, 1, 1), 100, true);
- }
- m_lastPoint = position;
- return index+1;
- } else
- {
- index = writeCell(order + 1, index, position + (- forward - up - right) * (m_spacing / Mathf.Pow(2, (float)order)),
- m_rotation[0] * right, m_rotation[0] * up, m_rotation[0] * forward, rotatedQuats(rotation, m_rotation[0]));
- index = writeCell(order + 1, index, position + (forward - up - right) * (m_spacing / Mathf.Pow(2, (float)order)),
- m_rotation[1] * right, m_rotation[1] * up, m_rotation[1] * forward, rotatedQuats(rotation, m_rotation[1]));
- index = writeCell(order + 1, index, position + (forward + up - right) * (m_spacing / Mathf.Pow(2, (float)order)),
- m_rotation[1] * right, m_rotation[1] * up, m_rotation[1] * forward, rotatedQuats(rotation, m_rotation[1]));
- index = writeCell(order + 1, index, position + (- forward + up - right) * (m_spacing / Mathf.Pow(2, (float)order)),
- m_rotation[2] * right, m_rotation[2] * up, m_rotation[2] * forward, rotatedQuats(rotation, m_rotation[2]));
- index = writeCell(order + 1, index, position + (-forward + up + right) * (m_spacing / Mathf.Pow(2, (float)order)),
- m_rotation[2] * right, m_rotation[2] * up, m_rotation[2] * forward, rotatedQuats(rotation, m_rotation[2]));
- index = writeCell(order + 1, index, position + (forward + up + right) * (m_spacing / Mathf.Pow(2, (float)order)),
- m_rotation[3] * right, m_rotation[3] * up, m_rotation[3] * forward, rotatedQuats(rotation, m_rotation[3]));
- index = writeCell(order + 1, index, position + (forward - up + right) * (m_spacing / Mathf.Pow(2, (float)order)),
- m_rotation[3] * right, m_rotation[3] * up, m_rotation[3] * forward, rotatedQuats(rotation, m_rotation[3]));
- index = writeCell(order + 1, index, position + (-forward - up + right) * (m_spacing / Mathf.Pow(2, (float)order)),
- m_rotation[4] * right, m_rotation[4] * up, m_rotation[4] * forward, rotatedQuats(rotation, m_rotation[4]));
- return index;
- }
- }
- Quaternion[] rotatedQuats(Quaternion[] input, Quaternion rotation)
- {
- for(int i = 0; i<input.Length; i++)
- {
- input[i] = rotation * input[i];
- }
- return input;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement