Advertisement
Krythic

BrushVolume

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