Krythic

AnimatedBrushVolume v1.3

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