Jump to content

  • Log In with Google      Sign In   
  • Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

Sign up from the right-hand sidebar on our homepage and read Tuesday's newsletter for details!


We're also offering banner ads on our site from just $5! 1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


SSAO camera angle issue


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 gchewood   Members   -  Reputation: 236

Like
0Likes
Like

Posted 22 June 2013 - 08:54 PM

Trying to get some decent SSAO running and I've come upon an annoying problem. These 3 images are of the same area in the level. The first 2 are roughly correct (I hope). Ignore the weird background, separate issue.

See images:

 

ssao1.jpg ssao2.jpg ssao3.jpg

 

For about 75% of viewing angles it looks consistent. However when the camera then faces one direction (image 3), it starts playing up and darkening most objects. What could be causing this? The depth map and normal map both look fine and it's not the gaussian blur either. The main hlsl shader code is as follows:

float Radius;
float MaxRadius;
float RandomTile;
float Bias;
float FarClip;

texture DepthBuffer;
sampler2D depthSampler = sampler_state
{
	Texture = <DepthBuffer>;
	MipFilter = NONE;
	MagFilter = POINT;
	MinFilter = POINT;
	AddressU = Clamp;
	AddressV = Clamp;
};

texture NormalBuffer;
sampler2D normalSampler = sampler_state
{
	Texture = <NormalBuffer>;
	MipFilter = NONE;
	MagFilter = LINEAR;
	MinFilter = LINEAR;
	AddressU = Clamp;
	AddressV = Clamp;
};

texture RandomMap;
sampler2D randomSampler = sampler_state
{
	Texture = <RandomMap>;
	MipFilter = NONE;
	MagFilter = POINT;
	MinFilter = POINT;
	AddressU = Wrap;
	AddressV = Wrap;
};


struct VertexShaderInput
{
    float4 Position : POSITION0;
	float2 TexCoord : TEXCOORD0;
};

struct VertexShaderOutput
{
    float4 Position : POSITION0;
	float2 TexCoord : TEXCOORD0;
};



VertexShaderOutput VertexShaderFunction(VertexShaderInput input)
{
    VertexShaderOutput output = (VertexShaderOutput)0;
	
	output.Position = float4(input.Position.xyz,1);
    output.TexCoord = input.TexCoord;  
	
	return output;
}

half3 DecodeNormal (half4 enc)
{
	float kScale = 1.7777;
	float3 nn = enc.xyz*float3(2*kScale,2*kScale,0) + float3(-kScale,-kScale,1);
	float g = 2.0 / dot(nn.xyz,nn.xyz);
	float3 n;
	n.xy = g*nn.xy;
	n.z = g-1;
	return n;
}

#define SAMPLE_COUNT 13
float3 RAND_SAMPLES[SAMPLE_COUNT] = 
{
      float3( 0.5381, 0.1856,-0.4319), 
	  float3( 0.1379, 0.2486, 0.4430),
      float3( 0.3371, 0.5679,-0.0057), 
	  float3(-0.6999,-0.0451,-0.0019),
      float3( 0.0689,-0.1598,-0.8547), 
	  float3( 0.0560, 0.0069,-0.1843),
      float3(-0.0146, 0.1402, 0.0762), 
	  float3( 0.0100,-0.1924,-0.0344),
      float3(-0.3577,-0.5301,-0.4358), 
	  float3(-0.3169, 0.1063, 0.0158),
      float3( 0.0103,-0.5869, 0.0046), 
	  float3(-0.0897,-0.4940, 0.3287),
	  float3( 0.0352,-0.0631, 0.5460)
  };


float4 PixelShaderFunction(VertexShaderOutput input): COLOR0
{
        float depth =tex2D(depthSampler,input.TexCoord).r;	
	clip(-depth + 0.9999f);
	float totalOcclusion = 0;
	depth *= FarClip;    
	float scale = min(MaxRadius,Radius / max(1,depth));
	half3 normal = DecodeNormal(tex2D(normalSampler, input.TexCoord));
	normal.y = -normal.y;
	half3 normalScaled = normal * 0.25f;
	half3 randNormal = (tex2D(randomSampler, input.TexCoord*RandomTile).rgb* 2.0 - 1.0);
	for (int i = 0; i < SAMPLE_COUNT; i++)
	{
		half3 randomDirection = reflect(RAND_SAMPLES[i], randNormal);	
		randomDirection *= sign( dot(normal , randomDirection) );
		randomDirection += normalScaled;
		half modifiedDepth = depth -(randomDirection.z * Radius.x);
		half2 offset = randomDirection.xy * scale;
		float newDepth = tex2D(depthSampler, input.TexCoord + offset).r * FarClip;
		float deltaDepth = saturate(modifiedDepth - newDepth );
		totalOcclusion += (1-deltaDepth)*(deltaDepth > Bias);			      
	}
	totalOcclusion /= SAMPLE_COUNT;
	
    return totalOcclusion;
}


technique SSAO
{
    pass Pass1
    {
        VertexShader = compile vs_3_0 VertexShaderFunction();
        PixelShader = compile ps_3_0 PixelShaderFunction();
    }
}

Help!

Thanks in advance people!



Sponsor:

#2 gchewood   Members   -  Reputation: 236

Like
0Likes
Like

Posted 22 June 2013 - 10:08 PM

Ok so it's definitely a problem with the normal buffer. Not entirely sure what though.....?



#3 kauna   Crossbones+   -  Reputation: 2745

Like
0Likes
Like

Posted 23 June 2013 - 12:50 AM

- Some thing to check : are you normalizing normals before encoding them? 

- I had issues with the used encoding/decoding method : I had to negate the normal before encoding and after decoding (logically).

normal.y = -normal.y; <- why?

 

Cheers!


Edited by kauna, 23 June 2013 - 12:53 AM.


#4 gchewood   Members   -  Reputation: 236

Like
0Likes
Like

Posted 23 June 2013 - 12:20 PM

Figured it out, finally. I wasn't getting the normals into view space so they were only correct for what I assume is the default view. That's sorted but now I'm having further issues with halo-ing. On 80% of models, it works as intended. However, if you look at the palm tree in the first image for example, you'll see it's darkening the right-hand side of the tree. I'm finding that I get an annoying amount of halo-ing for any radius that's sufficiently large. To get rid of it, I seeem to have to shrink the radius to so small a value that the effect is barely noticeable :-|

Ideas?

Thanks



#5 phil_t   Crossbones+   -  Reputation: 3946

Like
0Likes
Like

Posted 25 June 2013 - 07:51 PM

Is the darkening happening on the object or on the background behind the object? If the latter, then you should be able to fix it by ignoring the occlusion term when deltaDepth is bigger than some value (a good value to start with is your AO radius). It looks like you already do this for small terms too? (What is "Bias"?).






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS