Skip to content

Instantly share code, notes, and snippets.

@TinkerWorX
Created April 21, 2015 21:05
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 TinkerWorX/156565c7767c604ed37c to your computer and use it in GitHub Desktop.
Save TinkerWorX/156565c7767c604ed37c to your computer and use it in GitHub Desktop.
float4x4 MatrixTransform
: register(vs, c0)
: register(ps, c0);
texture2D Texture;
sampler TextureSampler
: register(s0) = sampler_state { Texture = (Texture); };
const float invSqrtTwo = 0.70710678118654752440084436210485;
float2 XUnit;
float2 YUnit;
float FadeSpeed;
float cmax(float4 color){
return max(max(color.r, color.g), color.b);
}
void AnalysisVS(
inout float4 blend : COLOR0,
inout float2 texCoord : TEXCOORD0,
inout float4 position : SV_Position)
{
position = mul(position, MatrixTransform);
}
float4 AnalysisPS(
in float4 blend : COLOR0,
in float2 texCoord : TEXCOORD0) : SV_Target0
{
float4 tl = tex2D(TextureSampler, texCoord - YUnit - XUnit); // top left
float4 tc = tex2D(TextureSampler, texCoord - YUnit); // top center
float4 tr = tex2D(TextureSampler, texCoord - YUnit + XUnit); // top right
float4 ml = tex2D(TextureSampler, texCoord - XUnit); // middle left
float4 mc = tex2D(TextureSampler, texCoord); // middle center
float4 mr = tex2D(TextureSampler, texCoord + XUnit); // middle right
float4 bl = tex2D(TextureSampler, texCoord + YUnit - XUnit); // bottom left
float4 bc = tex2D(TextureSampler, texCoord + YUnit); // bottom center
float4 br = tex2D(TextureSampler, texCoord + YUnit + XUnit); // bottom right
float x = (cmax(tl) * invSqrtTwo + cmax(ml) + cmax(bl) * invSqrtTwo) - (cmax(tr) * invSqrtTwo + cmax(mr) + cmax(br) * invSqrtTwo);
float y = (cmax(tl) * invSqrtTwo + cmax(tc) + cmax(tr) * invSqrtTwo) - (cmax(bl) * invSqrtTwo + cmax(bc) + cmax(br) * invSqrtTwo);
float p = cmax(mc);
// the destination of the pixel we're looking at
float2 dest = texCoord;// float2(texCoord.x * XUnit.x, texCoord.y * YUnit.y);
// the offset from the destination back to the source
float2 vect = float2(x, y);
normalize(vect);
vect.x *= -(1.00 / ((0.001 * FadeSpeed))) * (1 - p);
vect.y *= -(1.00 / ((0.001 * FadeSpeed))) * (1 - p);
// the height is just a magic value that is approximated in a linear way
float height = p / invSqrtTwo;
// the source is the approximated light source fo the destination, made from the vector, destination and height
float3 src = float3(dest.x + vect.x, dest.y + vect.y, height);
return float4(src.x * p, src.y * p, src.z * p, 1.00);
}
technique Analysis
{
pass
{
VertexShader = compile vs_3_0 AnalysisVS();
PixelShader = compile ps_3_0 AnalysisPS();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment