Bläddra i källkod

Fix FixCubeLookup()

1023/1024 anways = 0, so this function just didn't work
1vanK 5 år sedan
förälder
incheckning
5f14c2f529
2 ändrade filer med 20 tillägg och 16 borttagningar
  1. 11 9
      bin/CoreData/Shaders/GLSL/IBL.glsl
  2. 9 7
      bin/CoreData/Shaders/HLSL/IBL.hlsl

+ 11 - 9
bin/CoreData/Shaders/GLSL/IBL.glsl

@@ -17,14 +17,15 @@
         return SpecularColor * AB.x + AB.y;
         return SpecularColor * AB.x + AB.y;
     }
     }
 
 
-    vec3 FixCubeLookup(vec3 v) 
+    // https://web.archive.org/web/20200228213025/http://the-witness.net/news/2012/02/seamless-cube-map-filtering/
+    vec3 FixCubeLookup(vec3 v, float cubeMapSize) 
     {
     {
         float M = max(max(abs(v.x), abs(v.y)), abs(v.z));
         float M = max(max(abs(v.x), abs(v.y)), abs(v.z));
-        float scale = (1024 - 1) / 1024;
+        float scale = (cubeMapSize - 1.0) / cubeMapSize;
 
 
-        if (abs(v.x) != M) v.x += scale;
-        if (abs(v.y) != M) v.y += scale;
-        if (abs(v.z) != M) v.z += scale; 
+        if (abs(v.x) != M) v.x *= scale;
+        if (abs(v.y) != M) v.y *= scale;
+        if (abs(v.z) != M) v.z *= scale; 
 
 
         return v;
         return v;
     }
     }
@@ -49,12 +50,13 @@
         // OpenGL ES does not support textureLod without extensions and does not have the sZoneCubeMap sampler,
         // OpenGL ES does not support textureLod without extensions and does not have the sZoneCubeMap sampler,
         // so for now, sample without explicit LOD, and from the environment sampler, where the zone texture will be put
         // so for now, sample without explicit LOD, and from the environment sampler, where the zone texture will be put
         // on mobile hardware
         // on mobile hardware
+        const float cubeMapSize = 1024.0; // TODO This only works with textures of a given size
         #ifndef GL_ES
         #ifndef GL_ES
-            vec3 cube = textureLod(sZoneCubeMap, FixCubeLookup(reflectVec), mipSelect).rgb;
-            vec3 cubeD = textureLod(sZoneCubeMap, FixCubeLookup(wsNormal), 9.0).rgb;
+            vec3 cube = textureLod(sZoneCubeMap, FixCubeLookup(reflectVec, cubeMapSize), mipSelect).rgb;
+            vec3 cubeD = textureLod(sZoneCubeMap, FixCubeLookup(wsNormal, cubeMapSize), 9.0).rgb;
         #else
         #else
-            vec3 cube = textureCube(sEnvCubeMap, FixCubeLookup(reflectVec)).rgb;
-            vec3 cubeD = textureCube(sEnvCubeMap, FixCubeLookup(wsNormal)).rgb;
+            vec3 cube = textureCube(sEnvCubeMap, FixCubeLookup(reflectVec, cubeMapSize)).rgb;
+            vec3 cubeD = textureCube(sEnvCubeMap, FixCubeLookup(wsNormal, cubeMapSize)).rgb;
         #endif
         #endif
 
 
         // Fake the HDR texture
         // Fake the HDR texture

+ 9 - 7
bin/CoreData/Shaders/HLSL/IBL.hlsl

@@ -16,14 +16,15 @@
         return specColor * AB.x + AB.y;
         return specColor * AB.x + AB.y;
     }
     }
 
 
-    float3 FixCubeLookup(float3 v) 
+    // https://web.archive.org/web/20200228213025/http://the-witness.net/news/2012/02/seamless-cube-map-filtering/
+    float3 FixCubeLookup(float3 v, float cubeMapSize)
     {
     {
         float M = max(max(abs(v.x), abs(v.y)), abs(v.z));
         float M = max(max(abs(v.x), abs(v.y)), abs(v.z));
-        float scale = (1024 - 1) / 1024;
+        float scale = (cubeMapSize - 1.0) / cubeMapSize;
 
 
-        if (abs(v.x) != M) v.x += scale;
-        if (abs(v.y) != M) v.y += scale;
-        if (abs(v.z) != M) v.z += scale; 
+        if (abs(v.x) != M) v.x *= scale;
+        if (abs(v.y) != M) v.y *= scale;
+        if (abs(v.z) != M) v.z *= scale; 
 
 
         return v;
         return v;
     }
     }
@@ -55,8 +56,9 @@
         // reflectVec = intersectionPos - ((cZoneMin + cZoneMax )/ 2);
         // reflectVec = intersectionPos - ((cZoneMin + cZoneMax )/ 2);
 
 
         const float mipSelect = GetMipFromRoughness(roughness);
         const float mipSelect = GetMipFromRoughness(roughness);
-        float3 cube = SampleCubeLOD(ZoneCubeMap, float4(FixCubeLookup(reflectVec), mipSelect)).rgb;
-        float3 cubeD = SampleCubeLOD(ZoneCubeMap, float4(FixCubeLookup(wsNormal), 9.0)).rgb;
+        const float cubeMapSize = 1024.0; // TODO This only works with textures of a given size
+        float3 cube = SampleCubeLOD(ZoneCubeMap, float4(FixCubeLookup(reflectVec, cubeMapSize), mipSelect)).rgb;
+        float3 cubeD = SampleCubeLOD(ZoneCubeMap, float4(FixCubeLookup(wsNormal, cubeMapSize), 9.0)).rgb;
         
         
         // Fake the HDR texture
         // Fake the HDR texture
         float brightness = clamp(cAmbientColor.a, 0.0, 1.0);
         float brightness = clamp(cAmbientColor.a, 0.0, 1.0);