From 0ebad486c5ba0d7830fffe60f42313f84915f9e1 Mon Sep 17 00:00:00 2001 From: cosmonaut Date: Thu, 10 Dec 2020 13:53:55 -0800 Subject: [PATCH] diffuse sprite effect takes uv data --- Data/MeshSpriteDrawData.cs | 8 +++--- Data/UVData.cs | 19 +++++++++++++ Data/UVOffsets.cs | 22 --------------- Effects/DiffuseLitSpriteEffect.cs | 38 ++++++++++++++++++++++++++ Effects/EffectHelpers.cs | 1 + Effects/FXB/DiffuseLitSpriteEffect.fxb | 4 +-- Effects/HLSL/DiffuseLitSpriteEffect.fx | 28 +++++++++---------- Renderer.cs | 28 ++++++++++--------- 8 files changed, 93 insertions(+), 55 deletions(-) create mode 100644 Data/UVData.cs delete mode 100644 Data/UVOffsets.cs diff --git a/Data/MeshSpriteDrawData.cs b/Data/MeshSpriteDrawData.cs index 42f4eab..8d064bb 100644 --- a/Data/MeshSpriteDrawData.cs +++ b/Data/MeshSpriteDrawData.cs @@ -2,23 +2,23 @@ using Microsoft.Xna.Framework; namespace Kav.Data { - public struct MeshSpriteDrawData + public struct MeshSpriteDrawData { public MeshSprite MeshSprite { get; } public SpriteBillboardConstraint BillboardConstraint { get; } public Matrix TransformMatrix { get; } - public UVOffsets UVOffsets { get; } + public UVData UVOffset { get; } public MeshSpriteDrawData( MeshSprite meshSprite, SpriteBillboardConstraint billboardConstraint, Matrix transformMatrix, - UVOffsets uVOffsets + UVData offset ) { MeshSprite = meshSprite; BillboardConstraint = billboardConstraint; TransformMatrix = transformMatrix; - UVOffsets = uVOffsets; + UVOffset = offset; } } } diff --git a/Data/UVData.cs b/Data/UVData.cs new file mode 100644 index 0000000..0295804 --- /dev/null +++ b/Data/UVData.cs @@ -0,0 +1,19 @@ +using Microsoft.Xna.Framework; + +namespace Kav.Data +{ + public struct UVData + { + public Vector2 Offset { get; } + public Vector2 Percentage { get; } + + public UVData( + Vector2 positionInAtlas, + Vector2 subTextureDimensions, + Vector2 atlasDimensions + ) { + Percentage = subTextureDimensions / atlasDimensions; + Offset = positionInAtlas / atlasDimensions; + } + } +} diff --git a/Data/UVOffsets.cs b/Data/UVOffsets.cs deleted file mode 100644 index c73d2c9..0000000 --- a/Data/UVOffsets.cs +++ /dev/null @@ -1,22 +0,0 @@ -using Microsoft.Xna.Framework; - -namespace Kav.Data -{ - public struct UVOffsets - { - public static UVOffsets Default { get; } = new UVOffsets( - new Vector2(0, 0), new Vector2(1, 1) - ); - - // (start / width), (end / width) - public Vector2 StartUV { get; } - // (start / height), (end / height) - public Vector2 EndUV { get; } - - public UVOffsets(Vector2 startUV, Vector2 endUV) - { - StartUV = startUV; - EndUV = endUV; - } - } -} diff --git a/Effects/DiffuseLitSpriteEffect.cs b/Effects/DiffuseLitSpriteEffect.cs index ae8618c..0b7ea57 100644 --- a/Effects/DiffuseLitSpriteEffect.cs +++ b/Effects/DiffuseLitSpriteEffect.cs @@ -1,3 +1,4 @@ +using Kav.Data; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; @@ -10,6 +11,8 @@ namespace Kav EffectParameter directionalLightDirectionParam; EffectParameter directionalLightColorParam; + EffectParameter uvOffsetAndDimensionsParam; + EffectParameter worldParam; EffectParameter worldViewProjectionParam; EffectParameter worldInverseTransposeParam; @@ -30,6 +33,9 @@ namespace Kav Matrix view = Matrix.Identity; Matrix projection = Matrix.Identity; + Vector2 uvOffset; + Vector2 subTextureDimensions; + EffectDirtyFlags dirtyFlags = EffectDirtyFlags.All; public bool NormalMapEnabled @@ -109,6 +115,26 @@ namespace Kav } } + public Vector2 UVOffset + { + get { return uvOffset; } + set + { + uvOffset = value; + dirtyFlags |= EffectDirtyFlags.UVOrDimensions; + } + } + + public Vector2 SubTextureDimensions + { + get { return subTextureDimensions; } + set + { + subTextureDimensions = value; + dirtyFlags |= EffectDirtyFlags.UVOrDimensions; + } + } + public DiffuseLitSpriteEffect(GraphicsDevice graphicsDevice) : base(graphicsDevice, Resources.DiffuseLitSpriteEffect) { CacheEffectParameters(); @@ -142,6 +168,16 @@ namespace Kav dirtyFlags &= ~EffectDirtyFlags.WorldViewProj; } + if ((dirtyFlags & EffectDirtyFlags.UVOrDimensions) != 0) + { + uvOffsetAndDimensionsParam.SetValue(new Vector4( + UVOffset.X, UVOffset.Y, + SubTextureDimensions.X, SubTextureDimensions.Y + )); + + dirtyFlags &= ~EffectDirtyFlags.UVOrDimensions; + } + if ((dirtyFlags & EffectDirtyFlags.PixelShaderIndex) != 0) { int shaderIndex = 0; @@ -166,6 +202,8 @@ namespace Kav directionalLightDirectionParam = Parameters["DirectionalLightDirection"]; directionalLightColorParam = Parameters["DirectionalLightColor"]; + uvOffsetAndDimensionsParam = Parameters["UVOffsetAndDimensions"]; + shaderIndexParam = Parameters["ShaderIndex"]; } } diff --git a/Effects/EffectHelpers.cs b/Effects/EffectHelpers.cs index 56a0cec..8705b63 100644 --- a/Effects/EffectHelpers.cs +++ b/Effects/EffectHelpers.cs @@ -11,6 +11,7 @@ namespace Kav VertexShaderIndex = 8, PixelShaderIndex = 16, ViewProj = 32, + UVOrDimensions = 64, All = -1 } } diff --git a/Effects/FXB/DiffuseLitSpriteEffect.fxb b/Effects/FXB/DiffuseLitSpriteEffect.fxb index 4516be8..9c25af2 100644 --- a/Effects/FXB/DiffuseLitSpriteEffect.fxb +++ b/Effects/FXB/DiffuseLitSpriteEffect.fxb @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:ad16b1f9036e4c65e4615d3c71cb099bbe2dee346cefef61fc067e6111d3a7b5 -size 7168 +oid sha256:245ec4c40174cc29a8af797a30da477b6e2c9ddc95ed4999070bf1226d77ea7d +size 7692 diff --git a/Effects/HLSL/DiffuseLitSpriteEffect.fx b/Effects/HLSL/DiffuseLitSpriteEffect.fx index ca5e7d2..7114824 100644 --- a/Effects/HLSL/DiffuseLitSpriteEffect.fx +++ b/Effects/HLSL/DiffuseLitSpriteEffect.fx @@ -5,23 +5,19 @@ DECLARE_TEXTURE(Texture, 0); DECLARE_TEXTURE(Normal, 1); -BEGIN_CONSTANTS +float3 AmbientColor; - float3 AmbientColor _ps(c0) _cb(c0); +float3 PointLightPositions[8]; +float3 PointLightColors[8]; - float3 PointLightPositions[8] _ps(c1) _cb(c1); - float3 PointLightColors[8] _ps(c9) _cb(c9); +float3 DirectionalLightDirection; +float3 DirectionalLightColor; - float3 DirectionalLightDirection _ps(c17) _cb(c17); - float3 DirectionalLightColor _ps(c18) _cb(c18); +float4 UVOffsetAndDimensions; -MATRIX_CONSTANTS - - float4x4 WorldInverseTranspose _ps(c19) _cb(c19); - float4x4 World _vs(c0) _cb(c23); - float4x4 WorldViewProjection _vs(c4) _cb(c27); - -END_CONSTANTS +float4x4 WorldInverseTranspose; +float4x4 World; +float4x4 WorldViewProjection; struct VertexShaderInput { @@ -43,10 +39,14 @@ PixelShaderInput main_vs(VertexShaderInput input) PixelShaderInput output; output.Position = mul(input.Position, WorldViewProjection); - output.TexCoord = input.TexCoord; output.NormalWS = normalize(mul(input.Normal, (float3x3)WorldInverseTranspose)); output.PositionWS = mul(input.Position, World).xyz; + float2 texCoord; + texCoord.x = (input.TexCoord.x / UVOffsetAndDimensions.z) + UVOffsetAndDimensions.x; + texCoord.y = (input.TexCoord.y / UVOffsetAndDimensions.w) + UVOffsetAndDimensions.y; + output.TexCoord = texCoord; + return output; } diff --git a/Renderer.cs b/Renderer.cs index 3da20bb..2b4f8ae 100644 --- a/Renderer.cs +++ b/Renderer.cs @@ -171,6 +171,8 @@ namespace Kav DiffuseLitSpriteEffect.NormalMapEnabled = data.MeshSprite.Normal != null; DiffuseLitSpriteEffect.World = matrix; + DiffuseLitSpriteEffect.UVOffset = data.UVOffset.Offset; + DiffuseLitSpriteEffect.SubTextureDimensions = data.UVOffset.Percentage; GraphicsDevice.Textures[0] = data.MeshSprite.Texture; GraphicsDevice.Textures[1] = data.MeshSprite.Normal; @@ -232,7 +234,7 @@ namespace Kav BoundingFrustum boundingFrustum, IEnumerable<(T, Matrix)> drawableTransformPairs, 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)) { @@ -315,7 +317,7 @@ namespace Kav RenderTarget2D renderTarget, PerspectiveCamera camera, IEnumerable<(T, Matrix)> drawableTransforms - ) where T : ICullable, IIndexDrawable + ) where T : ICullable, IIndexDrawable { GraphicsDevice.SetRenderTarget(renderTarget); GraphicsDevice.DepthStencilState = DepthStencilState.Default; @@ -350,7 +352,7 @@ namespace Kav RenderIndexed( GraphicsDevice, - UnitCube.Meshes[0].MeshParts[0], + UnitCube.Meshes[0].MeshParts[0], SkyboxEffect ); @@ -498,7 +500,7 @@ namespace Kav DeferredPointLightEffect.Projection = camera.Projection; RenderIndexed( - GraphicsDevice, + GraphicsDevice, UnitSphere.Meshes[0].MeshParts[0], DeferredPointLightEffect ); @@ -688,8 +690,8 @@ namespace Kav for (var i = 0; i < shadowMapData.NumShadowCascades; i++) { RenderDirectionalShadowMapIndexed( - shadowMapData, - i, + shadowMapData, + i, drawableTransforms ); } @@ -708,9 +710,9 @@ namespace Kav SimpleDepthEffect.Projection = shadowMapData.LightSpaceProjections[shadowCascadeIndex]; CullAndRenderIndexed( - GraphicsDevice, - new BoundingFrustum(SimpleDepthEffect.View * SimpleDepthEffect.Projection), - drawableTransforms, + GraphicsDevice, + new BoundingFrustum(SimpleDepthEffect.View * SimpleDepthEffect.Projection), + drawableTransforms, SimpleDepthEffect ); } @@ -751,7 +753,7 @@ namespace Kav SimpleDepthEffectInstanced.Projection = shadowMapData.LightSpaceProjections[shadowCascadeIndex]; RenderInstanced( - GraphicsDevice, + GraphicsDevice, drawable, instanceVertexBuffer, numInstances, @@ -828,8 +830,8 @@ namespace Kav CullAndRenderIndexed( GraphicsDevice, - new BoundingFrustum(LinearDepthEffect.View * LinearDepthEffect.Projection), - modelTransforms, + new BoundingFrustum(LinearDepthEffect.View * LinearDepthEffect.Projection), + modelTransforms, LinearDepthEffect ); } @@ -841,7 +843,7 @@ namespace Kav VertexBuffer instanceVertexBuffer, int numInstances, PointLight pointLight - ) where T : ICullable, IIndexDrawable + ) where T : ICullable, IIndexDrawable { GraphicsDevice.DepthStencilState = DepthStencilState.Default; GraphicsDevice.BlendState = BlendState.Opaque;