diff options
3 files changed, 38 insertions, 4 deletions
diff --git a/source/blender/draw/engines/workbench/shaders/workbench_common_lib.glsl b/source/blender/draw/engines/workbench/shaders/workbench_common_lib.glsl index 5b646f8e911..2550f44271b 100644 --- a/source/blender/draw/engines/workbench/shaders/workbench_common_lib.glsl +++ b/source/blender/draw/engines/workbench/shaders/workbench_common_lib.glsl @@ -35,3 +35,30 @@ vec2 normal_encode(vec3 n) float p = sqrt(n.z * 8.0 + 8.0); return vec2(n.xy / p + 0.5); } + +void fresnel(vec3 I, vec3 N, float ior, out float kr) +{ + float cosi = clamp(dot(I, N), -1.0, 1.0); + float etai = 1.0; + float etat = ior; + if (cosi > 0) { + etat = 1.0; + etai = ior; + } + + // Compute sini using Snell's law + float sint = etai / etat * sqrt(max(0.0, 1.0 - cosi * cosi)); + // Total internal reflection + if (sint >= 1) { + kr = 1; + } + else { + float cost = sqrt(max(0.0, 1.0 - sint * sint)); + cosi = abs(cosi); + float Rs = ((etat * cosi) - (etai * cost)) / ((etat * cosi) + (etai * cost)); + float Rp = ((etai * cosi) - (etat * cost)) / ((etai * cosi) + (etat * cost)); + kr = (Rs * Rs + Rp * Rp) / 2; + } + // As a consequence of the conservation of energy, transmittance is given by: + // kt = 1 - kr; +} diff --git a/source/blender/draw/engines/workbench/shaders/workbench_forward_transparent_accum_frag.glsl b/source/blender/draw/engines/workbench/shaders/workbench_forward_transparent_accum_frag.glsl index 7eb5336e303..78981522c18 100644 --- a/source/blender/draw/engines/workbench/shaders/workbench_forward_transparent_accum_frag.glsl +++ b/source/blender/draw/engines/workbench/shaders/workbench_forward_transparent_accum_frag.glsl @@ -48,7 +48,16 @@ void main() #endif /* V3D_LIGHTING_STUDIO */ float alpha = world_data.see_through_transparency; +#ifdef NORMAL_VIEWPORT_PASS_ENABLED + vec3 normal = normalize(normal_viewport); + float kr; + fresnel(vec3(0.0, 0.0, -1.0), normal, 1.22, kr); + alpha = mix(alpha, alpha+alpha, kr); +#endif + vec4 premultiplied = vec4(shaded_color.rgb * alpha, alpha); + transparentAccum = calculate_transparent_accum(premultiplied); + } diff --git a/source/blender/draw/engines/workbench/workbench_forward.c b/source/blender/draw/engines/workbench/workbench_forward.c index 02d2e3bde38..2de86bf4be1 100644 --- a/source/blender/draw/engines/workbench/workbench_forward.c +++ b/source/blender/draw/engines/workbench/workbench_forward.c @@ -176,12 +176,10 @@ static WORKBENCH_MaterialData *get_or_create_material_data(WORKBENCH_Data *vedat static void ensure_forward_shaders(WORKBENCH_PrivateData *wpd, int index, int drawtype) { - if (e_data.composite_sh_cache[index] == NULL) { + if (e_data.composite_sh_cache[index] == NULL && drawtype == OB_SOLID) { char *defines = workbench_material_build_defines(wpd, drawtype); char *composite_frag = workbench_build_forward_composite_frag(); - if (drawtype == OB_SOLID) { - e_data.composite_sh_cache[index] = DRW_shader_create_fullscreen(composite_frag, defines); - } + e_data.composite_sh_cache[index] = DRW_shader_create_fullscreen(composite_frag, defines); MEM_freeN(composite_frag); MEM_freeN(defines); } |