-
Notifications
You must be signed in to change notification settings - Fork 0
GPU-based Anti-Aliased Line Rendering #233
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: fix-opengl-prober-5779355015471748127
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,30 @@ | ||
| uniform vec4 uColor; | ||
|
|
||
| in vec4 vColor; | ||
| in vec2 vUv; | ||
| in float vLineLength; | ||
| in float vWidth; | ||
|
|
||
| out vec4 vFragmentColor; | ||
|
|
||
| void main() | ||
| { | ||
| float halfWidth = vWidth * 0.5; | ||
|
|
||
| // Calculate distance to the segment | ||
| float dx = max(-vUv.x, 0.0) + max(vUv.x - vLineLength, 0.0); | ||
| float dy = vUv.y; | ||
| float dist = sqrt(dx * dx + dy * dy); | ||
|
|
||
| // Anti-aliasing | ||
| // Smoothstep creates a nice alpha ramp over 1 pixel | ||
| float feather = 1.0; | ||
| float alpha = 1.0 - smoothstep(halfWidth - feather, halfWidth + feather, dist); | ||
|
|
||
| if (alpha <= 0.0) { | ||
| discard; | ||
| } | ||
|
|
||
| vFragmentColor = vColor * uColor; | ||
| vFragmentColor.a *= alpha; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,26 @@ | ||
| uniform vec4 uColor; | ||
|
|
||
| in vec2 vUv; | ||
| in float vLineLength; | ||
| in float vWidth; | ||
|
|
||
| out vec4 vFragmentColor; | ||
|
|
||
| void main() | ||
| { | ||
| float halfWidth = vWidth * 0.5; | ||
|
|
||
| float dx = max(-vUv.x, 0.0) + max(vUv.x - vLineLength, 0.0); | ||
| float dy = vUv.y; | ||
| float dist = sqrt(dx * dx + dy * dy); | ||
|
|
||
| float feather = 1.0; | ||
| float alpha = 1.0 - smoothstep(halfWidth - feather, halfWidth + feather, dist); | ||
|
|
||
| if (alpha <= 0.0) { | ||
| discard; | ||
| } | ||
|
|
||
| vFragmentColor = uColor; | ||
| vFragmentColor.a *= alpha; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,6 +5,10 @@ uniform ivec4 uViewport; // x, y, width, height | |
| layout(location = 0) in vec3 aVert1; | ||
| layout(location = 1) in vec3 aVert2; | ||
|
|
||
| out vec2 vUv; | ||
| out float vLineLength; | ||
| out float vWidth; | ||
|
|
||
| void main() | ||
| { | ||
| vec4 clip1 = uMVP * vec4(aVert1, 1.0); | ||
|
|
@@ -25,14 +29,36 @@ void main() | |
| } | ||
| vec2 normal = vec2(-dir.y, dir.x); | ||
|
|
||
| vec2 offset = normal * uLineWidth * 0.5; | ||
| float feather = 1.0; | ||
| float halfWidth = uLineWidth * 0.5; | ||
| float totalHalfWidth = halfWidth + feather; | ||
|
Comment on lines
+32
to
+34
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion: The hardcoded
Suggested implementation: To fully centralize and share the feather parameter:
|
||
|
|
||
| vLineLength = len; | ||
| vWidth = uLineWidth; | ||
|
|
||
| vec2 pos; | ||
| float z; | ||
| if (gl_VertexID == 0) { pos = screen1 + offset; z = clip1.z / clip1.w; } | ||
| else if (gl_VertexID == 1) { pos = screen1 - offset; z = clip1.z / clip1.w; } | ||
| else if (gl_VertexID == 2) { pos = screen2 - offset; z = clip2.z / clip2.w; } | ||
| else { pos = screen2 + offset; z = clip2.z / clip2.w; } | ||
|
|
||
| vec2 offsetL = dir * totalHalfWidth; | ||
| vec2 offsetP = normal * totalHalfWidth; | ||
|
|
||
| if (gl_VertexID == 0) { | ||
| pos = screen1 - offsetL + offsetP; | ||
| z = clip1.z / clip1.w; | ||
| vUv = vec2(-totalHalfWidth, totalHalfWidth); | ||
| } else if (gl_VertexID == 1) { | ||
| pos = screen1 - offsetL - offsetP; | ||
| z = clip1.z / clip1.w; | ||
| vUv = vec2(-totalHalfWidth, -totalHalfWidth); | ||
| } else if (gl_VertexID == 2) { | ||
| pos = screen2 + offsetL - offsetP; | ||
| z = clip2.z / clip2.w; | ||
| vUv = vec2(len + totalHalfWidth, -totalHalfWidth); | ||
| } else { | ||
| pos = screen2 + offsetL + offsetP; | ||
| z = clip2.z / clip2.w; | ||
| vUv = vec2(len + totalHalfWidth, totalHalfWidth); | ||
| } | ||
|
|
||
| vec2 final_ndc = (pos / vec2(uViewport.zw)) * 2.0 - 1.0; | ||
| gl_Position = vec4(final_ndc, z, 1.0); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion (performance): Consider marking vLineLength and vWidth as
flatto avoid unnecessary interpolation and potential precision issues.These values are per-line, not per-vertex, so they should be declared as
flat out floatin the vertex shader andflat in floatin the fragment shader to reflect their per-primitive nature rather than being interpolated per-fragment.Suggested implementation:
In the matching fragment shader for this pipeline (likely something like
src/resources/shaders/legacy/line/acolor/frag.glsl), update the corresponding varyings to:in float vLineLength;withflat in float vLineLength;in float vWidth;withflat in float vWidth;This will ensure the per-line values are not interpolated per-fragment and avoids GLSL interface mismatch between vertex and fragment shaders.