trying to do tiled draws
							parent
							
								
									0ebad486c5
								
							
						
					
					
						commit
						40b5d71bfe
					
				|  | @ -0,0 +1,35 @@ | ||||||
|  | using Microsoft.Xna.Framework; | ||||||
|  | using Microsoft.Xna.Framework.Graphics; | ||||||
|  | 
 | ||||||
|  | namespace Kav.Data | ||||||
|  | { | ||||||
|  |     public struct MeshPartDrawData : IGBufferDrawable, IUVDrawable, ICullable, IIndexDrawable | ||||||
|  |     { | ||||||
|  |         public MeshPart MeshPart { get; } | ||||||
|  |         public Matrix TransformMatrix { get; } | ||||||
|  |         public UVData UVData { get; } | ||||||
|  | 
 | ||||||
|  |         public BoundingBox BoundingBox => MeshPart.BoundingBox; | ||||||
|  | 
 | ||||||
|  |         public IndexBuffer IndexBuffer => MeshPart.IndexBuffer; | ||||||
|  |         public VertexBuffer VertexBuffer => MeshPart.VertexBuffer; | ||||||
|  | 
 | ||||||
|  |         public Vector3 Albedo => MeshPart.Albedo; | ||||||
|  |         public float Metallic => MeshPart.Metallic; | ||||||
|  |         public float Roughness => MeshPart.Roughness; | ||||||
|  | 
 | ||||||
|  |         public Texture2D AlbedoTexture => MeshPart.AlbedoTexture; | ||||||
|  |         public Texture2D NormalTexture => MeshPart.NormalTexture; | ||||||
|  |         public Texture2D MetallicRoughnessTexture => MeshPart.MetallicRoughnessTexture; | ||||||
|  | 
 | ||||||
|  |         public MeshPartDrawData( | ||||||
|  |             MeshPart meshPart, | ||||||
|  |             Matrix transformMatrix, | ||||||
|  |             UVData uvData | ||||||
|  |         ) { | ||||||
|  |             MeshPart = meshPart; | ||||||
|  |             TransformMatrix = transformMatrix; | ||||||
|  |             UVData = uvData; | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | } | ||||||
|  | @ -2,23 +2,25 @@ using Microsoft.Xna.Framework; | ||||||
| 
 | 
 | ||||||
| namespace Kav.Data | namespace Kav.Data | ||||||
| { | { | ||||||
|     public struct MeshSpriteDrawData |     public struct MeshSpriteDrawData : ICullable | ||||||
|     { |     { | ||||||
|         public MeshSprite MeshSprite { get; } |         public MeshSprite MeshSprite { get; } | ||||||
|         public SpriteBillboardConstraint BillboardConstraint { get; } |         public SpriteBillboardConstraint BillboardConstraint { get; } | ||||||
|         public Matrix TransformMatrix { get; } |         public Matrix TransformMatrix { get; } | ||||||
|         public UVData UVOffset { get; } |         public UVData UVData { get; } | ||||||
|  | 
 | ||||||
|  |         public BoundingBox BoundingBox => MeshSprite.BoundingBox; | ||||||
| 
 | 
 | ||||||
|         public MeshSpriteDrawData( |         public MeshSpriteDrawData( | ||||||
|             MeshSprite meshSprite, |             MeshSprite meshSprite, | ||||||
|             SpriteBillboardConstraint billboardConstraint, |             SpriteBillboardConstraint billboardConstraint, | ||||||
|             Matrix transformMatrix, |             Matrix transformMatrix, | ||||||
|             UVData offset |             UVData uvData | ||||||
|         ) { |         ) { | ||||||
|             MeshSprite = meshSprite; |             MeshSprite = meshSprite; | ||||||
|             BillboardConstraint = billboardConstraint; |             BillboardConstraint = billboardConstraint; | ||||||
|             TransformMatrix = transformMatrix; |             TransformMatrix = transformMatrix; | ||||||
|             UVOffset = offset; |             UVData = uvData; | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -15,5 +15,10 @@ namespace Kav.Data | ||||||
|             Percentage = subTextureDimensions / atlasDimensions; |             Percentage = subTextureDimensions / atlasDimensions; | ||||||
|             Offset = positionInAtlas / atlasDimensions; |             Offset = positionInAtlas / atlasDimensions; | ||||||
|         } |         } | ||||||
|  | 
 | ||||||
|  |         public Vector4 ToVector4() | ||||||
|  |         { | ||||||
|  |             return new Vector4(Offset.X, Offset.Y, Percentage.X, Percentage.Y); | ||||||
|  |         } | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -12,14 +12,12 @@ namespace Kav | ||||||
|         EffectParameter normalTextureParam; |         EffectParameter normalTextureParam; | ||||||
|         EffectParameter metallicRoughnessTextureParam; |         EffectParameter metallicRoughnessTextureParam; | ||||||
| 
 | 
 | ||||||
|  |         EffectParameter uvOffsetAndDimensionsParam; | ||||||
|  | 
 | ||||||
|         EffectParameter albedoParam; |         EffectParameter albedoParam; | ||||||
|         EffectParameter metallicParam; |         EffectParameter metallicParam; | ||||||
|         EffectParameter roughnessParam; |         EffectParameter roughnessParam; | ||||||
| 
 | 
 | ||||||
|         EffectParameter numTextureRowsParam; |  | ||||||
|         EffectParameter numTextureColumnsParam; |  | ||||||
| 
 |  | ||||||
|         EffectParameter vertexShaderIndexParam; |  | ||||||
|         EffectParameter shaderIndexParam; |         EffectParameter shaderIndexParam; | ||||||
| 
 | 
 | ||||||
|         Matrix world = Matrix.Identity; |         Matrix world = Matrix.Identity; | ||||||
|  | @ -30,8 +28,7 @@ namespace Kav | ||||||
|         float metallic; |         float metallic; | ||||||
|         float roughness; |         float roughness; | ||||||
| 
 | 
 | ||||||
|         int numTextureRows = 1; |         Vector4 uvData; | ||||||
|         int numTextureColumns = 1; |  | ||||||
| 
 | 
 | ||||||
|         bool albedoTextureEnabled = false; |         bool albedoTextureEnabled = false; | ||||||
|         bool metallicRoughnessMapEnabled = false; |         bool metallicRoughnessMapEnabled = false; | ||||||
|  | @ -133,24 +130,15 @@ namespace Kav | ||||||
|             } |             } | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         public int NumTextureRows |         public Vector4 UVData | ||||||
|         { |         { | ||||||
|             get { return numTextureRows; } |             get { return uvData; } | ||||||
|             set |             set | ||||||
|             { |             { | ||||||
|                 numTextureRows = value; |                 uvData = value; | ||||||
|                 numTextureRowsParam.SetValue(numTextureRows); |                 uvOffsetAndDimensionsParam.SetValue(uvData); | ||||||
|             } |  | ||||||
|             } |             } | ||||||
| 
 | 
 | ||||||
|         public int NumTextureColumns |  | ||||||
|         { |  | ||||||
|             get { return numTextureColumns; } |  | ||||||
|             set |  | ||||||
|             { |  | ||||||
|                 numTextureColumns = value; |  | ||||||
|                 numTextureColumnsParam.SetValue(numTextureColumns); |  | ||||||
|             } |  | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         public bool HardwareInstancingEnabled |         public bool HardwareInstancingEnabled | ||||||
|  | @ -210,18 +198,6 @@ namespace Kav | ||||||
|                 dirtyFlags &= ~EffectDirtyFlags.ViewProj; |                 dirtyFlags &= ~EffectDirtyFlags.ViewProj; | ||||||
|             } |             } | ||||||
| 
 | 
 | ||||||
|             if ((dirtyFlags & EffectDirtyFlags.VertexShaderIndex) != 0) |  | ||||||
|             { |  | ||||||
|                 int vertexShaderIndex = 0; |  | ||||||
| 
 |  | ||||||
|                 if (hardwareInstancingEnabled) |  | ||||||
|                 { |  | ||||||
|                     vertexShaderIndex = 1; |  | ||||||
|                 } |  | ||||||
| 
 |  | ||||||
|                 vertexShaderIndexParam.SetValue(vertexShaderIndex); |  | ||||||
|             } |  | ||||||
| 
 |  | ||||||
|             if ((dirtyFlags & EffectDirtyFlags.PixelShaderIndex) != 0) |             if ((dirtyFlags & EffectDirtyFlags.PixelShaderIndex) != 0) | ||||||
|             { |             { | ||||||
|                 int shaderIndex = 0; |                 int shaderIndex = 0; | ||||||
|  | @ -274,11 +250,9 @@ namespace Kav | ||||||
|             metallicParam = Parameters["MetallicValue"]; |             metallicParam = Parameters["MetallicValue"]; | ||||||
|             roughnessParam = Parameters["RoughnessValue"]; |             roughnessParam = Parameters["RoughnessValue"]; | ||||||
| 
 | 
 | ||||||
|             numTextureRowsParam = Parameters["NumTextureRows"]; |             uvOffsetAndDimensionsParam = Parameters["UVOffsetAndDimensions"]; | ||||||
|             numTextureColumnsParam = Parameters["NumTextureColumns"]; |  | ||||||
| 
 | 
 | ||||||
|             shaderIndexParam = Parameters["PixelShaderIndex"]; |             shaderIndexParam = Parameters["PixelShaderIndex"]; | ||||||
|             vertexShaderIndexParam = Parameters["VertexShaderIndex"]; |  | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -33,8 +33,7 @@ namespace Kav | ||||||
|         Matrix view = Matrix.Identity; |         Matrix view = Matrix.Identity; | ||||||
|         Matrix projection = Matrix.Identity; |         Matrix projection = Matrix.Identity; | ||||||
| 
 | 
 | ||||||
|         Vector2 uvOffset; |         Vector4 uvData; | ||||||
|         Vector2 subTextureDimensions; |  | ||||||
| 
 | 
 | ||||||
|         EffectDirtyFlags dirtyFlags = EffectDirtyFlags.All; |         EffectDirtyFlags dirtyFlags = EffectDirtyFlags.All; | ||||||
| 
 | 
 | ||||||
|  | @ -115,23 +114,13 @@ namespace Kav | ||||||
|             } |             } | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         public Vector2 UVOffset |         public Vector4 UVData | ||||||
|         { |         { | ||||||
|             get { return uvOffset; } |             get { return uvData; } | ||||||
|             set |             set | ||||||
|             { |             { | ||||||
|                 uvOffset = value; |                 uvData = value; | ||||||
|                 dirtyFlags |= EffectDirtyFlags.UVOrDimensions; |                 dirtyFlags |= EffectDirtyFlags.UVData; | ||||||
|             } |  | ||||||
|         } |  | ||||||
| 
 |  | ||||||
|         public Vector2 SubTextureDimensions |  | ||||||
|         { |  | ||||||
|             get { return subTextureDimensions; } |  | ||||||
|             set |  | ||||||
|             { |  | ||||||
|                 subTextureDimensions = value; |  | ||||||
|                 dirtyFlags |= EffectDirtyFlags.UVOrDimensions; |  | ||||||
|             } |             } | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|  | @ -168,14 +157,11 @@ namespace Kav | ||||||
|                 dirtyFlags &= ~EffectDirtyFlags.WorldViewProj; |                 dirtyFlags &= ~EffectDirtyFlags.WorldViewProj; | ||||||
|             } |             } | ||||||
| 
 | 
 | ||||||
|             if ((dirtyFlags & EffectDirtyFlags.UVOrDimensions) != 0) |             if ((dirtyFlags & EffectDirtyFlags.UVData) != 0) | ||||||
|             { |             { | ||||||
|                 uvOffsetAndDimensionsParam.SetValue(new Vector4( |                 uvOffsetAndDimensionsParam.SetValue(uvData); | ||||||
|                     UVOffset.X, UVOffset.Y, |  | ||||||
|                     SubTextureDimensions.X, SubTextureDimensions.Y |  | ||||||
|                 )); |  | ||||||
| 
 | 
 | ||||||
|                 dirtyFlags &= ~EffectDirtyFlags.UVOrDimensions; |                 dirtyFlags &= ~EffectDirtyFlags.UVData; | ||||||
|             } |             } | ||||||
| 
 | 
 | ||||||
|             if ((dirtyFlags & EffectDirtyFlags.PixelShaderIndex) != 0) |             if ((dirtyFlags & EffectDirtyFlags.PixelShaderIndex) != 0) | ||||||
|  |  | ||||||
|  | @ -11,7 +11,7 @@ namespace Kav | ||||||
|         VertexShaderIndex = 8, |         VertexShaderIndex = 8, | ||||||
|         PixelShaderIndex = 16, |         PixelShaderIndex = 16, | ||||||
|         ViewProj = 32, |         ViewProj = 32, | ||||||
|         UVOrDimensions = 64, |         UVData = 64, | ||||||
|         All = -1 |         All = -1 | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  |  | ||||||
							
								
								
									
										
											BIN
										
									
								
								Effects/FXB/DeferredPBR_GBufferEffect.fxb (Stored with Git LFS)
								
								
								
								
							
							
						
						
									
										
											BIN
										
									
								
								Effects/FXB/DeferredPBR_GBufferEffect.fxb (Stored with Git LFS)
								
								
								
								
							
										
											Binary file not shown.
										
									
								
							|  | @ -4,29 +4,31 @@ DECLARE_TEXTURE(AlbedoTexture, 0); | ||||||
| DECLARE_TEXTURE(NormalTexture, 1); | DECLARE_TEXTURE(NormalTexture, 1); | ||||||
| DECLARE_TEXTURE(MetallicRoughnessTexture, 2); | DECLARE_TEXTURE(MetallicRoughnessTexture, 2); | ||||||
| 
 | 
 | ||||||
| BEGIN_CONSTANTS | float3 AlbedoValue; | ||||||
|  | float MetallicValue; | ||||||
|  | float RoughnessValue; | ||||||
| 
 | 
 | ||||||
|     float3 AlbedoValue                         _ps(c0)    _cb(c0); | float4 UVOffsetAndDimensions; | ||||||
|     float MetallicValue                        _ps(c1)    _cb(c1); |  | ||||||
|     float RoughnessValue                       _ps(c2)    _cb(c2); |  | ||||||
| 
 | 
 | ||||||
|     int NumTextureRows              _vs(c9)               _cb(c3); | float4x4 World; | ||||||
|     int NumTextureColumns           _vs(c10)              _cb(c4); | float4x4 ViewProjection; | ||||||
| 
 |  | ||||||
| MATRIX_CONSTANTS |  | ||||||
| 
 |  | ||||||
|     float4x4 World                  _vs(c0)               _cb(c7); |  | ||||||
|     float4x4 ViewProjection         _vs(c4)               _cb(c11); |  | ||||||
| 
 |  | ||||||
| END_CONSTANTS |  | ||||||
| 
 | 
 | ||||||
| struct VertexInput | struct VertexInput | ||||||
| { | { | ||||||
|     float4 Position : POSITION; |     float3 Position : POSITION; | ||||||
|     float3 Normal   : NORMAL; |     float3 Normal   : NORMAL; | ||||||
|     float2 TexCoord : TEXCOORD; |     float2 TexCoord : TEXCOORD; | ||||||
| }; | }; | ||||||
| 
 | 
 | ||||||
|  | struct VertexInstancedInput | ||||||
|  | { | ||||||
|  |     float3 Position : POSITION; | ||||||
|  |     float3 Normal   : NORMAL; | ||||||
|  |     float2 TexCoord : TEXCOORD0; | ||||||
|  |     float3 Translation : TEXCOORD2; | ||||||
|  |     float2 UVData : TANGENT; | ||||||
|  | }; | ||||||
|  | 
 | ||||||
| struct PixelInput | struct PixelInput | ||||||
| { | { | ||||||
|     float4 Position      : SV_POSITION; |     float4 Position      : SV_POSITION; | ||||||
|  | @ -51,7 +53,11 @@ PixelInput main_vs(VertexInput input) | ||||||
| 
 | 
 | ||||||
|     output.PositionWorld = mul(input.Position, World).xyz; |     output.PositionWorld = mul(input.Position, World).xyz; | ||||||
|     output.NormalWorld = normalize(mul(input.Normal, World)); |     output.NormalWorld = normalize(mul(input.Normal, World)); | ||||||
|     output.TexCoord = input.TexCoord; | 
 | ||||||
|  |     float2 texCoord; | ||||||
|  |     texCoord.x = (input.TexCoord.x / UVOffsetAndDimensions.z) + UVOffsetAndDimensions.x; | ||||||
|  |     texCoord.y = (input.TexCoord.y / UVOffsetAndDimensions.w) + UVOffsetAndDimensions.y; | ||||||
|  |     output.TexCoord = texCoord; | ||||||
| 
 | 
 | ||||||
|     float4x4 worldViewProjection = mul(World, ViewProjection); |     float4x4 worldViewProjection = mul(World, ViewProjection); | ||||||
|     output.Position = mul(input.Position, worldViewProjection); |     output.Position = mul(input.Position, worldViewProjection); | ||||||
|  | @ -60,9 +66,7 @@ PixelInput main_vs(VertexInput input) | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| PixelInput instanced_vs( | PixelInput instanced_vs( | ||||||
|     VertexInput input,  |     VertexInstancedInput input | ||||||
|     float3 Translation : TEXCOORD2, |  | ||||||
|     float2 UVOffset    : TEXCOORD5 |  | ||||||
| ) { | ) { | ||||||
|     PixelInput output; |     PixelInput output; | ||||||
| 
 | 
 | ||||||
|  | @ -70,7 +74,7 @@ PixelInput instanced_vs( | ||||||
|         float4(1, 0, 0, 0), |         float4(1, 0, 0, 0), | ||||||
|         float4(0, 1, 0, 0), |         float4(0, 1, 0, 0), | ||||||
|         float4(0, 0, 1, 0), |         float4(0, 0, 1, 0), | ||||||
|         float4(Translation.x, Translation.y, Translation.z, 1) |         float4(input.Translation.x, input.Translation.y, input.Translation.z, 1) | ||||||
|     ); |     ); | ||||||
| 
 | 
 | ||||||
|     float4x4 worldViewProjection = mul(world, ViewProjection); |     float4x4 worldViewProjection = mul(world, ViewProjection); | ||||||
|  | @ -79,9 +83,9 @@ PixelInput instanced_vs( | ||||||
|     output.NormalWorld = mul(input.Normal, world); |     output.NormalWorld = mul(input.Normal, world); | ||||||
| 
 | 
 | ||||||
|     float2 texCoord; |     float2 texCoord; | ||||||
|     texCoord.x = (input.TexCoord.x / NumTextureColumns + UVOffset.x); |     texCoord.x = (input.TexCoord.x / 8) + input.UVData.x; | ||||||
|     texCoord.y = (input.TexCoord.y / NumTextureRows + UVOffset.y); |     texCoord.y = (input.TexCoord.y / 1) + input.UVData.y; | ||||||
|     output.TexCoord = texCoord; |     output.TexCoord = input.TexCoord; | ||||||
| 
 | 
 | ||||||
|     output.Position = mul(input.Position, worldViewProjection); |     output.Position = mul(input.Position, worldViewProjection); | ||||||
| 
 | 
 | ||||||
|  | @ -204,19 +208,6 @@ PixelOutput AlbedoMetallicRoughnessNormalMapPS(PixelInput input) | ||||||
|     return output; |     return output; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| VertexShader VSArray[2] = |  | ||||||
| { |  | ||||||
|     compile vs_3_0 main_vs(), |  | ||||||
|     compile vs_3_0 instanced_vs() |  | ||||||
| }; |  | ||||||
| 
 |  | ||||||
| int VSIndices[2] = |  | ||||||
| { |  | ||||||
|     0, 1 |  | ||||||
| }; |  | ||||||
| 
 |  | ||||||
| int VertexShaderIndex = 0; |  | ||||||
| 
 |  | ||||||
| PixelShader PSArray[8] = | PixelShader PSArray[8] = | ||||||
| { | { | ||||||
|     compile ps_3_0 NonePS(), |     compile ps_3_0 NonePS(), | ||||||
|  | @ -243,7 +234,7 @@ Technique GBuffer | ||||||
| { | { | ||||||
|     Pass |     Pass | ||||||
|     { |     { | ||||||
|         VertexShader = (VSArray[VSIndices[VertexShaderIndex]]); |         VertexShader = compile vs_3_0 instanced_vs(); | ||||||
|         PixelShader  = (PSArray[PSIndices[PixelShaderIndex]]); |         PixelShader  = (PSArray[PSIndices[PixelShaderIndex]]); | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -5,5 +5,6 @@ namespace Kav | ||||||
|     public interface ICullable |     public interface ICullable | ||||||
|     { |     { | ||||||
|         BoundingBox BoundingBox { get; } |         BoundingBox BoundingBox { get; } | ||||||
|  |         Matrix TransformMatrix { get; } | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -1,3 +1,4 @@ | ||||||
|  | using Kav.Data; | ||||||
| using Microsoft.Xna.Framework; | using Microsoft.Xna.Framework; | ||||||
| using Microsoft.Xna.Framework.Graphics; | using Microsoft.Xna.Framework.Graphics; | ||||||
| 
 | 
 | ||||||
|  | @ -12,8 +13,5 @@ namespace Kav | ||||||
|         Texture2D AlbedoTexture { get; } |         Texture2D AlbedoTexture { get; } | ||||||
|         Texture2D NormalTexture { get; } |         Texture2D NormalTexture { get; } | ||||||
|         Texture2D MetallicRoughnessTexture { get; } |         Texture2D MetallicRoughnessTexture { get; } | ||||||
| 
 |  | ||||||
|         int NumTextureRows { get; } |  | ||||||
|         int NumTextureColumns { get; } |  | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -0,0 +1,9 @@ | ||||||
|  | using Kav.Data; | ||||||
|  | 
 | ||||||
|  | namespace Kav | ||||||
|  | { | ||||||
|  |     public interface IUVDrawable | ||||||
|  |     { | ||||||
|  |         UVData UVData { get; } | ||||||
|  |     } | ||||||
|  | } | ||||||
|  | @ -3,7 +3,7 @@ using Microsoft.Xna.Framework.Graphics; | ||||||
| 
 | 
 | ||||||
| namespace Kav | namespace Kav | ||||||
| { | { | ||||||
|     public class MeshPart : IIndexDrawable, IGBufferDrawable, ICullable, IHasVertexPositions |     public class MeshPart : IIndexDrawable, IGBufferDrawable, IHasVertexPositions | ||||||
|     { |     { | ||||||
|         public IndexBuffer IndexBuffer { get; } |         public IndexBuffer IndexBuffer { get; } | ||||||
|         public VertexBuffer VertexBuffer { get; } |         public VertexBuffer VertexBuffer { get; } | ||||||
|  | @ -38,9 +38,6 @@ namespace Kav | ||||||
|         public float Metallic { get; set; } = 0.5f; |         public float Metallic { get; set; } = 0.5f; | ||||||
|         public float Roughness { get; set; } = 0.5f; |         public float Roughness { get; set; } = 0.5f; | ||||||
| 
 | 
 | ||||||
|         public int NumTextureRows { get; set; } = 1; |  | ||||||
|         public int NumTextureColumns { get; set; } = 1; |  | ||||||
| 
 |  | ||||||
|         public bool DisableAlbedoMap { get; set; } = false; |         public bool DisableAlbedoMap { get; set; } = false; | ||||||
|         public bool DisableNormalMap { get; set; } = false; |         public bool DisableNormalMap { get; set; } = false; | ||||||
|         public bool DisableMetallicRoughnessMap { get; set; } = false; |         public bool DisableMetallicRoughnessMap { get; set; } = false; | ||||||
|  |  | ||||||
|  | @ -4,7 +4,7 @@ using Microsoft.Xna.Framework.Graphics; | ||||||
| 
 | 
 | ||||||
| namespace Kav | namespace Kav | ||||||
| { | { | ||||||
|     public class MeshSprite : ICullable, IIndexDrawable |     public class MeshSprite : IIndexDrawable | ||||||
|     { |     { | ||||||
|         public enum FlipOptions |         public enum FlipOptions | ||||||
|         { |         { | ||||||
|  |  | ||||||
|  | @ -2,7 +2,7 @@ using Microsoft.Xna.Framework; | ||||||
| 
 | 
 | ||||||
| namespace Kav | namespace Kav | ||||||
| { | { | ||||||
|     public class Model : ICullable |     public class Model | ||||||
|     { |     { | ||||||
|         public Mesh[] Meshes { get; } |         public Mesh[] Meshes { get; } | ||||||
|         public BoundingBox BoundingBox { get; } |         public BoundingBox BoundingBox { get; } | ||||||
|  |  | ||||||
							
								
								
									
										65
									
								
								Renderer.cs
								
								
								
								
							
							
						
						
									
										65
									
								
								Renderer.cs
								
								
								
								
							|  | @ -159,20 +159,14 @@ namespace Kav | ||||||
| 
 | 
 | ||||||
|             var boundingFrustum = new BoundingFrustum(camera.View * camera.Projection); |             var boundingFrustum = new BoundingFrustum(camera.View * camera.Projection); | ||||||
| 
 | 
 | ||||||
|             foreach (var data in meshSpriteDrawDatas) |             foreach (var data in FrustumCull(boundingFrustum, meshSpriteDrawDatas)) | ||||||
|             { |             { | ||||||
|                 var matrix = BillboardTransforms(camera, data.TransformMatrix, data.BillboardConstraint); |                 var matrix = BillboardTransforms(camera, data.TransformMatrix, data.BillboardConstraint); | ||||||
| 
 | 
 | ||||||
|                 if (FrustumCull(boundingFrustum, data.MeshSprite, matrix)) |  | ||||||
|                 { |  | ||||||
|                     continue; |  | ||||||
|                 } |  | ||||||
| 
 |  | ||||||
|                 DiffuseLitSpriteEffect.NormalMapEnabled = data.MeshSprite.Normal != null; |                 DiffuseLitSpriteEffect.NormalMapEnabled = data.MeshSprite.Normal != null; | ||||||
| 
 | 
 | ||||||
|                 DiffuseLitSpriteEffect.World = matrix; |                 DiffuseLitSpriteEffect.World = matrix; | ||||||
|                 DiffuseLitSpriteEffect.UVOffset = data.UVOffset.Offset; |                 DiffuseLitSpriteEffect.UVData = data.UVData.ToVector4(); | ||||||
|                 DiffuseLitSpriteEffect.SubTextureDimensions = data.UVOffset.Percentage; |  | ||||||
| 
 | 
 | ||||||
|                 GraphicsDevice.Textures[0] = data.MeshSprite.Texture; |                 GraphicsDevice.Textures[0] = data.MeshSprite.Texture; | ||||||
|                 GraphicsDevice.Textures[1] = data.MeshSprite.Normal; |                 GraphicsDevice.Textures[1] = data.MeshSprite.Normal; | ||||||
|  | @ -232,13 +226,13 @@ namespace Kav | ||||||
|         public static void CullAndRenderIndexed<T, U>( |         public static void CullAndRenderIndexed<T, U>( | ||||||
|             GraphicsDevice graphicsDevice, |             GraphicsDevice graphicsDevice, | ||||||
|             BoundingFrustum boundingFrustum, |             BoundingFrustum boundingFrustum, | ||||||
|             IEnumerable<(T, Matrix)> drawableTransformPairs, |             IEnumerable<T> drawables, | ||||||
|             U effect |             U effect | ||||||
|         ) where T : IIndexDrawable, ICullable where U : Effect, IHasWorldMatrix |         ) where T : IIndexDrawable, ICullable where U : Effect, IHasWorldMatrix | ||||||
|         { |         { | ||||||
|             foreach (var (drawable, transform) in FrustumCull(boundingFrustum, drawableTransformPairs)) |             foreach (var drawable in FrustumCull(boundingFrustum, drawables)) | ||||||
|             { |             { | ||||||
|                 effect.World = transform; |                 effect.World = drawable.TransformMatrix; | ||||||
| 
 | 
 | ||||||
|                 RenderIndexed( |                 RenderIndexed( | ||||||
|                     graphicsDevice, |                     graphicsDevice, | ||||||
|  | @ -316,7 +310,7 @@ namespace Kav | ||||||
|         public void RenderDepthIndexed<T>( |         public void RenderDepthIndexed<T>( | ||||||
|             RenderTarget2D renderTarget, |             RenderTarget2D renderTarget, | ||||||
|             PerspectiveCamera camera, |             PerspectiveCamera camera, | ||||||
|             IEnumerable<(T, Matrix)> drawableTransforms |             IEnumerable<T> drawables | ||||||
|         ) where T : ICullable, IIndexDrawable |         ) where T : ICullable, IIndexDrawable | ||||||
|         { |         { | ||||||
|             GraphicsDevice.SetRenderTarget(renderTarget); |             GraphicsDevice.SetRenderTarget(renderTarget); | ||||||
|  | @ -328,7 +322,7 @@ namespace Kav | ||||||
|             CullAndRenderIndexed( |             CullAndRenderIndexed( | ||||||
|                 GraphicsDevice, |                 GraphicsDevice, | ||||||
|                 new BoundingFrustum(camera.View * camera.Projection), |                 new BoundingFrustum(camera.View * camera.Projection), | ||||||
|                 drawableTransforms, |                 drawables, | ||||||
|                 SimpleDepthEffect |                 SimpleDepthEffect | ||||||
|             ); |             ); | ||||||
|         } |         } | ||||||
|  | @ -381,9 +375,6 @@ namespace Kav | ||||||
|             Deferred_GBufferEffect.Metallic = drawable.Metallic; |             Deferred_GBufferEffect.Metallic = drawable.Metallic; | ||||||
|             Deferred_GBufferEffect.Roughness = drawable.Roughness; |             Deferred_GBufferEffect.Roughness = drawable.Roughness; | ||||||
| 
 | 
 | ||||||
|             Deferred_GBufferEffect.NumTextureRows = drawable.NumTextureRows; |  | ||||||
|             Deferred_GBufferEffect.NumTextureColumns = drawable.NumTextureColumns; |  | ||||||
| 
 |  | ||||||
|             Deferred_GBufferEffect.AlbedoTexture = drawable.AlbedoTexture; |             Deferred_GBufferEffect.AlbedoTexture = drawable.AlbedoTexture; | ||||||
|             Deferred_GBufferEffect.NormalTexture = drawable.NormalTexture; |             Deferred_GBufferEffect.NormalTexture = drawable.NormalTexture; | ||||||
|             Deferred_GBufferEffect.MetallicRoughnessTexture = drawable.MetallicRoughnessTexture; |             Deferred_GBufferEffect.MetallicRoughnessTexture = drawable.MetallicRoughnessTexture; | ||||||
|  | @ -417,8 +408,8 @@ namespace Kav | ||||||
|         public void RenderGBufferIndexed<T>( |         public void RenderGBufferIndexed<T>( | ||||||
|             RenderTargetBinding[] gBuffer, |             RenderTargetBinding[] gBuffer, | ||||||
|             PerspectiveCamera camera, |             PerspectiveCamera camera, | ||||||
|             IEnumerable<(T, Matrix)> drawableTransforms |             IEnumerable<T> drawables | ||||||
|         ) where T : ICullable, IIndexDrawable, IGBufferDrawable { |         ) where T : ICullable, IIndexDrawable, IUVDrawable, IGBufferDrawable { | ||||||
|             GraphicsDevice.SetRenderTargets(gBuffer); |             GraphicsDevice.SetRenderTargets(gBuffer); | ||||||
|             GraphicsDevice.DepthStencilState = DepthStencilState.Default; |             GraphicsDevice.DepthStencilState = DepthStencilState.Default; | ||||||
|             GraphicsDevice.BlendState = BlendState.Opaque; |             GraphicsDevice.BlendState = BlendState.Opaque; | ||||||
|  | @ -429,9 +420,9 @@ namespace Kav | ||||||
| 
 | 
 | ||||||
|             var boundingFrustum = new BoundingFrustum(camera.View * camera.Projection); |             var boundingFrustum = new BoundingFrustum(camera.View * camera.Projection); | ||||||
| 
 | 
 | ||||||
|             foreach (var (drawable, transform) in FrustumCull(boundingFrustum, drawableTransforms)) |             foreach (var drawable in FrustumCull(boundingFrustum, drawables)) | ||||||
|             { |             { | ||||||
|                 Deferred_GBufferEffect.World = transform; |                 Deferred_GBufferEffect.World = drawable.TransformMatrix; | ||||||
| 
 | 
 | ||||||
|                 Deferred_GBufferEffect.HardwareInstancingEnabled = false; |                 Deferred_GBufferEffect.HardwareInstancingEnabled = false; | ||||||
| 
 | 
 | ||||||
|  | @ -439,6 +430,8 @@ namespace Kav | ||||||
|                 Deferred_GBufferEffect.Metallic = drawable.Metallic; |                 Deferred_GBufferEffect.Metallic = drawable.Metallic; | ||||||
|                 Deferred_GBufferEffect.Roughness = drawable.Roughness; |                 Deferred_GBufferEffect.Roughness = drawable.Roughness; | ||||||
| 
 | 
 | ||||||
|  |                 Deferred_GBufferEffect.UVData = drawable.UVData.ToVector4(); | ||||||
|  | 
 | ||||||
|                 Deferred_GBufferEffect.AlbedoTexture = drawable.AlbedoTexture; |                 Deferred_GBufferEffect.AlbedoTexture = drawable.AlbedoTexture; | ||||||
|                 Deferred_GBufferEffect.NormalTexture = drawable.NormalTexture; |                 Deferred_GBufferEffect.NormalTexture = drawable.NormalTexture; | ||||||
|                 Deferred_GBufferEffect.MetallicRoughnessTexture = drawable.MetallicRoughnessTexture; |                 Deferred_GBufferEffect.MetallicRoughnessTexture = drawable.MetallicRoughnessTexture; | ||||||
|  | @ -684,7 +677,7 @@ namespace Kav | ||||||
| 
 | 
 | ||||||
|         public void RenderDirectionalShadowsIndexed<T>( |         public void RenderDirectionalShadowsIndexed<T>( | ||||||
|             DirectionalShadowMapData shadowMapData, |             DirectionalShadowMapData shadowMapData, | ||||||
|             IEnumerable<(T, Matrix)> drawableTransforms |             IEnumerable<T> drawableTransforms | ||||||
|         ) where T : ICullable, IIndexDrawable { |         ) where T : ICullable, IIndexDrawable { | ||||||
|             // render the individual shadow cascades |             // render the individual shadow cascades | ||||||
|             for (var i = 0; i < shadowMapData.NumShadowCascades; i++) |             for (var i = 0; i < shadowMapData.NumShadowCascades; i++) | ||||||
|  | @ -700,7 +693,7 @@ namespace Kav | ||||||
|         private void RenderDirectionalShadowMapIndexed<T>( |         private void RenderDirectionalShadowMapIndexed<T>( | ||||||
|             DirectionalShadowMapData shadowMapData, |             DirectionalShadowMapData shadowMapData, | ||||||
|             int shadowCascadeIndex, |             int shadowCascadeIndex, | ||||||
|             IEnumerable<(T, Matrix)> drawableTransforms |             IEnumerable<T> drawables | ||||||
|         ) where T : ICullable, IIndexDrawable { |         ) where T : ICullable, IIndexDrawable { | ||||||
|             GraphicsDevice.SetRenderTarget(shadowMapData.ShadowMaps[shadowCascadeIndex]); |             GraphicsDevice.SetRenderTarget(shadowMapData.ShadowMaps[shadowCascadeIndex]); | ||||||
|             GraphicsDevice.DepthStencilState = DepthStencilState.Default; |             GraphicsDevice.DepthStencilState = DepthStencilState.Default; | ||||||
|  | @ -712,7 +705,7 @@ namespace Kav | ||||||
|             CullAndRenderIndexed( |             CullAndRenderIndexed( | ||||||
|                 GraphicsDevice, |                 GraphicsDevice, | ||||||
|                 new BoundingFrustum(SimpleDepthEffect.View * SimpleDepthEffect.Projection), |                 new BoundingFrustum(SimpleDepthEffect.View * SimpleDepthEffect.Projection), | ||||||
|                 drawableTransforms, |                 drawables, | ||||||
|                 SimpleDepthEffect |                 SimpleDepthEffect | ||||||
|             ); |             ); | ||||||
|         } |         } | ||||||
|  | @ -763,7 +756,7 @@ namespace Kav | ||||||
| 
 | 
 | ||||||
|         public void RenderPointShadowMapIndexed<T>( |         public void RenderPointShadowMapIndexed<T>( | ||||||
|             RenderTargetCube pointShadowCubeMap, |             RenderTargetCube pointShadowCubeMap, | ||||||
|             IEnumerable<(T, Matrix)> modelTransforms, |             IEnumerable<T> drawables, | ||||||
|             PointLight pointLight |             PointLight pointLight | ||||||
|         ) where T : ICullable, IIndexDrawable { |         ) where T : ICullable, IIndexDrawable { | ||||||
|             GraphicsDevice.DepthStencilState = DepthStencilState.Default; |             GraphicsDevice.DepthStencilState = DepthStencilState.Default; | ||||||
|  | @ -831,7 +824,7 @@ namespace Kav | ||||||
|                 CullAndRenderIndexed( |                 CullAndRenderIndexed( | ||||||
|                     GraphicsDevice, |                     GraphicsDevice, | ||||||
|                     new BoundingFrustum(LinearDepthEffect.View * LinearDepthEffect.Projection), |                     new BoundingFrustum(LinearDepthEffect.View * LinearDepthEffect.Projection), | ||||||
|                     modelTransforms, |                     drawables, | ||||||
|                     LinearDepthEffect |                     LinearDepthEffect | ||||||
|                 ); |                 ); | ||||||
|             } |             } | ||||||
|  | @ -843,7 +836,7 @@ namespace Kav | ||||||
|             VertexBuffer instanceVertexBuffer, |             VertexBuffer instanceVertexBuffer, | ||||||
|             int numInstances, |             int numInstances, | ||||||
|             PointLight pointLight |             PointLight pointLight | ||||||
|         ) where T : ICullable, IIndexDrawable |         ) where T : IIndexDrawable | ||||||
|         { |         { | ||||||
|             GraphicsDevice.DepthStencilState = DepthStencilState.Default; |             GraphicsDevice.DepthStencilState = DepthStencilState.Default; | ||||||
|             GraphicsDevice.BlendState = BlendState.Opaque; |             GraphicsDevice.BlendState = BlendState.Opaque; | ||||||
|  | @ -917,31 +910,21 @@ namespace Kav | ||||||
|             } |             } | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         private static IEnumerable<(T, Matrix)> FrustumCull<T>( |         private static IEnumerable<T> FrustumCull<T>( | ||||||
|             BoundingFrustum boundingFrustum, |             BoundingFrustum boundingFrustum, | ||||||
|             IEnumerable<(T, Matrix)> cullableTransforms |             IEnumerable<T> cullables | ||||||
|         ) where T : ICullable { |         ) where T : ICullable { | ||||||
|             foreach (var (cullable, transform) in cullableTransforms) |             foreach (var cullable in cullables) | ||||||
|             { |             { | ||||||
|                 var boundingBox = TransformedBoundingBox(cullable.BoundingBox, transform); |                 var boundingBox = TransformedBoundingBox(cullable.BoundingBox, cullable.TransformMatrix); | ||||||
|                 var containment = boundingFrustum.Contains(boundingBox); |                 var containment = boundingFrustum.Contains(boundingBox); | ||||||
|                 if (containment != ContainmentType.Disjoint) |                 if (containment != ContainmentType.Disjoint) | ||||||
|                 { |                 { | ||||||
|                     yield return (cullable, transform); |                     yield return cullable; | ||||||
|                 } |                 } | ||||||
|             } |             } | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         private static bool FrustumCull<T>( |  | ||||||
|             BoundingFrustum boundingFrustum, |  | ||||||
|             T cullable, |  | ||||||
|             Matrix transform |  | ||||||
|         ) where T : ICullable { |  | ||||||
|             var boundingBox = TransformedBoundingBox(cullable.BoundingBox, transform); |  | ||||||
|             var containment = boundingFrustum.Contains(boundingBox); |  | ||||||
|             return (containment == ContainmentType.Disjoint); |  | ||||||
|         } |  | ||||||
| 
 |  | ||||||
|         private static BoundingBox TransformedBoundingBox(BoundingBox boundingBox, Matrix matrix) |         private static BoundingBox TransformedBoundingBox(BoundingBox boundingBox, Matrix matrix) | ||||||
|         { |         { | ||||||
|             var center = (boundingBox.Min + boundingBox.Max) / 2f; |             var center = (boundingBox.Min + boundingBox.Max) / 2f; | ||||||
|  |  | ||||||
|  | @ -12,7 +12,7 @@ namespace Kav | ||||||
|         public static VertexDeclaration PositionTextureOffsetInstanceDeclaration = new VertexDeclaration |         public static VertexDeclaration PositionTextureOffsetInstanceDeclaration = new VertexDeclaration | ||||||
|         ( |         ( | ||||||
|             new VertexElement(0, VertexElementFormat.Vector3, VertexElementUsage.TextureCoordinate, 2), |             new VertexElement(0, VertexElementFormat.Vector3, VertexElementUsage.TextureCoordinate, 2), | ||||||
|             new VertexElement(12, VertexElementFormat.Vector2, VertexElementUsage.TextureCoordinate, 5) |             new VertexElement(12, VertexElementFormat.Vector4, VertexElementUsage.Tangent, 0) | ||||||
|         ); |         ); | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -16,16 +16,16 @@ namespace Kav | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         public Vector3 Translation { get; set; } |         public Vector3 Translation { get; set; } | ||||||
|         public Vector2 UVOffset { get; set; } |         public Vector4 UVData { get; set; } | ||||||
| 
 | 
 | ||||||
|         public static readonly VertexDeclaration VertexDeclaration; |         public static readonly VertexDeclaration VertexDeclaration; | ||||||
| 
 | 
 | ||||||
|         public PositionTextureOffsetInstanceVertex( |         public PositionTextureOffsetInstanceVertex( | ||||||
|             Vector3 translation, |             Vector3 translation, | ||||||
|             Vector2 uvOffset |             Vector4 uvData | ||||||
|         ) { |         ) { | ||||||
|             Translation = translation; |             Translation = translation; | ||||||
|             UVOffset = uvOffset; |             UVData = uvData; | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue