Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Roughness-adjusted fresnel. #150

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions lighting/fresnel.glsl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "common/schlick.glsl"

#include "../math/pow5.hlsl"
#include "../math/const.glsl"
#include "../math/saturate.glsl"

Expand All @@ -8,6 +9,7 @@ contributors: Patricio Gonzalez Vivo
description: Resolve fresnel coeficient
use:
- <float|vec3> fresnel(const <float|vec3> f0, <float> NoV)
- <float|vec3> fresnel(const <float|vec3> f0, <float> NoV, float roughness)
license:
- Copyright (c) 2021 Patricio Gonzalez Vivo under Prosperity License - https://prosperitylicense.com/versions/3.0.0
- Copyright (c) 2021 Patricio Gonzalez Vivo under Patron License - https://lygia.xyz/license
Expand All @@ -33,4 +35,12 @@ float fresnel(const in float f0, const in float NoV) {
return schlick(f0, 1.0, NoV);
}

// Roughness-adjusted fresnel function to attenuate high speculars at glancing angles
// Very useful when used with filtered environment maps
// See https://seblagarde.wordpress.com/2011/08/17/hello-world/
vec3 fresnel(vec3 f0, float NoV, float roughness)
{
return f0 + (max(1.0 - roughness, f0) - f0) * pow5(1.0 - NoV);
}

#endif
41 changes: 24 additions & 17 deletions lighting/fresnel.hlsl
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
#include "common/schlick.hlsl"

#include "fakeCube.hlsl"
#include "sphericalHarmonics.hlsl"
#include "../color/tonemap.hlsl"
#include "../math/pow5.hlsl"

/*
contributors: Patricio Gonzalez Vivo
description: Resolve fresnel coeficient
use:
- <float3> fresnel(const <float3> f0, <float> LoH)
- <float3> fresnel(<float3> _R, <float3> _f0, <float> _NoV)
- <float3> fresnel(const <float3> f0, <float> NoV)
- <float3> fresnel(const <float3> f0, <float> NoV, float roughness)
license:
- Copyright (c) 2021 Patricio Gonzalez Vivo under Prosperity License - https://prosperitylicense.com/versions/3.0.0
- Copyright (c) 2021 Patricio Gonzalez Vivo under Patron License - https://lygia.xyz/license
Expand All @@ -18,22 +15,32 @@ license:
#ifndef FNC_FRESNEL
#define FNC_FRESNEL

float3 fresnel(const float3 f0, float LoH) {
float3 fresnel(const in float3 f0, float3 normal, float3 view)
{
return schlick(f0, 1.0, dot(view, normal));
}

float3 fresnel(const float3 f0, float NoV)
{
#if defined(TARGET_MOBILE) || defined(PLATFORM_RPI)
return schlick(f0, 1.0, LoH);
return schlick(f0, 1.0, NoV);
#else
float f90 = saturate(dot(f0, float3(50.0, 50.0, 50.0) * 0.33));
return schlick(f0, f90, LoH);
return schlick(f0, f90, NoV);
#endif
}

// float fresnelf(float3 V, float3 N, float R0) {
// float cosAngle = 1.0-max(dot(V, N), 0.0);
// float result = cosAngle * cosAngle;
// result = result * result;
// result = result * cosAngle;
// result = clamp(result * (1.0 - R0) + R0, 0.0, 1.0);
// return result;
// }
float fresnel(const in float f0, const in float NoV)
{
return schlick(f0, 1.0, NoV);
}

// Roughness-adjusted fresnel function to attenuate high speculars at glancing angles
// Very useful when used with filtered environment maps
// See https://seblagarde.wordpress.com/2011/08/17/hello-world/
float3 fresnel(float3 f0, float NoV, float roughness)
{
return f0 + (max(1.0 - roughness, f0) - f0) * pow5(1.0-NoV);
}

#endif