Fractal.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. using System;
  2. using System.Collections;
  3. using UnityEngine;
  4. public class Fractal : MonoBehaviour
  5. {
  6. private void Start()
  7. {
  8. this._root = new TreeNode<Transform>(this.CreateAndSetPrimitive(Vector3.zero, 1f, base.transform));
  9. this.Repaint(1, this._root);
  10. }
  11. private void Update()
  12. {
  13. }
  14. private void Repaint(int level, TreeNode<Transform> parent)
  15. {
  16. if (level > this._maxLevel)
  17. {
  18. if (parent.ChildCount != 0)
  19. {
  20. for (int i = 0; i < parent.Children.Count; i++)
  21. {
  22. TreeNode<Transform> treeNode = parent.Children[i];
  23. treeNode.Traverse(delegate(Transform g)
  24. {
  25. if (g != null)
  26. {
  27. UnityEngine.Object.Destroy(g.gameObject);
  28. }
  29. });
  30. treeNode.ClearChild();
  31. }
  32. parent.ClearChild();
  33. }
  34. return;
  35. }
  36. for (int j = 0; j < 8; j++)
  37. {
  38. if (parent.ChildCount < j + 1)
  39. {
  40. parent.AddChild(this.CreateAndSetPrimitive(this.ChildPositions()[j], this._scale, parent.Value));
  41. }
  42. else
  43. {
  44. this.SetPrimitive(parent[j].Value, this.ChildPositions()[j], this._scale);
  45. }
  46. this.Repaint(level + 1, parent[j]);
  47. }
  48. }
  49. private void Clear()
  50. {
  51. this._root.Traverse(delegate(Transform g)
  52. {
  53. if (g != null)
  54. {
  55. UnityEngine.Object.Destroy(g.gameObject);
  56. }
  57. });
  58. this._root.ClearChild();
  59. this._root = new TreeNode<Transform>(this.CreateAndSetPrimitive(Vector3.zero, 1f, null));
  60. }
  61. private Transform CreateAndSetPrimitive(Vector3 position, float scale, Transform parent)
  62. {
  63. return this.SetPrimitive(this.CreatePrimitive(parent), position, scale);
  64. }
  65. private Transform CreatePrimitive(Transform parent)
  66. {
  67. Transform transform = GameObject.CreatePrimitive(this._sharpType).transform;
  68. transform.GetComponent<Renderer>().sharedMaterial = this.Mat;
  69. transform.parent = parent;
  70. return transform;
  71. }
  72. private Transform SetPrimitive(Transform primitive, Vector3 position, float scale)
  73. {
  74. primitive.transform.localPosition = position;
  75. primitive.transform.localScale = new Vector3(scale, scale, scale);
  76. return primitive;
  77. }
  78. private Vector3[] ChildPositions()
  79. {
  80. Vector3 rootPosition = this._rootPosition;
  81. Vector3 vector = new Vector3(rootPosition.x - this._distance, rootPosition.y + this._distance, rootPosition.z + this._distance);
  82. Vector3 vector2 = new Vector3(rootPosition.x + this._distance, rootPosition.y + this._distance, rootPosition.z + this._distance);
  83. Vector3 vector3 = new Vector3(rootPosition.x - this._distance, rootPosition.y - this._distance, rootPosition.z + this._distance);
  84. Vector3 vector4 = new Vector3(rootPosition.x + this._distance, rootPosition.y - this._distance, rootPosition.z + this._distance);
  85. Vector3 vector5 = new Vector3(rootPosition.x - this._distance, rootPosition.y + this._distance, rootPosition.z - this._distance);
  86. Vector3 vector6 = new Vector3(rootPosition.x + this._distance, rootPosition.y + this._distance, rootPosition.z - this._distance);
  87. Vector3 vector7 = new Vector3(rootPosition.x - this._distance, rootPosition.y - this._distance, rootPosition.z - this._distance);
  88. Vector3 vector8 = new Vector3(rootPosition.x + this._distance, rootPosition.y - this._distance, rootPosition.z - this._distance);
  89. return new Vector3[]
  90. {
  91. vector,
  92. vector2,
  93. vector3,
  94. vector4,
  95. vector5,
  96. vector6,
  97. vector7,
  98. vector8
  99. };
  100. }
  101. [SerializeField]
  102. private PrimitiveType _sharpType;
  103. [SerializeField]
  104. private float _distance = 1f;
  105. [SerializeField]
  106. private float _scale = 0.5f;
  107. [SerializeField]
  108. private Vector3 _rootPosition;
  109. [Range(0f, 10f)]
  110. [SerializeField]
  111. private int _maxLevel;
  112. private TreeNode<Transform> _root;
  113. [SerializeField]
  114. public Material Mat;
  115. }