X

HLSL Edge Detection Effect

  • October 7, 2013
  • By admin
  • Comments Off on HLSL Edge Detection Effect
  • in

Direct X High Level Shading Language

 

Edge Detection

*Use sobel filter for edge detect

 

 

 

 

1)Edge detection Pass

Vertax Shader

 

struct VS_INPUT

{

float4 mPosition : POSITION;

float2 mUV : TEXCOORD0;

};

 

struct VS_OUTPUT

{

float4 mPosition : POSITION;

float2 mUV : TEXCOORD0;

};

 

VS_OUTPUT vs_main ( VS_INPUT Input )

{

VS_OUTPUT Output;

 

Output.mPosition = Input.mPosition;

Output.mUV = Input.mUV;

 

return Output;

}

 

 

Pixel Shader

 

struct PS_INPUT

{

float2 mUV : TEXCOORD0;

};

 

sampler2D SceneSampler;

 

float3x3 Kx = {

-1, 0, 1,

-2, 0, 2,

-1, 0, 1};

 

float3x3 Ky = {

1, 2, 1,

0, 0, 0,

-1,-2,-1};

 

float2 gPixelOffset;

 

float4 ps_main (PS_INPUT Input) : COLOR

{

float Lx = 0;

float Ly = 0;

 

for (int y = -1; y <= 1; ++y)

{

for (int x = -1; x<= 1; ++x)

{

float2 offset = float2(x,y)*gPixelOffset;

float3 tex = tex2D(SceneSampler, Input.mUV + offset).rgb;

float luminance = dot(tex, float3(0.3, 0.59, 0.11));

 

Lx += luminance * Kx[y+1][x+1];

Ly += luminance * Ky[y+1][x+1];

}

}

 

float Line = sqrt((Lx*Lx) + (Ly*Ly));

 

return float4(Line.xxx, 1);

}

 

 

 

 

 

2) EmbossPass

Vertax Shader

 

struct VS_INPUT

{

float4 mPosition : POSITION;

float2 mUV : TEXCOORD0;

};

 

struct VS_OUTPUT

{

float4 mPosition : POSITION;

float2 mUV : TEXCOORD0;

};

 

VS_OUTPUT vs_main ( VS_INPUT Input )

{

VS_OUTPUT Output;

 

Output.mPosition = Input.mPosition;

Output.mUV = Input.mUV;

 

return Output;

}

 

 

Pixel Shader

struct PS_INPUT

{

float2 mUV : TEXCOORD0;

};

 

sampler2D SceneSampler;

 

float3x3 K = {

-2,-1, 0,

-1, 0, 1,

0, 1, 2};

 

float2 gPixelOffset;

 

float4 ps_main (PS_INPUT Input) : COLOR

{

float res = 0;

 

for (int y=-1; y<=1; ++y)

{

for(int x=-1; x<=1; ++x)

{

float2 offset = float2(x,y) * gPixelOffset;

float3 tex = tex2D(SceneSampler, Input.mUV+offset).rgb;

float luminance = dot(tex, float3(0.3, 0.59, 0.11));

 

res += luminance * K[y+1][x+1];

}

}

 

res += 0.5;

 

return float4(res.xxx, 1);

}

 

 

 

 

Contact Me

Using the contact form to send me email at below

Keep in touch with me

You can use the following information to contact me if anything need to communicate.

Name: Luke Lee (yongmin)