SSAO Question

Started by
2 comments, last by nullsquared 15 years, 9 months ago
Hello. I've tried to toon an edge detection shader into an SSAO shader using the shader example by JKLint. However the shader still turns into an edge detector and just gives everythingn an outline. Here is the shader...

texture depthTexture;
float4x4 matWorldViewProj;
float2 camerarange=float2(0.1f,800.f);

sampler RT = sampler_state
{
   Texture = < depthTexture>;
};

struct toonVSOUT
{
    float4 Pos         : POSITION;
    float2 Tex           :TEXCOORD0;
};

toonVSOUT toonVS(float4 Pos : POSITION,  float2 inTex : TEXCOORD)
{
    toonVSOUT Out;

   Pos.xy = sign(Pos.xy);
  
 Out.Pos  = float4(Pos.xy, 0.0, 1.0);


    Out.Tex.x = 0.5 *( 1 + Pos.x);
    Out.Tex.y = 0.5 *( 1 - Pos.y);

    return Out;

}

const float1 off = 1.0 /1024; //size of depth texture

struct PS_OUTPUT
{
   float4 Color    : COLOR0;
};

struct PS_INPUT
{
   float2 TexCoord : TEXCOORD0;
};
float readDepth(float2 coord)
{
    return (2.0 * camerarange.x) / (camerarange.y + camerarange.x - tex2D( RT, coord ).x * (camerarange.y - camerarange.x));    
}

float4 psToon2( PS_INPUT In): COLOR
{


float depth = readDepth( In.TexCoord );
float d;
float ao = 0.0;
float aoMultiplier=1000.0;
float depthTolerance = 0.001;

   // Sample neighbor pixels
     d=readDepth( In.TexCoord + float2(-off, -off)).r;
     ao+=max(0.0,d-depth-depthTolerance) * aoMultiplier;
   
     d=readDepth( In.TexCoord + float2( off*2,   -off)).r;
     ao+=max(0.0,d-depth-depthTolerance) * aoMultiplier;

    d=readDepth( In.TexCoord + float2( off, -off)).r;
    ao+=max(0.0,d-depth-depthTolerance) * aoMultiplier;

    d=readDepth( In.TexCoord + float2(-off,  off*2)).r;
    ao+=max(0.0,d-depth-depthTolerance) * aoMultiplier;

    d=readDepth( In.TexCoord + float2( off,  off*2)).r;
    ao+=max(0.0,d-depth-depthTolerance) * aoMultiplier;

    d=readDepth(  In.TexCoord + float2(-off,  off)).r;
    ao+=max(0.0,d-depth-depthTolerance) * aoMultiplier;

    ao/=6;
    ao=min(ao,0.5);

    return float4(1.0-ao,1.0-ao,1.0-ao,0);

}

technique toon2
{

    pass p0
    {
               cullmode = none;
                zenable = false;
//      alpharef = 16;
//       alphafunc = less;
//       alphatestenable=true;

               VertexShader = compile vs_1_1 toonVS();
                PixelShader  = compile ps_2_0 psToon2(); 
    }

}



Does anyone have an idea as to what I'm doing wrong? Thanks!
Advertisement
All that shader does is check some depth differences, which will obviously result in a type of edge detection. So, you're not doing anything wrong, it's just what the shader does. Check out this page for a nice base for SSAO.
Thanks I'll try to follow that example. It's the cg to hlsl that confuses me though. But thanks again.
Quote:Original post by Archie2112
Thanks I'll try to follow that example. It's the cg to hlsl that confuses me though. But thanks again.


CG's syntax is identical to HLSL. You can directly pass the CG source as HLSL.

This topic is closed to new replies.

Advertisement