Advertisement Jump to content
Sign in to follow this  

Inner shadow pixel shader

This topic is 1568 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Im having difficulties getting this inner shadow effect to work correctly. Basically I am check the neighbour pixel's (currentPixel + an adjustable offset) alpha component, if it is 0 then I set the current pixels to the shadow color. 

I want to control the shadow falloff, and I was thinking of lerping based on the distance to the centre of the texture. But now that I think about it, this will just give me a vignette effect. So really I need to lerp based on the distance from the edge of the the texture (nearest pixel whose alpha == 0 I guess). 

Here is what I have so far:

static const float3 off = {-Radius,0.0,Radius};
static const float2 nTex = {dim[0], dim[1]}; //texture width and height
static const float2 center = {nTex.x/2.0, nTex.y/2.0}; //center of texture
static const float hype = sqrt(pow(nTex.x,2) + pow(nTex.y,2)); //hypotenuse of texture 

float4 main(float2 uv : TEXCOORD) : COLOR 
  //float dist = distance(uv, center) / (hype/2.0);
  float4 color = tex2D( image , uv.xy);

  float2 offxy = {off.x/nTex.x , off.y/nTex.y};
  float2 offzy = {off.z/nTex.x , off.y/nTex.y};
  float2 offyx = {off.y/nTex.x , off.x/nTex.y};
  float2 offyz = {off.y/nTex.x , off.z/nTex.y};
  float s11 = color.a;
  float s01 = tex2D(image, uv.xy + offxy).a;
  float s21 = tex2D(image, uv.xy + offzy).a;
  float s10 = tex2D(image, uv.xy + offyx).a;
  float s12 = tex2D(image, uv.xy + offyz).a;

  if(s01 == 0 || s21 == 0 || s10 == 0 || s12 == 0)
    //color.rgb = lerp(float3(0,0,0), color.rgb, dist);
      color.rgb = float3(0,0,0);

  return color; 


Share this post

Link to post
Share on other sites

Blurr the alpha channel(you can control it), and save original alpha. In next step multiply color by blurred value(you get shadow), and use original alpha to get original shape. And you can do it in single shader.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!