Workbench: SeeThrough

added a fresnel effect

TODO: solve memory leak
This commit is contained in:
Jeroen Bakker 2018-05-22 16:59:12 +02:00
parent 8644eef5c3
commit 1b164cf81e
3 changed files with 38 additions and 4 deletions

View File

@ -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;
}

View File

@ -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);
}

View File

@ -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);
}