1
0
Fork 0
mirror of https://github.com/OpenMW/openmw.git synced 2025-01-22 11:23:52 +00:00
openmw/files/shaders/compatibility/objects.frag

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

274 lines
6.8 KiB
GLSL
Raw Normal View History

#version 120
#pragma import_defines(FORCE_OPAQUE)
#if @useUBO
#extension GL_ARB_uniform_buffer_object : require
#endif
#if @useGPUShader4
2021-01-08 17:32:15 +00:00
#extension GL_EXT_gpu_shader4: require
#endif
2021-02-21 18:38:15 +00:00
#if @diffuseMap
uniform sampler2D diffuseMap;
varying vec2 diffuseMapUV;
#endif
#if @darkMap
uniform sampler2D darkMap;
varying vec2 darkMapUV;
#endif
#if @detailMap
uniform sampler2D detailMap;
varying vec2 detailMapUV;
#endif
#if @decalMap
uniform sampler2D decalMap;
varying vec2 decalMapUV;
#endif
#if @emissiveMap
uniform sampler2D emissiveMap;
varying vec2 emissiveMapUV;
#endif
2016-02-17 23:00:12 +00:00
#if @normalMap
uniform sampler2D normalMap;
varying vec2 normalMapUV;
varying vec4 passTangent;
2016-02-17 23:00:12 +00:00
#endif
2016-02-18 22:05:44 +00:00
#if @envMap
uniform sampler2D envMap;
varying vec2 envMapUV;
uniform vec4 envMapColor;
#endif
2016-02-20 18:02:11 +00:00
#if @specularMap
uniform sampler2D specularMap;
varying vec2 specularMapUV;
#endif
2020-03-02 01:03:36 +00:00
#if @bumpMap
uniform sampler2D bumpMap;
varying vec2 bumpMapUV;
uniform vec2 envMapLumaBias;
uniform mat2 bumpMapMatrix;
#endif
#if @glossMap
uniform sampler2D glossMap;
varying vec2 glossMapUV;
#endif
2022-06-06 20:40:38 +00:00
uniform vec2 screenRes;
2023-02-25 19:03:39 +00:00
uniform float near;
uniform float far;
uniform float alphaRef;
2016-02-16 21:32:59 +00:00
2016-02-18 16:08:18 +00:00
#define PER_PIXEL_LIGHTING (@normalMap || @forcePPL)
2016-02-17 22:39:06 +00:00
#if !PER_PIXEL_LIGHTING
2020-12-19 17:17:42 +00:00
centroid varying vec3 passLighting;
2018-05-11 18:15:04 +00:00
centroid varying vec3 shadowDiffuseLighting;
2020-12-16 21:46:09 +00:00
#else
uniform float emissiveMult;
2016-02-17 22:39:06 +00:00
#endif
2021-11-10 16:58:06 +00:00
uniform float specStrength;
2016-02-20 18:02:11 +00:00
varying vec3 passViewPos;
2016-03-22 20:12:16 +00:00
varying vec3 passNormal;
2016-02-17 22:39:06 +00:00
2022-06-21 20:28:17 +00:00
#if @additiveBlending
#define ADDITIVE_BLENDING
#endif
2023-02-25 19:03:39 +00:00
#include "lib/light/lighting.glsl"
#include "lib/material/parallax.glsl"
#include "lib/material/alpha.glsl"
2023-02-26 22:31:41 +00:00
2022-06-06 20:40:38 +00:00
#include "fog.glsl"
2023-02-26 22:31:41 +00:00
#include "vertexcolors.glsl"
#include "shadows_fragment.glsl"
2016-02-16 22:30:23 +00:00
2021-10-23 20:46:39 +00:00
#if @softParticles
2023-02-25 19:03:39 +00:00
#include "lib/particle/soft.glsl"
uniform sampler2D opaqueDepthTex;
uniform float particleSize;
uniform bool particleFade;
2021-10-20 16:42:18 +00:00
#endif
2023-01-07 06:23:03 +00:00
#if @particleOcclusion
2023-02-25 19:03:39 +00:00
#include "lib/particle/occlusion.glsl"
2023-01-07 06:23:03 +00:00
uniform sampler2D orthoDepthMap;
varying vec3 orthoDepthMapCoord;
#endif
void main()
{
2023-01-07 06:23:03 +00:00
#if @particleOcclusion
2023-02-25 19:03:39 +00:00
applyOcclusionDiscard(orthoDepthMapCoord, texture2D(orthoDepthMap, orthoDepthMapCoord.xy * 0.5 + 0.5).r);
2023-01-07 06:23:03 +00:00
#endif
#if @diffuseMap
2016-03-22 20:43:07 +00:00
vec2 adjustedDiffuseUV = diffuseMapUV;
#endif
2016-03-22 20:43:07 +00:00
2023-01-19 16:39:38 +00:00
vec3 normal = normalize(passNormal);
vec3 viewVec = normalize(passViewPos.xyz);
2022-05-14 01:58:00 +00:00
2016-03-22 20:43:07 +00:00
#if @normalMap
vec4 normalTex = texture2D(normalMap, normalMapUV);
2023-01-19 16:39:38 +00:00
vec3 normalizedNormal = normal;
vec3 normalizedTangent = normalize(passTangent.xyz);
vec3 binormal = cross(normalizedTangent, normalizedNormal) * passTangent.w;
mat3 tbnTranspose = mat3(normalizedTangent, binormal, normalizedNormal);
2016-03-22 20:43:07 +00:00
2023-01-19 16:39:38 +00:00
normal = normalize(tbnTranspose * (normalTex.xyz * 2.0 - 1.0));
2016-03-22 20:43:07 +00:00
#endif
#if @parallax
vec3 cameraPos = (gl_ModelViewMatrixInverse * vec4(0,0,0,1)).xyz;
vec3 objectPos = (gl_ModelViewMatrixInverse * vec4(passViewPos, 1)).xyz;
vec3 eyeDir = normalize(cameraPos - objectPos);
2020-05-08 13:55:22 +00:00
vec2 offset = getParallaxOffset(eyeDir, tbnTranspose, normalTex.a, (passTangent.w > 0.0) ? -1.f : 1.f);
2016-03-22 20:43:07 +00:00
adjustedDiffuseUV += offset; // only offset diffuse for now, other textures are more likely to be using a completely different UV set
// TODO: check not working as the same UV buffer is being bound to different targets
// if diffuseMapUV == normalMapUV
#if 1
2016-03-22 20:43:07 +00:00
// fetch a new normal using updated coordinates
normalTex = texture2D(normalMap, adjustedDiffuseUV);
2022-05-14 01:58:00 +00:00
2023-01-19 16:39:38 +00:00
normal = normalize(tbnTranspose * (normalTex.xyz * 2.0 - 1.0));
2016-03-22 20:43:07 +00:00
#endif
#endif
2023-01-19 16:39:38 +00:00
vec3 viewNormal = normalize(gl_NormalMatrix * normal);
#if @diffuseMap
2016-03-22 20:43:07 +00:00
gl_FragData[0] = texture2D(diffuseMap, adjustedDiffuseUV);
gl_FragData[0].a *= coveragePreservingAlphaScale(diffuseMap, adjustedDiffuseUV);
#else
gl_FragData[0] = vec4(1.0);
#endif
2016-02-16 21:32:59 +00:00
vec4 diffuseColor = getDiffuseColor();
gl_FragData[0].a *= diffuseColor.a;
#if @darkMap
gl_FragData[0] *= texture2D(darkMap, darkMapUV);
gl_FragData[0].a *= coveragePreservingAlphaScale(darkMap, darkMapUV);
#endif
2023-02-26 22:31:41 +00:00
gl_FragData[0].a = alphaTest(gl_FragData[0].a, alphaRef);
#if @detailMap
gl_FragData[0].xyz *= texture2D(detailMap, detailMapUV).xyz * 2.0;
#endif
#if @decalMap
vec4 decalTex = texture2D(decalMap, decalMapUV);
2021-12-31 01:08:29 +00:00
gl_FragData[0].xyz = mix(gl_FragData[0].xyz, decalTex.xyz, decalTex.a * diffuseColor.a);
#endif
#if @envMap
vec2 envTexCoordGen = envMapUV;
float envLuma = 1.0;
#if @normalMap
// if using normal map + env map, take advantage of per-pixel normals for envTexCoordGen
vec3 r = reflect( viewVec, viewNormal );
float m = 2.0 * sqrt( r.x*r.x + r.y*r.y + (r.z+1.0)*(r.z+1.0) );
envTexCoordGen = vec2(r.x/m + 0.5, r.y/m + 0.5);
#endif
#if @bumpMap
vec4 bumpTex = texture2D(bumpMap, bumpMapUV);
envTexCoordGen += bumpTex.rg * bumpMapMatrix;
envLuma = clamp(bumpTex.b * envMapLumaBias.x + envMapLumaBias.y, 0.0, 1.0);
#endif
vec3 envEffect = texture2D(envMap, envTexCoordGen).xyz * envMapColor.xyz * envLuma;
#if @glossMap
envEffect *= texture2D(glossMap, glossMapUV).xyz;
#endif
#if @preLightEnv
gl_FragData[0].xyz += envEffect;
#endif
#endif
2022-06-21 20:28:17 +00:00
float shadowing = unshadowedLightRatio(-passViewPos.z);
2020-12-19 17:17:42 +00:00
vec3 lighting;
2016-02-17 22:39:06 +00:00
#if !PER_PIXEL_LIGHTING
2020-12-19 17:17:42 +00:00
lighting = passLighting + shadowDiffuseLighting * shadowing;
#else
2020-12-19 17:17:42 +00:00
vec3 diffuseLight, ambientLight;
2023-01-19 16:39:38 +00:00
doLighting(passViewPos, viewNormal, shadowing, diffuseLight, ambientLight);
2020-12-16 21:46:09 +00:00
vec3 emission = getEmissionColor().xyz * emissiveMult;
lighting = diffuseColor.xyz * diffuseLight + getAmbientColor().xyz * ambientLight + emission;
2016-02-17 22:39:06 +00:00
#endif
2016-02-16 22:30:23 +00:00
clampLightingResult(lighting);
2020-12-19 17:17:42 +00:00
gl_FragData[0].xyz *= lighting;
#if @envMap && !@preLightEnv
gl_FragData[0].xyz += envEffect;
2016-02-18 22:05:44 +00:00
#endif
#if @emissiveMap
gl_FragData[0].xyz += texture2D(emissiveMap, emissiveMapUV).xyz;
#endif
2016-02-20 18:02:11 +00:00
#if @specularMap
vec4 specTex = texture2D(specularMap, specularMapUV);
2020-05-08 13:55:22 +00:00
float shininess = specTex.a * 255.0;
2016-02-20 18:02:11 +00:00
vec3 matSpec = specTex.xyz;
#else
float shininess = gl_FrontMaterial.shininess;
2020-12-19 17:17:42 +00:00
vec3 matSpec = getSpecularColor().xyz;
2016-02-20 18:02:11 +00:00
#endif
2021-11-10 16:58:06 +00:00
matSpec *= specStrength;
if (matSpec != vec3(0.0))
2020-06-15 08:43:51 +00:00
{
2023-01-19 16:39:38 +00:00
gl_FragData[0].xyz += getSpecular(viewNormal, viewVec, shininess, matSpec) * shadowing;
2020-06-15 08:43:51 +00:00
}
2022-06-06 20:40:38 +00:00
2023-02-25 19:03:39 +00:00
gl_FragData[0] = applyFogAtPos(gl_FragData[0], passViewPos, far);
2018-06-28 16:24:36 +00:00
2023-02-25 19:03:39 +00:00
vec2 screenCoords = gl_FragCoord.xy / screenRes;
#if !defined(FORCE_OPAQUE) && @softParticles
2023-02-25 19:03:39 +00:00
gl_FragData[0].a *= calcSoftParticleFade(
viewVec,
passViewPos,
viewNormal,
near,
far,
texture2D(opaqueDepthTex, screenCoords).x,
particleSize,
particleFade
);
2021-10-20 16:42:18 +00:00
#endif
#if defined(FORCE_OPAQUE) && FORCE_OPAQUE
// having testing & blending isn't enough - we need to write an opaque pixel to be opaque
gl_FragData[0].a = 1.0;
#endif
2022-05-14 01:58:00 +00:00
#if !defined(FORCE_OPAQUE) && !@disableNormals
2023-01-19 16:39:38 +00:00
gl_FragData[1].xyz = viewNormal * 0.5 + 0.5;
2022-05-14 01:58:00 +00:00
#endif
2018-06-28 16:24:36 +00:00
applyShadowDebugOverlay();
}