Advertisement
Zgragselus

Untitled

Aug 13th, 2024
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.93 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using Unity.VisualScripting;
  4. using Unity.VisualScripting.FullSerializer;
  5. using UnityEngine;
  6.  
  7. [ExecuteInEditMode]
  8. public class WardrobeController : MonoBehaviour
  9. {
  10.     [SerializeField]
  11.     public List<PanelGeometryData> panels;
  12.  
  13.     private BinarySpacePartition bsp;
  14.  
  15.     // Start is called before the first frame update
  16.     void Start()
  17.     {
  18.        
  19.     }
  20.  
  21.     // Update is called once per frame
  22.     void Update()
  23.     {
  24.    
  25.     }
  26.  
  27.     private void OnDrawGizmos()
  28.     {
  29.         bsp = new BinarySpacePartition();
  30.         BinarySpacePartition.Node node = bsp.root;
  31.         int counter = 0;
  32.  
  33.         // Loop through all panel geometry data
  34.         foreach (PanelGeometryData panel in panels)
  35.         {
  36.             // Get size of the panel - note it is as laid down on ground (thickness being Y axis)
  37.             Vector3 size = new Vector3(panel.Size.x, panel.Size.z, panel.Size.y);
  38.  
  39.             // Build bounding box vertices for the panel adding size on one side only
  40.             Vector4[] vertices = new Vector4[8];
  41.             vertices[0] = new Vector4(0.0f, 0.0f, 0.0f, 1.0f);
  42.             vertices[1] = new Vector4(size.x, 0.0f, 0.0f, 1.0f);
  43.             vertices[2] = new Vector4(0.0f, size.y, 0.0f, 1.0f);
  44.             vertices[3] = new Vector4(size.x, size.y, 0.0f, 1.0f);
  45.             vertices[4] = new Vector4(0.0f, 0.0f, size.z, 1.0f);
  46.             vertices[5] = new Vector4(size.x, 0.0f, size.z, 1.0f);
  47.             vertices[6] = new Vector4(0.0f, size.y, size.z, 1.0f);
  48.             vertices[7] = new Vector4(size.x, size.y, size.z, 1.0f);
  49.  
  50.             // Built transformation matrix for the panel
  51.             Matrix4x4 matrix = Matrix4x4.Translate(panel.Position) * Matrix4x4.Rotate(Quaternion.Euler(panel.Rotation));
  52.  
  53.             // Transform vertices with matrix
  54.             for (int i = 0; i < vertices.Length; i++)
  55.             {
  56.                 vertices[i] = matrix * vertices[i];
  57.             }
  58.  
  59.             // Get center point
  60.             Vector3 center = new Vector3(0.0f, 0.0f, 0.0f);
  61.             for (int i = 0; i < vertices.Length; i++)
  62.             {
  63.                 center += new Vector3(vertices[i].x, vertices[i].y, vertices[i].z);
  64.             }
  65.             center /= vertices.Length;
  66.  
  67.             // Get normal of the panel
  68.             Vector3 u = new Vector3(vertices[1].x - vertices[0].x, vertices[1].y - vertices[0].y, vertices[1].z - vertices[0].z);
  69.             Vector3 v = new Vector3(vertices[4].x - vertices[0].x, vertices[4].y - vertices[0].y, vertices[4].z - vertices[0].z);
  70.             Vector3 normal = Vector3.Cross(u, v).normalized;
  71.  
  72.             // Draw lines of bounding box for the panel
  73.             Gizmos.color = Color.green;
  74.             Gizmos.DrawLine(new Vector3(vertices[0].x, vertices[0].y, vertices[0].z), new Vector3(vertices[1].x, vertices[1].y, vertices[1].z));
  75.             Gizmos.DrawLine(new Vector3(vertices[0].x, vertices[0].y, vertices[0].z), new Vector3(vertices[2].x, vertices[2].y, vertices[2].z));
  76.             Gizmos.DrawLine(new Vector3(vertices[1].x, vertices[1].y, vertices[1].z), new Vector3(vertices[3].x, vertices[3].y, vertices[3].z));
  77.             Gizmos.DrawLine(new Vector3(vertices[2].x, vertices[2].y, vertices[2].z), new Vector3(vertices[3].x, vertices[3].y, vertices[3].z));
  78.             Gizmos.DrawLine(new Vector3(vertices[4].x, vertices[4].y, vertices[4].z), new Vector3(vertices[5].x, vertices[5].y, vertices[5].z));
  79.             Gizmos.DrawLine(new Vector3(vertices[4].x, vertices[4].y, vertices[4].z), new Vector3(vertices[6].x, vertices[6].y, vertices[6].z));
  80.             Gizmos.DrawLine(new Vector3(vertices[5].x, vertices[5].y, vertices[5].z), new Vector3(vertices[7].x, vertices[7].y, vertices[7].z));
  81.             Gizmos.DrawLine(new Vector3(vertices[6].x, vertices[6].y, vertices[6].z), new Vector3(vertices[7].x, vertices[7].y, vertices[7].z));
  82.             Gizmos.DrawLine(new Vector3(vertices[0].x, vertices[0].y, vertices[0].z), new Vector3(vertices[4].x, vertices[4].y, vertices[4].z));
  83.             Gizmos.DrawLine(new Vector3(vertices[1].x, vertices[1].y, vertices[1].z), new Vector3(vertices[5].x, vertices[5].y, vertices[5].z));
  84.             Gizmos.DrawLine(new Vector3(vertices[2].x, vertices[2].y, vertices[2].z), new Vector3(vertices[6].x, vertices[6].y, vertices[6].z));
  85.             Gizmos.DrawLine(new Vector3(vertices[3].x, vertices[3].y, vertices[3].z), new Vector3(vertices[7].x, vertices[7].y, vertices[7].z));
  86.  
  87.             // Draw normal
  88.             Gizmos.color = Color.red;
  89.             Gizmos.DrawLine(center, center + normal * 10.0f);
  90.  
  91.             if (Mathf.Abs(normal.z) > Mathf.Abs(normal.x) && Mathf.Abs(normal.z) > Mathf.Abs(normal.y))
  92.             {
  93.                 continue;
  94.             }
  95.  
  96.             // Extract 2D splitting line from normal
  97.             Vector2 split = new Vector2(normal.x, normal.y);
  98.             split.Normalize();
  99.  
  100.             if (counter > 0)
  101.             {
  102.                 // Check if we have to split left or right one
  103.                 Vector2 center2D = new Vector2(center.x, center.y);
  104.                 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);
  105.                 if (Vector2.Dot(center2D - nodeCenter2D, split) <= 0.0f)
  106.                 {
  107.                     node = node.left;
  108.                 }
  109.                 else
  110.                 {
  111.                     node = node.right;
  112.                 }
  113.             }
  114.  
  115.             // Split the panel
  116.             if (Mathf.Abs(split.x) > Mathf.Abs(split.y))
  117.             {
  118.                 node.Split(0, center.x);
  119.             }
  120.             else
  121.             {
  122.                 node.Split(1, center.y);
  123.             }
  124.  
  125.             counter++;
  126.         }
  127.  
  128.         // Draw the BSP tree
  129.         Stack<BinarySpacePartition.Node> stack = new Stack<BinarySpacePartition.Node>();
  130.         stack.Push(bsp.root);
  131.  
  132.         int iters = 0;
  133.  
  134.         while (stack.Count > 0)
  135.         {
  136.             BinarySpacePartition.Node n = stack.Pop();
  137.  
  138.             // If n is leaf node (no children)
  139.             if (n.left == null && n.right == null)
  140.             {
  141.                 Gizmos.color = Color.blue;
  142.                 Gizmos.DrawLine(new Vector3(n.min.x, n.min.y, 0.0f), new Vector3(n.max.x, n.min.y, 0.0f));
  143.                 Gizmos.DrawLine(new Vector3(n.max.x, n.min.y, 0.0f), new Vector3(n.max.x, n.max.y, 0.0f));
  144.                 Gizmos.DrawLine(new Vector3(n.max.x, n.max.y, 0.0f), new Vector3(n.min.x, n.max.y, 0.0f));
  145.                 Gizmos.DrawLine(new Vector3(n.min.x, n.max.y, 0.0f), new Vector3(n.min.x, n.min.y, 0.0f));
  146.             }
  147.             else
  148.             {
  149.                 if (n.left != null)
  150.                 {
  151.                     stack.Push(n.left);
  152.                 }
  153.  
  154.                 if (n.right != null)
  155.                 {
  156.                     stack.Push(n.right);
  157.                 }
  158.             }
  159.  
  160.             iters++;
  161.         }
  162.     }
  163. }
  164.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement