1
0
Fork 1
mirror of https://github.com/TES3MP/openmw-tes3mp.git synced 2025-01-19 21:23:52 +00:00
openmw-tes3mp/files/shaders/lighting.glsl

239 lines
7.5 KiB
Text
Raw Normal View History

#define LIGHTING_MODEL_FFP 0
#define LIGHTING_MODEL_SINGLE_UBO 1
#define LIGHTING_MODEL_PER_OBJECT_UNIFORM 2
2016-02-17 22:29:26 +00:00
2021-04-04 05:25:13 +00:00
#if @lightingModel != LIGHTING_MODEL_FFP
#define getLight LightBuffer
float quickstep(float x)
{
x = clamp(x, 0.0, 1.0);
x = 1.0 - x*x;
x = 1.0 - x*x;
return x;
}
2016-02-17 22:29:26 +00:00
2021-04-04 05:25:13 +00:00
#if @lightingModel == LIGHTING_MODEL_SINGLE_UBO
const int mask = int(0xff);
const ivec4 shift = ivec4(int(0), int(8), int(16), int(24));
vec3 unpackRGB(int data)
{
2021-03-27 19:47:41 +00:00
return vec3( (float(((data >> shift.x) & mask)) / 255.0)
,(float(((data >> shift.y) & mask)) / 255.0)
,(float(((data >> shift.z) & mask)) / 255.0));
}
2021-02-21 18:38:15 +00:00
vec4 unpackRGBA(int data)
{
2021-03-27 19:47:41 +00:00
return vec4( (float(((data >> shift.x) & mask)) / 255.0)
,(float(((data >> shift.y) & mask)) / 255.0)
,(float(((data >> shift.z) & mask)) / 255.0)
,(float(((data >> shift.w) & mask)) / 255.0));
}
2021-02-21 18:38:15 +00:00
2021-04-04 05:25:13 +00:00
/* Layout:
packedColors: 8-bit unsigned RGB packed as (diffuse, ambient, specular).
sign bit is stored in diffuse alpha component
attenuation: constant, linear, quadratic, light radius (as defined in content)
*/
struct LightData
2021-02-21 18:38:15 +00:00
{
2021-04-04 05:25:13 +00:00
ivec4 packedColors;
2021-03-02 07:30:54 +00:00
vec4 position;
2021-04-04 05:25:13 +00:00
vec4 attenuation;
2021-02-21 18:38:15 +00:00
};
uniform int PointLightIndex[@maxLights];
uniform int PointLightCount;
2021-02-21 18:38:15 +00:00
// Defaults to shared layout. If we ever move to GLSL 140, std140 layout should be considered
uniform LightBufferBinding
2021-02-21 18:38:15 +00:00
{
LightData LightBuffer[@maxLightsInScene];
2021-02-21 18:38:15 +00:00
};
#else
2021-04-04 05:25:13 +00:00
/* Layout:
--------------------------------------- -----------
| pos_x | ambi_r | diff_r | spec_r |
| pos_y | ambi_g | diff_g | spec_g |
| pos_z | ambi_b | diff_b | spec_b |
| att_c | att_l | att_q | radius/spec_a |
--------------------------------------------------
*/
uniform mat4 LightBuffer[@maxLights];
uniform int PointLightCount;
#endif
2021-02-21 18:38:15 +00:00
#else
#define getLight gl_LightSource
#endif
void perLightSun(out vec3 ambientOut, out vec3 diffuseOut, vec3 viewPos, vec3 viewNormal)
{
2021-04-04 05:25:13 +00:00
#if @lightingModel == LIGHTING_MODEL_PER_OBJECT_UNIFORM
vec3 lightDir = normalize(getLight[0][0].xyz);
#else
vec3 lightDir = normalize(getLight[0].position.xyz);
2021-04-04 05:25:13 +00:00
#endif
2021-02-21 18:38:15 +00:00
2021-04-04 05:25:13 +00:00
#if @lightingModel == LIGHTING_MODEL_PER_OBJECT_UNIFORM
ambientOut = getLight[0][1].xyz;
vec3 sunDiffuse = getLight[0][2].xyz;
#elif @lightingModel == LIGHTING_MODEL_SINGLE_UBO
ivec4 data = getLight[0].packedColors;
ambientOut = unpackRGB(data.y);
vec3 sunDiffuse = unpackRGB(data.x);
#else
ambientOut = getLight[0].ambient.xyz;
vec3 sunDiffuse = getLight[0].diffuse.xyz;
#endif
2021-04-04 05:25:13 +00:00
2021-02-21 18:38:15 +00:00
float lambert = dot(viewNormal.xyz, lightDir);
#ifndef GROUNDCOVER
lambert = max(lambert, 0.0);
#else
float eyeCosine = dot(normalize(viewPos), viewNormal.xyz);
if (lambert < 0.0)
{
lambert = -lambert;
eyeCosine = -eyeCosine;
}
lambert *= clamp(-8.0 * (1.0 - 0.3) * eyeCosine + 1.0, 0.3, 1.0);
#endif
diffuseOut = sunDiffuse * lambert;
}
2021-03-02 07:30:54 +00:00
2021-02-21 18:38:15 +00:00
void perLightPoint(out vec3 ambientOut, out vec3 diffuseOut, int lightIndex, vec3 viewPos, vec3 viewNormal)
2016-02-17 22:29:26 +00:00
{
2021-04-04 05:25:13 +00:00
#if @lightingModel == LIGHTING_MODEL_PER_OBJECT_UNIFORM
vec3 lightPos = getLight[lightIndex][0].xyz - viewPos;
#else
vec3 lightPos = getLight[lightIndex].position.xyz - viewPos;
2021-04-04 05:25:13 +00:00
#endif
2021-02-21 18:38:15 +00:00
float lightDistance = length(lightPos);
2021-03-28 06:41:22 +00:00
2021-04-04 05:25:13 +00:00
#if @lightingModel != LIGHTING_MODEL_FFP
#if @lightingModel == LIGHTING_MODEL_PER_OBJECT_UNIFORM
float radius = getLight[lightIndex][3][3];
#else
float radius = getLight[lightIndex].attenuation.w;
#endif
if (lightDistance > radius * 2.0)
2021-03-28 06:41:22 +00:00
{
ambientOut = vec3(0.0);
diffuseOut = vec3(0.0);
return;
}
#endif
lightPos = normalize(lightPos);
2017-09-20 23:25:48 +00:00
2021-04-04 05:25:13 +00:00
#if @lightingModel == LIGHTING_MODEL_PER_OBJECT_UNIFORM
float illumination = clamp(1.0 / (getLight[lightIndex][0].w + getLight[lightIndex][1].w * lightDistance + getLight[lightIndex][2].w * lightDistance * lightDistance), 0.0, 1.0);
illumination *= 1.0 - quickstep((lightDistance / radius) - 1.0);
ambientOut = getLight[lightIndex][1].xyz * illumination;
#elif @lightingModel == LIGHTING_MODEL_SINGLE_UBO
float illumination = clamp(1.0 / (getLight[lightIndex].attenuation.x + getLight[lightIndex].attenuation.y * lightDistance + getLight[lightIndex].attenuation.z * lightDistance * lightDistance), 0.0, 1.0);
2021-04-04 05:25:13 +00:00
illumination *= 1.0 - quickstep((lightDistance / radius) - 1.0);
ivec4 data = getLight[lightIndex].packedColors;
ambientOut = unpackRGB(data.y) * illumination;
#else
2021-04-04 05:25:13 +00:00
float illumination = clamp(1.0 / (getLight[lightIndex].constantAttenuation + getLight[lightIndex].linearAttenuation * lightDistance + getLight[lightIndex].quadraticAttenuation * lightDistance * lightDistance), 0.0, 1.0);
2021-02-21 18:38:15 +00:00
ambientOut = getLight[lightIndex].ambient.xyz * illumination;
#endif
2020-01-12 07:42:47 +00:00
float lambert = dot(viewNormal.xyz, lightPos) * illumination;
2020-01-12 07:42:47 +00:00
#ifndef GROUNDCOVER
lambert = max(lambert, 0.0);
#else
2021-02-09 21:56:21 +00:00
float eyeCosine = dot(normalize(viewPos), viewNormal.xyz);
if (lambert < 0.0)
2020-01-12 07:42:47 +00:00
{
2021-02-09 21:56:21 +00:00
lambert = -lambert;
eyeCosine = -eyeCosine;
2020-01-12 07:42:47 +00:00
}
2021-02-09 21:56:21 +00:00
lambert *= clamp(-8.0 * (1.0 - 0.3) * eyeCosine + 1.0, 0.3, 1.0);
2020-01-12 07:42:47 +00:00
#endif
2021-03-02 07:30:54 +00:00
2021-04-04 05:25:13 +00:00
#if @lightingModel == LIGHTING_MODEL_SINGLE_UBO
2021-03-27 19:47:41 +00:00
diffuseOut = unpackRGB(data.x) * lambert * float(int(data.w));
2021-04-04 05:25:13 +00:00
#elif @lightingModel == LIGHTING_MODEL_PER_OBJECT_UNIFORM
diffuseOut = getLight[lightIndex][2].xyz * lambert;
2021-03-02 07:30:54 +00:00
#else
diffuseOut = getLight[lightIndex].diffuse.xyz * lambert;
2021-03-02 07:30:54 +00:00
#endif
2017-09-20 23:25:48 +00:00
}
#if PER_PIXEL_LIGHTING
2020-12-19 17:17:42 +00:00
void doLighting(vec3 viewPos, vec3 viewNormal, float shadowing, out vec3 diffuseLight, out vec3 ambientLight)
2017-09-20 23:25:48 +00:00
#else
2020-12-19 17:17:42 +00:00
void doLighting(vec3 viewPos, vec3 viewNormal, out vec3 diffuseLight, out vec3 ambientLight, out vec3 shadowDiffuse)
2017-09-20 23:25:48 +00:00
#endif
{
2020-12-19 17:17:42 +00:00
vec3 ambientOut, diffuseOut;
// This light gets added a second time in the loop to fix Mesa users' slowdown, so we need to negate its contribution here.
2021-02-21 18:38:15 +00:00
perLightSun(ambientOut, diffuseOut, viewPos, viewNormal);
#if PER_PIXEL_LIGHTING
2020-12-19 17:17:42 +00:00
diffuseLight = diffuseOut * shadowing - diffuseOut;
2017-09-20 23:25:48 +00:00
#else
2020-12-19 17:17:42 +00:00
shadowDiffuse = diffuseOut;
diffuseLight = -diffuseOut;
2017-09-20 23:25:48 +00:00
#endif
2020-12-19 17:17:42 +00:00
ambientLight = gl_LightModel.ambient.xyz;
2021-02-21 18:38:15 +00:00
perLightSun(ambientOut, diffuseOut, viewPos, viewNormal);
ambientLight += ambientOut;
diffuseLight += diffuseOut;
#if @lightingModel == LIGHTING_MODEL_FFP
for (int i=1; i < @maxLights; ++i)
2016-02-17 22:29:26 +00:00
{
perLightPoint(ambientOut, diffuseOut, i, viewPos, viewNormal);
#elif @lightingModel == LIGHTING_MODEL_PER_OBJECT_UNIFORM
for (int i=1; i <= PointLightCount; ++i)
2021-02-21 18:38:15 +00:00
{
perLightPoint(ambientOut, diffuseOut, i, viewPos, viewNormal);
#else
for (int i=0; i < PointLightCount; ++i)
{
perLightPoint(ambientOut, diffuseOut, PointLightIndex[i], viewPos, viewNormal);
2021-02-21 18:38:15 +00:00
#endif
2020-12-19 17:17:42 +00:00
ambientLight += ambientOut;
diffuseLight += diffuseOut;
2016-02-17 22:29:26 +00:00
}
}
2016-02-20 18:02:11 +00:00
2016-02-21 00:28:42 +00:00
vec3 getSpecular(vec3 viewNormal, vec3 viewDirection, float shininess, vec3 matSpec)
2016-02-20 18:02:11 +00:00
{
2021-04-04 05:25:13 +00:00
#if @lightingModel == LIGHTING_MODEL_PER_OBJECT_UNIFORM
vec3 sunDir = getLight[0][0].xyz;
#else
vec3 sunDir = getLight[0].position.xyz;
2021-04-04 05:25:13 +00:00
#endif
#if @lightingModel == LIGHTING_MODEL_SINGLE_UBO
vec3 sunSpec = unpackRGB(getLight[0].packedColors.z);
2021-04-04 05:25:13 +00:00
#elif @lightingModel == LIGHTING_MODEL_PER_OBJECT_UNIFORM
vec3 sunSpec = getLight[0][3].xyz;
#else
vec3 sunSpec = getLight[0].specular.xyz;
#endif
vec3 lightDir = normalize(sunDir);
2019-03-18 14:07:27 +00:00
float NdotL = dot(viewNormal, lightDir);
if (NdotL <= 0.0)
2020-12-19 17:17:42 +00:00
return vec3(0.0);
2016-02-21 00:28:42 +00:00
vec3 halfVec = normalize(lightDir - viewDirection);
2019-03-18 14:07:27 +00:00
float NdotH = dot(viewNormal, halfVec);
return pow(max(NdotH, 0.0), max(1e-4, shininess)) * sunSpec * matSpec;
2016-02-20 18:02:11 +00:00
}