Created
November 23, 2018 04:07
-
-
Save renaudbedard/f14c7e8bdc924c011ecaf24ccbfeb224 to your computer and use it in GitHub Desktop.
Response to https://gamedev.stackexchange.com/questions/165514/texture-atlas-and-hlsl-shader-for-sprite-tiling
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
float4x4 WorldViewProj : WorldViewProjection; | |
texture ColorTexture; | |
sampler2D ColorSampler = sampler_state { | |
Texture = <ColorTexture>; | |
FILTER = MIN_MAG_MIP_LINEAR; | |
AddressU = Wrap; | |
AddressV = Wrap; | |
}; | |
float2 CellCount; | |
float2 CellIndex; | |
float2 TCRepeat; | |
struct VertexInput | |
{ | |
float4 Position : POSITION0; | |
float2 TextureCoordinates : TEXCOORD0; | |
}; | |
struct VertexOutput { | |
float4 Position : POSITION0; | |
float2 TextureCoordinates : TEXCOORD0; | |
}; | |
VertexOutput mainVS(VertexInput input) | |
{ | |
VertexOutput output; | |
float2 cellSize = 1.0f / CellCount; | |
float2 cellOffset = cellSize * CellIndex; | |
output.TextureCoordinates = (input.TextureCoordinates * cellSize + cellOffset) * TCRepeat; | |
output.Position = mul(input.Position, WorldViewProj); | |
return output; | |
} | |
float4 mainPS(VertexOutput input) : COLOR0 | |
{ | |
float2 cellSize = 1.0f / CellCount; | |
float2 cellOffset = cellSize * CellIndex; | |
float2 tc = input.TextureCoordinates; | |
tc = frac((tc - cellOffset) / cellSize) * cellSize + cellOffset; | |
float4 sample = tex2D(ColorSampler, tc); | |
return sample; | |
} | |
technique technique0 { | |
pass p0 { | |
CullMode = None; | |
VertexShader = compile vs_3_0 mainVS(); | |
PixelShader = compile ps_3_0 mainPS(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment