Files
blitzkit/packages/website/src/components/PrimaryArmorSceneComponent/shaders/fragment.glsl
T

121 lines
4.0 KiB
GLSL
Raw Normal View History

2024-02-27 11:52:36 -06:00
varying vec3 vNormal;
varying vec3 vViewPosition;
2025-11-27 13:42:37 -06:00
uniform float opacity;
2026-11-29 21:57:28 -06:00
uniform float thicknessCoefficient;
varying float vThickness;
2024-02-27 11:52:36 -06:00
uniform float penetration;
uniform float caliber;
uniform float ricochet;
uniform float normalization;
uniform vec2 resolution;
2024-02-27 17:15:24 -06:00
uniform bool isExplosive;
2024-02-27 17:21:41 -06:00
uniform bool canSplash;
uniform float damage;
uniform float explosionRadius;
2024-02-27 17:36:05 -06:00
uniform bool greenPenetration;
2024-12-15 18:13:23 -06:00
uniform bool advancedHighlighting;
2024-02-27 17:44:18 -06:00
uniform bool opaque;
2024-03-30 11:31:44 -05:00
uniform mat4 inverseProjectionMatrix;
2024-02-27 11:52:36 -06:00
2024-02-27 17:15:24 -06:00
uniform highp sampler2D spacedArmorBuffer;
2024-02-27 11:52:36 -06:00
uniform highp sampler2D spacedArmorDepth;
2024-12-15 18:21:03 -06:00
const float EXPLOSION_DAMAGE_FACTOR = 1.1;
const float RANDOMIZATION_FACTOR = 0.05;
const float GREEN_VALUE = 0.392;
2024-12-15 18:26:34 -06:00
float getDistance(vec2 coord, float depth) {
vec4 clipPos = vec4(coord * 2.0 - 1.0, depth * 2.0 - 1.0, 1.0);
vec4 eyePos = inverseProjectionMatrix * clipPos;
return length(eyePos.xyz / eyePos.w);
}
2024-12-22 20:21:23 -06:00
vec3 getPenetrationColor(bool isThreeCalibersRule, bool couldHaveRicochet) {
if (advancedHighlighting && couldHaveRicochet) {
return vec3(0.0, 1.0, isThreeCalibersRule ? 1.0 : 0.0);
2024-12-15 18:33:15 -06:00
}
return vec3(0.0, 1.0, 0.0);
}
2024-02-27 11:52:36 -06:00
void main() {
2025-04-14 18:52:49 -05:00
#include <clipping_planes_fragment>
2026-11-29 21:57:28 -06:00
float thickness = vThickness * thicknessCoefficient;
2024-12-15 18:33:15 -06:00
vec2 screenCoord = gl_FragCoord.xy / resolution;
vec4 spacedArmorData = texture2D(spacedArmorBuffer, screenCoord);
bool isUnderSpacedArmor = spacedArmorData.a != 0.0;
2024-02-27 11:52:36 -06:00
2024-12-15 18:33:15 -06:00
float viewDistance = length(vViewPosition);
float angle = acos(dot(vNormal, -vViewPosition) / viewDistance);
2024-02-27 11:52:36 -06:00
2024-02-28 17:41:59 -06:00
bool threeCalibersRule = caliber > thickness * 3.0 || isUnderSpacedArmor;
2024-12-22 20:21:23 -06:00
bool couldHaveRicochet = angle >= ricochet;
2024-12-15 18:33:15 -06:00
float penetrationChance = -1.0;
float splashChance = 0.0;
bool didRicochet = false;
2024-12-15 18:13:23 -06:00
2024-12-22 20:21:23 -06:00
if (!threeCalibersRule && couldHaveRicochet) {
2024-02-27 11:52:36 -06:00
penetrationChance = 0.0;
2024-12-15 18:13:23 -06:00
didRicochet = true;
2024-02-27 11:52:36 -06:00
} else {
2024-12-15 18:33:15 -06:00
float thicknessRatio = thickness > 0.0 ? caliber / thickness : 0.0;
bool twoCalibersRule = thicknessRatio > 2.0;
float finalNorm = twoCalibersRule ? ((1.4 * normalization * caliber) / (2.0 * thickness)) : normalization;
float finalThickness = thickness / cos(max(0.0, angle - finalNorm));
2024-02-27 13:50:15 -06:00
2024-12-15 18:33:15 -06:00
float remainingPen = penetration;
2024-02-27 17:21:41 -06:00
2024-12-15 18:33:15 -06:00
if (isUnderSpacedArmor) {
float spacedArmorThickness = spacedArmorData.r * penetration;
remainingPen -= spacedArmorThickness;
2024-02-27 17:15:24 -06:00
2024-12-15 18:33:15 -06:00
if (isExplosive && remainingPen > 0.0) {
float spacedDist = getDistance(screenCoord, texture2D(spacedArmorDepth, screenCoord).r);
float primaryDist = getDistance(screenCoord, gl_FragCoord.z);
float distFromArmor = primaryDist - spacedDist;
2024-03-30 11:28:34 -05:00
2024-12-15 18:33:15 -06:00
if (canSplash) {
2025-08-06 12:33:50 -05:00
float finalDamage = 0.5 * damage * (1.0 - distFromArmor / explosionRadius) - EXPLOSION_DAMAGE_FACTOR * (finalThickness + spacedArmorThickness);
2024-12-15 18:33:15 -06:00
splashChance = step(0.0, finalDamage);
penetrationChance = 0.0;
2024-03-30 11:28:34 -05:00
2024-12-15 18:33:15 -06:00
} else {
2025-08-06 12:33:50 -05:00
remainingPen -= 0.5 * remainingPen * distFromArmor;
2024-03-30 11:28:34 -05:00
}
}
2024-12-15 18:33:15 -06:00
}
2024-03-30 11:28:34 -05:00
2024-12-15 18:33:15 -06:00
if (penetrationChance < 0.0) {
remainingPen = max(0.0, remainingPen);
float delta = finalThickness - remainingPen;
float randomization = remainingPen * RANDOMIZATION_FACTOR;
2024-03-30 11:28:34 -05:00
penetrationChance = clamp(1.0 - (delta + randomization) / (2.0 * randomization), 0.0, 1.0);
if (canSplash) {
2025-08-06 12:33:50 -05:00
float splashDamage = 0.5 * damage - EXPLOSION_DAMAGE_FACTOR * finalThickness;
2024-12-15 18:33:15 -06:00
splashChance = step(0.0, splashDamage);
2024-03-30 11:28:34 -05:00
}
2024-02-27 17:21:41 -06:00
}
2024-02-27 11:52:36 -06:00
}
2025-08-06 12:33:50 -05:00
float alpha = opaque ? 1.0 : 0.5;
2024-12-15 18:33:15 -06:00
vec3 baseColor = vec3(1.0, splashChance * GREEN_VALUE, 0.0);
2024-12-15 18:13:23 -06:00
if (advancedHighlighting && didRicochet) {
baseColor = vec3(1.0, baseColor.g, 1.0);
2024-12-15 18:13:23 -06:00
}
if (greenPenetration || advancedHighlighting) {
2024-12-15 18:33:15 -06:00
float falling = -penetrationChance * penetrationChance + 1.0;
float gaining = -(penetrationChance - 1.0) * (penetrationChance - 1.0) + 1.0;
2024-12-22 20:21:23 -06:00
vec3 penColor = getPenetrationColor(threeCalibersRule, couldHaveRicochet);
2024-12-15 18:33:15 -06:00
gl_FragColor = vec4(falling * baseColor + gaining * penColor, alpha);
2024-02-27 17:36:05 -06:00
} else {
2024-12-15 18:33:15 -06:00
gl_FragColor = vec4(baseColor, (1.0 - penetrationChance) * alpha);
2024-02-27 17:36:05 -06:00
}
2025-04-14 18:52:49 -05:00
2025-11-27 13:42:37 -06:00
gl_FragColor.a *= opacity;
2024-02-27 11:52:36 -06:00
}