Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions Retna3D
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// GLSL Fragment Shader Pseudocode

uniform sampler2D u_image; // The captured camera texture
varying vec2 v_texCoord; // Current texture coordinate (0.0 to 1.0)
Comment on lines +3 to +4

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

The shader uses texture_width_pixels on line 11, but it's not declared as a uniform. This will cause a compilation error. You should declare it as a uniform to pass the texture's width from your application code.

uniform sampler2D u_image; // The captured camera texture
uniform float texture_width_pixels; // The width of the texture in pixels
varying vec2 v_texCoord;   // Current texture coordinate (0.0 to 1.0)


void main() {
// Define the peripheral zone (bottom 50% of the image)
float peripheral_y_boundary = 0.5;

// Define the maximum shift amount (0.88 pixels converted to texture coordinate units)
float max_shift_coord = 0.88 / texture_width_pixels;

// Check if the current pixel is in the peripheral zone (bottom)
if (v_texCoord.y > peripheral_y_boundary) {

// Calculate a shift amount that smoothly ramps down to 0 at the boundary
// and is maximum at the bottom edge (y=1.0)
float ramp_factor = (v_texCoord.y - peripheral_y_boundary) * 2.0;
float current_shift_x = max_shift_coord * ramp_factor;

// Apply the shift to the horizontal coordinate (x)
vec2 shifted_coord = vec2(v_texCoord.x + current_shift_x, v_texCoord.y);

// Fetch the color from the shifted coordinate
gl_FragColor = texture2D(u_image, shifted_coord);
} else {
// Top 50% (Clear Top) remains unshifted
gl_FragColor = texture2D(u_image, v_texCoord);
}
Comment on lines +13 to +29

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

This block of code can be refactored for better maintainability and robustness. Here are a few points:

  • Code Duplication: The texture2D call is repeated in both the if and else branches. You can avoid this by using a variable for the texture coordinate and having a single texture2D call at the end.
  • Magic Number: The value 2.0 on line 18 is a magic number. It's derived from peripheral_y_boundary being 0.5. Calculating it dynamically makes the code more robust if peripheral_y_boundary changes.
  • Edge Artifacts: The shifted x-coordinate on line 22 can go beyond 1.0, which may cause texture wrapping artifacts. It's safer to clamp the coordinate to the [0.0, 1.0] range.

The suggested code below addresses these points.

    // By default, we sample from the original texture coordinate.
    vec2 sample_coord = v_texCoord;

    // Check if the current pixel is in the peripheral zone (bottom)
    if (v_texCoord.y > peripheral_y_boundary) {
        
        // Calculate a shift amount that smoothly ramps down to 0 at the boundary
        // and is maximum at the bottom edge (y=1.0)
        float ramp_factor = (v_texCoord.y - peripheral_y_boundary) / (1.0 - peripheral_y_boundary); 
        float current_shift_x = max_shift_coord * ramp_factor;

        // Apply the shift to the horizontal coordinate (x), clamping to avoid artifacts.
        sample_coord.x = clamp(v_texCoord.x + current_shift_x, 0.0, 1.0);
    }

    // Fetch the color from the (potentially shifted) coordinate.
    gl_FragColor = texture2D(u_image, sample_coord);

}