Java-Gaming.org Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (803)
Games in Android Showcase (237)
games submitted by our members
Games in WIP (867)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Weird SSAO halo  (Read 2810 times)
0 Members and 1 Guest are viewing this topic.
Offline ShadedVertex
« Posted 2016-09-20 10:10:24 »

I was goofing around with SSAO and I realized something.

Firstly, here's my SSAO fragment shader code:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
#version 400 core

in vec2 passTextureCoords;

out vec4 outColour;

uniform sampler2D gPositionDepth;
uniform sampler2D gNormals;
uniform sampler2D noiseTexture;
uniform vec3 samples[32];
uniform mat4 projMatrix;

const vec2 noiseScale = vec2(1280.0/4.0, 780.0/4.0);

void main(void){
   
   vec3 fragPos = texture(gPositionDepth, passTextureCoords).xyz;
   vec3 normal = texture(gNormals, passTextureCoords).xyz;
   vec3 randomVec = texture(noiseTexture, passTextureCoords * noiseScale).xyz;
   
   vec3 tangent = normalize(randomVec - normal * dot(randomVec, normal)).xyz;
   vec3 bitangent = cross(normal, tangent);
   mat3 TBN = mat3(tangent, bitangent, normal);
   
   float occlusion = 0;
   int radius = 2;
   for(int i = 0; i < 32; i++){
      vec3 samp = TBN * samples[i];
      samp = fragPos + samp * radius;
     
      vec4 offset = vec4(samp, 1.0);
      offset = projMatrix * offset;
      offset.xyz /= offset.w;
      offset.xyz = offset.xyz * 0.5 + 0.5;
     
      float sampleDepth = texture(gPositionDepth, offset.xy).z;
      float rangeCheck = abs(samp.z - sampleDepth) < radius ? 1.0 : 0.0;
      occlusion += (sampleDepth >= samp.z ? 1.0 : 0.0) * rangeCheck;
   }
   
   occlusion = 1 - occlusion/32;
   outColour = vec4(occlusion);
   
}


This works perfectly fine. No halos, nothing.

However, take a look at this part:

1  
2  
3  
4  
5  
6  
        float sampleDepth = texture(gPositionDepth, offset.xy).z;
        float rangeCheck = abs(samp.z - sampleDepth) < radius ? 1.0 : 0.0;
   occlusion += (sampleDepth >= samp.z ? 1.0 : 0.0) * rangeCheck;
}
   
occlusion = 1 - occlusion/32;


I thought it didn't make sense. I mean, I should be checking if there's any geometry occluding the sample in the sample kernel. Following logic, I should be checking if sampleDepth is less than samp.z

But then I noticed that I was doing
1  
occlusion = 1 - occlusion/32;
. Because of the subtraction of the normalized occlusion factor from 1, it was working properly.

So I tried changing it to the following:

1  
2  
3  
4  
5  
6  
        float sampleDepth = texture(gPositionDepth, offset.xy).z;
        float rangeCheck = abs(samp.z - sampleDepth) < radius ? 1.0 : 0.0;
   occlusion += (sampleDepth <= samp.z ? 1.0 : 0.0) * rangeCheck;
}
   
occlusion = occlusion/32;


As you can see, I changed sampleDepth >= samp.z to sampleDepth <= samp.z, which makes more sense.

And I also removed the "1 - " part. But now there's a black halo around the mesh, and it doesn't look very nice. I see no reason why it should be like this. Maybe it's just me being dumb, I dunno Tongue

Initial results:


After change:
Offline theagentd
« Reply #1 - Posted 2016-09-20 11:11:01 »

Your algorithm places a couple of samples in 3D around the pixel in question, then compares the depth of the sample with the depth in the depth buffer. For the white background the depth is 1.0, so if anything is nearby it'll obviously fail the depth test. You can prevent this by not applying SSAO when the depth is exactly 1.0. However, this problem occurs when you have any large differences in depth, as an object 10km away will get occluded by an ant 10cm from the camera. This this is an inherent flaw of your algorithm.

Myomyomyo.
Offline ShadedVertex
« Reply #2 - Posted 2016-09-20 12:05:21 »

I didn't set the background colour though. The background colour for both scenes is supposed to be white. I don't get why the colour of the background changes as I move the camera around.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline theagentd
« Reply #3 - Posted 2016-09-20 15:58:02 »

I may have misunderstood but here goes.

For the background, fragPos, normal and hence tangent and bitangent will (persumably) all be zero since they've been set when clearing the G-buffer. This causes all the samples to end up at (0, 0, 0), which is turned into the texture coordinates (0.5, 0.5, 0.5). In other words, every single background pixel will read the center of the screen and compare the depth of that pixel with 0.5. As you move the camera around, the background will change/flicker as the center pixel it's compared to changes depth.

Myomyomyo.
Offline ShadedVertex
« Reply #4 - Posted 2016-09-20 22:33:47 »

Hmm...actually, since I'm using the colour white to clear the G-buffer, the fragPos and normals for the background pixels should all be vec3(1,1,1), right?
But your theory still makes sense, it's just that it's being compared to a different pixel, not the center pixel. That's actually ingenious, I never would've thought of that.
Pages: [1]
  ignore  |  Print  
 
 

 
Riven (397 views)
2019-09-04 15:33:17

hadezbladez (5280 views)
2018-11-16 13:46:03

hadezbladez (2204 views)
2018-11-16 13:41:33

hadezbladez (5544 views)
2018-11-16 13:35:35

hadezbladez (1150 views)
2018-11-16 13:32:03

EgonOlsen (4584 views)
2018-06-10 19:43:48

EgonOlsen (5462 views)
2018-06-10 19:43:44

EgonOlsen (3119 views)
2018-06-10 19:43:20

DesertCoockie (4015 views)
2018-05-13 18:23:11

nelsongames (4708 views)
2018-04-24 18:15:36
A NON-ideal modular configuration for Eclipse with JavaFX
by philfrei
2019-12-19 19:35:12

Java Gaming Resources
by philfrei
2019-05-14 16:15:13

Deployment and Packaging
by philfrei
2019-05-08 15:15:36

Deployment and Packaging
by philfrei
2019-05-08 15:13:34

Deployment and Packaging
by philfrei
2019-02-17 20:25:53

Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!