Files
blitzkit/src/components/ArmorMesh/shaders/fragment.glsl
T

45 lines
1.6 KiB
GLSL
Raw Normal View History

2024-01-10 17:29:49 -06:00
// split shaders due to https://github.com/FarazzShaikh/THREE-CustomShaderMaterial/issues/48
varying vec3 vCSMViewPosition;
2024-01-10 14:56:33 -06:00
uniform float thickness;
uniform float penetration;
uniform float ricochet;
uniform float caliber;
uniform float normalization;
void main() {
vec3 normalizedNormal = normalize(vNormal);
2024-01-10 17:29:49 -06:00
vec3 normalizedViewPosition = normalize(vCSMViewPosition);
2024-01-10 14:56:33 -06:00
float cosAngle = dot(normalizedNormal, -normalizedViewPosition);
float angle = acos(cosAngle);
float penetrationChance = 0.0;
bool twoCaliberRule = caliber > 2.0 * thickness;
bool threeCaliberRule = caliber > 3.0 * thickness;
float finalNormalization = twoCaliberRule ? (normalization * 1.4 * caliber) / (2.0 * thickness) : normalization;
2024-01-10 15:27:14 -06:00
float finalPenetration = penetration;
2024-01-10 14:56:33 -06:00
float effectiveArmor = thickness / cos(angle - finalNormalization);
2024-01-10 17:29:49 -06:00
if (angle < ricochet) {
2024-01-10 14:56:33 -06:00
// no auto-ricochet, resume checks
2024-01-10 17:29:49 -06:00
if (threeCaliberRule) {
2024-01-10 14:56:33 -06:00
penetrationChance = 1.0;
2024-01-10 15:27:14 -06:00
} else {
float maxRandomPenetrationBuff = finalPenetration * 0.05;
float penetrationDifference = effectiveArmor - finalPenetration;
2024-01-10 17:29:49 -06:00
if (penetrationDifference > maxRandomPenetrationBuff) {
2024-01-10 15:27:14 -06:00
// even with a +5% buff, can't penetrate
penetrationChance = 0.0;
2024-01-10 17:29:49 -06:00
} else if (penetrationDifference < -maxRandomPenetrationBuff) {
2024-01-10 15:27:14 -06:00
// event with a -5% nerf, can penetrate
penetrationChance = 1.0;
} else {
// penetration gradiant
penetrationChance = 1.0 - (penetrationDifference + maxRandomPenetrationBuff) / (2.0 * maxRandomPenetrationBuff);
}
2024-01-10 14:56:33 -06:00
}
}
2024-01-10 17:29:49 -06:00
csm_FragColor = vec4(1.0, 0.0, 0.0, (1.0 - penetrationChance) * 0.5);
2024-01-10 14:56:33 -06:00
}