uniform sampler2D uTexture; uniform float uGridSize; uniform float uStrength; uniform vec2 uViewOffset; uniform vec2 uResolution; uniform float uTime; uniform vec2 uPetSize; uniform bool uIsVideo; varying vec2 vUv; varying vec3 vWorldPosition; float hash(vec2 p) { return fract(sin(dot(p, vec2(127.1, 311.7))) * 43758.5453); } vec3 srgbToLinear(vec3 value) { return mix(value / 12.92, pow((value + 0.055) / 1.055, vec3(2.4)), step(vec3(0.04045), value)); } vec4 mediaSample(vec2 uv) { vec3 background = srgbToLinear(vec3(17.0, 25.0, 35.0) / 255.0); if (any(lessThan(uv, vec2(0.0))) || any(greaterThan(uv, vec2(1.0)))) return vec4(background, 1.0); vec4 color = texture2D(uTexture, uv); if (uIsVideo) color.rgb = srgbToLinear(color.rgb); return color; } vec2 hitUv(vec3 ray) { float rayZ = ray.z; rayZ = abs(rayZ) < 0.0001 ? (rayZ < 0.0 ? -0.0001 : 0.0001) : rayZ; float signedT = -vWorldPosition.z / rayZ; return (vWorldPosition + ray * signedT).xy / uPetSize + 0.5; } void main() { vec2 grid = vec2(uGridSize, max(1.0, uGridSize * uPetSize.y / uPetSize.x)); vec2 scaled = vUv * grid; vec2 cell = floor(scaled); vec2 local = fract(scaled) - 0.5; float variation = mix(0.85, 1.15, hash(cell)); vec2 lensLocal = local + uViewOffset * (0.12 + 0.08 * hash(cell + vec2(9.0))); float bulge = max(0.0, 0.25 - dot(lensLocal, lensLocal)); vec3 normal = normalize(vec3(-lensLocal * bulge * 1.8 * variation, 1.0)); vec3 incident = normalize(vWorldPosition - cameraPosition); vec3 refracted = refract(incident, normal, 0.75); vec2 straightUv = hitUv(incident); vec2 refractedUv = hitUv(refracted); vec2 uv = mix(straightUv, refractedUv, clamp(uStrength, 0.0, 1.0)); vec4 color = mediaSample(uv); float edge = smoothstep(0.46, 0.5, max(abs(local.x), abs(local.y))); color.rgb *= mix(1.0, 0.75, edge * clamp(uStrength, 0.0, 1.0)); color.a = 1.0; gl_FragColor = color; #include }