Urho3D/bin/CoreData/Shaders/HLSL/Lighting.hlsl
Lasse Öörni b8d55583f7 Squashed commit of the following:
commit a384431cd1ce230630d6a4522022de919cb55ac9
Author: dragonCASTjosh <dragonCASTjosh@gmail.com>
Date:   Thu Nov 10 00:16:26 2016 +0000

    Fixed typing and style errors

commit d4f8ca2eeed2b0196b46bd391e96a511594dc738
Author: dragonCASTjosh <dragonCASTjosh@gmail.com>
Date:   Thu Nov 10 00:14:02 2016 +0000

    Removed isAreaLight from attibutes

commit 62d50f3ba94d9c665358dcd7377730d968fde7be
Author: dragonCASTjosh <dragonCASTjosh@gmail.com>
Date:   Mon Nov 7 22:53:45 2016 +0000

    Added documentation on area lighting options

commit 53e5844bee5caccc2a37f0dcdac3b8d4a984dd62
Author: dragonCASTjosh <dragonCASTjosh@gmail.com>
Date:   Sat Nov 5 17:15:14 2016 +0000

    Fixd spacing

commit 5afa4ec59c1ecd2aa4892d4854e3b954af9cc73f
Author: dragonCASTjosh <dragonCASTjosh@gmail.com>
Date:   Sat Nov 5 06:00:19 2016 +0000

    Fixed materials under deferred and light position in the scene

commit 314bc5847a44979b89f46a0272fcf5cd0f04e866
Author: dragonCASTjosh <dragonCASTjosh@gmail.com>
Date:   Sat Nov 5 05:14:27 2016 +0000

    Fixed Deferred rendering

commit bde3560de71ab5a33c9cc3a4ee40029567cca5bf
Author: dragonCASTjosh <dragonCASTjosh@gmail.com>
Date:   Sat Nov 5 03:26:54 2016 +0000

    Area lighting, improved IBL and improved attenuation for OpenGL

commit 52be0fc67626a9c8901091445be52bdbe99657d1
Author: dragonCASTjosh <dragonCASTjosh@gmail.com>
Date:   Sat Nov 5 01:23:53 2016 +0000

    Intergrated Area ligthing with the editor, and improved texture quality

commit 5044bebe3d8799dee687ca676334f4493586d376
Author: dragonCASTjosh <dragonCASTjosh@gmail.com>
Date:   Mon Oct 24 09:25:15 2016 +0100

    Tube lighting HLSL

commit f3849a93d1812345b21918a7418d12c8d3e1e3dd
Author: dragonCASTjosh <dragonCASTjosh@gmail.com>
Date:   Mon Oct 24 09:01:10 2016 +0100

    Improve IBL

commit 210bfaebb40ed977eb46f0f6f91b3b6a49ec25a7
Author: joshua Nuttall <dragoncastjosh@live.com>
Date:   Thu Sep 15 12:32:15 2016 +0100

    Made sphere lights closer to epics paper

commit c186870e9e79de9572b5302019598ac059af58cf
Author: joshua Nuttall <dragoncastjosh@live.com>
Date:   Thu Sep 15 11:57:39 2016 +0100

    Added sphere light and beginning of tube lights
2016-11-18 02:02:05 +02:00

382 lines
13 KiB
HLSL

#pragma warning(disable:3571)
#ifdef COMPILEVS
float3 GetAmbient(float zonePos)
{
return cAmbientStartColor + zonePos * cAmbientEndColor;
}
#ifdef NUMVERTEXLIGHTS
float GetVertexLight(int index, float3 worldPos, float3 normal)
{
float3 lightDir = cVertexLights[index * 3 + 1].xyz;
float3 lightPos = cVertexLights[index * 3 + 2].xyz;
float invRange = cVertexLights[index * 3].w;
float cutoff = cVertexLights[index * 3 + 1].w;
float invCutoff = cVertexLights[index * 3 + 2].w;
// Directional light
if (invRange == 0.0)
{
#ifdef TRANSLUCENT
float NdotL = abs(dot(normal, lightDir));
#else
float NdotL = max(dot(normal, lightDir), 0.0);
#endif
return NdotL;
}
// Point/spot light
else
{
float3 lightVec = (lightPos - worldPos) * invRange;
float lightDist = length(lightVec);
float3 localDir = lightVec / lightDist;
#ifdef TRANSLUCENT
float NdotL = abs(dot(normal, localDir));
#else
float NdotL = max(dot(normal, localDir), 0.0);
#endif
float atten = saturate(1.0 - lightDist * lightDist);
float spotEffect = dot(localDir, lightDir);
float spotAtten = saturate((spotEffect - cutoff) * invCutoff);
return NdotL * atten * spotAtten;
}
}
float GetVertexLightVolumetric(int index, float3 worldPos)
{
float3 lightDir = cVertexLights[index * 3 + 1].xyz;
float3 lightPos = cVertexLights[index * 3 + 2].xyz;
float invRange = cVertexLights[index * 3].w;
float cutoff = cVertexLights[index * 3 + 1].w;
float invCutoff = cVertexLights[index * 3 + 2].w;
// Directional light
if (invRange == 0.0)
{
return 1.0;
}
// Point/spot light
else
{
float3 lightVec = (lightPos - worldPos) * invRange;
float lightDist = length(lightVec);
float3 localDir = lightVec / lightDist;
float atten = saturate(1.0 - lightDist * lightDist);
float spotEffect = dot(localDir, lightDir);
float spotAtten = saturate((spotEffect - cutoff) * invCutoff);
return atten * spotAtten;
}
}
#endif
#ifdef SHADOW
#ifdef DIRLIGHT
#define NUMCASCADES 4
#else
#define NUMCASCADES 1
#endif
void GetShadowPos(float4 projWorldPos, float3 normal, out float4 shadowPos[NUMCASCADES])
{
// Shadow projection: transform from world space to shadow space
#ifdef NORMALOFFSET
#ifdef DIRLIGHT
float cosAngle = saturate(1.0 - dot(normal, cLightDir));
#else
float cosAngle = saturate(1.0 - dot(normal, normalize(cLightPos.xyz - projWorldPos.xyz)));
#endif
#if defined(DIRLIGHT)
shadowPos[0] = mul(float4(projWorldPos.xyz + cosAngle * cNormalOffsetScale.x * normal, 1.0), cLightMatrices[0]);
shadowPos[1] = mul(float4(projWorldPos.xyz + cosAngle * cNormalOffsetScale.y * normal, 1.0), cLightMatrices[1]);
shadowPos[2] = mul(float4(projWorldPos.xyz + cosAngle * cNormalOffsetScale.z * normal, 1.0), cLightMatrices[2]);
shadowPos[3] = mul(float4(projWorldPos.xyz + cosAngle * cNormalOffsetScale.w * normal, 1.0), cLightMatrices[3]);
#elif defined(SPOTLIGHT)
shadowPos[0] = mul(float4(projWorldPos.xyz + cosAngle * cNormalOffsetScale.x * normal, 1.0), cLightMatrices[1]);
#else
shadowPos[0] = float4(projWorldPos.xyz + cosAngle * cNormalOffsetScale.x * normal - cLightPos.xyz, 0.0);
#endif
#else
#if defined(DIRLIGHT)
shadowPos[0] = mul(projWorldPos, cLightMatrices[0]);
shadowPos[1] = mul(projWorldPos, cLightMatrices[1]);
shadowPos[2] = mul(projWorldPos, cLightMatrices[2]);
shadowPos[3] = mul(projWorldPos, cLightMatrices[3]);
#elif defined(SPOTLIGHT)
shadowPos[0] = mul(projWorldPos, cLightMatrices[1]);
#else
shadowPos[0] = float4(projWorldPos.xyz - cLightPos.xyz, 0.0);
#endif
#endif
}
#endif
#endif
#ifdef COMPILEPS
float GetDiffuse(float3 normal, float3 worldPos, out float3 lightDir)
{
#ifdef DIRLIGHT
lightDir = cLightDirPS;
#ifdef TRANSLUCENT
return abs(dot(normal, lightDir));
#else
return saturate(dot(normal, lightDir));
#endif
#else
float3 lightVec = (cLightPosPS.xyz - worldPos) * cLightPosPS.w;
float lightDist = length(lightVec);
lightDir = lightVec / lightDist;
#ifdef TRANSLUCENT
return abs(dot(normal, lightDir)) * Sample2D(LightRampMap, float2(lightDist, 0.0)).r;
#else
return saturate(dot(normal, lightDir)) * Sample2D(LightRampMap, float2(lightDist, 0.0)).r;
#endif
#endif
}
float GetAtten(float3 normal, float3 worldPos, out float3 lightDir)
{
lightDir = cLightDirPS;
return saturate(dot(normal, lightDir));
}
float GetAttenPoint(float3 normal, float3 worldPos, out float3 lightDir)
{
float3 lightVec = (cLightPosPS.xyz - worldPos) * cLightPosPS.w;
float lightDist = length(lightVec);
float falloff = pow(saturate(1.0 - pow(lightDist / 1.0, 4.0)), 2.0) * 3.14159265358979323846 / (4 * 3.14159265358979323846)*(pow(lightDist, 2.0) + 1.0);
lightDir = lightVec / lightDist;
return saturate(dot(normal, lightDir)) * falloff;
}
float GetAttenSpot(float3 normal, float3 worldPos, out float3 lightDir)
{
float3 lightVec = (cLightPosPS.xyz - worldPos) * cLightPosPS.w;
float lightDist = length(lightVec);
float falloff = pow(saturate(1.0 - pow(lightDist / 1.0, 4.0)), 2.0) / (pow(lightDist, 2.0) + 1.0);
lightDir = lightVec / lightDist;
return saturate(dot(normal, lightDir)) * falloff;
}
float GetDiffuseVolumetric(float3 worldPos)
{
#ifdef DIRLIGHT
return 1.0;
#else
float3 lightVec = (cLightPosPS.xyz - worldPos) * cLightPosPS.w;
float lightDist = length(lightVec);
return Sample2D(LightRampMap, float2(lightDist, 0.0)).r;
#endif
}
float GetSpecular(float3 normal, float3 eyeVec, float3 lightDir, float specularPower)
{
float3 halfVec = normalize(normalize(eyeVec) + lightDir);
return saturate(pow(dot(normal, halfVec), specularPower));
}
float GetIntensity(float3 color)
{
return dot(color, float3(0.299, 0.587, 0.114));
}
#ifdef SHADOW
#ifdef DIRLIGHT
#define NUMCASCADES 4
#else
#define NUMCASCADES 1
#endif
#ifdef VSM_SHADOW
float ReduceLightBleeding(float min, float p_max)
{
return clamp((p_max - min) / (1.0 - min), 0.0, 1.0);
}
float Chebyshev(float2 Moments, float depth)
{
//One-tailed inequality valid if depth > Moments.x
float p = float(depth <= Moments.x);
//Compute variance.
float Variance = Moments.y - (Moments.x * Moments.x);
float minVariance = cVSMShadowParams.x;
Variance = max(Variance, minVariance);
//Compute probabilistic upper bound.
float d = depth - Moments.x;
float p_max = Variance / (Variance + d*d);
// Prevent light bleeding
p_max = ReduceLightBleeding(cVSMShadowParams.y, p_max);
return max(p, p_max);
}
#endif
float GetShadow(float4 shadowPos)
{
#if defined(SIMPLE_SHADOW)
// Take one sample
#ifdef D3D11
shadowPos.xyz /= shadowPos.w;
#endif
float inLight = SampleShadow(ShadowMap, shadowPos).r;
#ifndef SHADOWCMP
return cShadowIntensity.y + cShadowIntensity.x * inLight;
#else
#ifndef POINTLIGHT
return cShadowIntensity.y + cShadowIntensity.x * (inLight * shadowPos.w > shadowPos.z);
#else
return cShadowIntensity.y + cShadowIntensity.x * (inLight > shadowPos.z);
#endif
#endif
#elif defined(PCF_SHADOW)
// Take four samples and average them
// Note: in case of sampling a point light cube shadow, we optimize out the w divide as it has already been performed
#ifdef D3D11
shadowPos.xyz /= shadowPos.w;
#endif
#if !defined(POINTLIGHT) && !defined(D3D11)
float2 offsets = cShadowMapInvSize * shadowPos.w;
#else
float2 offsets = cShadowMapInvSize;
#endif
float4 shadowPos2 = float4(shadowPos.x + offsets.x, shadowPos.yzw);
float4 shadowPos3 = float4(shadowPos.x, shadowPos.y + offsets.y, shadowPos.zw);
float4 shadowPos4 = float4(shadowPos.xy + offsets.xy, shadowPos.zw);
float4 inLight = float4(
SampleShadow(ShadowMap, shadowPos).r,
SampleShadow(ShadowMap, shadowPos2).r,
SampleShadow(ShadowMap, shadowPos3).r,
SampleShadow(ShadowMap, shadowPos4).r
);
#ifndef SHADOWCMP
return cShadowIntensity.y + dot(inLight, cShadowIntensity.x);
#else
#ifndef POINTLIGHT
return cShadowIntensity.y + dot(inLight * shadowPos.w > shadowPos.z, cShadowIntensity.x);
#else
return cShadowIntensity.y + dot(inLight > shadowPos.z, cShadowIntensity.x);
#endif
#endif
#elif defined(VSM_SHADOW)
float2 samples = Sample2D(ShadowMap, shadowPos.xy / shadowPos.w).rg;
return cShadowIntensity.y + cShadowIntensity.x * Chebyshev(samples, shadowPos.z/shadowPos.w);
#endif
}
#ifdef POINTLIGHT
float GetPointShadow(float3 lightVec)
{
float3 axis = SampleCube(FaceSelectCubeMap, lightVec).rgb;
float depth = abs(dot(lightVec, axis));
// Expand the maximum component of the light vector to get full 0.0 - 1.0 UV range from the cube map,
// and to avoid sampling across faces. Some GPU's filter across faces, while others do not, and in this
// case filtering across faces is wrong
const float factor = 1.0 / 256.0;
lightVec += factor * axis * lightVec;
// Read the 2D UV coordinates, adjust according to shadow map size and add face offset
float4 indirectPos = SampleCube(IndirectionCubeMap, lightVec);
indirectPos.xy *= cShadowCubeAdjust.xy;
indirectPos.xy += float2(cShadowCubeAdjust.z + indirectPos.z * 0.5, cShadowCubeAdjust.w + indirectPos.w);
float4 shadowPos = float4(indirectPos.xy, cShadowDepthFade.x + cShadowDepthFade.y / depth, 1.0);
return GetShadow(shadowPos);
}
#endif
#ifdef DIRLIGHT
float GetDirShadowFade(float inLight, float depth)
{
return saturate(inLight + saturate((depth - cShadowDepthFade.z) * cShadowDepthFade.w));
}
float GetDirShadow(const float4 iShadowPos[NUMCASCADES], float depth)
{
float4 shadowPos;
if (depth < cShadowSplits.x)
shadowPos = iShadowPos[0];
else if (depth < cShadowSplits.y)
shadowPos = iShadowPos[1];
else if (depth < cShadowSplits.z)
shadowPos = iShadowPos[2];
else
shadowPos = iShadowPos[3];
return GetDirShadowFade(GetShadow(shadowPos), depth);
}
float GetDirShadowDeferred(float4 projWorldPos, float3 normal, float depth)
{
float4 shadowPos;
#ifdef NORMALOFFSET
float cosAngle = saturate(1.0 - dot(normal, cLightDirPS));
if (depth < cShadowSplits.x)
shadowPos = mul(float4(projWorldPos.xyz + cosAngle * cNormalOffsetScalePS.x * normal, 1.0), cLightMatricesPS[0]);
else if (depth < cShadowSplits.y)
shadowPos = mul(float4(projWorldPos.xyz + cosAngle * cNormalOffsetScalePS.y * normal, 1.0), cLightMatricesPS[1]);
else if (depth < cShadowSplits.z)
shadowPos = mul(float4(projWorldPos.xyz + cosAngle * cNormalOffsetScalePS.z * normal, 1.0), cLightMatricesPS[2]);
else
shadowPos = mul(float4(projWorldPos.xyz + cosAngle * cNormalOffsetScalePS.w * normal, 1.0), cLightMatricesPS[3]);
#else
if (depth < cShadowSplits.x)
shadowPos = mul(projWorldPos, cLightMatricesPS[0]);
else if (depth < cShadowSplits.y)
shadowPos = mul(projWorldPos, cLightMatricesPS[1]);
else if (depth < cShadowSplits.z)
shadowPos = mul(projWorldPos, cLightMatricesPS[2]);
else
shadowPos = mul(projWorldPos, cLightMatricesPS[3]);
#endif
return GetDirShadowFade(GetShadow(shadowPos), depth);
}
#endif
float GetShadow(float4 iShadowPos[NUMCASCADES], float depth)
{
#if defined(DIRLIGHT)
return GetDirShadow(iShadowPos, depth);
#elif defined(SPOTLIGHT)
return GetShadow(iShadowPos[0]);
#else
return GetPointShadow(iShadowPos[0].xyz);
#endif
}
float GetShadowDeferred(float4 projWorldPos, float3 normal, float depth)
{
#ifdef DIRLIGHT
return GetDirShadowDeferred(projWorldPos, normal, depth);
#else
#ifdef NORMALOFFSET
float cosAngle = saturate(1.0 - dot(normal, normalize(cLightPosPS.xyz - projWorldPos.xyz)));
projWorldPos.xyz += cosAngle * cNormalOffsetScalePS.x * normal;
#endif
#ifdef SPOTLIGHT
float4 shadowPos = mul(projWorldPos, cLightMatricesPS[1]);
return GetShadow(shadowPos);
#else
float3 shadowPos = projWorldPos.xyz - cLightPosPS.xyz;
return GetPointShadow(shadowPos);
#endif
#endif
}
#endif
#endif