armory/Shaders/std/light.glsl

222 lines
5.8 KiB
Plaintext
Raw Normal View History

2018-12-10 17:25:29 +01:00
#ifndef _LIGHT_GLSL_
#define _LIGHT_GLSL_
#include "compiled.inc"
#include "std/brdf.glsl"
#include "std/math.glsl"
#ifdef _ShadowMap
#include "std/shadows.glsl"
#endif
2019-01-27 19:12:00 +01:00
#ifdef _VoxelAOvar
#include "std/conetrace.glsl"
#endif
2019-01-27 20:13:21 +01:00
#ifdef _LTC
#include "std/ltc.glsl"
#endif
#ifdef _LightIES
#include "std/ies.glsl"
#endif
2019-05-30 20:22:57 +02:00
#ifdef _SSRS
#include "std/ssrs.glsl"
#endif
2018-12-10 17:25:29 +01:00
#ifdef _ShadowMap
2018-12-14 15:27:43 +01:00
#ifdef _SinglePoint
2018-12-15 19:03:11 +01:00
#ifdef _Spot
uniform sampler2DShadow shadowMapSpot[1];
uniform mat4 LWVPSpot0;
#else
uniform samplerCubeShadow shadowMapPoint[1];
uniform vec2 lightProj;
#endif
2018-12-14 15:27:43 +01:00
#endif
2018-12-15 19:03:11 +01:00
#ifdef _Clusters
uniform samplerCubeShadow shadowMapPoint[4];
2018-12-10 17:25:29 +01:00
uniform vec2 lightProj;
#ifdef _Spot
2018-12-15 19:03:11 +01:00
uniform sampler2DShadow shadowMapSpot[4];
2018-12-10 17:25:29 +01:00
uniform mat4 LWVPSpot0;
2018-12-10 23:29:04 +01:00
uniform mat4 LWVPSpot1;
uniform mat4 LWVPSpot2;
uniform mat4 LWVPSpot3;
2018-12-10 17:25:29 +01:00
#endif
#endif
2018-12-15 19:03:11 +01:00
#endif
2018-12-10 17:25:29 +01:00
2019-01-27 20:13:21 +01:00
#ifdef _LTC
uniform vec3 lightArea0;
uniform vec3 lightArea1;
uniform vec3 lightArea2;
uniform vec3 lightArea3;
uniform sampler2D sltcMat;
uniform sampler2D sltcMag;
2019-01-27 23:48:54 +01:00
#ifdef _ShadowMap
2019-04-10 22:08:45 +02:00
#ifndef _Spot
2019-01-27 23:48:54 +01:00
#ifdef _SinglePoint
uniform sampler2DShadow shadowMapSpot[1];
uniform mat4 LWVPSpot0;
#endif
#ifdef _Clusters
uniform sampler2DShadow shadowMapSpot[4];
uniform mat4 LWVPSpot0;
uniform mat4 LWVPSpot1;
uniform mat4 LWVPSpot2;
uniform mat4 LWVPSpot3;
#endif
2019-04-10 22:08:45 +02:00
#endif
2019-01-27 23:48:54 +01:00
#endif
2019-01-27 20:13:21 +01:00
#endif
2018-12-10 17:25:29 +01:00
vec3 sampleLight(const vec3 p, const vec3 n, const vec3 v, const float dotNV, const vec3 lp, const vec3 lightCol,
const vec3 albedo, const float rough, const float spec, const vec3 f0
#ifdef _ShadowMap
, int index, float bias, bool receiveShadow
2018-12-10 17:25:29 +01:00
#endif
#ifdef _Spot
, bool isSpot, float spotA, float spotB, vec3 spotDir
#endif
2019-01-27 19:12:00 +01:00
#ifdef _VoxelAOvar
#ifdef _VoxelShadow
, sampler3D voxels, vec3 voxpos
#endif
2019-01-28 11:28:21 +01:00
#endif
2019-05-21 21:53:57 +02:00
#ifdef _MicroShadowing
, float occ
#endif
2019-05-30 20:22:57 +02:00
#ifdef _SSRS
, sampler2D gbufferD, mat4 invVP, vec3 eye
#endif
2018-12-10 17:25:29 +01:00
) {
vec3 ld = lp - p;
vec3 l = normalize(ld);
vec3 h = normalize(v + l);
float dotNH = dot(n, h);
float dotVH = dot(v, h);
float dotNL = dot(n, l);
2019-01-27 20:13:21 +01:00
#ifdef _LTC
float theta = acos(dotNV);
vec2 tuv = vec2(rough, theta / (0.5 * PI));
tuv = tuv * LUT_SCALE + LUT_BIAS;
vec4 t = textureLod(sltcMat, tuv, 0.0);
mat3 invM = mat3(
vec3(1.0, 0.0, t.y),
vec3(0.0, t.z, 0.0),
vec3(t.w, 0.0, t.x));
float ltcspec = ltcEvaluate(n, v, dotNV, p, invM, lightArea0, lightArea1, lightArea2, lightArea3);
ltcspec *= textureLod(sltcMag, tuv, 0.0).a;
float ltcdiff = ltcEvaluate(n, v, dotNV, p, mat3(1.0), lightArea0, lightArea1, lightArea2, lightArea3);
2019-02-27 21:31:21 +01:00
vec3 direct = albedo * ltcdiff + ltcspec * spec * 0.05;
2019-01-27 20:13:21 +01:00
#else
2018-12-10 17:25:29 +01:00
vec3 direct = lambertDiffuseBRDF(albedo, dotNL) +
specularBRDF(f0, rough, dotNL, dotNH, dotNV, dotVH) * spec;
2019-01-27 20:13:21 +01:00
#endif
2019-02-07 21:33:27 +01:00
direct *= attenuate(distance(p, lp));
2018-12-10 17:25:29 +01:00
direct *= lightCol;
2019-05-21 21:53:57 +02:00
#ifdef _MicroShadowing
direct *= dotNL + 2.0 * occ * occ - 1.0;
#endif
2019-05-30 20:22:57 +02:00
#ifdef _SSRS
direct *= traceShadowSS(l, p, gbufferD, invVP, eye);
#endif
2019-01-27 19:12:00 +01:00
#ifdef _VoxelAOvar
#ifdef _VoxelShadow
2019-02-09 15:34:16 +01:00
direct *= 1.0 - traceShadow(voxels, voxpos, l);
2019-01-27 19:12:00 +01:00
#endif
#endif
2019-01-27 23:48:54 +01:00
#ifdef _LTC
#ifdef _ShadowMap
2020-07-13 23:36:49 +02:00
if (receiveShadow) {
#ifdef _SinglePoint
vec4 lPos = LWVPSpot0 * vec4(p + n * bias * 10, 1.0);
direct *= shadowTest(shadowMapSpot[0], lPos.xyz / lPos.w, bias);
#endif
#ifdef _Clusters
if (index == 0) {
vec4 lPos = LWVPSpot0 * vec4(p + n * bias * 10, 1.0);
direct *= shadowTest(shadowMapSpot[0], lPos.xyz / lPos.w, bias);
}
else if (index == 1) {
vec4 lPos = LWVPSpot1 * vec4(p + n * bias * 10, 1.0);
direct *= shadowTest(shadowMapSpot[1], lPos.xyz / lPos.w, bias);
}
else if (index == 2) {
vec4 lPos = LWVPSpot2 * vec4(p + n * bias * 10, 1.0);
direct *= shadowTest(shadowMapSpot[2], lPos.xyz / lPos.w, bias);
}
else if (index == 3) {
vec4 lPos = LWVPSpot3 * vec4(p + n * bias * 10, 1.0);
direct *= shadowTest(shadowMapSpot[3], lPos.xyz / lPos.w, bias);
}
#endif
}
2019-01-27 23:48:54 +01:00
#endif
return direct;
#endif
2018-12-10 17:25:29 +01:00
#ifdef _Spot
if (isSpot) {
float spotEffect = dot(spotDir, l); // lightDir
// x - cutoff, y - cutoff - exponent
if (spotEffect < spotA) {
direct *= smoothstep(spotB, spotA, spotEffect);
}
#ifdef _ShadowMap
2020-07-13 23:36:49 +02:00
if (receiveShadow) {
#ifdef _SinglePoint
vec4 lPos = LWVPSpot0 * vec4(p + n * bias * 10, 1.0);
direct *= shadowTest(shadowMapSpot[0], lPos.xyz / lPos.w, bias);
#endif
#ifdef _Clusters
if (index == 0) {
vec4 lPos = LWVPSpot0 * vec4(p + n * bias * 10, 1.0);
direct *= shadowTest(shadowMapSpot[0], lPos.xyz / lPos.w, bias);
}
else if (index == 1) {
vec4 lPos = LWVPSpot1 * vec4(p + n * bias * 10, 1.0);
direct *= shadowTest(shadowMapSpot[1], lPos.xyz / lPos.w, bias);
}
else if (index == 2) {
vec4 lPos = LWVPSpot2 * vec4(p + n * bias * 10, 1.0);
direct *= shadowTest(shadowMapSpot[2], lPos.xyz / lPos.w, bias);
}
else if (index == 3) {
vec4 lPos = LWVPSpot3 * vec4(p + n * bias * 10, 1.0);
direct *= shadowTest(shadowMapSpot[3], lPos.xyz / lPos.w, bias);
}
#endif
}
2018-12-14 15:27:43 +01:00
#endif
2018-12-10 17:25:29 +01:00
return direct;
}
#endif
#ifdef _LightIES
direct *= iesAttenuation(-l);
#endif
#ifdef _ShadowMap
2020-07-13 23:36:49 +02:00
if (receiveShadow) {
#ifdef _SinglePoint
#ifndef _Spot
direct *= PCFCube(shadowMapPoint[0], ld, -l, bias, lightProj, n);
#endif
#endif
#ifdef _Clusters
if (index == 0) direct *= PCFCube(shadowMapPoint[0], ld, -l, bias, lightProj, n);
else if (index == 1) direct *= PCFCube(shadowMapPoint[1], ld, -l, bias, lightProj, n);
else if (index == 2) direct *= PCFCube(shadowMapPoint[2], ld, -l, bias, lightProj, n);
else if (index == 3) direct *= PCFCube(shadowMapPoint[3], ld, -l, bias, lightProj, n);
#endif
}
2018-12-14 15:27:43 +01:00
#endif
2018-12-10 17:25:29 +01:00
return direct;
}
#endif