ExperimentalGear/shaders/laser.fs

82 lines
1.9 KiB
GLSL

#ifdef EMBEDDED
varying vec2 fsTex;
varying vec4 position;
#else
#extension GL_ARB_separate_shader_objects : enable
layout(location=1) in vec2 fsTex;
layout(location=0) out vec4 target;
in vec4 position;
#endif
uniform sampler2D mainTex;
uniform vec4 color;
uniform float objectGlow;
// 0 = body, 1 = entry, 2 = exit
uniform int laserPart;
// 20Hz flickering. 0 = Miss, 1 = Inactive, 2 & 3 = Active alternating.
uniform int hitState;
const float laserSize = 1.0675;
vec3 rgb2hsv(vec3 c)
{
vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0);
vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g));
vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r));
float d = q.x - min(q.w, q.y);
float e = 1.0e-10;
return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x);
}
vec3 hsv2rgb(vec3 c)
{
vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);
vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www);
return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y);
}
void main() {
if (laserPart == 1) {
target = texture(mainTex, fsTex);
return;
}
float x = fsTex.x;
// if (x < 0.0 || x > 1.0) {
// target = vec4(0.0);
// return;
// }
x -= (laserSize / 2);
x /= laserSize;
x += (laserSize / 2);
float y = 0.25 * ceil(float(hitState)) + 0.01;
vec4 channels = texture(mainTex, vec2(x, y));
vec3 baseColor = color.rgb * channels.g;
vec3 baseHsv = rgb2hsv(baseColor);
float h = baseHsv.x;
float hilightHue = 0.0;
if (h < 2.0 / 12.0)
hilightHue = 1.0 - smoothstep(0, 1.0 / 12.0, h);
else hilightHue = smoothstep(2.0 / 12.0, 6.0 / 12.0, h);
hilightHue = mod((280.0 / 360.0) + (hilightHue * 140.0 / 360.0), 1.0);
vec3 hilightColor = hsv2rgb(vec3(hilightHue, 1, 1));
baseColor = baseColor * (1.0 - channels.b) + vec3(channels.b);
//hilightColor = hilightColor * channels.r;
vec3 mixedColor = clamp(mix(baseColor, hilightColor, channels.r), 0.0, 1.0);
target = vec4(mixedColor, 1);
}