Advertisement
Krythic

BoundingBoxBuilder

Aug 5th, 2020
1,613
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.49 KB | None | 0 0
  1. using SharpDX;
  2. using SharpDX.Direct3D11;
  3. using VoidwalkerEngine.Framework.Maths;
  4.  
  5. namespace VoidwalkerEngine.Framework.DirectX.Rendering
  6. {
  7.     public class BoundingBoxBuilder
  8.     {
  9.         private Vector3 _minimum;
  10.         public Vector3 Minimum
  11.         {
  12.             get
  13.             {
  14.                 return _minimum;
  15.             }
  16.             set
  17.             {
  18.                 this._minimum = value;
  19.                 Rebuild();
  20.             }
  21.         }
  22.  
  23.         private Vector3 _maximum;
  24.         public Vector3 Maximum
  25.         {
  26.             get
  27.             {
  28.                 return _maximum;
  29.             }
  30.             set
  31.             {
  32.                 this._maximum = value;
  33.                 Rebuild();
  34.             }
  35.         }
  36.  
  37.         private Vector3 _translation;
  38.         public Vector3 Translation
  39.         {
  40.             get
  41.             {
  42.                 return _translation;
  43.             }
  44.             set
  45.             {
  46.                 this._translation = value;
  47.                 Rebuild();
  48.             }
  49.         }
  50.  
  51.         private Vector3 _rotation;
  52.         public Vector3 Rotation
  53.         {
  54.             get
  55.             {
  56.                 return _rotation;
  57.             }
  58.             set
  59.             {
  60.                 this._rotation = value;
  61.                 Rebuild();
  62.             }
  63.         }
  64.  
  65.         private BoundingBox _internalBoundingBox;
  66.  
  67.         public Vector3[] Corners { get; private set; }
  68.         public ModelMesh BoundingBoxMesh;
  69.  
  70.         public BoundingBoxBuilder()
  71.         {
  72.             Corners = new Vector3[8];
  73.             Rebuild();
  74.         }
  75.  
  76.         public void Update(Vector3 translation, Vector3 rotation)
  77.         {
  78.             this._translation = translation;
  79.             this._rotation = rotation;
  80.             Rebuild();
  81.         }
  82.  
  83.         public BoundingBoxBuilder(Vector3 minimum, Vector3 maximum)
  84.         {
  85.             this._minimum = minimum;
  86.             this._maximum = maximum;
  87.             Corners = new Vector3[8];
  88.             Rebuild();
  89.         }
  90.  
  91.         /// <summary>
  92.         /// Clears the current Translation and Rotation of this builder, then rebuilds it.
  93.         /// </summary>
  94.         public void Clear()
  95.         {
  96.             this._translation = Vector3.Zero;
  97.             this._rotation = Vector3.Zero;
  98.             Rebuild();
  99.         }
  100.  
  101.         /// <summary>
  102.         /// Creates BoundingBox data using vertices.
  103.         /// </summary>
  104.         /// <param name="vertices"></param>
  105.         public void FromVertices(Vertex[] vertices)
  106.         {
  107.             // Calculate Bounding Box
  108.             float minX = float.PositiveInfinity;
  109.             float maxX = float.NegativeInfinity;
  110.             float minY = float.PositiveInfinity;
  111.             float maxY = float.NegativeInfinity;
  112.             float minZ = float.PositiveInfinity;
  113.             float maxZ = float.NegativeInfinity;
  114.             for (int i = 0; i < vertices.Length; i++)
  115.             {
  116.                 Vector3 vertex = vertices[i].Location;
  117.                 // Check for maximum
  118.                 if (vertex.X > maxX)
  119.                 {
  120.                     maxX = vertex.X;
  121.                 }
  122.                 if (vertex.Y > maxY)
  123.                 {
  124.                     maxY = vertex.Y;
  125.                 }
  126.                 if (vertex.Z > maxZ)
  127.                 {
  128.                     maxZ = vertex.Z;
  129.                 }
  130.                 // Check for Minimum
  131.                 if (vertex.X < minX)
  132.                 {
  133.                     minX = vertex.X;
  134.                 }
  135.                 if (vertex.Y < minY)
  136.                 {
  137.                     minY = vertex.Y;
  138.                 }
  139.                 if (vertex.Z < minZ)
  140.                 {
  141.                     minZ = vertex.Z;
  142.                 }
  143.             }
  144.             this.Minimum = new Vector3(minX, minY, minZ);
  145.             this.Maximum = new Vector3(maxX, maxY, maxZ);
  146.         }
  147.  
  148.         public Matrix GetTransform()
  149.         {
  150.             return VoidwalkerMath.CreateRotationMatrix(this.Rotation) * Matrix.Translation(this.Translation);
  151.         }
  152.  
  153.         private void Rebuild()
  154.         {
  155.             Matrix transform = GetTransform();
  156.             this.Corners[0] = Vector3.TransformCoordinate(new Vector3(this.Minimum.X, this.Maximum.Y, this.Maximum.Z), transform);
  157.             this.Corners[1] = Vector3.TransformCoordinate(new Vector3(this.Maximum.X, this.Maximum.Y, this.Maximum.Z), transform);
  158.             this.Corners[2] = Vector3.TransformCoordinate(new Vector3(this.Maximum.X, this.Minimum.Y, this.Maximum.Z), transform);
  159.             this.Corners[3] = Vector3.TransformCoordinate(new Vector3(this.Minimum.X, this.Minimum.Y, this.Maximum.Z), transform);
  160.             this.Corners[4] = Vector3.TransformCoordinate(new Vector3(this.Minimum.X, this.Maximum.Y, this.Minimum.Z), transform);
  161.             this.Corners[5] = Vector3.TransformCoordinate(new Vector3(this.Maximum.X, this.Maximum.Y, this.Minimum.Z), transform);
  162.             this.Corners[6] = Vector3.TransformCoordinate(new Vector3(this.Maximum.X, this.Minimum.Y, this.Minimum.Z), transform);
  163.             this.Corners[7] = Vector3.TransformCoordinate(new Vector3(this.Minimum.X, this.Minimum.Y, this.Minimum.Z), transform);
  164.             this._internalBoundingBox = BoundingBox.FromPoints(this.Corners);
  165.         }
  166.  
  167.         /// <summary>
  168.         /// Gets the currently built BoundingBox.
  169.         /// </summary>
  170.         /// <returns></returns>
  171.         public BoundingBox ToBoundingBox()
  172.         {
  173.             return _internalBoundingBox;
  174.         }
  175.     }
  176. }
  177.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement