|
| 1 | +#version 300 es |
| 2 | + |
| 3 | +precision highp float; |
| 4 | + |
| 5 | +in vec2 v_texcoord; |
| 6 | + |
| 7 | +uniform sampler2D tex1; // Start texture (T1) |
| 8 | +uniform sampler2D tex2; // End texture (T2) |
| 9 | +uniform float progress; // 0.0 = fully T1, 1.0 = fully T2 |
| 10 | +uniform float alpha; |
| 11 | + |
| 12 | +uniform vec2 topLeft; |
| 13 | +uniform vec2 fullSize; |
| 14 | +uniform float radius; |
| 15 | + |
| 16 | +uniform vec2 tex1Size; |
| 17 | +uniform vec2 tex2Size; |
| 18 | +uniform int fitMode; |
| 19 | +uniform vec2 randomPixel; // Circle center (0-1 normalized) |
| 20 | +uniform float u_duration; // Host transition duration in seconds (from hyprland config) |
| 21 | + |
| 22 | +// Shader-defined visual duration override: the circle effect completes in 0.5s |
| 23 | +// regardless of the host's transition duration. The host's u_duration is used |
| 24 | +// to remap progress so the effect fits within this window. |
| 25 | +const float SHADER_DURATION = 0.5; |
| 26 | + |
| 27 | +layout(location = 0) out vec4 fragColor; |
| 28 | + |
| 29 | +vec2 containUV(vec2 uv, vec2 texSize, vec2 boxSize) { |
| 30 | + float boxAspect = boxSize.x / boxSize.y; |
| 31 | + float texAspect = texSize.x / texSize.y; |
| 32 | + |
| 33 | + vec2 scale; |
| 34 | + vec2 offset = vec2(0.0); |
| 35 | + |
| 36 | + if (texAspect > boxAspect) { |
| 37 | + scale.x = 1.0; |
| 38 | + scale.y = boxAspect / texAspect; |
| 39 | + offset.y = (1.0 - scale.y) * 0.5; |
| 40 | + } else { |
| 41 | + scale.x = texAspect / boxAspect; |
| 42 | + scale.y = 1.0; |
| 43 | + offset.x = (1.0 - scale.x) * 0.5; |
| 44 | + } |
| 45 | + |
| 46 | + return (uv - offset) / scale; |
| 47 | +} |
| 48 | + |
| 49 | +vec2 coverUV(vec2 uv, vec2 texSize, vec2 boxSize) { |
| 50 | + float boxAspect = boxSize.x / boxSize.y; |
| 51 | + float texAspect = texSize.x / texSize.y; |
| 52 | + |
| 53 | + vec2 scale; |
| 54 | + vec2 offset = vec2(0.0); |
| 55 | + |
| 56 | + if (texAspect > boxAspect) { |
| 57 | + scale.x = boxAspect / texAspect; |
| 58 | + scale.y = 1.0; |
| 59 | + offset.x = (1.0 - scale.x) * 0.5; |
| 60 | + } else { |
| 61 | + scale.x = 1.0; |
| 62 | + scale.y = texAspect / boxAspect; |
| 63 | + offset.y = (1.0 - scale.y) * 0.5; |
| 64 | + } |
| 65 | + |
| 66 | + return (uv - offset) / scale; |
| 67 | +} |
| 68 | + |
| 69 | +void main() { |
| 70 | + vec2 uv = v_texcoord; |
| 71 | + |
| 72 | + // Remap progress so the effect completes in SHADER_DURATION seconds. |
| 73 | + // progress goes 0->1 over u_duration (host config). We compress it so the |
| 74 | + // effect finishes at SHADER_DURATION, then holds at the final state. |
| 75 | + float p = (u_duration > 0.0) |
| 76 | + ? clamp(progress * (u_duration / SHADER_DURATION), 0.0, 1.0) |
| 77 | + : progress; |
| 78 | + |
| 79 | + // Apply UV adjustment based on fit mode |
| 80 | + vec2 uv1 = uv; |
| 81 | + vec2 uv2 = uv; |
| 82 | + |
| 83 | + if (fitMode == 1) { // cover |
| 84 | + uv1 = coverUV(uv, tex1Size, fullSize); |
| 85 | + uv2 = coverUV(uv, tex2Size, fullSize); |
| 86 | + } else if (fitMode == 2) { // contain |
| 87 | + uv1 = containUV(uv, tex1Size, fullSize); |
| 88 | + uv2 = containUV(uv, tex2Size, fullSize); |
| 89 | + } |
| 90 | + |
| 91 | + // Sample both textures |
| 92 | + vec4 startColor = texture(tex1, uv1); |
| 93 | + vec4 endColor = texture(tex2, uv2); |
| 94 | + |
| 95 | + // For contain mode, make out-of-bounds pixels black |
| 96 | + if (fitMode == 2) { |
| 97 | + if (uv1.x < 0.0 || uv1.x > 1.0 || uv1.y < 0.0 || uv1.y > 1.0) |
| 98 | + startColor = vec4(0.0, 0.0, 0.0, 1.0); |
| 99 | + if (uv2.x < 0.0 || uv2.x > 1.0 || uv2.y < 0.0 || uv2.y > 1.0) |
| 100 | + endColor = vec4(0.0, 0.0, 0.0, 1.0); |
| 101 | + } |
| 102 | + |
| 103 | + // Circle reveal effect |
| 104 | + // randomPixel is 0-1 normalized, use as circle center |
| 105 | + vec2 center = randomPixel; |
| 106 | + |
| 107 | + // Convert to pixel space for aspect-ratio correct circle |
| 108 | + vec2 pixelPos = uv * fullSize; |
| 109 | + vec2 centerPixel = center * fullSize; |
| 110 | + |
| 111 | + // Euclidean distance in pixel space (aspect-ratio correct) |
| 112 | + float dist = distance(pixelPos, centerPixel); |
| 113 | + |
| 114 | + // Calculate max distance (farthest corner from center in pixel space) |
| 115 | + float maxDist = 0.0; |
| 116 | + maxDist = max(maxDist, distance(vec2(0.0, 0.0), centerPixel)); |
| 117 | + maxDist = max(maxDist, distance(vec2(fullSize.x, 0.0), centerPixel)); |
| 118 | + maxDist = max(maxDist, distance(vec2(0.0, fullSize.y), centerPixel)); |
| 119 | + maxDist = max(maxDist, distance(fullSize, centerPixel)); |
| 120 | + |
| 121 | + // Normalize distance (0 at center, 1 at farthest corner) |
| 122 | + float normalizedDist = dist / maxDist; |
| 123 | + |
| 124 | + // Circle mask: 0 = show start, 1 = show end |
| 125 | + // Smooth edge for anti-aliasing |
| 126 | + float edgeWidth = 0.02; |
| 127 | + float circleMask = smoothstep(p - edgeWidth, p + edgeWidth, normalizedDist); |
| 128 | + |
| 129 | + // Mix: where normalizedDist < progress, show endColor (new) |
| 130 | + vec4 blended = mix(endColor, startColor, circleMask); |
| 131 | + |
| 132 | + // Apply alpha |
| 133 | + fragColor = blended * alpha; |
| 134 | +} |
0 commit comments