Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using Unity.VisualScripting;
- using Unity.VisualScripting.FullSerializer;
- using UnityEngine;
- [ExecuteInEditMode]
- public class WardrobeController : MonoBehaviour
- {
- [SerializeField]
- public List<PanelGeometryData> panels;
- private BinarySpacePartition bsp;
- // Start is called before the first frame update
- void Start()
- {
- }
- // Update is called once per frame
- void Update()
- {
- }
- private void OnDrawGizmos()
- {
- bsp = new BinarySpacePartition();
- BinarySpacePartition.Node node = bsp.root;
- int counter = 0;
- // Loop through all panel geometry data
- foreach (PanelGeometryData panel in panels)
- {
- // Get size of the panel - note it is as laid down on ground (thickness being Y axis)
- Vector3 size = new Vector3(panel.Size.x, panel.Size.z, panel.Size.y);
- // Build bounding box vertices for the panel adding size on one side only
- Vector4[] vertices = new Vector4[8];
- vertices[0] = new Vector4(0.0f, 0.0f, 0.0f, 1.0f);
- vertices[1] = new Vector4(size.x, 0.0f, 0.0f, 1.0f);
- vertices[2] = new Vector4(0.0f, size.y, 0.0f, 1.0f);
- vertices[3] = new Vector4(size.x, size.y, 0.0f, 1.0f);
- vertices[4] = new Vector4(0.0f, 0.0f, size.z, 1.0f);
- vertices[5] = new Vector4(size.x, 0.0f, size.z, 1.0f);
- vertices[6] = new Vector4(0.0f, size.y, size.z, 1.0f);
- vertices[7] = new Vector4(size.x, size.y, size.z, 1.0f);
- // Built transformation matrix for the panel
- Matrix4x4 matrix = Matrix4x4.Translate(panel.Position) * Matrix4x4.Rotate(Quaternion.Euler(panel.Rotation));
- // Transform vertices with matrix
- for (int i = 0; i < vertices.Length; i++)
- {
- vertices[i] = matrix * vertices[i];
- }
- // Get center point
- Vector3 center = new Vector3(0.0f, 0.0f, 0.0f);
- for (int i = 0; i < vertices.Length; i++)
- {
- center += new Vector3(vertices[i].x, vertices[i].y, vertices[i].z);
- }
- center /= vertices.Length;
- // Get normal of the panel
- Vector3 u = new Vector3(vertices[1].x - vertices[0].x, vertices[1].y - vertices[0].y, vertices[1].z - vertices[0].z);
- Vector3 v = new Vector3(vertices[4].x - vertices[0].x, vertices[4].y - vertices[0].y, vertices[4].z - vertices[0].z);
- Vector3 normal = Vector3.Cross(u, v).normalized;
- // Draw lines of bounding box for the panel
- Gizmos.color = Color.green;
- Gizmos.DrawLine(new Vector3(vertices[0].x, vertices[0].y, vertices[0].z), new Vector3(vertices[1].x, vertices[1].y, vertices[1].z));
- Gizmos.DrawLine(new Vector3(vertices[0].x, vertices[0].y, vertices[0].z), new Vector3(vertices[2].x, vertices[2].y, vertices[2].z));
- Gizmos.DrawLine(new Vector3(vertices[1].x, vertices[1].y, vertices[1].z), new Vector3(vertices[3].x, vertices[3].y, vertices[3].z));
- Gizmos.DrawLine(new Vector3(vertices[2].x, vertices[2].y, vertices[2].z), new Vector3(vertices[3].x, vertices[3].y, vertices[3].z));
- Gizmos.DrawLine(new Vector3(vertices[4].x, vertices[4].y, vertices[4].z), new Vector3(vertices[5].x, vertices[5].y, vertices[5].z));
- Gizmos.DrawLine(new Vector3(vertices[4].x, vertices[4].y, vertices[4].z), new Vector3(vertices[6].x, vertices[6].y, vertices[6].z));
- Gizmos.DrawLine(new Vector3(vertices[5].x, vertices[5].y, vertices[5].z), new Vector3(vertices[7].x, vertices[7].y, vertices[7].z));
- Gizmos.DrawLine(new Vector3(vertices[6].x, vertices[6].y, vertices[6].z), new Vector3(vertices[7].x, vertices[7].y, vertices[7].z));
- Gizmos.DrawLine(new Vector3(vertices[0].x, vertices[0].y, vertices[0].z), new Vector3(vertices[4].x, vertices[4].y, vertices[4].z));
- Gizmos.DrawLine(new Vector3(vertices[1].x, vertices[1].y, vertices[1].z), new Vector3(vertices[5].x, vertices[5].y, vertices[5].z));
- Gizmos.DrawLine(new Vector3(vertices[2].x, vertices[2].y, vertices[2].z), new Vector3(vertices[6].x, vertices[6].y, vertices[6].z));
- Gizmos.DrawLine(new Vector3(vertices[3].x, vertices[3].y, vertices[3].z), new Vector3(vertices[7].x, vertices[7].y, vertices[7].z));
- // Draw normal
- Gizmos.color = Color.red;
- Gizmos.DrawLine(center, center + normal * 10.0f);
- if (Mathf.Abs(normal.z) > Mathf.Abs(normal.x) && Mathf.Abs(normal.z) > Mathf.Abs(normal.y))
- {
- continue;
- }
- // Extract 2D splitting line from normal
- Vector2 split = new Vector2(normal.x, normal.y);
- split.Normalize();
- if (counter > 0)
- {
- // Check if we have to split left or right one
- Vector2 center2D = new Vector2(center.x, center.y);
- Vector2 nodeCenter2D = new Vector2(node.min.x + (node.max.x - node.min.x) / 2.0f, node.min.y + (node.max.y - node.min.y) / 2.0f);
- if (Vector2.Dot(center2D - nodeCenter2D, split) <= 0.0f)
- {
- node = node.left;
- }
- else
- {
- node = node.right;
- }
- }
- // Split the panel
- if (Mathf.Abs(split.x) > Mathf.Abs(split.y))
- {
- node.Split(0, center.x);
- }
- else
- {
- node.Split(1, center.y);
- }
- counter++;
- }
- // Draw the BSP tree
- Stack<BinarySpacePartition.Node> stack = new Stack<BinarySpacePartition.Node>();
- stack.Push(bsp.root);
- int iters = 0;
- while (stack.Count > 0)
- {
- BinarySpacePartition.Node n = stack.Pop();
- // If n is leaf node (no children)
- if (n.left == null && n.right == null)
- {
- Gizmos.color = Color.blue;
- Gizmos.DrawLine(new Vector3(n.min.x, n.min.y, 0.0f), new Vector3(n.max.x, n.min.y, 0.0f));
- Gizmos.DrawLine(new Vector3(n.max.x, n.min.y, 0.0f), new Vector3(n.max.x, n.max.y, 0.0f));
- Gizmos.DrawLine(new Vector3(n.max.x, n.max.y, 0.0f), new Vector3(n.min.x, n.max.y, 0.0f));
- Gizmos.DrawLine(new Vector3(n.min.x, n.max.y, 0.0f), new Vector3(n.min.x, n.min.y, 0.0f));
- }
- else
- {
- if (n.left != null)
- {
- stack.Push(n.left);
- }
- if (n.right != null)
- {
- stack.Push(n.right);
- }
- }
- iters++;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement