armory/Shaders/deferred/translucent.frag.glsl

188 lines
4.4 KiB
Plaintext
Raw Normal View History

2016-05-27 01:12:21 +02:00
#version 450
#ifdef GL_ES
precision mediump float;
#endif
2016-08-13 20:38:45 +02:00
#include "../compiled.glsl"
2016-10-17 17:39:40 +02:00
#include "../std/brdf.glsl"
// ...
#include "../std/math.glsl"
// envMapEquirect()
#include "../std/shirr.glsl"
// shIrradiance()
//!uniform float shirr[27];
2016-05-27 01:12:21 +02:00
2016-08-25 00:26:01 +02:00
#ifdef _BaseTex
uniform sampler2D sbase;
2016-05-27 01:12:21 +02:00
#endif
#ifdef _Rad
uniform sampler2D senvmapRadiance;
uniform sampler2D senvmapBrdf;
uniform int envmapNumMipmaps;
#endif
2016-08-25 00:26:01 +02:00
#ifdef _NorTex
2016-07-19 19:42:46 +02:00
uniform sampler2D snormal;
2016-05-27 01:12:21 +02:00
#endif
#ifdef _NorStr
2016-10-17 17:39:40 +02:00
uniform float normalStrength;
#endif
2016-08-25 00:26:01 +02:00
#ifdef _OccTex
uniform sampler2D socclusion;
#else
uniform float occlusion;
2016-05-27 01:12:21 +02:00
#endif
2016-08-25 00:26:01 +02:00
#ifdef _RoughTex
uniform sampler2D srough;
2016-05-27 01:12:21 +02:00
#else
2016-07-19 19:42:46 +02:00
uniform float roughness;
2016-05-27 01:12:21 +02:00
#endif
#ifdef _RoughStr
2016-10-17 17:39:40 +02:00
uniform float roughnessStrength;
#endif
2016-08-25 00:26:01 +02:00
#ifdef _MetTex
uniform sampler2D smetal;
2016-05-27 01:12:21 +02:00
#else
2016-07-19 19:42:46 +02:00
uniform float metalness;
2016-05-27 01:12:21 +02:00
#endif
uniform float envmapStrength;
uniform vec3 lightPos;
uniform vec3 lightDir;
uniform int lightType;
2016-05-27 01:12:21 +02:00
uniform vec3 lightColor;
uniform float lightStrength;
uniform float spotlightCutoff;
uniform float spotlightExponent;
2016-05-27 01:12:21 +02:00
uniform vec3 eye;
2016-08-25 00:26:01 +02:00
in vec4 wvpposition;
2016-05-27 01:12:21 +02:00
in vec3 position;
2016-07-19 19:42:46 +02:00
#ifdef _Tex
in vec2 texCoord;
2016-05-27 01:12:21 +02:00
#endif
in vec4 matColor;
in vec3 eyeDir;
2016-08-25 00:26:01 +02:00
#ifdef _NorTex
2016-07-19 19:42:46 +02:00
in mat3 TBN;
2016-05-27 01:12:21 +02:00
#else
2016-07-19 19:42:46 +02:00
in vec3 normal;
2016-05-27 01:12:21 +02:00
#endif
2016-10-12 17:52:27 +02:00
out vec4[2] fragColor;
2016-05-27 01:12:21 +02:00
void main() {
2016-08-25 00:26:01 +02:00
#ifdef _NorTex
2016-05-27 01:12:21 +02:00
vec3 n = (texture(snormal, texCoord).rgb * 2.0 - 1.0);
n = normalize(TBN * normalize(n));
#else
vec3 n = normalize(normal);
#endif
#ifdef _NorStr
2016-10-17 17:39:40 +02:00
n *= normalStrength;
#endif
2016-05-27 01:12:21 +02:00
// Move out
2016-10-17 17:39:40 +02:00
vec3 l;
if (lightType == 0) { // Sun
l = lightDir;
}
else { // Point, spot
l = normalize(lightPos - position.xyz);
}
float dotNL = max(dot(n, l), 0.0);
vec3 baseColor = matColor.rgb;
2016-08-25 00:26:01 +02:00
#ifdef _BaseTex
vec4 texel = texture(sbase, texCoord);
2016-05-27 01:12:21 +02:00
#ifdef _AlphaTest
if(texel.a < 0.4)
discard;
#endif
texel.rgb = pow(texel.rgb, vec3(2.2));
baseColor *= texel.rgb;
2016-05-27 01:12:21 +02:00
#endif
vec3 v = normalize(eyeDir);
vec3 h = normalize(v + l);
float dotNV = max(dot(n, v), 0.0);
float dotNH = max(dot(n, h), 0.0);
float dotVH = max(dot(v, h), 0.0);
float dotLV = max(dot(l, v), 0.0);
float dotLH = max(dot(l, h), 0.0);
2016-08-25 00:26:01 +02:00
#ifdef _MetTex
float metalness = texture(smetal, texCoord).r;
2016-05-27 01:12:21 +02:00
#endif
vec3 albedo = surfaceAlbedo(baseColor, metalness);
vec3 f0 = surfaceF0(baseColor, metalness);
2016-05-27 01:12:21 +02:00
2016-08-25 00:26:01 +02:00
#ifdef _RoughTex
float roughness = texture(srough, texCoord).r;
2016-05-27 01:12:21 +02:00
#endif
#ifdef _RoughStr
2016-10-17 17:39:40 +02:00
roughness *= roughnessStrength;
2016-05-27 01:12:21 +02:00
#endif
2016-10-12 17:52:27 +02:00
#ifdef _OccTex
float occlusion = texture(socclusion, texCoord).r;
#endif
2016-05-27 01:12:21 +02:00
// Direct
2016-10-23 15:32:26 +02:00
#ifdef _OrenNayar
vec3 direct = orenNayarDiffuseBRDF(albedo, roughness, dotNV, dotNL, dotVH) + specularBRDF(f0, roughness, dotNL, dotNH, dotNV, dotVH);
#else
vec3 direct = lambertDiffuseBRDF(albedo, dotNL) + specularBRDF(f0, roughness, dotNL, dotNH, dotNV, dotVH);
#endif
2016-10-17 17:39:40 +02:00
if (lightType == 2) { // Spot
float spotEffect = dot(lightDir, l);
if (spotEffect < spotlightCutoff) {
spotEffect = smoothstep(spotlightCutoff - spotlightExponent, spotlightCutoff, spotEffect);
direct *= spotEffect;
}
}
2016-05-27 01:12:21 +02:00
direct = direct * lightColor * lightStrength;
// Indirect
vec3 indirectDiffuse = shIrradiance(n, 2.2) / PI;
#ifdef _EnvLDR
indirectDiffuse = pow(indirectDiffuse, vec3(2.2));
#endif
2016-05-27 01:12:21 +02:00
indirectDiffuse *= albedo;
vec3 indirect = indirectDiffuse;
2016-05-27 01:12:21 +02:00
#ifdef _Rad
2016-05-27 01:12:21 +02:00
vec3 reflectionWorld = reflect(-v, n);
2016-10-17 17:39:40 +02:00
float lod = getMipFromRoughness(roughness, envmapNumMipmaps);// + 1.0;
2016-05-27 01:12:21 +02:00
vec3 prefilteredColor = textureLod(senvmapRadiance, envMapEquirect(reflectionWorld), lod).rgb;
#ifdef _EnvLDR
prefilteredColor = pow(prefilteredColor, vec3(2.2));
#endif
2016-05-27 01:12:21 +02:00
vec2 envBRDF = texture(senvmapBrdf, vec2(roughness, 1.0 - dotNV)).xy;
vec3 indirectSpecular = prefilteredColor * (f0 * envBRDF.x + envBRDF.y);
indirect += indirectSpecular;
#endif
indirect = indirect * envmapStrength;// * lightColor * lightStrength;
2016-05-27 01:12:21 +02:00
vec4 premultipliedReflect = vec4(vec3(direct + indirect * occlusion), matColor.a);
2016-05-27 01:12:21 +02:00
// vec4 premultipliedReflect = vec4(1.0, 0.0, 0.0, 0.01);
2016-06-03 17:18:38 +02:00
// vec4 premultipliedReflect = baseColor;
2016-08-25 00:26:01 +02:00
float fragZ = wvpposition.z / wvpposition.w;
2016-05-27 01:12:21 +02:00
float a = min(1.0, premultipliedReflect.a) * 8.0 + 0.01;
2016-10-17 17:39:40 +02:00
float b = -fragZ * 0.95 + 1.0;
2016-05-27 01:12:21 +02:00
float w = clamp(a * a * a * 1e8 * b * b * b, 1e-2, 3e2);
2016-10-17 17:39:40 +02:00
// accum = premultipliedReflect * w;
// revealage = premultipliedReflect.a;
// RT0 = vec4(C * w, a)
// RT1 = vec4(vec3(a * w), 1)
2016-10-12 17:52:27 +02:00
fragColor[0] = vec4(premultipliedReflect.rgb * w, premultipliedReflect.a);
fragColor[1] = vec4(premultipliedReflect.a * w, 0.0, 0.0, 1.0);
2016-05-27 01:12:21 +02:00
}