Advertisement
Krythic

StaticBrushVolume v1.3

Jul 15th, 2021 (edited)
652
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 16.23 KB | None | 0 0
  1. using SharpDX;
  2. using SharpDX.Direct3D11;
  3. using VoidwalkerEngine.Framework.DirectX.Rendering;
  4. using VoidwalkerEngine.Framework.Logic;
  5.  
  6. namespace VoidwalkerEngine.Framework.Game.Systems.WorldObjects
  7. {
  8.     /**
  9.      * StaticBrushVolume v1.3
  10.      */
  11.     public class StaticBrushVolume : WorldObject
  12.     {
  13.         public int Width { get; private set; }
  14.         public int Height { get; private set; }
  15.         public int Depth { get; private set; }
  16.         public BrushVolumeFaceVisibility FaceVisibility { get; private set; }
  17.         public int FaceCount { get; private set; }
  18.         public Material DiffuseMaterial { get; set; }
  19.         public BrushVolumeHullMode HullMode { get; private set; }
  20.         public Device Device; // This might need to be better protected, idfk.
  21.         private VertexBufferBinding _bufferBinding;
  22.         // 36 Vertices maximum
  23.         private int _drawCount;
  24.         public const BrushVolumeHullMode DefaultHullMode = BrushVolumeHullMode.Exterior;
  25.  
  26.         public StaticBrushVolume(Device device, Material diffuseMaterial,
  27.             int width, int height, int length,
  28.             BrushVolumeFaceVisibility visibilityOptions,
  29.             BrushVolumeHullMode hullMode = DefaultHullMode)
  30.         {
  31.             this.Device = device;
  32.             this.Width = width;
  33.             this.Height = height;
  34.             this.Depth = length;
  35.             this.FaceVisibility = visibilityOptions;
  36.             this.DiffuseMaterial = diffuseMaterial;
  37.             this.HullMode = hullMode;
  38.             Rebuild();
  39.         }
  40.  
  41.         public StaticBrushVolume(Device device, Material diffuseMaterial,
  42.             int width, int height, int length,
  43.             Vector3 location,
  44.             BrushVolumeFaceVisibility visibilityOptions,
  45.             BrushVolumeHullMode hullMode = DefaultHullMode)
  46.         {
  47.             this.Device = device;
  48.             this.Width = width;
  49.             this.Height = height;
  50.             this.Depth = length;
  51.             this.Location = location;
  52.             this.FaceVisibility = visibilityOptions;
  53.             this.DiffuseMaterial = diffuseMaterial;
  54.             this.HullMode = hullMode;
  55.             Rebuild();
  56.         }
  57.  
  58.         public void Resize(int width, int height, int depth)
  59.         {
  60.             this.Width = width;
  61.             this.Height = height;
  62.             this.Depth = depth;
  63.             Rebuild();
  64.         }
  65.  
  66.         public void Rebuild(int width, int height, int depth, BrushVolumeFaceVisibility visibilityOptions, BrushVolumeHullMode hullMode = DefaultHullMode)
  67.         {
  68.             this.Width = width;
  69.             this.Height = height;
  70.             this.Depth = depth;
  71.             this.FaceVisibility = visibilityOptions;
  72.             this.HullMode = hullMode;
  73.             Rebuild();
  74.         }
  75.  
  76.         public void SetHullMode(BrushVolumeHullMode mode)
  77.         {
  78.             this.HullMode = mode;
  79.             Rebuild();
  80.         }
  81.  
  82.         public void SetFaceVisibility(BrushVolumeFaceVisibility visibilityOptions)
  83.         {
  84.             this.FaceVisibility = visibilityOptions;
  85.             Rebuild();
  86.         }
  87.  
  88.         private int CountVisibleFaces(BrushVolumeFaceVisibility visibilityOptions)
  89.         {
  90.             // Here be dragons!
  91.             int value = (int)visibilityOptions;
  92.             value -= (value >> 1) & 1431655765;
  93.             value = (value & 858993459) + ((value >> 2) & 858993459);
  94.             int count = ((value + (value >> 4) & 252645135) * 16843009) >> 24;
  95.             return count;
  96.         }
  97.  
  98.         public void Rebuild()
  99.         {
  100.             /**
  101.              * No faces are present; empty the StaticBrushVolume
  102.              */
  103.             if (this.FaceVisibility == BrushVolumeFaceVisibility.None)
  104.             {
  105.                 if (_bufferBinding.Buffer != null)
  106.                 {
  107.                     _bufferBinding.Buffer.Dispose();
  108.                 }
  109.                 this.FaceCount = 0;
  110.                 _drawCount = 0;
  111.                 return;
  112.             }
  113.             this.FaceCount = CountVisibleFaces(this.FaceVisibility);
  114.             Vertex[] vertices = new Vertex[this.FaceCount * 6];
  115.             float width = this.Width;
  116.             float height = this.Height;
  117.             float depth = this.Depth;
  118.             float uvX = width / (float)this.DiffuseMaterial.Width;
  119.             float uvY = height / (float)this.DiffuseMaterial.Height;
  120.             float uvZWidth = depth / (float)this.DiffuseMaterial.Width;
  121.             float uvZHeight = depth / (float)this.DiffuseMaterial.Height;
  122.             #region Generate BrushVolume Faces
  123.             int index = 0;
  124.             if (this.FaceVisibility.HasFlag(BrushVolumeFaceVisibility.Front))
  125.             {
  126.                 if (this.HullMode == BrushVolumeHullMode.Interior)
  127.                 {
  128.                     Vector3 normal = new Vector3(0, 0, 1);
  129.                     vertices[index++] = new Vertex(new Vector3(-width, height, -depth), new Vector2(0, 0), normal);
  130.                     vertices[index++] = new Vertex(new Vector3(width, height, -depth), new Vector2(uvX, 0), normal);
  131.                     vertices[index++] = new Vertex(new Vector3(width, -height, -depth), new Vector2(uvX, uvY), normal);
  132.                     vertices[index++] = new Vertex(new Vector3(width, -height, -depth), new Vector2(uvX, uvY), normal);
  133.                     vertices[index++] = new Vertex(new Vector3(-width, -height, -depth), new Vector2(0, uvY), normal);
  134.                     vertices[index++] = new Vertex(new Vector3(-width, height, -depth), new Vector2(0, 0), normal);
  135.                 }
  136.                 else
  137.                 {
  138.                     Vector3 normal = new Vector3(0, 0, -1);
  139.                     vertices[index++] = new Vertex(new Vector3(width, -height, -depth), new Vector2(0, uvY), normal);
  140.                     vertices[index++] = new Vertex(new Vector3(width, height, -depth), new Vector2(0, 0), normal);
  141.                     vertices[index++] = new Vertex(new Vector3(-width, height, -depth), new Vector2(uvX, 0), normal);
  142.                     vertices[index++] = new Vertex(new Vector3(-width, height, -depth), new Vector2(uvX, 0), normal);
  143.                     vertices[index++] = new Vertex(new Vector3(-width, -height, -depth), new Vector2(uvX, uvY), normal);
  144.                     vertices[index++] = new Vertex(new Vector3(width, -height, -depth), new Vector2(0, uvY), normal);
  145.                 }
  146.             }
  147.             if (this.FaceVisibility.HasFlag(BrushVolumeFaceVisibility.Back))
  148.             {
  149.                 if (this.HullMode == BrushVolumeHullMode.Interior)
  150.                 {
  151.                     Vector3 normal = new Vector3(0, 0, -1);
  152.                     vertices[index++] = new Vertex(new Vector3(width, height, depth), new Vector2(0, 0), normal);
  153.                     vertices[index++] = new Vertex(new Vector3(-width, height, depth), new Vector2(uvX, 0), normal);
  154.                     vertices[index++] = new Vertex(new Vector3(-width, -height, depth), new Vector2(uvX, uvY), normal);
  155.                     vertices[index++] = new Vertex(new Vector3(-width, -height, depth), new Vector2(uvX, uvY), normal);
  156.                     vertices[index++] = new Vertex(new Vector3(width, -height, depth), new Vector2(0, uvY), normal);
  157.                     vertices[index++] = new Vertex(new Vector3(width, height, depth), new Vector2(0, 0), normal);
  158.                 }
  159.                 else
  160.                 {
  161.                     Vector3 normal = new Vector3(0, 0, 1);
  162.                     vertices[index++] = new Vertex(new Vector3(-width, -height, depth), new Vector2(0, uvY), normal);
  163.                     vertices[index++] = new Vertex(new Vector3(-width, height, depth), new Vector2(0, 0), normal);
  164.                     vertices[index++] = new Vertex(new Vector3(width, height, depth), new Vector2(uvX, 0), normal);
  165.                     vertices[index++] = new Vertex(new Vector3(width, height, depth), new Vector2(uvX, 0), normal);
  166.                     vertices[index++] = new Vertex(new Vector3(width, -height, depth), new Vector2(uvX, uvY), normal);
  167.                     vertices[index++] = new Vertex(new Vector3(-width, -height, depth), new Vector2(0, uvY), normal);
  168.                 }
  169.             }
  170.             // Generate East Face
  171.             if (this.FaceVisibility.HasFlag(BrushVolumeFaceVisibility.Left))
  172.             {
  173.                 if (this.HullMode == BrushVolumeHullMode.Interior)
  174.                 {
  175.                     Vector3 normal = new Vector3(-1, 0, 0);
  176.                     vertices[index++] = new Vertex(new Vector3(width, -height, depth), new Vector2(uvZWidth, uvY), normal);
  177.                     vertices[index++] = new Vertex(new Vector3(width, -height, -depth), new Vector2(0, uvY), normal);
  178.                     vertices[index++] = new Vertex(new Vector3(width, height, -depth), new Vector2(0, 0), normal);
  179.                     vertices[index++] = new Vertex(new Vector3(width, height, -depth), new Vector2(0, 0), normal);
  180.                     vertices[index++] = new Vertex(new Vector3(width, height, depth), new Vector2(uvZWidth, 0), normal);
  181.                     vertices[index++] = new Vertex(new Vector3(width, -height, depth), new Vector2(uvZWidth, uvY), normal);
  182.                 }
  183.                 else
  184.                 {
  185.                     Vector3 normal = new Vector3(1, 0, 0);
  186.                     vertices[index++] = new Vertex(new Vector3(width, height, -depth), new Vector2(uvZWidth, 0), normal);
  187.                     vertices[index++] = new Vertex(new Vector3(width, -height, -depth), new Vector2(uvZWidth, uvY), normal);
  188.                     vertices[index++] = new Vertex(new Vector3(width, -height, depth), new Vector2(0, uvY), normal);
  189.                     vertices[index++] = new Vertex(new Vector3(width, -height, depth), new Vector2(0, uvY), normal);
  190.                     vertices[index++] = new Vertex(new Vector3(width, height, depth), new Vector2(0, 0), normal);
  191.                     vertices[index++] = new Vertex(new Vector3(width, height, -depth), new Vector2(uvZWidth, 0), normal);
  192.                 }
  193.             }
  194.             if (this.FaceVisibility.HasFlag(BrushVolumeFaceVisibility.Right))
  195.             {
  196.                 if (this.HullMode == BrushVolumeHullMode.Interior)
  197.                 {
  198.                     Vector3 normal = new Vector3(1, 0, 0);
  199.                     vertices[index++] = new Vertex(new Vector3(-width, -height, -depth), new Vector2(uvZWidth, uvY), normal);
  200.                     vertices[index++] = new Vertex(new Vector3(-width, -height, depth), new Vector2(0, uvY), normal);
  201.                     vertices[index++] = new Vertex(new Vector3(-width, height, depth), new Vector2(0, 0), normal);
  202.                     vertices[index++] = new Vertex(new Vector3(-width, height, depth), new Vector2(0, 0), normal);
  203.                     vertices[index++] = new Vertex(new Vector3(-width, height, -depth), new Vector2(uvZWidth, 0), normal);
  204.                     vertices[index++] = new Vertex(new Vector3(-width, -height, -depth), new Vector2(uvZWidth, uvY), normal);
  205.                 }
  206.                 else
  207.                 {
  208.                     Vector3 normal = new Vector3(-1, 0, 0);
  209.                     vertices[index++] = new Vertex(new Vector3(-width, height, depth), new Vector2(uvZWidth, 0), normal);
  210.                     vertices[index++] = new Vertex(new Vector3(-width, -height, depth), new Vector2(uvZWidth, uvY), normal);
  211.                     vertices[index++] = new Vertex(new Vector3(-width, -height, -depth), new Vector2(0, uvY), normal);
  212.                     vertices[index++] = new Vertex(new Vector3(-width, -height, -depth), new Vector2(0, uvY), normal);
  213.                     vertices[index++] = new Vertex(new Vector3(-width, height, -depth), new Vector2(0, 0), normal);
  214.                     vertices[index++] = new Vertex(new Vector3(-width, height, depth), new Vector2(uvZWidth, 0), normal);
  215.                 }
  216.             }
  217.             if (this.FaceVisibility.HasFlag(BrushVolumeFaceVisibility.Top))
  218.             {
  219.                 if (this.HullMode == BrushVolumeHullMode.Interior)
  220.                 {
  221.                     Vector3 normal = new Vector3(0, -1, 0);
  222.                     vertices[index++] = new Vertex(new Vector3(-width, height, -depth), new Vector2(uvX, 0), normal);
  223.                     vertices[index++] = new Vertex(new Vector3(-width, height, depth), new Vector2(uvX, uvZHeight), normal);
  224.                     vertices[index++] = new Vertex(new Vector3(width, height, depth), new Vector2(0, uvZHeight), normal);
  225.                     vertices[index++] = new Vertex(new Vector3(width, height, depth), new Vector2(0, uvZHeight), normal);
  226.                     vertices[index++] = new Vertex(new Vector3(width, height, -depth), new Vector2(0, 0), normal);
  227.                     vertices[index++] = new Vertex(new Vector3(-width, height, -depth), new Vector2(uvX, 0), normal);
  228.                 }
  229.                 else
  230.                 {
  231.                     Vector3 normal = new Vector3(0, 1, 0);
  232.                     vertices[index++] = new Vertex(new Vector3(width, height, depth), new Vector2(uvX, uvZHeight), normal);
  233.                     vertices[index++] = new Vertex(new Vector3(-width, height, depth), new Vector2(0, uvZHeight), normal);
  234.                     vertices[index++] = new Vertex(new Vector3(-width, height, -depth), new Vector2(0, 0), normal);
  235.  
  236.                     vertices[index++] = new Vertex(new Vector3(-width, height, -depth), new Vector2(0, 0), normal);
  237.                     vertices[index++] = new Vertex(new Vector3(width, height, -depth), new Vector2(uvX, 0), normal);
  238.                     vertices[index++] = new Vertex(new Vector3(width, height, depth), new Vector2(uvX, uvZHeight), normal);
  239.                 }
  240.             }
  241.             // Generate Bottom Face
  242.             if (this.FaceVisibility.HasFlag(BrushVolumeFaceVisibility.Bottom))
  243.             {
  244.                 if (this.HullMode == BrushVolumeHullMode.Interior)
  245.                 {
  246.                     Vector3 normal = new Vector3(0, 1, 0);
  247.                     vertices[index++] = new Vertex(new Vector3(width, -height, -depth), new Vector2(uvX, 0), normal);
  248.                     vertices[index++] = new Vertex(new Vector3(width, -height, depth), new Vector2(uvX, uvZWidth), normal);
  249.                     vertices[index++] = new Vertex(new Vector3(-width, -height, depth), new Vector2(0, uvZWidth), normal);
  250.                     vertices[index++] = new Vertex(new Vector3(-width, -height, depth), new Vector2(0, uvZWidth), normal);
  251.                     vertices[index++] = new Vertex(new Vector3(-width, -height, -depth), new Vector2(0, 0), normal);
  252.                     vertices[index++] = new Vertex(new Vector3(width, -height, -depth), new Vector2(uvX, 0), normal);
  253.                 }
  254.                 else
  255.                 {
  256.                     Vector3 normal = new Vector3(0, -1, 0);
  257.                     vertices[index++] = new Vertex(new Vector3(-width, -height, depth), new Vector2(uvX, uvZWidth), normal);
  258.                     vertices[index++] = new Vertex(new Vector3(width, -height, depth), new Vector2(0, uvZWidth), normal);
  259.                     vertices[index++] = new Vertex(new Vector3(width, -height, -depth), new Vector2(0, 0), normal);
  260.                     vertices[index++] = new Vertex(new Vector3(width, -height, -depth), new Vector2(0, 0), normal);
  261.                     vertices[index++] = new Vertex(new Vector3(-width, -height, -depth), new Vector2(uvX, 0), normal);
  262.                     vertices[index++] = new Vertex(new Vector3(-width, -height, depth), new Vector2(uvX, uvZWidth), normal);
  263.                 }
  264.             }
  265.             #endregion
  266.             if (this._bufferBinding.Buffer != null)
  267.             {
  268.                 this._bufferBinding.Buffer.Dispose();
  269.             }
  270.             this._drawCount = vertices.Length;
  271.             Buffer vertexBuffer = Buffer.Create(Device, BindFlags.VertexBuffer, vertices);
  272.             this._bufferBinding = new VertexBufferBinding(vertexBuffer, 32, 0); // 32 is Vertex Size In Bytes
  273.         }
  274.  
  275.         public void Draw(DeviceContext context)
  276.         {
  277.             if (this.FaceCount == 0)
  278.             {
  279.                 return;
  280.             }
  281.             context.InputAssembler.SetVertexBuffers(0, this._bufferBinding);
  282.             if (this.DiffuseMaterial != null)
  283.             {
  284.                 this.DiffuseMaterial.Bind(context);
  285.             }
  286.             context.Draw(_drawCount, 0);
  287.         }
  288.     }
  289. }
  290.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement