Browse Source

small ssao changes, e.g. better vec2 noise

AronKovacs 4 years ago
parent
commit
5a14fcabc5
1 changed files with 8 additions and 7 deletions
  1. 8 7
      src/mol-gl/shader/ssao.frag.ts

+ 8 - 7
src/mol-gl/shader/ssao.frag.ts

@@ -27,12 +27,12 @@ float noise(const in vec2 coords) {
 	float b = 78.233;
 	float c = 43758.5453;
 	float dt = dot(coords, vec2(a,b));
-	float sn = mod(dt, 3.14159);
+	float sn = mod(dt, PI);
 	return abs(fract(sin(sn) * c)); // is abs necessary?
 }
 
 vec2 getNoiseVec2(const in vec2 coords) {
-	return vec2(noise(coords), noise(coords) + 2.71828);
+	return vec2(noise(coords), noise(coords + vec2(PI, 2.71828)));
 }
 
 bool isBackground(const in float depth) {
@@ -53,8 +53,10 @@ vec3 normalFromDepth(const in float depth, const in float depth1, const in float
     return normalize(normal);
 }
 
+// StarCraft II Ambient Occlusion by [Filion and McNaughton 2008]
 void main(void) {
-	vec2 selfCoords = gl_FragCoord.xy / uTexSize;
+	vec2 invTexSize = 1.0 / uTexSize;
+	vec2 selfCoords = gl_FragCoord.xy * invTexSize;
 
 	float selfDepth = getDepth(selfCoords);
 	vec2 selfPackedDepth = packUnitIntervalToRG(selfDepth);
@@ -64,8 +66,8 @@ void main(void) {
 		return;
 	}
 	
-	vec2 offset1 = vec2(0.0, 0.001);
-    vec2 offset2 = vec2(0.001, 0.0);
+	vec2 offset1 = vec2(0.0, invTexSize.y);
+    vec2 offset2 = vec2(invTexSize.x, 0.0);
 
 	float selfDepth1 = getDepth(selfCoords + offset1);
 	float selfDepth2 = getDepth(selfCoords + offset2);
@@ -89,8 +91,7 @@ void main(void) {
         offset.xyz /= offset.w;
         offset.xyz = offset.xyz * 0.5 + 0.5;
         
-		float sampleDepth = getDepth(offset.xy);
-		float sampleViewZ = screenSpaceToViewSpace(vec3(offset.xy, sampleDepth), uInvProjection).z;
+		float sampleViewZ = screenSpaceToViewSpace(vec3(offset.xy, getDepth(offset.xy)), uInvProjection).z;
 
         occlusion += (sampleViewZ >= sampleViewPos.z + uBias ? 1.0 : 0.0) * smootherstep(0.0, 1.0, uRadius / abs(selfViewPos.z - sampleViewZ));           
     }