Skip to content

Instantly share code, notes, and snippets.

@equinox2k
Created February 4, 2019 02:32
Show Gist options
  • Select an option

  • Save equinox2k/e99bd03a557187093f6e84943d64067d to your computer and use it in GitHub Desktop.

Select an option

Save equinox2k/e99bd03a557187093f6e84943d64067d to your computer and use it in GitHub Desktop.
Texture2D ShaderTexture : register(t0);
SamplerState Sampler : register(s0);
cbuffer PerObject: register(b0)
{
float4x4 ViewMatrix;
float2 TextureSize;
float Bias;
float InvertR;
float InvertG;
float Unused1;
float Unused2;
float Unused3;
};
struct VertexShaderInput
{
float4 Position : SV_Position;
float2 TextureUV : TEXCOORD0;
};
struct VertexShaderOutput
{
float4 Position : SV_Position;
float2 TextureUV : TEXCOORD0;
};
VertexShaderOutput VSMain(VertexShaderInput input)
{
VertexShaderOutput output = (VertexShaderOutput)0;
output.Position = mul(input.Position, ViewMatrix);
output.TextureUV = input.TextureUV;
return output;
}
float4 PSMain(VertexShaderOutput input) : SV_Target
{
float2 step = float2(1.0f, 1.0f) / TextureSize;
float2 tlv = float2(input.TextureUV.x - step.x, input.TextureUV.y + step.y);
float2 lv = float2(input.TextureUV.x - step.x, input.TextureUV.y);
float2 blv = float2(input.TextureUV.x - step.x, input.TextureUV.y - step.y);
float2 tv = float2(input.TextureUV.x, input.TextureUV.y + step.y);
float2 bv = float2(input.TextureUV.x, input.TextureUV.y - step.y);
float2 trv = float2(input.TextureUV.x + step.x, input.TextureUV.y + step.y);
float2 rv = float2(input.TextureUV.x + step.x, input.TextureUV.y);
float2 brv = float2(input.TextureUV.x + step.x, input.TextureUV.y - step.y);
tlv = float2(tlv.x >= 0.0 ? tlv.x : (1.0 + tlv.x), tlv.y >= 0.0 ? tlv.y : (1.0 + tlv.y));
tlv = float2(tlv.x < 1.0 ? tlv.x : (tlv.x - 1.0), tlv.y < 1.0 ? tlv.y : (tlv.y - 1.0));
lv = float2(lv.x >= 0.0 ? lv.x : (1.0 + lv.x), lv.y >= 0.0 ? lv.y : (1.0 + lv.y));
lv = float2(lv.x < 1.0 ? lv.x : (lv.x - 1.0), lv.y < 1.0 ? lv.y : (lv.y - 1.0));
blv = float2(blv.x >= 0.0 ? blv.x : (1.0 + blv.x), blv.y >= 0.0 ? blv.y : (1.0 + blv.y));
blv = float2(blv.x < 1.0 ? blv.x : (blv.x - 1.0), blv.y < 1.0 ? blv.y : (blv.y - 1.0));
tv = float2(tv.x >= 0.0 ? tv.x : (1.0 + tv.x), tv.y >= 0.0 ? tv.y : (1.0 + tv.y));
tv = float2(tv.x < 1.0 ? tv.x : (tv.x - 1.0), tv.y < 1.0 ? tv.y : (tv.y - 1.0));
bv = float2(bv.x >= 0.0 ? bv.x : (1.0 + bv.x), bv.y >= 0.0 ? bv.y : (1.0 + bv.y));
bv = float2(bv.x < 1.0 ? bv.x : (bv.x - 1.0), bv.y < 1.0 ? bv.y : (bv.y - 1.0));
trv = float2(trv.x >= 0.0 ? trv.x : (1.0 + trv.x), trv.y >= 0.0 ? trv.y : (1.0 + trv.y));
trv = float2(trv.x < 1.0 ? trv.x : (trv.x - 1.0), trv.y < 1.0 ? trv.y : (trv.y - 1.0));
rv = float2(rv.x >= 0.0 ? rv.x : (1.0 + rv.x), rv.y >= 0.0 ? rv.y : (1.0 + rv.y));
rv = float2(rv.x < 1.0 ? rv.x : (rv.x - 1.0), rv.y < 1.0 ? rv.y : (rv.y - 1.0));
brv = float2(brv.x >= 0.0 ? brv.x : (1.0 + brv.x), brv.y >= 0.0 ? brv.y : (1.0 + brv.y));
brv = float2(brv.x < 1.0 ? brv.x : (brv.x - 1.0), brv.y < 1.0 ? brv.y : (brv.y - 1.0));
float tl = abs(ShaderTexture.Sample(Sampler, tlv).r);
float l = abs(ShaderTexture.Sample(Sampler, lv).r);
float bl = abs(ShaderTexture.Sample(Sampler, blv).r);
float t = abs(ShaderTexture.Sample(Sampler, tv).r);
float b = abs(ShaderTexture.Sample(Sampler, bv).r);
float tr = abs(ShaderTexture.Sample(Sampler, trv).r);
float r = abs(ShaderTexture.Sample(Sampler, rv).r);
float br = abs(ShaderTexture.Sample(Sampler, brv).r);
//Sobel
float dx = tl + l * 2.0 + bl - tr - r * 2.0 - br;
float dy = tl + t * 2.0 + tr - bl - b * 2.0 - br;
// Scharr
//float dx = tl * 3.0 + l * 10.0 + bl * 3.0 - tr * 3.0 - r * 10.0 - br * 3.0;
//float dy = tl * 3.0 + t * 10.0 + tr * 3.0 - bl * 3.0 - b * 10.0 - br * 3.0;
float4 normal = float4(normalize(float3(dx * InvertR, dy * InvertG, 1.0f - (Bias / 100.0f))), 1.0f);
return float4(normal.xy * 0.5f + 0.5f, normal.zw);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment