Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using SharpDX;
- using SharpDX.Direct3D11;
- using System.Collections.Generic;
- using VoidwalkerEngine.Framework.DirectX.Rendering;
- using VoidwalkerEngine.Framework.Interfaces;
- using VoidwalkerEngine.Framework.Logic;
- namespace VoidwalkerEngine.Framework.Game.Systems.World.Objects
- {
- /**
- * AnimatedBrushVolume v1.3
- */
- public class AnimatedBrushVolume : WorldObject, ITickable
- {
- public int Width { get; private set; }
- public int Height { get; private set; }
- public int Depth { get; private set; }
- public int TickFrequency = 1;
- public int TickCount = 0;
- public BrushVolumeFaceVisibility FaceVisibility { get; private set; }
- public int FaceCount { get; private set; }
- public BrushVolumeHullMode HullMode { get; private set; }
- public List<Material> Materials { get; set; }
- public Device Device;
- private VertexBufferBinding _bufferBinding;
- public const BrushVolumeHullMode DefaultHullMode = BrushVolumeHullMode.Exterior;
- // 36 Vertices maximum
- private int _drawCount;
- private int _currentFrame;
- public AnimatedBrushVolume(Device device, List<Material> materials,
- int width, int height, int length,
- BrushVolumeFaceVisibility visibilityOptions, BrushVolumeHullMode hullMode = DefaultHullMode )
- {
- this.Device = device;
- this.Width = width;
- this.Height = height;
- this.Depth = length;
- this.FaceVisibility = visibilityOptions;
- this.Materials = materials;
- this.HullMode = hullMode;
- Rebuild();
- }
- public void Resize(int width, int height, int depth)
- {
- this.Width = width;
- this.Height = height;
- this.Depth = depth;
- Rebuild();
- }
- public void Rebuild(int width, int height, int depth, BrushVolumeFaceVisibility visibilityOptions, BrushVolumeHullMode hullMode = DefaultHullMode)
- {
- this.Width = width;
- this.Height = height;
- this.Depth = depth;
- this.FaceVisibility = visibilityOptions;
- this.HullMode = hullMode;
- Rebuild();
- }
- public void SetHullMode(BrushVolumeHullMode mode)
- {
- this.HullMode = mode;
- Rebuild();
- }
- public void SetFaceVisibility(BrushVolumeFaceVisibility visibilityOptions)
- {
- this.FaceVisibility = visibilityOptions;
- Rebuild();
- }
- private int CountVisibleFaces(BrushVolumeFaceVisibility visibilityOptions)
- {
- int value = (int)visibilityOptions;
- value -= (value >> 1) & 1431655765;
- value = (value & 858993459) + ((value >> 2) & 858993459);
- int count = ((value + (value >> 4) & 252645135) * 16843009) >> 24;
- return count;
- }
- public void Rebuild()
- {
- this.FaceCount = CountVisibleFaces(this.FaceVisibility);
- Vertex[] vertices = new Vertex[this.FaceCount * 6];
- float width = this.Width;
- float height = this.Height;
- float depth = this.Depth;
- float uvX = width / (float)this.Materials[this._currentFrame].Width;
- float uvY = height / (float)this.Materials[this._currentFrame].Height;
- float uvZ = depth / (float)this.Materials[this._currentFrame].Width;
- /**
- * No faces are present; empty the AnimatedBrushVolume
- */
- if (this.FaceCount == 0)
- {
- if (_bufferBinding.Buffer != null)
- {
- _bufferBinding.Buffer.Dispose();
- }
- _drawCount = 0;
- return;
- }
- #region Generate BrushVolume Faces
- int index = 0;
- // Generate Front Face
- if (this.FaceVisibility.HasFlag(BrushVolumeFaceVisibility.Front))
- {
- if (this.HullMode == BrushVolumeHullMode.Exterior)
- {
- Vector3 normal = new Vector3(0, 0, 1);
- vertices[index++] = new Vertex(new Vector3(-width, height, -depth), new Vector2(0, 0), normal);
- vertices[index++] = new Vertex(new Vector3(width, height, -depth), new Vector2(uvX, 0), normal);
- vertices[index++] = new Vertex(new Vector3(width, -height, -depth), new Vector2(uvX, uvY), normal);
- vertices[index++] = new Vertex(new Vector3(width, -height, -depth), new Vector2(uvX, uvY), normal);
- vertices[index++] = new Vertex(new Vector3(-width, -height, -depth), new Vector2(0, uvY), normal);
- vertices[index++] = new Vertex(new Vector3(-width, height, -depth), new Vector2(0, 0), normal);
- }
- else
- {
- Vector3 normal = new Vector3(0, 0, -1);
- vertices[index++] = new Vertex(new Vector3(width, -height, -depth), new Vector2(0, uvY), normal);
- vertices[index++] = new Vertex(new Vector3(width, height, -depth), new Vector2(0, 0), normal);
- vertices[index++] = new Vertex(new Vector3(-width, height, -depth), new Vector2(uvX, 0), normal);
- vertices[index++] = new Vertex(new Vector3(-width, height, -depth), new Vector2(uvX, 0), normal);
- vertices[index++] = new Vertex(new Vector3(-width, -height, -depth), new Vector2(uvX, uvY), normal);
- vertices[index++] = new Vertex(new Vector3(width, -height, -depth), new Vector2(0, uvY), normal);
- }
- }
- if (this.FaceVisibility.HasFlag(BrushVolumeFaceVisibility.Back))
- {
- if (this.HullMode == BrushVolumeHullMode.Exterior)
- {
- Vector3 normal = new Vector3(0, 0, -1);
- vertices[index++] = new Vertex(new Vector3(width, height, depth), new Vector2(0, 0), normal);
- vertices[index++] = new Vertex(new Vector3(-width, height, depth), new Vector2(uvX, 0), normal);
- vertices[index++] = new Vertex(new Vector3(-width, -height, depth), new Vector2(uvX, uvY), normal);
- vertices[index++] = new Vertex(new Vector3(-width, -height, depth), new Vector2(uvX, uvY), normal);
- vertices[index++] = new Vertex(new Vector3(width, -height, depth), new Vector2(0, uvY), normal);
- vertices[index++] = new Vertex(new Vector3(width, height, depth), new Vector2(0, 0), normal);
- }
- else
- {
- Vector3 normal = new Vector3(0, 0, 1);
- vertices[index++] = new Vertex(new Vector3(-width, -height, depth), new Vector2(0, uvY), normal);
- vertices[index++] = new Vertex(new Vector3(-width, height, depth), new Vector2(0, 0), normal);
- vertices[index++] = new Vertex(new Vector3(width, height, depth), new Vector2(uvX, 0), normal);
- vertices[index++] = new Vertex(new Vector3(width, height, depth), new Vector2(uvX, 0), normal);
- vertices[index++] = new Vertex(new Vector3(width, -height, depth), new Vector2(uvX, uvY), normal);
- vertices[index++] = new Vertex(new Vector3(-width, -height, depth), new Vector2(0, uvY), normal);
- }
- }
- // Generate East Face
- if (this.FaceVisibility.HasFlag(BrushVolumeFaceVisibility.Left))
- {
- if (this.HullMode == BrushVolumeHullMode.Exterior)
- {
- Vector3 normal = new Vector3(-1, 0, 0);
- vertices[index++] = new Vertex(new Vector3(width, -height, depth), new Vector2(uvZ, uvY), normal);
- vertices[index++] = new Vertex(new Vector3(width, -height, -depth), new Vector2(0, uvY), normal);
- vertices[index++] = new Vertex(new Vector3(width, height, -depth), new Vector2(0, 0), normal);
- vertices[index++] = new Vertex(new Vector3(width, height, -depth), new Vector2(0, 0), normal);
- vertices[index++] = new Vertex(new Vector3(width, height, depth), new Vector2(uvZ, 0), normal);
- vertices[index++] = new Vertex(new Vector3(width, -height, depth), new Vector2(uvZ, uvY), normal);
- }
- else
- {
- Vector3 normal = new Vector3(1, 0, 0);
- vertices[index++] = new Vertex(new Vector3(width, height, -depth), new Vector2(uvZ, 0), normal);
- vertices[index++] = new Vertex(new Vector3(width, -height, -depth), new Vector2(uvZ, uvY), normal);
- vertices[index++] = new Vertex(new Vector3(width, -height, depth), new Vector2(0, uvY), normal);
- vertices[index++] = new Vertex(new Vector3(width, -height, depth), new Vector2(0, uvY), normal);
- vertices[index++] = new Vertex(new Vector3(width, height, depth), new Vector2(0, 0), normal);
- vertices[index++] = new Vertex(new Vector3(width, height, -depth), new Vector2(uvZ, 0), normal);
- }
- }
- if (this.FaceVisibility.HasFlag(BrushVolumeFaceVisibility.Right))
- {
- if (this.HullMode == BrushVolumeHullMode.Exterior)
- {
- Vector3 normal = new Vector3(1, 0, 0);
- vertices[index++] = new Vertex(new Vector3(-width, -height, -depth), new Vector2(uvZ, uvY), normal);
- vertices[index++] = new Vertex(new Vector3(-width, -height, depth), new Vector2(0, uvY), normal);
- vertices[index++] = new Vertex(new Vector3(-width, height, depth), new Vector2(0, 0), normal);
- vertices[index++] = new Vertex(new Vector3(-width, height, depth), new Vector2(0, 0), normal);
- vertices[index++] = new Vertex(new Vector3(-width, height, -depth), new Vector2(uvZ, 0), normal);
- vertices[index++] = new Vertex(new Vector3(-width, -height, -depth), new Vector2(uvZ, uvY), normal);
- }
- else
- {
- Vector3 normal = new Vector3(-1, 0, 0);
- vertices[index++] = new Vertex(new Vector3(-width, height, depth), new Vector2(uvZ, 0), normal);
- vertices[index++] = new Vertex(new Vector3(-width, -height, depth), new Vector2(uvZ, uvY), normal);
- vertices[index++] = new Vertex(new Vector3(-width, -height, -depth), new Vector2(0, uvY), normal);
- vertices[index++] = new Vertex(new Vector3(-width, -height, -depth), new Vector2(0, uvY), normal);
- vertices[index++] = new Vertex(new Vector3(-width, height, -depth), new Vector2(0, 0), normal);
- vertices[index++] = new Vertex(new Vector3(-width, height, depth), new Vector2(uvZ, 0), normal);
- }
- }
- if (this.FaceVisibility.HasFlag(BrushVolumeFaceVisibility.Top))
- {
- if (this.HullMode == BrushVolumeHullMode.Exterior)
- {
- Vector3 normal = new Vector3(0, -1, 0);
- vertices[index++] = new Vertex(new Vector3(-width, height, -depth), new Vector2(uvX, 0), normal);
- vertices[index++] = new Vertex(new Vector3(-width, height, depth), new Vector2(uvX, uvZ), normal);
- vertices[index++] = new Vertex(new Vector3(width, height, depth), new Vector2(0, uvZ), normal);
- vertices[index++] = new Vertex(new Vector3(width, height, depth), new Vector2(0, uvZ), normal);
- vertices[index++] = new Vertex(new Vector3(width, height, -depth), new Vector2(0, 0), normal);
- vertices[index++] = new Vertex(new Vector3(-width, height, -depth), new Vector2(uvX, 0), normal);
- }
- else
- {
- Vector3 normal = new Vector3(0, 1, 0);
- vertices[index++] = new Vertex(new Vector3(width, height, depth), new Vector2(uvX, uvZ), normal);
- vertices[index++] = new Vertex(new Vector3(-width, height, depth), new Vector2(0, uvZ), normal);
- vertices[index++] = new Vertex(new Vector3(-width, height, -depth), new Vector2(0, 0), normal);
- vertices[index++] = new Vertex(new Vector3(-width, height, -depth), new Vector2(0, 0), normal);
- vertices[index++] = new Vertex(new Vector3(width, height, -depth), new Vector2(uvX, 0), normal);
- vertices[index++] = new Vertex(new Vector3(width, height, depth), new Vector2(uvX, uvZ), normal);
- }
- }
- // Generate Bottom Face
- if (this.FaceVisibility.HasFlag(BrushVolumeFaceVisibility.Bottom))
- {
- if (this.HullMode == BrushVolumeHullMode.Exterior)
- {
- Vector3 normal = new Vector3(0, 1, 0);
- vertices[index++] = new Vertex(new Vector3(width, -height, -depth), new Vector2(uvX, 0), normal);
- vertices[index++] = new Vertex(new Vector3(width, -height, depth), new Vector2(uvX, uvZ), normal);
- vertices[index++] = new Vertex(new Vector3(-width, -height, depth), new Vector2(0, uvZ), normal);
- vertices[index++] = new Vertex(new Vector3(-width, -height, depth), new Vector2(0, uvZ), normal);
- vertices[index++] = new Vertex(new Vector3(-width, -height, -depth), new Vector2(0, 0), normal);
- vertices[index++] = new Vertex(new Vector3(width, -height, -depth), new Vector2(uvX, 0), normal);
- }
- else
- {
- Vector3 normal = new Vector3(0, -1, 0);
- vertices[index++] = new Vertex(new Vector3(-width, -height, depth), new Vector2(uvX, uvZ), normal);
- vertices[index++] = new Vertex(new Vector3(width, -height, depth), new Vector2(0, uvZ), normal);
- vertices[index++] = new Vertex(new Vector3(width, -height, -depth), new Vector2(0, 0), normal);
- vertices[index++] = new Vertex(new Vector3(width, -height, -depth), new Vector2(0, 0), normal);
- vertices[index++] = new Vertex(new Vector3(-width, -height, -depth), new Vector2(uvX, 0), normal);
- vertices[index++] = new Vertex(new Vector3(-width, -height, depth), new Vector2(uvX, uvZ), normal);
- }
- }
- #endregion
- if (this._bufferBinding.Buffer != null)
- {
- this._bufferBinding.Buffer.Dispose();
- }
- this._drawCount = vertices.Length;
- Buffer vertexBuffer = Buffer.Create(Device, BindFlags.VertexBuffer, vertices);
- this._bufferBinding = new VertexBufferBinding(vertexBuffer, 32, 0); // 32 is Vertex Size In Bytes
- }
- public Material GetCurrentMaterial()
- {
- return this.Materials[this._currentFrame];
- }
- public void Draw(DeviceContext context)
- {
- if (this.FaceCount == 0)
- {
- return;
- }
- context.InputAssembler.SetVertexBuffers(0, this._bufferBinding);
- if (this.Materials != null)
- {
- Material material = GetCurrentMaterial();
- if (material != null)
- {
- material.Bind(context);
- }
- }
- context.Draw(_drawCount, 0);
- }
- public void Tick()
- {
- this.TickCount++;
- if (TickCount >= TickFrequency)
- {
- int frameOffset = this._currentFrame + 1;
- if (frameOffset > this.Materials.Count - 1)
- {
- frameOffset = 0;
- }
- this._currentFrame = frameOffset;
- TickCount = 0;
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment