From a99b6f222f319eebcc5b42bf036c072f0fa9f9c2 Mon Sep 17 00:00:00 2001 From: Ole-Morten Duesund Date: Thu, 29 Jan 2026 16:53:34 +0100 Subject: [PATCH] Fix coordinate transformation in shader for position and rotation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Transform screen coordinates to texture coordinates (90° CW rotation): - Position: (x,y) -> (y, 1-x) - Angle: θ -> θ + 90° Applied in linearFocusDistance, radialFocusDistance, and sampleBlurred to fix preview not matching UI overlay position and rotation. Co-Authored-By: Claude Opus 4.5 --- app/src/main/res/raw/tiltshift_fragment.glsl | 22 +++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/app/src/main/res/raw/tiltshift_fragment.glsl b/app/src/main/res/raw/tiltshift_fragment.glsl index 72a6a30..e05ee2c 100644 --- a/app/src/main/res/raw/tiltshift_fragment.glsl +++ b/app/src/main/res/raw/tiltshift_fragment.glsl @@ -24,11 +24,14 @@ varying vec2 vTexCoord; // Calculate signed distance from the focus region for LINEAR mode float linearFocusDistance(vec2 uv) { // Center point of the focus region - vec2 center = vec2(uPositionX, uPositionY); + // Transform from screen coordinates to texture coordinates (90° rotation) + // Screen (x,y) -> Texture (y, 1-x) + vec2 center = vec2(uPositionY, 1.0 - uPositionX); vec2 offset = uv - center; - // Adjust angle by -90 degrees to compensate for portrait texture rotation - float adjustedAngle = uAngle - 1.5707963; + // Adjust angle by +90 degrees to compensate for the coordinate transformation + // The position transform is a 90° CW rotation, so angles transform as θ + 90° + float adjustedAngle = uAngle + 1.5707963; float cosA = cos(adjustedAngle); float sinA = sin(adjustedAngle); @@ -41,7 +44,9 @@ float linearFocusDistance(vec2 uv) { // Calculate signed distance from the focus region for RADIAL mode float radialFocusDistance(vec2 uv) { // Center point of the focus region - vec2 center = vec2(uPositionX, uPositionY); + // Transform from screen coordinates to texture coordinates (90° rotation) + // Screen (x,y) -> Texture (y, 1-x) + vec2 center = vec2(uPositionY, 1.0 - uPositionX); vec2 offset = uv - center; // Adjust for aspect ratio to create ellipse @@ -50,7 +55,8 @@ float radialFocusDistance(vec2 uv) { offset.x *= screenAspect; // Apply rotation - float adjustedAngle = uAngle - 1.5707963; + // Adjust angle by +90 degrees to compensate for the coordinate transformation + float adjustedAngle = uAngle + 1.5707963; float cosA = cos(adjustedAngle); float sinA = sin(adjustedAngle); vec2 rotated = vec2( @@ -107,7 +113,8 @@ vec4 sampleBlurred(vec2 uv, float blur) { vec2 blurDir; if (uMode == 1) { // Radial: blur away from center - vec2 center = vec2(uPositionX, uPositionY); + // Transform from screen coordinates to texture coordinates (90° rotation) + vec2 center = vec2(uPositionY, 1.0 - uPositionX); vec2 toCenter = uv - center; float len = length(toCenter); if (len > 0.001) { @@ -117,7 +124,8 @@ vec4 sampleBlurred(vec2 uv, float blur) { } } else { // Linear: blur perpendicular to focus line - float blurAngle = uAngle; + // Adjust angle for coordinate transformation + float blurAngle = uAngle + 1.5707963; blurDir = vec2(cos(blurAngle), sin(blurAngle)); }