2016-02-16 18:18:48 +01:00
|
|
|
#version 120
|
2023-11-10 08:02:53 -08:00
|
|
|
#pragma import_defines(FORCE_OPAQUE, DISTORTION)
|
2016-02-16 18:18:48 +01:00
|
|
|
|
2021-03-14 21:42:34 -07:00
|
|
|
#if @useUBO
|
|
|
|
#extension GL_ARB_uniform_buffer_object : require
|
|
|
|
#endif
|
|
|
|
|
2021-01-07 18:13:51 +00:00
|
|
|
#if @useGPUShader4
|
2021-01-08 17:32:15 +00:00
|
|
|
#extension GL_EXT_gpu_shader4: require
|
2021-01-07 18:13:51 +00:00
|
|
|
#endif
|
2021-02-21 10:38:15 -08:00
|
|
|
|
2016-02-16 18:18:48 +01:00
|
|
|
#if @diffuseMap
|
|
|
|
uniform sampler2D diffuseMap;
|
|
|
|
varying vec2 diffuseMapUV;
|
|
|
|
#endif
|
|
|
|
|
2016-02-17 02:52:44 +01:00
|
|
|
#if @darkMap
|
|
|
|
uniform sampler2D darkMap;
|
|
|
|
varying vec2 darkMapUV;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if @detailMap
|
|
|
|
uniform sampler2D detailMap;
|
|
|
|
varying vec2 detailMapUV;
|
|
|
|
#endif
|
|
|
|
|
2016-03-01 18:41:36 +01:00
|
|
|
#if @decalMap
|
|
|
|
uniform sampler2D decalMap;
|
|
|
|
varying vec2 decalMapUV;
|
|
|
|
#endif
|
|
|
|
|
2016-02-17 02:52:44 +01:00
|
|
|
#if @emissiveMap
|
|
|
|
uniform sampler2D emissiveMap;
|
|
|
|
varying vec2 emissiveMapUV;
|
|
|
|
#endif
|
|
|
|
|
2016-02-18 00:00:12 +01:00
|
|
|
#if @normalMap
|
|
|
|
uniform sampler2D normalMap;
|
|
|
|
varying vec2 normalMapUV;
|
|
|
|
#endif
|
|
|
|
|
2016-02-18 23:05:44 +01:00
|
|
|
#if @envMap
|
|
|
|
uniform sampler2D envMap;
|
|
|
|
varying vec2 envMapUV;
|
|
|
|
uniform vec4 envMapColor;
|
|
|
|
#endif
|
|
|
|
|
2016-02-20 19:02:11 +01:00
|
|
|
#if @specularMap
|
|
|
|
uniform sampler2D specularMap;
|
|
|
|
varying vec2 specularMapUV;
|
|
|
|
#endif
|
|
|
|
|
2020-03-02 04:03:36 +03:00
|
|
|
#if @bumpMap
|
|
|
|
uniform sampler2D bumpMap;
|
|
|
|
varying vec2 bumpMapUV;
|
|
|
|
uniform vec2 envMapLumaBias;
|
|
|
|
uniform mat2 bumpMapMatrix;
|
|
|
|
#endif
|
|
|
|
|
2022-01-08 05:53:10 +03:00
|
|
|
#if @glossMap
|
|
|
|
uniform sampler2D glossMap;
|
|
|
|
varying vec2 glossMapUV;
|
|
|
|
#endif
|
|
|
|
|
2022-06-06 22:40:38 +02:00
|
|
|
uniform vec2 screenRes;
|
2023-02-25 11:03:39 -08:00
|
|
|
uniform float near;
|
|
|
|
uniform float far;
|
|
|
|
uniform float alphaRef;
|
2023-11-10 08:02:53 -08:00
|
|
|
uniform float distortionStrength;
|
2016-02-16 22:32:59 +01:00
|
|
|
|
2023-12-15 10:23:10 +03:00
|
|
|
#define PER_PIXEL_LIGHTING (@normalMap || @specularMap || @forcePPL)
|
2016-02-17 23:39:06 +01:00
|
|
|
|
|
|
|
#if !PER_PIXEL_LIGHTING
|
2020-12-19 20:17:42 +03:00
|
|
|
centroid varying vec3 passLighting;
|
2023-12-15 10:23:10 +03:00
|
|
|
centroid varying vec3 passSpecular;
|
2018-05-11 19:15:04 +01:00
|
|
|
centroid varying vec3 shadowDiffuseLighting;
|
2023-12-15 10:23:10 +03:00
|
|
|
centroid varying vec3 shadowSpecularLighting;
|
2020-12-17 00:46:09 +03:00
|
|
|
#else
|
|
|
|
uniform float emissiveMult;
|
2021-11-10 19:58:06 +03:00
|
|
|
uniform float specStrength;
|
2023-12-15 10:23:10 +03:00
|
|
|
#endif
|
2016-02-20 19:02:11 +01:00
|
|
|
varying vec3 passViewPos;
|
2016-03-22 21:12:16 +01:00
|
|
|
varying vec3 passNormal;
|
2023-12-10 21:45:15 +03:00
|
|
|
#if @normalMap || @diffuseParallax
|
|
|
|
varying vec4 passTangent;
|
|
|
|
#endif
|
2016-02-17 23:39:06 +01:00
|
|
|
|
2022-06-21 22:28:17 +02:00
|
|
|
#if @additiveBlending
|
|
|
|
#define ADDITIVE_BLENDING
|
|
|
|
#endif
|
|
|
|
|
2023-02-25 11:03:39 -08:00
|
|
|
#include "lib/light/lighting.glsl"
|
|
|
|
#include "lib/material/parallax.glsl"
|
|
|
|
#include "lib/material/alpha.glsl"
|
2023-11-10 08:02:53 -08:00
|
|
|
#include "lib/util/distortion.glsl"
|
2023-02-26 14:31:41 -08:00
|
|
|
|
2022-06-06 22:40:38 +02:00
|
|
|
#include "fog.glsl"
|
2023-02-26 14:31:41 -08:00
|
|
|
#include "vertexcolors.glsl"
|
|
|
|
#include "shadows_fragment.glsl"
|
2023-12-10 21:45:15 +03:00
|
|
|
#include "compatibility/normals.glsl"
|
2016-02-16 23:30:23 +01:00
|
|
|
|
2021-10-23 13:46:39 -07:00
|
|
|
#if @softParticles
|
2023-02-25 11:03:39 -08:00
|
|
|
#include "lib/particle/soft.glsl"
|
|
|
|
|
|
|
|
uniform float particleSize;
|
|
|
|
uniform bool particleFade;
|
2023-10-28 10:23:48 -07:00
|
|
|
uniform float softFalloffDepth;
|
2021-10-20 09:42:18 -07:00
|
|
|
#endif
|
|
|
|
|
2023-01-06 22:23:03 -08:00
|
|
|
#if @particleOcclusion
|
2023-02-25 11:03:39 -08:00
|
|
|
#include "lib/particle/occlusion.glsl"
|
2023-01-06 22:23:03 -08:00
|
|
|
uniform sampler2D orthoDepthMap;
|
|
|
|
varying vec3 orthoDepthMapCoord;
|
|
|
|
#endif
|
|
|
|
|
2023-11-10 08:02:53 -08:00
|
|
|
uniform sampler2D opaqueDepthTex;
|
|
|
|
|
2016-02-16 18:18:48 +01:00
|
|
|
void main()
|
|
|
|
{
|
2023-01-06 22:23:03 -08:00
|
|
|
#if @particleOcclusion
|
2023-02-25 11:03:39 -08:00
|
|
|
applyOcclusionDiscard(orthoDepthMapCoord, texture2D(orthoDepthMap, orthoDepthMapCoord.xy * 0.5 + 0.5).r);
|
2023-01-06 22:23:03 -08:00
|
|
|
#endif
|
|
|
|
|
2023-12-10 21:45:15 +03:00
|
|
|
// only offset diffuse and normal maps for now, other textures are more likely to be using a completely different UV set
|
|
|
|
vec2 offset = vec2(0.0);
|
2022-05-13 18:58:00 -07:00
|
|
|
|
2023-12-10 21:45:15 +03:00
|
|
|
#if @parallax || @diffuseParallax
|
2023-11-04 20:02:55 +03:00
|
|
|
#if @parallax
|
2023-12-10 21:45:15 +03:00
|
|
|
float height = texture2D(normalMap, normalMapUV).a;
|
2023-11-04 20:02:55 +03:00
|
|
|
float flipY = (passTangent.w > 0.0) ? -1.f : 1.f;
|
|
|
|
#else
|
|
|
|
float height = texture2D(diffuseMap, diffuseMapUV).a;
|
|
|
|
// FIXME: shouldn't be necessary, but in this path false-positives are common
|
|
|
|
float flipY = -1.f;
|
|
|
|
#endif
|
2023-12-10 21:45:15 +03:00
|
|
|
offset = getParallaxOffset(transpose(normalToViewMatrix) * normalize(-passViewPos), height, flipY);
|
2016-03-22 21:43:07 +01:00
|
|
|
#endif
|
|
|
|
|
2023-11-10 08:02:53 -08:00
|
|
|
vec2 screenCoords = gl_FragCoord.xy / screenRes;
|
|
|
|
|
2023-12-10 21:45:15 +03:00
|
|
|
#if @diffuseMap
|
|
|
|
gl_FragData[0] = texture2D(diffuseMap, diffuseMapUV + offset);
|
2023-11-10 08:02:53 -08:00
|
|
|
|
|
|
|
#if defined(DISTORTION) && DISTORTION
|
2024-07-19 15:58:49 -07:00
|
|
|
gl_FragData[0].a *= getDiffuseColor().a;
|
2023-11-10 08:02:53 -08:00
|
|
|
gl_FragData[0] = applyDistortion(gl_FragData[0], distortionStrength, gl_FragCoord.z, texture2D(opaqueDepthTex, screenCoords / @distorionRTRatio).x);
|
|
|
|
return;
|
|
|
|
#endif
|
|
|
|
|
2023-12-10 21:45:15 +03:00
|
|
|
#if @diffuseParallax
|
2023-11-04 20:02:55 +03:00
|
|
|
gl_FragData[0].a = 1.0;
|
2023-12-10 21:45:15 +03:00
|
|
|
#else
|
|
|
|
gl_FragData[0].a *= coveragePreservingAlphaScale(diffuseMap, diffuseMapUV + offset);
|
2016-02-16 18:18:48 +01:00
|
|
|
#endif
|
2023-12-10 21:45:15 +03:00
|
|
|
#else
|
|
|
|
gl_FragData[0] = vec4(1.0);
|
2023-11-04 20:02:55 +03:00
|
|
|
#endif
|
|
|
|
|
2020-12-27 03:07:04 +00:00
|
|
|
vec4 diffuseColor = getDiffuseColor();
|
|
|
|
gl_FragData[0].a *= diffuseColor.a;
|
2021-07-29 22:28:05 +03:00
|
|
|
|
|
|
|
#if @darkMap
|
|
|
|
gl_FragData[0] *= texture2D(darkMap, darkMapUV);
|
|
|
|
gl_FragData[0].a *= coveragePreservingAlphaScale(darkMap, darkMapUV);
|
|
|
|
#endif
|
|
|
|
|
2023-02-26 14:31:41 -08:00
|
|
|
gl_FragData[0].a = alphaTest(gl_FragData[0].a, alphaRef);
|
2020-12-27 03:07:04 +00:00
|
|
|
|
2023-12-10 21:45:15 +03:00
|
|
|
#if @normalMap
|
2024-03-23 19:46:31 +03:00
|
|
|
vec4 normalTex = texture2D(normalMap, normalMapUV + offset);
|
2024-04-18 11:23:21 +03:00
|
|
|
vec3 normal = normalTex.xyz * 2.0 - 1.0;
|
2024-03-23 19:46:31 +03:00
|
|
|
#if @reconstructNormalZ
|
2024-04-18 11:23:21 +03:00
|
|
|
normal.z = sqrt(1.0 - dot(normal.xy, normal.xy));
|
2024-03-23 19:46:31 +03:00
|
|
|
#endif
|
2024-04-18 11:23:21 +03:00
|
|
|
vec3 viewNormal = normalToView(normal);
|
2023-12-10 21:45:15 +03:00
|
|
|
#else
|
2023-12-30 02:43:38 +03:00
|
|
|
vec3 viewNormal = normalize(gl_NormalMatrix * passNormal);
|
2023-12-10 21:45:15 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
vec3 viewVec = normalize(passViewPos);
|
|
|
|
|
2016-02-17 02:52:44 +01:00
|
|
|
#if @detailMap
|
|
|
|
gl_FragData[0].xyz *= texture2D(detailMap, detailMapUV).xyz * 2.0;
|
|
|
|
#endif
|
|
|
|
|
2016-03-01 18:41:36 +01:00
|
|
|
#if @decalMap
|
|
|
|
vec4 decalTex = texture2D(decalMap, decalMapUV);
|
2021-12-30 17:08:29 -08:00
|
|
|
gl_FragData[0].xyz = mix(gl_FragData[0].xyz, decalTex.xyz, decalTex.a * diffuseColor.a);
|
2020-03-03 20:08:59 +03:00
|
|
|
#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
|
|
|
|
|
2022-01-08 05:53:10 +03:00
|
|
|
vec3 envEffect = texture2D(envMap, envTexCoordGen).xyz * envMapColor.xyz * envLuma;
|
|
|
|
|
|
|
|
#if @glossMap
|
|
|
|
envEffect *= texture2D(glossMap, glossMapUV).xyz;
|
|
|
|
#endif
|
|
|
|
|
2020-03-03 20:08:59 +03:00
|
|
|
#if @preLightEnv
|
2022-01-08 05:53:10 +03:00
|
|
|
gl_FragData[0].xyz += envEffect;
|
2020-03-03 20:08:59 +03:00
|
|
|
#endif
|
|
|
|
|
2016-03-01 18:41:36 +01:00
|
|
|
#endif
|
|
|
|
|
2022-06-21 22:28:17 +02:00
|
|
|
float shadowing = unshadowedLightRatio(-passViewPos.z);
|
2023-12-15 10:23:10 +03:00
|
|
|
vec3 lighting, specular;
|
2016-02-17 23:39:06 +01:00
|
|
|
#if !PER_PIXEL_LIGHTING
|
2020-12-19 20:17:42 +03:00
|
|
|
lighting = passLighting + shadowDiffuseLighting * shadowing;
|
2023-12-15 10:23:10 +03:00
|
|
|
specular = passSpecular + shadowSpecularLighting * shadowing;
|
2019-05-01 11:33:19 +03:00
|
|
|
#else
|
2023-12-15 10:23:10 +03:00
|
|
|
#if @specularMap
|
|
|
|
vec4 specTex = texture2D(specularMap, specularMapUV);
|
|
|
|
float shininess = specTex.a * 255.0;
|
|
|
|
vec3 specularColor = specTex.xyz;
|
|
|
|
#else
|
|
|
|
float shininess = gl_FrontMaterial.shininess;
|
|
|
|
vec3 specularColor = getSpecularColor().xyz;
|
|
|
|
#endif
|
|
|
|
vec3 diffuseLight, ambientLight, specularLight;
|
|
|
|
doLighting(passViewPos, viewNormal, shininess, shadowing, diffuseLight, ambientLight, specularLight);
|
|
|
|
lighting = diffuseColor.xyz * diffuseLight + getAmbientColor().xyz * ambientLight + getEmissionColor().xyz * emissiveMult;
|
|
|
|
specular = specularColor * specularLight * specStrength;
|
2016-02-17 23:39:06 +01:00
|
|
|
#endif
|
2016-02-16 23:30:23 +01:00
|
|
|
|
2022-04-28 20:02:13 -07:00
|
|
|
clampLightingResult(lighting);
|
2023-12-15 10:23:10 +03:00
|
|
|
gl_FragData[0].xyz = gl_FragData[0].xyz * lighting + specular;
|
2020-12-19 20:17:42 +03:00
|
|
|
|
2020-03-03 20:08:59 +03:00
|
|
|
#if @envMap && !@preLightEnv
|
2022-01-08 05:53:10 +03:00
|
|
|
gl_FragData[0].xyz += envEffect;
|
2016-02-18 23:05:44 +01:00
|
|
|
#endif
|
|
|
|
|
2020-03-03 20:08:59 +03:00
|
|
|
#if @emissiveMap
|
|
|
|
gl_FragData[0].xyz += texture2D(emissiveMap, emissiveMapUV).xyz;
|
2016-02-24 21:28:55 +01:00
|
|
|
#endif
|
|
|
|
|
2023-02-25 11:03:39 -08:00
|
|
|
gl_FragData[0] = applyFogAtPos(gl_FragData[0], passViewPos, far);
|
2018-06-28 17:24:36 +01:00
|
|
|
|
2021-11-20 18:39:20 -08:00
|
|
|
#if !defined(FORCE_OPAQUE) && @softParticles
|
2023-02-25 11:03:39 -08:00
|
|
|
gl_FragData[0].a *= calcSoftParticleFade(
|
|
|
|
viewVec,
|
|
|
|
passViewPos,
|
|
|
|
viewNormal,
|
|
|
|
near,
|
|
|
|
far,
|
|
|
|
texture2D(opaqueDepthTex, screenCoords).x,
|
|
|
|
particleSize,
|
2023-10-28 10:23:48 -07:00
|
|
|
particleFade,
|
|
|
|
softFalloffDepth
|
2023-02-25 11:03:39 -08:00
|
|
|
);
|
2021-10-20 09:42:18 -07:00
|
|
|
#endif
|
|
|
|
|
2021-10-06 08:05:10 +02:00
|
|
|
#if defined(FORCE_OPAQUE) && FORCE_OPAQUE
|
2021-01-20 23:37:19 +00:00
|
|
|
// having testing & blending isn't enough - we need to write an opaque pixel to be opaque
|
2021-10-05 12:37:08 +00:00
|
|
|
gl_FragData[0].a = 1.0;
|
2021-01-20 23:37:19 +00:00
|
|
|
#endif
|
2021-01-20 01:17:16 +00:00
|
|
|
|
2022-05-13 18:58:00 -07:00
|
|
|
#if !defined(FORCE_OPAQUE) && !@disableNormals
|
2023-01-19 08:39:38 -08:00
|
|
|
gl_FragData[1].xyz = viewNormal * 0.5 + 0.5;
|
2022-05-13 18:58:00 -07:00
|
|
|
#endif
|
|
|
|
|
2018-06-28 17:24:36 +01:00
|
|
|
applyShadowDebugOverlay();
|
2016-02-16 18:18:48 +01:00
|
|
|
}
|