Skip to content

Instantly share code, notes, and snippets.

@Apostolique
Created October 5, 2018 20:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Apostolique/f6179a6d4fd0217835b6c17e9dd5d21a to your computer and use it in GitHub Desktop.
Save Apostolique/f6179a6d4fd0217835b6c17e9dd5d21a to your computer and use it in GitHub Desktop.
Tile Shader for Rashtal
sampler TextureSampler : register(s0);
float2 ViewportSize;
float2 TextureSize;
float4x4 ScrollMatrix;
Texture2D GrassTile;
Texture2D DirtTile;
Texture2D VulcanicTile;
SamplerState GrassSampler
{
Texture = ( GrassTile );
MagFilter = POINT;
MinFilter = POINT;
Mipfilter = POINT;
AddressU = WRAP;
AddressV = WRAP;
};
SamplerState DirtSampler
{
Texture = ( DirtTile );
MagFilter = POINT;
MinFilter = POINT;
Mipfilter = POINT;
AddressU = WRAP;
AddressV = WRAP;
};
SamplerState VulcanicSampler
{
Texture = ( VulcanicTile );
MagFilter = POINT;
MinFilter = POINT;
Mipfilter = POINT;
AddressU = WRAP;
AddressV = WRAP;
};
struct VertexToPixel {
float4 Position : SV_Position0;
float4 TexCoord : TEXCOORD0;
float4 MaskCoord : TEXCOORD1;
float4 Color : COLOR0;
};
VertexToPixel SpriteVertexShader(float4 color : COLOR0, float4 texCoord : TEXCOORD0, float4 position : POSITION0) {
VertexToPixel Output = (VertexToPixel)0;
Output.MaskCoord = texCoord;
texCoord = position;
texCoord.xy /= TextureSize;
position = mul(position, ScrollMatrix);
// Half pixel offset for correct texel centering. - This is solved by DX10 and half pixel offset would actually mess it up
position.xy -= 0.5;
// Viewport adjustment.
position.xy /= ViewportSize;
position.xy *= float2(2, -2);
position.xy -= float2(1, -1);
//pass position and color to PS
Output.Color = color;
Output.Position = position;
Output.TexCoord = texCoord;
return Output;
}
float4 SpritePixelShader(VertexToPixel PSIn): COLOR0 {
int index = int(PSIn.Color.x * 3);
float4 mask = tex2D(TextureSampler, PSIn.MaskCoord.xy);
float4 diffuse;
if (index == 0) {
diffuse = tex2D(VulcanicSampler, PSIn.TexCoord.xy);
} else if (index == 1) {
diffuse = tex2D(DirtSampler, PSIn.TexCoord.xy);
} else {
diffuse = tex2D(GrassSampler, PSIn.TexCoord.xy);
}
return diffuse * float4(mask.r, mask.r, mask.r, mask.r);
}
technique SpriteBatch {
pass {
VertexShader = compile vs_2_0 SpriteVertexShader();
PixelShader = compile ps_2_0 SpritePixelShader();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment