Compare commits
12 Commits
mesh_sprit
...
main
Author | SHA1 | Date |
---|---|---|
cosmonaut | f5cdd0b566 | |
cosmonaut | 808d50ccc2 | |
cosmonaut | d537993530 | |
cosmonaut | 3338bf3b06 | |
cosmonaut | 16ecb5afa6 | |
cosmonaut | 51d7f3e772 | |
cosmonaut | 3a0aa8894d | |
cosmonaut | c468d6276d | |
cosmonaut | d476f254e4 | |
cosmonaut | 2ff5cb1ca5 | |
cosmonaut | 40ca0402d6 | |
cosmonaut | 0ebad486c5 |
|
@ -0,0 +1,14 @@
|
||||||
|
namespace Kav
|
||||||
|
{
|
||||||
|
public struct AtlasAnimation
|
||||||
|
{
|
||||||
|
public UVData[] Frames { get; }
|
||||||
|
public int Framerate { get; }
|
||||||
|
|
||||||
|
public AtlasAnimation(UVData[] frames, int framerate)
|
||||||
|
{
|
||||||
|
Frames = frames;
|
||||||
|
Framerate = framerate;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,63 @@
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using Microsoft.Xna.Framework.Graphics;
|
||||||
|
|
||||||
|
namespace Kav
|
||||||
|
{
|
||||||
|
public class InstanceData<T> where T : struct, IVertexType
|
||||||
|
{
|
||||||
|
public T[] InstanceDataArray { get; }
|
||||||
|
public DynamicVertexBuffer VertexBuffer { get; }
|
||||||
|
public int InstanceCount { get; private set; }
|
||||||
|
|
||||||
|
public InstanceData(GraphicsDevice graphicsDevice, int size)
|
||||||
|
{
|
||||||
|
InstanceDataArray = new T[size];
|
||||||
|
|
||||||
|
VertexBuffer = new DynamicVertexBuffer(
|
||||||
|
graphicsDevice,
|
||||||
|
typeof(T),
|
||||||
|
size,
|
||||||
|
BufferUsage.WriteOnly
|
||||||
|
);
|
||||||
|
|
||||||
|
InstanceCount = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void AddAndSetData(IEnumerable<T> data)
|
||||||
|
{
|
||||||
|
InstanceCount = 0;
|
||||||
|
foreach (var datum in data)
|
||||||
|
{
|
||||||
|
AddData(datum);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (InstanceCount == 0) { throw new System.Exception(); }
|
||||||
|
|
||||||
|
SetData();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void AddData(T datum)
|
||||||
|
{
|
||||||
|
InstanceDataArray[InstanceCount] = datum;
|
||||||
|
InstanceCount += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void SetData()
|
||||||
|
{
|
||||||
|
if (InstanceCount > 0)
|
||||||
|
{
|
||||||
|
VertexBuffer.SetData(
|
||||||
|
InstanceDataArray,
|
||||||
|
0,
|
||||||
|
InstanceCount,
|
||||||
|
SetDataOptions.NoOverwrite
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Clear()
|
||||||
|
{
|
||||||
|
InstanceCount = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,24 +1,35 @@
|
||||||
using Microsoft.Xna.Framework;
|
using Microsoft.Xna.Framework;
|
||||||
|
using Microsoft.Xna.Framework.Graphics;
|
||||||
|
|
||||||
namespace Kav.Data
|
namespace Kav.Data
|
||||||
{
|
{
|
||||||
public struct MeshSpriteDrawData
|
public struct MeshSpriteDrawData : IIndexDrawable, ICullable, ITransformable
|
||||||
{
|
{
|
||||||
public MeshSprite MeshSprite { get; }
|
public SpriteMesh MeshSprite { get; }
|
||||||
|
public Texture2D Texture { get; }
|
||||||
|
public Texture2D Normal { get; }
|
||||||
public SpriteBillboardConstraint BillboardConstraint { get; }
|
public SpriteBillboardConstraint BillboardConstraint { get; }
|
||||||
public Matrix TransformMatrix { get; }
|
public Matrix TransformMatrix { get; }
|
||||||
public UVOffsets UVOffsets { get; }
|
public UVData UVOffset { get; }
|
||||||
|
|
||||||
|
public IndexBuffer IndexBuffer => MeshSprite.IndexBuffer;
|
||||||
|
public VertexBuffer VertexBuffer => MeshSprite.VertexBuffer;
|
||||||
|
public BoundingBox BoundingBox => MeshSprite.BoundingBox;
|
||||||
|
|
||||||
public MeshSpriteDrawData(
|
public MeshSpriteDrawData(
|
||||||
MeshSprite meshSprite,
|
SpriteMesh meshSprite,
|
||||||
|
Texture2D texture,
|
||||||
|
Texture2D normal,
|
||||||
SpriteBillboardConstraint billboardConstraint,
|
SpriteBillboardConstraint billboardConstraint,
|
||||||
Matrix transformMatrix,
|
Matrix transformMatrix,
|
||||||
UVOffsets uVOffsets
|
UVData offset
|
||||||
) {
|
) {
|
||||||
MeshSprite = meshSprite;
|
MeshSprite = meshSprite;
|
||||||
|
Texture = texture;
|
||||||
|
Normal = normal;
|
||||||
BillboardConstraint = billboardConstraint;
|
BillboardConstraint = billboardConstraint;
|
||||||
TransformMatrix = transformMatrix;
|
TransformMatrix = transformMatrix;
|
||||||
UVOffsets = uVOffsets;
|
UVOffset = offset;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,96 @@
|
||||||
|
using Kav.Data;
|
||||||
|
using Microsoft.Xna.Framework;
|
||||||
|
using Microsoft.Xna.Framework.Graphics;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
|
|
||||||
|
namespace Kav
|
||||||
|
{
|
||||||
|
public struct TextureAtlasSlice
|
||||||
|
{
|
||||||
|
public int X { get; }
|
||||||
|
public int Y { get; }
|
||||||
|
public int W { get; }
|
||||||
|
public int H { get; }
|
||||||
|
|
||||||
|
public UVData UVData { get; }
|
||||||
|
|
||||||
|
// for use with tiling
|
||||||
|
public Vector2 TiledUVOffset { get; }
|
||||||
|
|
||||||
|
public TextureAtlasSlice(int x, int y, int w, int h, int totalW, int totalH)
|
||||||
|
{
|
||||||
|
X = x;
|
||||||
|
Y = y;
|
||||||
|
W = w;
|
||||||
|
H = h;
|
||||||
|
|
||||||
|
UVData = new UVData(new Vector2(x, y), new Vector2(w, h), new Vector2(totalW, totalH));
|
||||||
|
|
||||||
|
TiledUVOffset = new Vector2(x / (float)totalW, y / (float)totalH);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class TextureAtlas
|
||||||
|
{
|
||||||
|
public Texture2D Texture { get; }
|
||||||
|
protected List<string> Names { get; } = new List<string>();
|
||||||
|
protected Dictionary<string, TextureAtlasSlice> Slices { get; } = new Dictionary<string, TextureAtlasSlice>();
|
||||||
|
|
||||||
|
public TextureAtlas(GraphicsDevice graphicsDevice, FileInfo atlasMetadataFile)
|
||||||
|
{
|
||||||
|
var atlasData = CrunchAtlasReader.ReadTextureAtlas(atlasMetadataFile);
|
||||||
|
|
||||||
|
var textureData = atlasData.Textures[0];
|
||||||
|
|
||||||
|
using var stream = File.OpenRead(Path.Combine(atlasMetadataFile.DirectoryName, textureData.Name + ".png"));
|
||||||
|
Texture = Texture2D.FromStream(graphicsDevice, stream);
|
||||||
|
|
||||||
|
foreach (var slice in textureData.Images)
|
||||||
|
{
|
||||||
|
Names.Add(slice.N);
|
||||||
|
|
||||||
|
Slices.Add(
|
||||||
|
slice.N,
|
||||||
|
new TextureAtlasSlice(
|
||||||
|
slice.X,
|
||||||
|
slice.Y,
|
||||||
|
slice.W,
|
||||||
|
slice.H,
|
||||||
|
Texture.Width,
|
||||||
|
Texture.Height
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public TextureAtlasSlice Lookup(string name)
|
||||||
|
{
|
||||||
|
return Slices[name];
|
||||||
|
}
|
||||||
|
|
||||||
|
public string Name(int index)
|
||||||
|
{
|
||||||
|
return Names[index];
|
||||||
|
}
|
||||||
|
|
||||||
|
public int Count()
|
||||||
|
{
|
||||||
|
return Names.Count;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Assumes all subimages are the same size
|
||||||
|
public class TiledTextureAtlas : TextureAtlas
|
||||||
|
{
|
||||||
|
public int NumRows { get; }
|
||||||
|
public int NumColumns { get; }
|
||||||
|
|
||||||
|
public TiledTextureAtlas(GraphicsDevice graphicsDevice, FileInfo atlasMetadataFile) : base(graphicsDevice, atlasMetadataFile)
|
||||||
|
{
|
||||||
|
var subImageSlice = Slices[Names[0]];
|
||||||
|
NumRows = Texture.Height / subImageSlice.H;
|
||||||
|
NumColumns = Texture.Width / subImageSlice.W;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,19 @@
|
||||||
|
using Microsoft.Xna.Framework;
|
||||||
|
|
||||||
|
namespace Kav
|
||||||
|
{
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -16,6 +16,9 @@ namespace Kav
|
||||||
EffectParameter metallicParam;
|
EffectParameter metallicParam;
|
||||||
EffectParameter roughnessParam;
|
EffectParameter roughnessParam;
|
||||||
|
|
||||||
|
EffectParameter uvOffsetAndDimensionsParam;
|
||||||
|
EffectParameter isSpriteParam;
|
||||||
|
|
||||||
EffectParameter numTextureRowsParam;
|
EffectParameter numTextureRowsParam;
|
||||||
EffectParameter numTextureColumnsParam;
|
EffectParameter numTextureColumnsParam;
|
||||||
|
|
||||||
|
@ -30,6 +33,11 @@ namespace Kav
|
||||||
float metallic;
|
float metallic;
|
||||||
float roughness;
|
float roughness;
|
||||||
|
|
||||||
|
Vector2 uvOffset;
|
||||||
|
Vector2 subTextureDimensions;
|
||||||
|
|
||||||
|
bool isSprite = false;
|
||||||
|
|
||||||
int numTextureRows = 1;
|
int numTextureRows = 1;
|
||||||
int numTextureColumns = 1;
|
int numTextureColumns = 1;
|
||||||
|
|
||||||
|
@ -153,6 +161,36 @@ 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 bool IsSprite
|
||||||
|
{
|
||||||
|
get { return isSprite; }
|
||||||
|
set
|
||||||
|
{
|
||||||
|
isSprite = value;
|
||||||
|
isSpriteParam.SetValue(isSprite ? 1f : 0f);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public bool HardwareInstancingEnabled
|
public bool HardwareInstancingEnabled
|
||||||
{
|
{
|
||||||
get { return hardwareInstancingEnabled; }
|
get { return hardwareInstancingEnabled; }
|
||||||
|
@ -210,6 +248,16 @@ namespace Kav
|
||||||
dirtyFlags &= ~EffectDirtyFlags.ViewProj;
|
dirtyFlags &= ~EffectDirtyFlags.ViewProj;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ((dirtyFlags & EffectDirtyFlags.UVOrDimensions) != 0)
|
||||||
|
{
|
||||||
|
uvOffsetAndDimensionsParam.SetValue(new Vector4(
|
||||||
|
UVOffset.X, UVOffset.Y,
|
||||||
|
SubTextureDimensions.X, SubTextureDimensions.Y
|
||||||
|
));
|
||||||
|
|
||||||
|
dirtyFlags &= ~EffectDirtyFlags.UVOrDimensions;
|
||||||
|
}
|
||||||
|
|
||||||
if ((dirtyFlags & EffectDirtyFlags.VertexShaderIndex) != 0)
|
if ((dirtyFlags & EffectDirtyFlags.VertexShaderIndex) != 0)
|
||||||
{
|
{
|
||||||
int vertexShaderIndex = 0;
|
int vertexShaderIndex = 0;
|
||||||
|
@ -277,6 +325,9 @@ namespace Kav
|
||||||
numTextureRowsParam = Parameters["NumTextureRows"];
|
numTextureRowsParam = Parameters["NumTextureRows"];
|
||||||
numTextureColumnsParam = Parameters["NumTextureColumns"];
|
numTextureColumnsParam = Parameters["NumTextureColumns"];
|
||||||
|
|
||||||
|
uvOffsetAndDimensionsParam = Parameters["UVOffsetAndDimensions"];
|
||||||
|
isSpriteParam = Parameters["IsSprite"];
|
||||||
|
|
||||||
shaderIndexParam = Parameters["PixelShaderIndex"];
|
shaderIndexParam = Parameters["PixelShaderIndex"];
|
||||||
vertexShaderIndexParam = Parameters["VertexShaderIndex"];
|
vertexShaderIndexParam = Parameters["VertexShaderIndex"];
|
||||||
}
|
}
|
||||||
|
|
|
@ -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;
|
||||||
|
|
||||||
|
@ -10,6 +11,8 @@ namespace Kav
|
||||||
EffectParameter directionalLightDirectionParam;
|
EffectParameter directionalLightDirectionParam;
|
||||||
EffectParameter directionalLightColorParam;
|
EffectParameter directionalLightColorParam;
|
||||||
|
|
||||||
|
EffectParameter uvOffsetAndDimensionsParam;
|
||||||
|
|
||||||
EffectParameter worldParam;
|
EffectParameter worldParam;
|
||||||
EffectParameter worldViewProjectionParam;
|
EffectParameter worldViewProjectionParam;
|
||||||
EffectParameter worldInverseTransposeParam;
|
EffectParameter worldInverseTransposeParam;
|
||||||
|
@ -30,6 +33,9 @@ namespace Kav
|
||||||
Matrix view = Matrix.Identity;
|
Matrix view = Matrix.Identity;
|
||||||
Matrix projection = Matrix.Identity;
|
Matrix projection = Matrix.Identity;
|
||||||
|
|
||||||
|
Vector2 uvOffset;
|
||||||
|
Vector2 subTextureDimensions;
|
||||||
|
|
||||||
EffectDirtyFlags dirtyFlags = EffectDirtyFlags.All;
|
EffectDirtyFlags dirtyFlags = EffectDirtyFlags.All;
|
||||||
|
|
||||||
public bool NormalMapEnabled
|
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)
|
public DiffuseLitSpriteEffect(GraphicsDevice graphicsDevice) : base(graphicsDevice, Resources.DiffuseLitSpriteEffect)
|
||||||
{
|
{
|
||||||
CacheEffectParameters();
|
CacheEffectParameters();
|
||||||
|
@ -142,6 +168,16 @@ namespace Kav
|
||||||
dirtyFlags &= ~EffectDirtyFlags.WorldViewProj;
|
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)
|
if ((dirtyFlags & EffectDirtyFlags.PixelShaderIndex) != 0)
|
||||||
{
|
{
|
||||||
int shaderIndex = 0;
|
int shaderIndex = 0;
|
||||||
|
@ -166,6 +202,8 @@ namespace Kav
|
||||||
directionalLightDirectionParam = Parameters["DirectionalLightDirection"];
|
directionalLightDirectionParam = Parameters["DirectionalLightDirection"];
|
||||||
directionalLightColorParam = Parameters["DirectionalLightColor"];
|
directionalLightColorParam = Parameters["DirectionalLightColor"];
|
||||||
|
|
||||||
|
uvOffsetAndDimensionsParam = Parameters["UVOffsetAndDimensions"];
|
||||||
|
|
||||||
shaderIndexParam = Parameters["ShaderIndex"];
|
shaderIndexParam = Parameters["ShaderIndex"];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,6 +11,7 @@ namespace Kav
|
||||||
VertexShaderIndex = 8,
|
VertexShaderIndex = 8,
|
||||||
PixelShaderIndex = 16,
|
PixelShaderIndex = 16,
|
||||||
ViewProj = 32,
|
ViewProj = 32,
|
||||||
|
UVOrDimensions = 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.
BIN
Effects/FXB/DeferredPBR_PointLightEffect.fxb (Stored with Git LFS)
BIN
Effects/FXB/DeferredPBR_PointLightEffect.fxb (Stored with Git LFS)
Binary file not shown.
BIN
Effects/FXB/DiffuseLitSpriteEffect.fxb (Stored with Git LFS)
BIN
Effects/FXB/DiffuseLitSpriteEffect.fxb (Stored with Git LFS)
Binary file not shown.
BIN
Effects/FXB/PaletteCrushEffect.fxb (Stored with Git LFS)
BIN
Effects/FXB/PaletteCrushEffect.fxb (Stored with Git LFS)
Binary file not shown.
|
@ -4,6 +4,9 @@ DECLARE_TEXTURE(AlbedoTexture, 0);
|
||||||
DECLARE_TEXTURE(NormalTexture, 1);
|
DECLARE_TEXTURE(NormalTexture, 1);
|
||||||
DECLARE_TEXTURE(MetallicRoughnessTexture, 2);
|
DECLARE_TEXTURE(MetallicRoughnessTexture, 2);
|
||||||
|
|
||||||
|
float4 UVOffsetAndDimensions;
|
||||||
|
float IsSprite;
|
||||||
|
|
||||||
BEGIN_CONSTANTS
|
BEGIN_CONSTANTS
|
||||||
|
|
||||||
float3 AlbedoValue _ps(c0) _cb(c0);
|
float3 AlbedoValue _ps(c0) _cb(c0);
|
||||||
|
@ -51,7 +54,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);
|
||||||
|
@ -113,7 +120,7 @@ PixelOutput NonePS(PixelInput input)
|
||||||
PixelOutput output;
|
PixelOutput output;
|
||||||
|
|
||||||
output.gPosition = float4(input.PositionWorld, 1.0);
|
output.gPosition = float4(input.PositionWorld, 1.0);
|
||||||
output.gNormal = float4(normalize(input.NormalWorld), 1.0);
|
output.gNormal = float4(normalize(input.NormalWorld), IsSprite);
|
||||||
output.gAlbedo = float4(AlbedoValue, 1.0);
|
output.gAlbedo = float4(AlbedoValue, 1.0);
|
||||||
output.gMetallicRoughness = float4(MetallicValue, RoughnessValue, 0.0, 1.0);
|
output.gMetallicRoughness = float4(MetallicValue, RoughnessValue, 0.0, 1.0);
|
||||||
|
|
||||||
|
@ -125,10 +132,12 @@ PixelOutput AlbedoPS(PixelInput input)
|
||||||
PixelOutput output;
|
PixelOutput output;
|
||||||
|
|
||||||
output.gPosition = float4(input.PositionWorld, 1.0);
|
output.gPosition = float4(input.PositionWorld, 1.0);
|
||||||
output.gNormal = float4(normalize(input.NormalWorld), 1.0);
|
output.gNormal = float4(normalize(input.NormalWorld), IsSprite);
|
||||||
output.gAlbedo = SAMPLE_TEXTURE(AlbedoTexture, input.TexCoord);
|
output.gAlbedo = SAMPLE_TEXTURE(AlbedoTexture, input.TexCoord);
|
||||||
output.gMetallicRoughness = float4(MetallicValue, RoughnessValue, 0.0, 1.0);
|
output.gMetallicRoughness = float4(MetallicValue, RoughnessValue, 0.0, 1.0);
|
||||||
|
|
||||||
|
if (output.gAlbedo.a == 0.0) { discard; }
|
||||||
|
|
||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -137,7 +146,7 @@ PixelOutput MetallicRoughnessPS(PixelInput input)
|
||||||
PixelOutput output;
|
PixelOutput output;
|
||||||
|
|
||||||
output.gPosition = float4(input.PositionWorld, 1.0);
|
output.gPosition = float4(input.PositionWorld, 1.0);
|
||||||
output.gNormal = float4(normalize(input.NormalWorld), 1.0);
|
output.gNormal = float4(normalize(input.NormalWorld), IsSprite);
|
||||||
output.gAlbedo = float4(AlbedoValue, 1.0);
|
output.gAlbedo = float4(AlbedoValue, 1.0);
|
||||||
output.gMetallicRoughness = SAMPLE_TEXTURE(MetallicRoughnessTexture, input.TexCoord);
|
output.gMetallicRoughness = SAMPLE_TEXTURE(MetallicRoughnessTexture, input.TexCoord);
|
||||||
|
|
||||||
|
@ -149,7 +158,7 @@ PixelOutput NormalPS(PixelInput input)
|
||||||
PixelOutput output;
|
PixelOutput output;
|
||||||
|
|
||||||
output.gPosition = float4(input.PositionWorld, 1.0);
|
output.gPosition = float4(input.PositionWorld, 1.0);
|
||||||
output.gNormal = float4(GetNormalFromMap(input.PositionWorld, input.TexCoord, input.NormalWorld), 1.0);
|
output.gNormal = float4(GetNormalFromMap(input.PositionWorld, input.TexCoord, input.NormalWorld), IsSprite);
|
||||||
output.gAlbedo = float4(AlbedoValue, 1.0);
|
output.gAlbedo = float4(AlbedoValue, 1.0);
|
||||||
output.gMetallicRoughness = float4(MetallicValue, RoughnessValue, 0.0, 1.0);
|
output.gMetallicRoughness = float4(MetallicValue, RoughnessValue, 0.0, 1.0);
|
||||||
|
|
||||||
|
@ -161,10 +170,12 @@ PixelOutput AlbedoMetallicRoughnessPS(PixelInput input)
|
||||||
PixelOutput output;
|
PixelOutput output;
|
||||||
|
|
||||||
output.gPosition = float4(input.PositionWorld, 1.0);
|
output.gPosition = float4(input.PositionWorld, 1.0);
|
||||||
output.gNormal = float4(normalize(input.NormalWorld), 1.0);
|
output.gNormal = float4(normalize(input.NormalWorld), IsSprite);
|
||||||
output.gAlbedo = SAMPLE_TEXTURE(AlbedoTexture, input.TexCoord);
|
output.gAlbedo = SAMPLE_TEXTURE(AlbedoTexture, input.TexCoord);
|
||||||
output.gMetallicRoughness = SAMPLE_TEXTURE(MetallicRoughnessTexture, input.TexCoord);
|
output.gMetallicRoughness = SAMPLE_TEXTURE(MetallicRoughnessTexture, input.TexCoord);
|
||||||
|
|
||||||
|
if (output.gAlbedo.a == 0.0) { discard; }
|
||||||
|
|
||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -173,10 +184,12 @@ PixelOutput AlbedoNormalPS(PixelInput input)
|
||||||
PixelOutput output;
|
PixelOutput output;
|
||||||
|
|
||||||
output.gPosition = float4(input.PositionWorld, 1.0);
|
output.gPosition = float4(input.PositionWorld, 1.0);
|
||||||
output.gNormal = float4(GetNormalFromMap(input.PositionWorld, input.TexCoord, input.NormalWorld), 1.0);
|
output.gNormal = float4(GetNormalFromMap(input.PositionWorld, input.TexCoord, input.NormalWorld), IsSprite);
|
||||||
output.gAlbedo = SAMPLE_TEXTURE(AlbedoTexture, input.TexCoord);
|
output.gAlbedo = SAMPLE_TEXTURE(AlbedoTexture, input.TexCoord);
|
||||||
output.gMetallicRoughness = float4(MetallicValue, RoughnessValue, 0.0, 1.0);
|
output.gMetallicRoughness = float4(MetallicValue, RoughnessValue, 0.0, 1.0);
|
||||||
|
|
||||||
|
if (output.gAlbedo.a == 0.0) { discard; }
|
||||||
|
|
||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -185,7 +198,7 @@ PixelOutput MetallicRoughnessNormalPS(PixelInput input)
|
||||||
PixelOutput output;
|
PixelOutput output;
|
||||||
|
|
||||||
output.gPosition = float4(input.PositionWorld, 1.0);
|
output.gPosition = float4(input.PositionWorld, 1.0);
|
||||||
output.gNormal = float4(GetNormalFromMap(input.PositionWorld, input.TexCoord, input.NormalWorld), 1.0);
|
output.gNormal = float4(GetNormalFromMap(input.PositionWorld, input.TexCoord, input.NormalWorld), IsSprite);
|
||||||
output.gAlbedo = float4(AlbedoValue, 1.0);
|
output.gAlbedo = float4(AlbedoValue, 1.0);
|
||||||
output.gMetallicRoughness = SAMPLE_TEXTURE(MetallicRoughnessTexture, input.TexCoord);
|
output.gMetallicRoughness = SAMPLE_TEXTURE(MetallicRoughnessTexture, input.TexCoord);
|
||||||
|
|
||||||
|
@ -197,10 +210,12 @@ PixelOutput AlbedoMetallicRoughnessNormalMapPS(PixelInput input)
|
||||||
PixelOutput output;
|
PixelOutput output;
|
||||||
|
|
||||||
output.gPosition = float4(input.PositionWorld, 1.0);
|
output.gPosition = float4(input.PositionWorld, 1.0);
|
||||||
output.gNormal = float4(GetNormalFromMap(input.PositionWorld, input.TexCoord, input.NormalWorld), 1.0);
|
output.gNormal = float4(GetNormalFromMap(input.PositionWorld, input.TexCoord, input.NormalWorld), IsSprite);
|
||||||
output.gAlbedo = SAMPLE_TEXTURE(AlbedoTexture, input.TexCoord);
|
output.gAlbedo = SAMPLE_TEXTURE(AlbedoTexture, input.TexCoord);
|
||||||
output.gMetallicRoughness = SAMPLE_TEXTURE(MetallicRoughnessTexture, input.TexCoord);
|
output.gMetallicRoughness = SAMPLE_TEXTURE(MetallicRoughnessTexture, input.TexCoord);
|
||||||
|
|
||||||
|
if (output.gAlbedo.a == 0.0) { discard; }
|
||||||
|
|
||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -80,10 +80,24 @@ float4 main_ps(PixelInput input) : SV_TARGET0
|
||||||
float2 texCoord = 0.5f * float2(input.ScreenPosition.x,-input.ScreenPosition.y) + 0.5f;
|
float2 texCoord = 0.5f * float2(input.ScreenPosition.x,-input.ScreenPosition.y) + 0.5f;
|
||||||
|
|
||||||
float3 worldPosition = SAMPLE_TEXTURE(gPosition, texCoord).rgb;
|
float3 worldPosition = SAMPLE_TEXTURE(gPosition, texCoord).rgb;
|
||||||
float3 normal = SAMPLE_TEXTURE(gNormal, texCoord).xyz;
|
float4 normalSample = SAMPLE_TEXTURE(gNormal, texCoord);
|
||||||
|
float3 normal = normalSample.xyz;
|
||||||
|
float isSprite = normalSample.a;
|
||||||
float3 albedo = SAMPLE_TEXTURE(gAlbedo, texCoord).rgb;
|
float3 albedo = SAMPLE_TEXTURE(gAlbedo, texCoord).rgb;
|
||||||
float2 metallicRoughness = SAMPLE_TEXTURE(gMetallicRoughness, texCoord).rg;
|
float2 metallicRoughness = SAMPLE_TEXTURE(gMetallicRoughness, texCoord).rg;
|
||||||
|
|
||||||
|
if (isSprite == 1.0)
|
||||||
|
{
|
||||||
|
float3 lightDir = PointLightPosition - worldPosition;
|
||||||
|
float3 L = normalize(lightDir);
|
||||||
|
float distance = length(lightDir);
|
||||||
|
float attenuation = 1.0 / (distance * distance);
|
||||||
|
float3 radiance = PointLightColor * attenuation;
|
||||||
|
|
||||||
|
return float4(albedo * radiance * 0.1, 1.0);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
return ComputeColor(
|
return ComputeColor(
|
||||||
worldPosition,
|
worldPosition,
|
||||||
normal,
|
normal,
|
||||||
|
@ -91,6 +105,7 @@ float4 main_ps(PixelInput input) : SV_TARGET0
|
||||||
metallicRoughness.r,
|
metallicRoughness.r,
|
||||||
metallicRoughness.g
|
metallicRoughness.g
|
||||||
);
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Technique DeferredPBR_Point
|
Technique DeferredPBR_Point
|
||||||
|
|
|
@ -5,23 +5,19 @@
|
||||||
DECLARE_TEXTURE(Texture, 0);
|
DECLARE_TEXTURE(Texture, 0);
|
||||||
DECLARE_TEXTURE(Normal, 1);
|
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 DirectionalLightDirection;
|
||||||
float3 PointLightColors[8] _ps(c9) _cb(c9);
|
float3 DirectionalLightColor;
|
||||||
|
|
||||||
float3 DirectionalLightDirection _ps(c17) _cb(c17);
|
float4 UVOffsetAndDimensions;
|
||||||
float3 DirectionalLightColor _ps(c18) _cb(c18);
|
|
||||||
|
|
||||||
MATRIX_CONSTANTS
|
float4x4 WorldInverseTranspose;
|
||||||
|
float4x4 World;
|
||||||
float4x4 WorldInverseTranspose _ps(c19) _cb(c19);
|
float4x4 WorldViewProjection;
|
||||||
float4x4 World _vs(c0) _cb(c23);
|
|
||||||
float4x4 WorldViewProjection _vs(c4) _cb(c27);
|
|
||||||
|
|
||||||
END_CONSTANTS
|
|
||||||
|
|
||||||
struct VertexShaderInput
|
struct VertexShaderInput
|
||||||
{
|
{
|
||||||
|
@ -43,10 +39,14 @@ PixelShaderInput main_vs(VertexShaderInput input)
|
||||||
PixelShaderInput output;
|
PixelShaderInput output;
|
||||||
|
|
||||||
output.Position = mul(input.Position, WorldViewProjection);
|
output.Position = mul(input.Position, WorldViewProjection);
|
||||||
output.TexCoord = input.TexCoord;
|
|
||||||
output.NormalWS = normalize(mul(input.Normal, (float3x3)WorldInverseTranspose));
|
output.NormalWS = normalize(mul(input.Normal, (float3x3)WorldInverseTranspose));
|
||||||
output.PositionWS = mul(input.Position, World).xyz;
|
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;
|
return output;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -98,6 +98,9 @@ float4 LightColor(float3 worldPosition, float3 worldNormal)
|
||||||
float4 WithoutNormalMap(PixelShaderInput input) : COLOR0
|
float4 WithoutNormalMap(PixelShaderInput input) : COLOR0
|
||||||
{
|
{
|
||||||
float4 tex = SAMPLE_TEXTURE(Texture, input.TexCoord);
|
float4 tex = SAMPLE_TEXTURE(Texture, input.TexCoord);
|
||||||
|
|
||||||
|
if (tex.a == 0.0) { discard; }
|
||||||
|
|
||||||
float3 normalWS = normalize(input.NormalWS);
|
float3 normalWS = normalize(input.NormalWS);
|
||||||
|
|
||||||
return tex * LightColor(input.PositionWS, normalWS);
|
return tex * LightColor(input.PositionWS, normalWS);
|
||||||
|
@ -106,6 +109,9 @@ float4 WithoutNormalMap(PixelShaderInput input) : COLOR0
|
||||||
float4 WithNormalMap(PixelShaderInput input) : COLOR0
|
float4 WithNormalMap(PixelShaderInput input) : COLOR0
|
||||||
{
|
{
|
||||||
float4 tex = SAMPLE_TEXTURE(Texture, input.TexCoord);
|
float4 tex = SAMPLE_TEXTURE(Texture, input.TexCoord);
|
||||||
|
|
||||||
|
if (tex.a == 0.0) { discard; }
|
||||||
|
|
||||||
float3 normalWS = GetNormalFromMap(input.PositionWS, input.TexCoord, input.NormalWS);
|
float3 normalWS = GetNormalFromMap(input.PositionWS, input.TexCoord, input.NormalWS);
|
||||||
|
|
||||||
return tex * LightColor(input.PositionWS, normalWS);
|
return tex * LightColor(input.PositionWS, normalWS);
|
||||||
|
|
|
@ -1,7 +1,5 @@
|
||||||
#include "Macros.fxh"
|
#include "Macros.fxh"
|
||||||
|
|
||||||
#define FLT_MAX 3.402823466e+38
|
|
||||||
|
|
||||||
DECLARE_TEXTURE(Texture, 0);
|
DECLARE_TEXTURE(Texture, 0);
|
||||||
DECLARE_TEXTURE(Palette, 1);
|
DECLARE_TEXTURE(Palette, 1);
|
||||||
|
|
||||||
|
@ -40,11 +38,12 @@ float4 main_ps(PixelInput input) : SV_TARGET0
|
||||||
float3 sampled_color = sampled.rgb;
|
float3 sampled_color = sampled.rgb;
|
||||||
|
|
||||||
float3 closest_color = float3(0, 0, 0);
|
float3 closest_color = float3(0, 0, 0);
|
||||||
float closest_dist = FLT_MAX;
|
float closest_dist = 100000;
|
||||||
|
|
||||||
for (int i = 0; i < PaletteWidth; i++)
|
for (int i = 0; i < PaletteWidth; i++)
|
||||||
{
|
{
|
||||||
float3 palette_color = SAMPLE_TEXTURE(Palette, float2(i / (float)PaletteWidth, 0));
|
float texX = (i / (float)PaletteWidth);
|
||||||
|
float3 palette_color = SAMPLE_TEXTURE(Palette, float2(texX, 0));
|
||||||
float dist = distance(palette_color, sampled_color);
|
float dist = distance(palette_color, sampled_color);
|
||||||
if (dist < closest_dist)
|
if (dist < closest_dist)
|
||||||
{
|
{
|
||||||
|
|
|
@ -0,0 +1,9 @@
|
||||||
|
using Microsoft.Xna.Framework;
|
||||||
|
|
||||||
|
namespace Kav
|
||||||
|
{
|
||||||
|
public interface ITransformable
|
||||||
|
{
|
||||||
|
Matrix TransformMatrix { get; }
|
||||||
|
}
|
||||||
|
}
|
|
@ -4,7 +4,7 @@ using Microsoft.Xna.Framework.Graphics;
|
||||||
|
|
||||||
namespace Kav
|
namespace Kav
|
||||||
{
|
{
|
||||||
public class MeshSprite : ICullable, IIndexDrawable
|
public class SpriteMesh : ICullable, IIndexDrawable
|
||||||
{
|
{
|
||||||
public enum FlipOptions
|
public enum FlipOptions
|
||||||
{
|
{
|
||||||
|
@ -14,7 +14,6 @@ namespace Kav
|
||||||
Both
|
Both
|
||||||
}
|
}
|
||||||
|
|
||||||
private static readonly int PixelScale = 40;
|
|
||||||
private static readonly short[] Indices = new short[]
|
private static readonly short[] Indices = new short[]
|
||||||
{
|
{
|
||||||
0,
|
0,
|
||||||
|
@ -25,21 +24,16 @@ namespace Kav
|
||||||
2
|
2
|
||||||
};
|
};
|
||||||
|
|
||||||
public Texture2D Texture { get; }
|
|
||||||
public Texture2D Normal { get; }
|
|
||||||
|
|
||||||
public IndexBuffer IndexBuffer { get; }
|
public IndexBuffer IndexBuffer { get; }
|
||||||
public VertexBuffer VertexBuffer { get; }
|
public VertexBuffer VertexBuffer { get; }
|
||||||
public BoundingBox BoundingBox { get; }
|
public BoundingBox BoundingBox { get; }
|
||||||
|
|
||||||
public MeshSprite(
|
public SpriteMesh(
|
||||||
GraphicsDevice graphicsDevice,
|
GraphicsDevice graphicsDevice,
|
||||||
Texture2D texture,
|
int width,
|
||||||
|
int height,
|
||||||
FlipOptions flipOptions
|
FlipOptions flipOptions
|
||||||
) {
|
) {
|
||||||
Texture = texture;
|
|
||||||
Normal = null;
|
|
||||||
|
|
||||||
IndexBuffer = new IndexBuffer(
|
IndexBuffer = new IndexBuffer(
|
||||||
graphicsDevice,
|
graphicsDevice,
|
||||||
IndexElementSize.SixteenBits,
|
IndexElementSize.SixteenBits,
|
||||||
|
@ -48,7 +42,7 @@ namespace Kav
|
||||||
);
|
);
|
||||||
IndexBuffer.SetData(Indices);
|
IndexBuffer.SetData(Indices);
|
||||||
|
|
||||||
var vertexArray = GenerateVertexArray(Texture, flipOptions);
|
var vertexArray = GenerateVertexArray(width, height, flipOptions);
|
||||||
|
|
||||||
VertexBuffer = new VertexBuffer(
|
VertexBuffer = new VertexBuffer(
|
||||||
graphicsDevice,
|
graphicsDevice,
|
||||||
|
@ -61,38 +55,11 @@ namespace Kav
|
||||||
BoundingBox = BoundingBox.CreateFromPoints(Positions(vertexArray));
|
BoundingBox = BoundingBox.CreateFromPoints(Positions(vertexArray));
|
||||||
}
|
}
|
||||||
|
|
||||||
public MeshSprite(
|
private static VertexPositionNormalTexture[] GenerateVertexArray(int pixelWidth, int pixelHeight, FlipOptions flipOptions)
|
||||||
GraphicsDevice graphicsDevice,
|
|
||||||
Texture2D texture,
|
|
||||||
Texture2D normal,
|
|
||||||
FlipOptions flipOptions
|
|
||||||
) {
|
|
||||||
Texture = texture;
|
|
||||||
Normal = normal;
|
|
||||||
|
|
||||||
IndexBuffer = new IndexBuffer(
|
|
||||||
graphicsDevice,
|
|
||||||
IndexElementSize.SixteenBits,
|
|
||||||
6,
|
|
||||||
BufferUsage.WriteOnly
|
|
||||||
);
|
|
||||||
IndexBuffer.SetData(Indices);
|
|
||||||
|
|
||||||
var vertexArray = GenerateVertexArray(Texture, flipOptions);
|
|
||||||
|
|
||||||
VertexBuffer = new VertexBuffer(
|
|
||||||
graphicsDevice,
|
|
||||||
typeof(VertexPositionNormalTexture),
|
|
||||||
4,
|
|
||||||
BufferUsage.WriteOnly
|
|
||||||
);
|
|
||||||
VertexBuffer.SetData(vertexArray);
|
|
||||||
|
|
||||||
BoundingBox = BoundingBox.CreateFromPoints(Positions(vertexArray));
|
|
||||||
}
|
|
||||||
|
|
||||||
private static VertexPositionNormalTexture[] GenerateVertexArray(Texture2D texture, FlipOptions flipOptions)
|
|
||||||
{
|
{
|
||||||
|
var width = pixelWidth / (float)40;
|
||||||
|
var height = pixelHeight / (float)40;
|
||||||
|
|
||||||
VertexPositionNormalTexture[] result = new VertexPositionNormalTexture[4];
|
VertexPositionNormalTexture[] result = new VertexPositionNormalTexture[4];
|
||||||
|
|
||||||
var xLeft = 0;
|
var xLeft = 0;
|
||||||
|
@ -112,20 +79,20 @@ namespace Kav
|
||||||
yBottom = 0;
|
yBottom = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
result[0].Position = new Vector3(-texture.Width / 2, texture.Height / 2, 0) / PixelScale;
|
result[0].Position = new Vector3(-width / 2, height / 2, 0);
|
||||||
result[0].Normal = new Vector3(0, 0, 1);
|
result[0].Normal = new Vector3(0, 0, -1);
|
||||||
result[0].TextureCoordinate = new Vector2(xLeft, yTop);
|
result[0].TextureCoordinate = new Vector2(xLeft, yTop);
|
||||||
|
|
||||||
result[1].Position = new Vector3(texture.Width / 2, texture.Height / 2, 0) / PixelScale;
|
result[1].Position = new Vector3(width / 2, height / 2, 0);
|
||||||
result[1].Normal = new Vector3(0, 0, 1);
|
result[1].Normal = new Vector3(0, 0, -1);
|
||||||
result[1].TextureCoordinate = new Vector2(xRight, yTop);
|
result[1].TextureCoordinate = new Vector2(xRight, yTop);
|
||||||
|
|
||||||
result[2].Position = new Vector3(-texture.Width / 2, -texture.Height / 2, 0) / PixelScale;
|
result[2].Position = new Vector3(-width / 2, -height / 2, 0);
|
||||||
result[2].Normal = new Vector3(0, 0, 1);
|
result[2].Normal = new Vector3(0, 0, -1);
|
||||||
result[2].TextureCoordinate = new Vector2(xLeft, yBottom);
|
result[2].TextureCoordinate = new Vector2(xLeft, yBottom);
|
||||||
|
|
||||||
result[3].Position = new Vector3(texture.Width / 2, -texture.Height / 2, 0) / PixelScale;
|
result[3].Position = new Vector3(width / 2, -height / 2, 0);
|
||||||
result[3].Normal = new Vector3(0, 0, 1);
|
result[3].Normal = new Vector3(0, 0, -1);
|
||||||
result[3].TextureCoordinate = new Vector2(xRight, yBottom);
|
result[3].TextureCoordinate = new Vector2(xRight, yBottom);
|
||||||
|
|
||||||
return result;
|
return result;
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFramework>netstandard2.0</TargetFramework>
|
<TargetFramework>netstandard2.0</TargetFramework>
|
||||||
|
<LangVersion>8.0</LangVersion>
|
||||||
<RootNamespace>Kav</RootNamespace>
|
<RootNamespace>Kav</RootNamespace>
|
||||||
<Authors>Evan Hemsley</Authors>
|
<Authors>Evan Hemsley</Authors>
|
||||||
<Copyright>Evan Hemsley 2020</Copyright>
|
<Copyright>Evan Hemsley 2020</Copyright>
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFramework>netstandard2.0</TargetFramework>
|
<TargetFramework>netstandard2.0</TargetFramework>
|
||||||
|
<LangVersion>8.0</LangVersion>
|
||||||
<RootNamespace>Kav</RootNamespace>
|
<RootNamespace>Kav</RootNamespace>
|
||||||
<Authors>Evan Hemsley</Authors>
|
<Authors>Evan Hemsley</Authors>
|
||||||
<Copyright>Evan Hemsley 2020</Copyright>
|
<Copyright>Evan Hemsley 2020</Copyright>
|
||||||
|
|
|
@ -0,0 +1,18 @@
|
||||||
|
using System.IO;
|
||||||
|
using System.Text.Json;
|
||||||
|
|
||||||
|
namespace Kav
|
||||||
|
{
|
||||||
|
public static class CrunchAtlasReader
|
||||||
|
{
|
||||||
|
static JsonSerializerOptions options = new JsonSerializerOptions
|
||||||
|
{
|
||||||
|
PropertyNameCaseInsensitive = true
|
||||||
|
};
|
||||||
|
|
||||||
|
public static CrunchTextureAtlasData ReadTextureAtlas(FileInfo file)
|
||||||
|
{
|
||||||
|
return JsonSerializer.Deserialize<CrunchTextureAtlasData>(File.ReadAllText(file.FullName), options);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,22 @@
|
||||||
|
namespace Kav
|
||||||
|
{
|
||||||
|
public struct CrunchTextureAtlasData
|
||||||
|
{
|
||||||
|
public CrunchTextureAtlasTextureData[] Textures { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public struct CrunchTextureAtlasTextureData
|
||||||
|
{
|
||||||
|
public string Name { get; set; }
|
||||||
|
public CrunchTextureAtlasImageData[] Images { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public struct CrunchTextureAtlasImageData
|
||||||
|
{
|
||||||
|
public string N { get; set; }
|
||||||
|
public int X { get; set; }
|
||||||
|
public int Y { get; set; }
|
||||||
|
public int W { get; set; }
|
||||||
|
public int H { get; set; }
|
||||||
|
}
|
||||||
|
}
|
87
Renderer.cs
87
Renderer.cs
|
@ -125,7 +125,7 @@ namespace Kav
|
||||||
) {
|
) {
|
||||||
GraphicsDevice.SetRenderTarget(renderTarget);
|
GraphicsDevice.SetRenderTarget(renderTarget);
|
||||||
|
|
||||||
GraphicsDevice.DepthStencilState = DepthStencilState.DepthRead;
|
GraphicsDevice.DepthStencilState = DepthStencilState.Default;
|
||||||
GraphicsDevice.RasterizerState = RasterizerState.CullNone;
|
GraphicsDevice.RasterizerState = RasterizerState.CullNone;
|
||||||
GraphicsDevice.SamplerStates[0] = SamplerState.PointClamp;
|
GraphicsDevice.SamplerStates[0] = SamplerState.PointClamp;
|
||||||
GraphicsDevice.SamplerStates[1] = SamplerState.PointClamp;
|
GraphicsDevice.SamplerStates[1] = SamplerState.PointClamp;
|
||||||
|
@ -168,12 +168,14 @@ namespace Kav
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
DiffuseLitSpriteEffect.NormalMapEnabled = data.MeshSprite.Normal != null;
|
DiffuseLitSpriteEffect.NormalMapEnabled = data.Normal != null;
|
||||||
|
|
||||||
DiffuseLitSpriteEffect.World = matrix;
|
DiffuseLitSpriteEffect.World = matrix;
|
||||||
|
DiffuseLitSpriteEffect.UVOffset = data.UVOffset.Offset;
|
||||||
|
DiffuseLitSpriteEffect.SubTextureDimensions = data.UVOffset.Percentage;
|
||||||
|
|
||||||
GraphicsDevice.Textures[0] = data.MeshSprite.Texture;
|
GraphicsDevice.Textures[0] = data.Texture;
|
||||||
GraphicsDevice.Textures[1] = data.MeshSprite.Normal;
|
GraphicsDevice.Textures[1] = data.Normal;
|
||||||
|
|
||||||
GraphicsDevice.SetVertexBuffer(data.MeshSprite.VertexBuffer);
|
GraphicsDevice.SetVertexBuffer(data.MeshSprite.VertexBuffer);
|
||||||
GraphicsDevice.Indices = data.MeshSprite.IndexBuffer;
|
GraphicsDevice.Indices = data.MeshSprite.IndexBuffer;
|
||||||
|
@ -194,6 +196,51 @@ namespace Kav
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void RenderMeshSpriteGBuffer(
|
||||||
|
RenderTargetBinding[] gBuffer,
|
||||||
|
PerspectiveCamera camera,
|
||||||
|
IEnumerable<MeshSpriteDrawData> meshSpriteDrawDatas,
|
||||||
|
DepthStencilState depthStencilState
|
||||||
|
) {
|
||||||
|
GraphicsDevice.SetRenderTargets(gBuffer);
|
||||||
|
GraphicsDevice.RasterizerState = RasterizerState.CullNone;
|
||||||
|
GraphicsDevice.DepthStencilState = depthStencilState;
|
||||||
|
GraphicsDevice.BlendState = BlendState.AlphaBlend;
|
||||||
|
|
||||||
|
Deferred_GBufferEffect.HardwareInstancingEnabled = false;
|
||||||
|
Deferred_GBufferEffect.IsSprite = true;
|
||||||
|
|
||||||
|
Deferred_GBufferEffect.View = camera.View;
|
||||||
|
Deferred_GBufferEffect.Projection = camera.Projection;
|
||||||
|
|
||||||
|
var boundingFrustum = new BoundingFrustum(camera.View * camera.Projection);
|
||||||
|
|
||||||
|
foreach (var data in meshSpriteDrawDatas)
|
||||||
|
{
|
||||||
|
var matrix = BillboardTransforms(camera, data.TransformMatrix, data.BillboardConstraint);
|
||||||
|
|
||||||
|
if (FrustumCull(boundingFrustum, data.MeshSprite, matrix))
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
Deferred_GBufferEffect.World = matrix;
|
||||||
|
|
||||||
|
Deferred_GBufferEffect.UVOffset = data.UVOffset.Offset;
|
||||||
|
Deferred_GBufferEffect.SubTextureDimensions = data.UVOffset.Percentage;
|
||||||
|
|
||||||
|
Deferred_GBufferEffect.Albedo = Color.White.ToVector3();
|
||||||
|
Deferred_GBufferEffect.Metallic = 0f;
|
||||||
|
Deferred_GBufferEffect.Roughness = 1f;
|
||||||
|
|
||||||
|
Deferred_GBufferEffect.AlbedoTexture = data.Texture;
|
||||||
|
Deferred_GBufferEffect.NormalTexture = data.Normal;
|
||||||
|
Deferred_GBufferEffect.MetallicRoughnessTexture = null;
|
||||||
|
|
||||||
|
RenderIndexed(GraphicsDevice, data, Deferred_GBufferEffect);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private static Matrix BillboardTransforms(
|
private static Matrix BillboardTransforms(
|
||||||
PerspectiveCamera camera,
|
PerspectiveCamera camera,
|
||||||
Matrix transform,
|
Matrix transform,
|
||||||
|
@ -331,6 +378,21 @@ namespace Kav
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void RenderDepthIndexed<T>(
|
||||||
|
RenderTarget2D renderTarget,
|
||||||
|
PerspectiveCamera camera,
|
||||||
|
T drawable,
|
||||||
|
DepthStencilState depthStencilState
|
||||||
|
) where T : IIndexDrawable {
|
||||||
|
GraphicsDevice.SetRenderTarget(renderTarget);
|
||||||
|
GraphicsDevice.DepthStencilState = depthStencilState;
|
||||||
|
|
||||||
|
SimpleDepthEffect.View = camera.View;
|
||||||
|
SimpleDepthEffect.Projection = camera.Projection;
|
||||||
|
|
||||||
|
RenderIndexed(GraphicsDevice, drawable, SimpleDepthEffect);
|
||||||
|
}
|
||||||
|
|
||||||
public void RenderSkybox(
|
public void RenderSkybox(
|
||||||
RenderTarget2D renderTarget,
|
RenderTarget2D renderTarget,
|
||||||
PerspectiveCamera camera,
|
PerspectiveCamera camera,
|
||||||
|
@ -374,6 +436,7 @@ namespace Kav
|
||||||
GraphicsDevice.BlendState = BlendState.Opaque;
|
GraphicsDevice.BlendState = BlendState.Opaque;
|
||||||
|
|
||||||
Deferred_GBufferEffect.HardwareInstancingEnabled = true;
|
Deferred_GBufferEffect.HardwareInstancingEnabled = true;
|
||||||
|
Deferred_GBufferEffect.IsSprite = false;
|
||||||
|
|
||||||
Deferred_GBufferEffect.Albedo = drawable.Albedo;
|
Deferred_GBufferEffect.Albedo = drawable.Albedo;
|
||||||
Deferred_GBufferEffect.Metallic = drawable.Metallic;
|
Deferred_GBufferEffect.Metallic = drawable.Metallic;
|
||||||
|
@ -421,6 +484,7 @@ namespace Kav
|
||||||
GraphicsDevice.DepthStencilState = DepthStencilState.Default;
|
GraphicsDevice.DepthStencilState = DepthStencilState.Default;
|
||||||
GraphicsDevice.BlendState = BlendState.Opaque;
|
GraphicsDevice.BlendState = BlendState.Opaque;
|
||||||
|
|
||||||
|
Deferred_GBufferEffect.IsSprite = false;
|
||||||
Deferred_GBufferEffect.HardwareInstancingEnabled = false;
|
Deferred_GBufferEffect.HardwareInstancingEnabled = false;
|
||||||
Deferred_GBufferEffect.View = camera.View;
|
Deferred_GBufferEffect.View = camera.View;
|
||||||
Deferred_GBufferEffect.Projection = camera.Projection;
|
Deferred_GBufferEffect.Projection = camera.Projection;
|
||||||
|
@ -940,6 +1004,21 @@ namespace Kav
|
||||||
return (containment == ContainmentType.Disjoint);
|
return (containment == ContainmentType.Disjoint);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static IEnumerable<T> FrustumCull<T>(
|
||||||
|
BoundingFrustum boundingFrustum,
|
||||||
|
IEnumerable<T> cullableTransformables
|
||||||
|
) where T : ICullable, ITransformable {
|
||||||
|
foreach (var cullableTransformable in cullableTransformables)
|
||||||
|
{
|
||||||
|
var boundingBox = TransformedBoundingBox(cullableTransformable.BoundingBox, cullableTransformable.TransformMatrix);
|
||||||
|
var containment = boundingFrustum.Contains(boundingBox);
|
||||||
|
if (containment != ContainmentType.Disjoint)
|
||||||
|
{
|
||||||
|
yield return cullableTransformable;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
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;
|
||||||
|
|
Loading…
Reference in New Issue