Skip to content

Commit 3ab747a

Browse files
authored
Added support for environment refraction (#583)
1 parent 492a3e7 commit 3ab747a

File tree

3 files changed

+54
-16
lines changed

3 files changed

+54
-16
lines changed

Resources/Engine/Shaders/Lighting/IBL.ovfxh

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -73,33 +73,51 @@ vec3 CalculateImageBasedLighting(
7373
float metallic,
7474
vec3 F0,
7575
float NdotV,
76-
vec3 albedo
76+
vec3 albedo,
77+
float transmission,
78+
float refractionIndex
7779
) {
7880
// 0. Constants
7981
const int environmentMaxLOD = textureQueryLevels(environmentMap) - 1;
80-
82+
8183
// 1. Calculate reflection vector
8284
const vec3 R = ParallaxCorrect(reflect(-V, N), fragPos);
8385

84-
// 2. Diffuse irradiance (environment lighting)
85-
const vec3 irradiance = textureLod(environmentMap, N, environmentMaxLOD).rgb; // High mip level for diffuse
86+
// 2. Calculate refraction vector
87+
const vec3 T = ParallaxCorrect(refract(-V, N, 1.0 / refractionIndex), fragPos);
88+
89+
// 3. Diffuse irradiance (environment lighting)
90+
const vec3 irradiance = textureLod(environmentMap, N, environmentMaxLOD).rgb;
91+
92+
// 4. Specular reflection
93+
const vec3 reflectedColor = textureLod(environmentMap, R, roughness * environmentMaxLOD).rgb;
8694

87-
// 3. Specular reflection
88-
const vec3 prefilteredColor = textureLod(environmentMap, R, roughness * environmentMaxLOD).rgb;
95+
// 5. Refracted environment sampling
96+
const vec3 refractedColor = textureLod(environmentMap, T, roughness * environmentMaxLOD).rgb;
8997

90-
// 4. Apply BRDF
98+
// 6. Apply BRDF
9199
const vec2 brdf = EnvBRDFApprox(NdotV, roughness);
92-
93-
// 5. Calculate Fresnel for IBL
100+
101+
// 7. Calculate Fresnel for IBL
94102
const vec3 F_IBL = FresnelSchlickRoughness(NdotV, F0, roughness);
95103

96-
// 6. Calculate specular and diffuse IBL components
104+
// 8. Calculate transmission factor based on Fresnel
105+
// Higher Fresnel = more reflection, less transmission
106+
const float avgFresnel = (F_IBL.r + F_IBL.g + F_IBL.b) / 3.0;
107+
const float transmissionFactor = transmission * (1.0 - avgFresnel);
108+
109+
// 9. Calculate diffuse and reflection IBL components
97110
const vec3 kS_IBL = F_IBL;
98111
const vec3 kD_IBL = (1.0 - kS_IBL) * (1.0 - metallic);
99-
100112
const vec3 diffuseIBL = kD_IBL * irradiance * albedo;
101-
const vec3 specularIBL = prefilteredColor * (F_IBL * brdf.x + brdf.y);
113+
const vec3 reflectionIBL = reflectedColor * (F_IBL * brdf.x + brdf.y);
102114

103-
// 7. Return IBL contribution
115+
// 10. Calculate refraction IBL component
116+
const vec3 refractionIBL = refractedColor * albedo;
117+
118+
// 11. Mix reflection and refraction contributions based on transmission factor
119+
const vec3 specularIBL = mix(reflectionIBL, refractionIBL, transmissionFactor);
120+
121+
// 12. Return combined IBL contribution
104122
return (diffuseIBL + specularIBL) * ubo_ReflectionProbeData.brightness;
105123
}

Resources/Engine/Shaders/Lighting/PBR.ovfxh

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,20 @@ vec3 CalculateBRDF(LightContribution lightContrib, vec3 V, vec3 N, vec3 albedo,
7272
return (kD * albedo / PI + specular) * radiance * NdotL;
7373
}
7474

75-
vec3 PBRLightingModel(vec3 albedo, float metallic, float roughness, float ao, vec3 normal, vec3 viewPos, vec3 fragPos, sampler2D shadowMap, mat4 lightSpaceMatrix, samplerCube environmentMap)
75+
vec3 PBRLightingModel(
76+
vec3 albedo,
77+
float metallic,
78+
float roughness,
79+
float ao,
80+
vec3 normal,
81+
vec3 viewPos,
82+
vec3 fragPos,
83+
sampler2D shadowMap,
84+
mat4 lightSpaceMatrix,
85+
samplerCube environmentMap,
86+
float transmission,
87+
float refractionIndex
88+
)
7689
{
7790
// Sanitize inputs
7891
metallic = clamp(metallic, 0.0, 1.0);
@@ -148,7 +161,9 @@ vec3 PBRLightingModel(vec3 albedo, float metallic, float roughness, float ao, ve
148161
metallic,
149162
F0,
150163
NdotV,
151-
albedo
164+
albedo,
165+
transmission,
166+
refractionIndex
152167
);
153168
#endif
154169

Resources/Engine/Shaders/Standard.ovfx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,9 @@ uniform sampler2D u_RoughnessMap;
9393
uniform float u_Roughness = 1.0;
9494
#endif
9595

96+
uniform float u_RefractionIndex = 1.5; // 1.0 = air, 1.33 = water, 1.5 = glass, etc.
97+
uniform float u_Transmission = 0.0; // 0.0 = opaque, 1.0 = fully transmissive
98+
9699
#if defined(NORMAL_MAPPING)
97100
uniform sampler2D u_NormalMap;
98101
#endif
@@ -200,7 +203,9 @@ void main()
200203
fs_in.FragPos,
201204
_ShadowMap,
202205
_LightSpaceMatrix,
203-
_EnvironmentMap
206+
_EnvironmentMap,
207+
u_Transmission,
208+
u_RefractionIndex
204209
);
205210

206211
// Simple built-in tonemapping (Reinhard) and gamma correction for elements

0 commit comments

Comments
 (0)