Skip to main content
gamedev.net
🔒 Locked 🎮 Unity

SSAO help

Started by murderv 0 replies 800+ views
Original Post
murderv
murderv
( i tried posting on the ssao but havent got any answer, so i thought i would try my luck at a new post ) im using iq's code to give SSAO a try, but im only getting a grey screen. i was hoping anyone could take a look at the code below and see if you can spot the problem?? i believe its the camera ray, but im not i cant be sure. depthSampler is my depthbuffer. its linear. i compute it as: float3 viewSpacePos = mul( matView, IN.worldSpacePos ); // done in vertex float depth = length(IN.viewSpacePos) / farPlane; // done in pixelshader float4 color = float4( depth, depth, depth, 1 ); as for the camera ray i tried several ways. for example: float3 ViewVec = CameraTarget-CameraPos; // both in world space OUT.cameraRay = ViewVec; ----- float3 ViewVec = WorldSpacePosition-CameraPos; // world space OUT.cameraRay = ViewVec; ----- float3 ViewVec = WorldSpacePosition-CameraPos; float3 ViewVec = mul( matView, ViewVec ).xyz; // vector in viewspace OUT.cameraRay = ViewVec; ----- float3 ViewVec = mul( matView, ViewVec ).xyz; // ViewVec is in worldspace and gets transformed to viewspace OUT.cameraRay = ViewVec; This is my vertex/pixel shader code. just converted from glsl to Cg

//
// VERTEX SHADER
//

VS_OUTPUT VS_SSAO( in VS_INPUT IN )
{
    VS_OUTPUT Out = (VS_OUTPUT)0;

    //Out.pos = IN.Position;	// for some weird reason passing direct screen coords doesnt work ?!? need to transform by WVP
	Out.pos = mul( worldViewProj, IN.Position ); 

	IN.Position.xy = sign( IN.Position.xy );
    Out.TexCoord = (float2(IN.Position.x, -IN.Position.y) + float2( 1.0f, 1.0f )) * 0.5f;

	float4 pos = IN.Position;
	float3 worldPos = mul( matWorld, pos ).xyz;

	float3 eyevec = worldPos - cameraPos;

	float3 eyeview = mul( matView, float4(eyevec, 1) ).xyz;
	normalize( eyeview );

	Out.viewDirection = eyeview;

    return Out;
}



//
// PIXEL SHADER
//

float4 PS_SSAO( in VS_OUTPUT IN ) : COLOR
{
	float4 samples[16] =
	{
		float4(0.355512, 	-0.709318, 	-0.102371,	0.0 ),
		float4(0.534186, 	0.71511, 	-0.115167,	0.0 ),
		float4(-0.87866, 	0.157139, 	-0.115167,	0.0 ),
		float4(0.140679, 	-0.475516, 	-0.0639818,	0.0 ),
		float4(-0.0796121, 	0.158842, 	-0.677075,	0.0 ),
		float4(-0.0759516, 	-0.101676, 	-0.483625,	0.0 ),
		float4(0.12493, 	-0.0223423,	-0.483625,	0.0 ),
		float4(-0.0720074, 	0.243395, 	-0.967251,	0.0 ),
		float4(-0.207641, 	0.414286, 	0.187755,	0.0 ),
		float4(-0.277332, 	-0.371262, 	0.187755,	0.0 ),
		float4(0.63864, 	-0.114214, 	0.262857,	0.0 ),
		float4(-0.184051, 	0.622119, 	0.262857,	0.0 ),
		float4(0.110007, 	-0.219486, 	0.435574,	0.0 ),
		float4(0.235085, 	0.314707, 	0.696918,	0.0 ),
		float4(-0.290012, 	0.0518654, 	0.522688,	0.0 ),
		float4(0.0975089, 	-0.329594, 	0.609803,	0.0 )
	};

	float depth = tex2D( depthSampler, IN.TexCoord ).r;

	float3 se = (depth * IN.viewDirection.xyz) / IN.viewDirection.z;

	//float3 viewnormal = tex2D(depthSampler, IN.TexCoord).rgb;

	float3 randNormal = tex2D( randSampler, IN.TexCoord * 200 ).rgb;
	//randNormal = randNormal - float3(0.5);
	randNormal = randNormal * 2 - 1.0;

	float finalColor = 0.0f;
	for( int i=0; i<16; i++ )
	{
		float3 ray = se + sampleRadius * reflect(samples.xyz, randNormal);

		float2 ss = (ray.xy/ray.z) * float2( 0.75, 1.0 );
        float2 sn = ss*.5 + float2(0.5);
        float sz = tex2D( depthSampler, sn ).r;

        float zd = 50.0*max( ray.z-sz, 0.0 );
        finalColor += 1.0 / (1.0+zd*zd);
	}

	return float4( finalColor/16, finalColor/16, finalColor/16, 1.0f );
}


Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.