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 Vector3[] m_rotation = new Vector3[5] { new Vector3(-90, 0, -90), new Vector3(0, 90, 90),
- new Vector3(180, 0, 0), new Vector3(0, -90, -90), new Vector3(-90, 0, 90)};
- // Use this for initialization
- void Start ()
- {
- 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);
- }
- int writeCell(int order, int index, Vector3 position, Vector3 right, Vector3 up, Vector3 forward)
- {
- if (order >= m_order)
- {
- if (index != 0)
- {
- Debug.DrawLine(m_lastPoint, position, Color.HSVToRGB(m_hilbert[index - 1] / 256, 1, 1), 100);
- }
- m_lastPoint = position;
- return index+1;
- } else
- {
- index = writeCell(order + 1, index, position + (- forward - up - right) * (m_spacing / Mathf.Pow(2, (float)order)),
- Quaternion.Euler(m_rotation[0]) * right, Quaternion.Euler(m_rotation[0]) * up, Quaternion.Euler(m_rotation[0]) * forward);
- index = writeCell(order + 1, index, position + (forward - up - right) * (m_spacing / Mathf.Pow(2, (float)order)),
- Quaternion.Euler(m_rotation[1]) * right, Quaternion.Euler(m_rotation[1]) * up, Quaternion.Euler(m_rotation[1]) * forward);
- index = writeCell(order + 1, index, position + (forward + up - right) * (m_spacing / Mathf.Pow(2, (float)order)),
- Quaternion.Euler(m_rotation[1]) * right, Quaternion.Euler(m_rotation[1]) * up, Quaternion.Euler(m_rotation[1]) * forward);
- index = writeCell(order + 1, index, position + (- forward + up - right) * (m_spacing / Mathf.Pow(2, (float)order)),
- Quaternion.Euler(m_rotation[2]) * right, Quaternion.Euler(m_rotation[2]) * up, Quaternion.Euler(m_rotation[2]) * forward);
- index = writeCell(order + 1, index, position + (-forward + up + right) * (m_spacing / Mathf.Pow(2, (float)order)),
- Quaternion.Euler(m_rotation[2]) * right, Quaternion.Euler(m_rotation[2]) * up, Quaternion.Euler(m_rotation[2]) * forward);
- index = writeCell(order + 1, index, position + (forward + up + right) * (m_spacing / Mathf.Pow(2, (float)order)),
- Quaternion.Euler(m_rotation[3]) * right, Quaternion.Euler(m_rotation[3]) * up, Quaternion.Euler(m_rotation[3]) * forward);
- index = writeCell(order + 1, index, position + (forward - up + right) * (m_spacing / Mathf.Pow(2, (float)order)),
- Quaternion.Euler(m_rotation[3]) * right, Quaternion.Euler(m_rotation[3]) * up, Quaternion.Euler(m_rotation[3]) * forward);
- index = writeCell(order + 1, index, position + (-forward - up + right) * (m_spacing / Mathf.Pow(2, (float)order)),
- Quaternion.Euler(m_rotation[4]) * right, Quaternion.Euler(m_rotation[4]) * up, Quaternion.Euler(m_rotation[4]) * forward);
- return index;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement