When you code a shader on a standard widescreen display (like a 1920×1080 monitor), your screen is wider than it is tall. If you calculate distances using raw normalized uv coordinates without aspect correction, your circles will turn into stretched ovals.
Here is why this happens and how a simple fix using u_resolution keeps your geometry perfectly proportional.
1. The Problem: The Non-Square Pixel Trap
When you write:
OpenGL Shading Language
vec2 uv = fragCoord / u_resolution.xy;
You are dividing the horizontal pixel position by 1920 and the vertical pixel position by 1080.
- The X-axis spans
1920units of data over a physical distance (e.g., 24 inches). - The Y-axis spans only
1080units of data over the exact same physical distance.
This means one unit on the X-axis is physically wider than one unit on the Y-axis. When length(p) calculates distance ($\sqrt{x^2 + y^2}$), a radius of 0.35 stretches horizontally because it takes more physical pixels to travel 0.35 units left-to-right than it does bottom-to-top. Your circle gets squashed into an ellipse!
2. The Solution: Aspect Ratio Correction
To fix this, you must normalize the coordinate space so that 1 unit in X equals 1 unit in Y physically.
The standard approach is to multiply the horizontal coordinate (uv.x) by the screen’s aspect ratio (u_resolution.x / u_resolution.y), or scale relative to the screen height (u_resolution.y).
Step-by-Step Code Adjustment:
OpenGL Shading Language
Alternative Approach: Centering & Scaling by Height (u_resolution.y)
Many graphics developers prefer centering coordinates around (0.0, 0.0) first and dividing by u_resolution.y directly. This maps the vertical height strictly from -0.5 to 0.5, while the width automatically expands based on the monitor’s aspect ratio:
OpenGL Shading Language
Summary Rule of Thumb
- If your shape looks like an egg or oval on a widescreen monitor, your horizontal coordinates are moving faster than your vertical coordinates.
- Always scale your X-axis by the aspect ratio (
u_resolution.x / u_resolution.y) to ensure your math operates on true, uniform physical units!