mirror of
https://github.com/HifiExperiments/overte.git
synced 2025-08-10 12:26:25 +02:00
73 lines
2.5 KiB
Text
Executable file
73 lines
2.5 KiB
Text
Executable file
<!
|
|
// DeferredLighting.slh
|
|
// libraries/render-utils/src
|
|
//
|
|
// Created by Sam Gateau on 1/15/15.
|
|
// Copyright 2013 High Fidelity, Inc.
|
|
//
|
|
// Distributed under the Apache License, Version 2.0.
|
|
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
|
|
!>
|
|
<@if not DEFERRED_LIGHTING_SLH@>
|
|
<@def DEFERRED_LIGHTING_SLH@>
|
|
|
|
|
|
<@func declareEvalPBRShading()@>
|
|
|
|
vec3 fresnelSchlick(vec3 fresnelColor, vec3 lightDir, vec3 halfDir) {
|
|
return fresnelColor + (1.0f - fresnelColor) * pow(1.0f - clamp(dot(lightDir, halfDir), 0.0, 1.0), 5);
|
|
}
|
|
|
|
float specularDistribution(float roughness, vec3 normal, vec3 halfDir) {
|
|
float gloss = (1.0 - roughness) * 128.0;
|
|
gloss *= gloss;
|
|
float power = pow(clamp(dot(halfDir, normal), 0.0, 1.0), gloss);
|
|
power *= (gloss * 0.125 + 0.25);
|
|
return power;
|
|
}
|
|
|
|
// Frag Shading returns the diffuse amount as W and the specular rgb as xyz
|
|
vec4 evalPBRShading(vec3 fragNormal, vec3 fragLightDir, vec3 fragEyeDir, float metallic, vec3 fresnel, float roughness) {
|
|
// Diffuse Lighting
|
|
float diffuse = clamp(dot(fragNormal, fragLightDir), 0.0, 1.0);
|
|
|
|
// Specular Lighting
|
|
vec3 halfDir = normalize(fragEyeDir + fragLightDir);
|
|
vec3 fresnelColor = fresnelSchlick(fresnel, fragLightDir,halfDir);
|
|
float power = specularDistribution(roughness, fragNormal, halfDir);
|
|
vec3 specular = power * fresnelColor * diffuse;
|
|
|
|
return vec4(specular, diffuse * (1 - fresnelColor.x));
|
|
}
|
|
<@endfunc@>
|
|
|
|
<@func declareEvalBlinnRShading()@>
|
|
|
|
vec4 evalBlinnShading(vec3 fragNormal, vec3 fragLightDir, vec3 fragEyeDir, vec3 specular, float roughness) {
|
|
// Diffuse Lighting
|
|
float diffuseDot = dot(fragNormal, fragLightDir);
|
|
float facingLight = step(0.0, diffuseDot);
|
|
float diffuse = diffuseDot * facingLight;
|
|
|
|
// Specular Lighting depends on the half vector and the roughness
|
|
vec3 halfDir = normalize(fragEyeDir + fragLightDir);
|
|
|
|
float gloss = (1.0 - roughness) * 128.0;
|
|
glos *= gloss;
|
|
float specularPower = pow(facingLight * max(0.0, dot(halfDir, fragNormal)), gloss);
|
|
vec3 reflect = specularPower * specular * diffuse;
|
|
|
|
return vec4(reflect, diffuse);
|
|
}
|
|
|
|
<@endfunc@>
|
|
|
|
|
|
<$declareEvalPBRShading()$>
|
|
|
|
// Return xyz the specular/reflection component and w the diffuse component
|
|
vec4 evalFragShading(vec3 fragNormal, vec3 fragLightDir, vec3 fragEyeDir, float metallic, vec3 specular, float roughness) {
|
|
return evalPBRShading(fragNormal, fragLightDir, fragEyeDir, metallic, specular, roughness);
|
|
}
|
|
|
|
<@endif@>
|