Krythic

BrushVolume

Jun 14th, 2021
988
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using SharpDX;
  2. using SharpDX.Direct3D11;
  3.  
  4. namespace VoidwalkerEngine.Framework.DirectX.Rendering
  5. {
  6.  
  7.     public enum FaceVisibilityOptions
  8.     {
  9.         None = 0,
  10.         Front = 1,
  11.         Back = 2,
  12.         Left = 4,
  13.         Right = 8,
  14.         Top = 16,
  15.         Bottom = 32,
  16.         All = Front | Back | Left | Right | Top | Bottom
  17.     }
  18.  
  19.     public class BrushVolume
  20.     {
  21.         public Vector3 Location { get; set; }
  22.         public int Width { get; private set; }
  23.         public int Height { get; private set; }
  24.         public int Depth { get; private set; }
  25.         public FaceVisibilityOptions FaceVisibility { get; private set; }
  26.         public Material DiffuseMaterial { get; set; }
  27.         public Device Device;
  28.         private VertexBufferBinding _bufferBinding;
  29.         private const float _stride = 16;
  30.         // 36 Vertices maximum
  31.         private int _drawCount;
  32.  
  33.         public BrushVolume(Device device, Material diffuseMaterial,
  34.             int width, int height, int length,
  35.             FaceVisibilityOptions visibilityOptions)
  36.         {
  37.             this.Device = device;
  38.             this.Width = width;
  39.             this.Height = height;
  40.             this.Depth = length;
  41.             this.FaceVisibility = visibilityOptions;
  42.             this.DiffuseMaterial = diffuseMaterial;
  43.             Rebuild();
  44.         }
  45.  
  46.         public BrushVolume(Device device, Material diffuseMaterial,
  47.             int width, int height, int length, Vector3 location,
  48.             FaceVisibilityOptions visibilityOptions)
  49.         {
  50.             this.Device = device;
  51.             this.Width = width;
  52.             this.Height = height;
  53.             this.Depth = length;
  54.             this.Location = location;
  55.             this.FaceVisibility = visibilityOptions;
  56.             this.DiffuseMaterial = diffuseMaterial;
  57.             Rebuild();
  58.         }
  59.  
  60.         public void Resize(int width, int height, int depth)
  61.         {
  62.             this.Width = width;
  63.             this.Height = height;
  64.             this.Depth = depth;
  65.             Rebuild();
  66.         }
  67.  
  68.         public void Rebuild(int width, int height, int depth, FaceVisibilityOptions visibilityOptions)
  69.         {
  70.             this.Width = width;
  71.             this.Height = height;
  72.             this.Depth = depth;
  73.             this.FaceVisibility = visibilityOptions;
  74.             Rebuild();
  75.         }
  76.  
  77.         public void SetFaceVisibility(FaceVisibilityOptions visibilityOptions)
  78.         {
  79.             this.FaceVisibility = visibilityOptions;
  80.             Rebuild();
  81.         }
  82.  
  83.         private int CountVisibleFaces(FaceVisibilityOptions visibilityOptions)
  84.         {
  85.             int value = (int)visibilityOptions;
  86.             value -= ((value >> 1) & 1431655765);
  87.             value = (value & 858993459) + ((value >> 2) & 858993459);
  88.             int count = ((value + (value >> 4) & 252645135) * 16843009) >> 24;
  89.             return count;
  90.         }
  91.  
  92.         public void Rebuild()
  93.         {
  94.             Vertex[] vertices = new Vertex[CountVisibleFaces(this.FaceVisibility) * 6];
  95.             float width = _stride * this.Width;
  96.             float height = _stride * this.Height;
  97.             float depth = _stride * this.Depth;
  98.             float uvX = width / this.DiffuseMaterial.Width;
  99.             float uvY = height / this.DiffuseMaterial.Height;
  100.             float uvZ = depth / this.DiffuseMaterial.Width;
  101.  
  102.             // Generate Front Face
  103.             int index = 0;
  104.             if (this.FaceVisibility.HasFlag(FaceVisibilityOptions.Front))
  105.             {
  106.                 Vector3 normal = new Vector3(0, 0, -1);
  107.                 vertices[index++] = new Vertex(new Vector3(-width, height, -depth), new Vector2(-uvX, -uvY), normal);
  108.                 vertices[index++] = new Vertex(new Vector3(width, -height, -depth), new Vector2(0, 0), normal);
  109.                 vertices[index++] = new Vertex(new Vector3(-width, -height, -depth), new Vector2(-uvX, 0), normal);
  110.                 vertices[index++] = new Vertex(new Vector3(-width, height, -depth), new Vector2(-uvX, -uvY), normal);
  111.                 vertices[index++] = new Vertex(new Vector3(width, height, -depth), new Vector2(0, -uvY), normal);
  112.                 vertices[index++] = new Vertex(new Vector3(width, -height, -depth), new Vector2(0, 0), normal);
  113.             }
  114.  
  115.             // Generate Back Face
  116.             if (this.FaceVisibility.HasFlag(FaceVisibilityOptions.Back))
  117.             {
  118.                 Vector3 normal = new Vector3(0, 0, 1);
  119.                 vertices[index++] = new Vertex(new Vector3(width, height, depth), new Vector2(-uvX, -uvY), normal);
  120.                 vertices[index++] = new Vertex(new Vector3(-width, -height, depth), new Vector2(0, 0), normal);
  121.                 vertices[index++] = new Vertex(new Vector3(width, -height, depth), new Vector2(-uvX, 0), normal);
  122.                 vertices[index++] = new Vertex(new Vector3(width, height, depth), new Vector2(-uvX, -uvY), normal);
  123.                 vertices[index++] = new Vertex(new Vector3(-width, height, depth), new Vector2(0, -uvY), normal);
  124.                 vertices[index++] = new Vertex(new Vector3(-width, -height, depth), new Vector2(0, 0), normal);
  125.             }
  126.  
  127.             // Generate Right Face
  128.             if (this.FaceVisibility.HasFlag(FaceVisibilityOptions.Right))
  129.             {
  130.                 Vector3 normal = new Vector3(1, 0, 0);
  131.                 vertices[index++] = new Vertex(new Vector3(width, height, -depth), new Vector2(-uvZ, -uvY), normal);
  132.                 vertices[index++] = new Vertex(new Vector3(width, -height, depth), new Vector2(0, 0), normal);
  133.                 vertices[index++] = new Vertex(new Vector3(width, -height, -depth), new Vector2(-uvZ, 0), normal);
  134.                 vertices[index++] = new Vertex(new Vector3(width, height, -depth), new Vector2(-uvZ, -uvY), normal);
  135.                 vertices[index++] = new Vertex(new Vector3(width, height, depth), new Vector2(0, -uvY), normal);
  136.                 vertices[index++] = new Vertex(new Vector3(width, -height, depth), new Vector2(0, 0), normal);
  137.             }
  138.  
  139.             // Generate Left Face
  140.             if (this.FaceVisibility.HasFlag(FaceVisibilityOptions.Left))
  141.             {
  142.                 Vector3 normal = new Vector3(-1, 0, 0);
  143.                 vertices[index++] = new Vertex(new Vector3(-width, height, depth), new Vector2(-uvZ, -uvY), normal);
  144.                 vertices[index++] = new Vertex(new Vector3(-width, -height, -depth), new Vector2(0, 0), normal);
  145.                 vertices[index++] = new Vertex(new Vector3(-width, -height, depth), new Vector2(-uvZ, 0), normal);
  146.                 vertices[index++] = new Vertex(new Vector3(-width, height, depth), new Vector2(-uvZ, -uvY), normal);
  147.                 vertices[index++] = new Vertex(new Vector3(-width, height, -depth), new Vector2(0, -uvY), normal);
  148.                 vertices[index++] = new Vertex(new Vector3(-width, -height, -depth), new Vector2(0, 0), normal);
  149.             }
  150.  
  151.             // Generate Bottom Face
  152.             if (this.FaceVisibility.HasFlag(FaceVisibilityOptions.Bottom))
  153.             {
  154.                 Vector3 normal = new Vector3(0, -1, 0);
  155.                 vertices[index++] = new Vertex(new Vector3(-width, -height, depth), new Vector2(-uvZ, -uvX), normal);
  156.                 vertices[index++] = new Vertex(new Vector3(width, -height, -depth), new Vector2(0, 0), normal);
  157.                 vertices[index++] = new Vertex(new Vector3(width, -height, depth), new Vector2(-uvZ, 0), normal);
  158.                 vertices[index++] = new Vertex(new Vector3(-width, -height, depth), new Vector2(-uvZ, -uvX), normal);
  159.                 vertices[index++] = new Vertex(new Vector3(-width, -height, -depth), new Vector2(0, -uvX), normal);
  160.                 vertices[index++] = new Vertex(new Vector3(width, -height, -depth), new Vector2(0, 0), normal);
  161.             }
  162.  
  163.             // Generate Top Face
  164.             if (this.FaceVisibility.HasFlag(FaceVisibilityOptions.Top))
  165.             {
  166.                 Vector3 normal = new Vector3(0, 1, 0);
  167.                 vertices[index++] = new Vertex(new Vector3(width, height, depth), new Vector2(-uvZ, -uvX), normal);
  168.                 vertices[index++] = new Vertex(new Vector3(-width, height, -depth), new Vector2(0, 0), normal);
  169.                 vertices[index++] = new Vertex(new Vector3(-width, height, depth), new Vector2(-uvZ, 0), normal);
  170.                 vertices[index++] = new Vertex(new Vector3(width, height, depth), new Vector2(-uvZ, -uvX), normal);
  171.                 vertices[index++] = new Vertex(new Vector3(width, height, -depth), new Vector2(0, -uvX), normal);
  172.                 vertices[index++] = new Vertex(new Vector3(-width, height, -depth), new Vector2(0, 0), normal);
  173.             }
  174.             if (this._bufferBinding.Buffer != null)
  175.             {
  176.                 this._bufferBinding.Buffer.Dispose();
  177.             }
  178.             this._drawCount = vertices.Length;
  179.             Buffer vertexBuffer = Buffer.Create(Device, BindFlags.VertexBuffer, vertices);
  180.             this._bufferBinding = new VertexBufferBinding(vertexBuffer, 32, 0); // 32 is Vertex Size In Bytes
  181.         }
  182.  
  183.         public void Draw(DeviceContext context)
  184.         {
  185.             context.InputAssembler.SetVertexBuffers(0, this._bufferBinding);
  186.             if (DiffuseMaterial != null)
  187.             {
  188.                 context.PixelShader.SetShaderResource(0, DiffuseMaterial.ResourceView);
  189.                 context.PixelShader.SetSampler(0, DiffuseMaterial.SamplerState);
  190.             }
  191.             context.Draw(_drawCount, 0);
  192.         }
  193.  
  194.     }
  195. }
RAW Paste Data