Asked 3 months ago by AstralCaptain167
How can I remove jagged edges from my shader-generated grid pattern despite antialiasing?
The post content has been automatically edited by the Moderator Agent for consistency and clarity.
Asked 3 months ago by AstralCaptain167
The post content has been automatically edited by the Moderator Agent for consistency and clarity.
Hi there,
I am writing a fragment shader to display a grid pattern on a plane, but all the edges of the squares appear jagged even though antialiasing is enabled. I increased the pixelRatio, which improved the result slightly, however, the anti-aliasing isn’t sufficient.
Below is the fragment shader code I am using:
GLSLuniform vec2 uSize; // The size of the plane in meters uniform float uSquareSize; // Square size in meters uniform float uGapSize; // Gap size in meters uniform float uInvert; varying vec2 vUv; void main() { // Calculate total size of one grid cell (square + gap) float gridCellSizeX = uSquareSize + uGapSize; float gridCellSizeY = uSquareSize + uGapSize; // Calculate number of complete grid cells that fit in each dimension float numberOfColumns = floor(uSize.x / gridCellSizeX); float numberOfRows = floor(uSize.y / gridCellSizeY); // Calculate total width and height occupied by these grid cells float totalGridWidth = numberOfColumns * gridCellSizeX; float totalGridHeight = numberOfRows * gridCellSizeY; // Calculate remaining space after fitting complete grid cells float restX = uSize.x - totalGridWidth; float restY = uSize.y - totalGridHeight; // Calculate offsets to center the pattern float offsetX = (restX - uGapSize) / 2.0; float offsetY = (restY - uGapSize) / 2.0; // Adjust UV coordinates to apply centering offsets vec2 adjustedUV = vUv * uSize - vec2(offsetX, offsetY); // Calculate step value for square and gap transitions float stepValueX = uGapSize / gridCellSizeX; float stepValueY = uGapSize / gridCellSizeY; // Determine pattern presence using step function float patternX = step(stepValueX, mod(adjustedUV.x / gridCellSizeX, 1.0)); float patternY = step(stepValueY, mod(adjustedUV.y / gridCellSizeY, 1.0)); // Combine patterns for both dimensions float pattern = patternX * patternY; // Output color based on pattern pattern = mix(pattern, 1.0 - pattern, uInvert); //keep result between 0 and 0.95 pattern = clamp(pattern, 0.0, 0.95); gl_FragColor = vec4(vec3(0.9), pattern); }
I have also included a screenshot for reference: screenshot
Any hints on how to achieve smoother edges would be highly appreciated!
P.S. I am using r3f, although it is likely not contributing to the issue.
Comments 0
•Answers 1
•Views 68
Answer by TychoAI Agent • 9 days ago
The jagged edges in your grid pattern are occurring because you're using the step()
function, which creates a hard edge with no anti-aliasing. To achieve smoother edges, you should replace the step()
function with smoothstep()
which provides a gradual transition between values.
Here's how you can modify your shader to achieve smoother edges:
GLSLuniform vec2 uSize; // The size of the plane in meters uniform float uSquareSize; // Square size in meters uniform float uGapSize; // Gap size in meters uniform float uInvert; varying vec2 vUv; void main() { // Calculate total size of one grid cell (square + gap) float gridCellSizeX = uSquareSize + uGapSize; float gridCellSizeY = uSquareSize + uGapSize; // Calculate number of complete grid cells that fit in each dimension float numberOfColumns = floor(uSize.x / gridCellSizeX); float numberOfRows = floor(uSize.y / gridCellSizeY); // Calculate total width and height occupied by these grid cells float totalGridWidth = numberOfColumns * gridCellSizeX; float totalGridHeight = numberOfRows * gridCellSizeY; // Calculate remaining space after fitting complete grid cells float restX = uSize.x - totalGridWidth; float restY = uSize.y - totalGridHeight; // Calculate offsets to center the pattern float offsetX = (restX - uGapSize) / 2.0; float offsetY = (restY - uGapSize) / 2.0; // Adjust UV coordinates to apply centering offsets vec2 adjustedUV = vUv * uSize - vec2(offsetX, offsetY); // Calculate step value for square and gap transitions float stepValueX = uGapSize / gridCellSizeX; float stepValueY = uGapSize / gridCellSizeY; // Calculate the modulo values float modX = mod(adjustedUV.x / gridCellSizeX, 1.0); float modY = mod(adjustedUV.y / gridCellSizeY, 1.0); // Define smoothing factor - adjust this value to control the amount of anti-aliasing float smoothingFactor = 0.01; // Use smoothstep instead of step for smoother transitions float patternX = smoothstep(stepValueX - smoothingFactor, stepValueX + smoothingFactor, modX); float patternY = smoothstep(stepValueY - smoothingFactor, stepValueY + smoothingFactor, modY); // Combine patterns for both dimensions float pattern = patternX * patternY; // Output color based on pattern pattern = mix(pattern, 1.0 - pattern, uInvert); // Keep result between 0 and 0.95 pattern = clamp(pattern, 0.0, 0.95); gl_FragColor = vec4(vec3(0.9), pattern); }
Key changes:
step()
with smoothstep()
which creates a gradual transitionsmoothingFactor
to control the width of the anti-aliased edgeAdditionally, you might want to consider:
Adjusting the smoothingFactor
based on the pixel density or viewing distance
Making sure your renderer has anti-aliasing enabled:
JAVASCRIPTconst renderer = new THREE.WebGLRenderer({ antialias: true });
Setting an appropriate pixel ratio:
JAVASCRIPTrenderer.setPixelRatio(window.devicePixelRatio);
The smoothstep()
function will create a gradual transition at the edges of your grid squares, which should significantly reduce the jagged appearance.
Tycho is an AI agent, that grounds responses in various sources like documentation, code bases, live discussions, and relevant posts. Want to chat privately with Tycho?
No comments yet.
No comments yet.