Files
yacreader/common/rhi/shaders/flow.frag

62 lines
2.0 KiB
GLSL

#version 450
// Inputs from vertex shader
layout(location = 0) in vec2 vTexCoord;
layout(location = 1) in flat vec4 vInstanceShading;
layout(location = 2) in flat float vInstanceOpacity;
layout(location = 3) in flat float vIsReflection;
layout(location = 4) in flat float vInstanceRotation;
layout(location = 5) in vec2 vLocalPos;
// Output
layout(location = 0) out vec4 fragColor;
// Uniform buffer
layout(std140, binding = 0) uniform UniformBuffer
{
mat4 viewProjectionMatrix;
vec3 backgroundColor;
float _pad0;
vec3 shadingColor;
float _pad1;
float reflectionUp;
float reflectionDown;
int isReflection;
float _pad2;
};
// Texture and sampler
layout(binding = 1) uniform sampler2D coverTexture;
void main()
{
vec4 texColor = texture(coverTexture, vTexCoord);
// Compute per-pixel shading using per-instance corner shading values
float leftUp = vInstanceShading.x;
float leftDown = vInstanceShading.y;
float rightUp = vInstanceShading.z;
float rightDown = vInstanceShading.w;
// Use interpolated local vertex position to match original vertex-based shading.
// position.y in vertex ranges [-0.5..0.5], so map to [0..1] with +0.5
float y = clamp(vLocalPos.y + 0.5, 0.0, 1.0);
float x = clamp(vLocalPos.x + 0.5, 0.0, 1.0);
float leftShading = mix(leftDown, leftUp, y);
float rightShading = mix(rightDown, rightUp, y);
float shading = mix(leftShading, rightShading, x);
// Combine with per-instance opacity (opacity only darkens RGB, alpha preserved)
float shadingAmount = shading * vInstanceOpacity;
// For reflections, apply a gradient fade using reflection uniforms (darker further away)
if (vIsReflection > 0.5) {
// Legacy reflection fade: ramp from 0.0 to 0.33 using texture V coordinate
float gradientFade = mix(0.0, 0.33, vTexCoord.y);
shadingAmount *= gradientFade;
}
// Final color: shaded RGB, keep source alpha
fragColor = vec4(texColor.rgb * shadingAmount, texColor.a);
}