Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement OpenGL Shader Anti-aliasing

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly shows you "OpenGL Shader anti-aliasing how to achieve", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "OpenGL Shader anti-aliasing how to achieve" this article.

Draw circular aliasing problem

When you draw a circular shape, you can see that the edges of the graphics are obviously aliased, and the edges are not as smooth as the real circular shape. This is a common situation in glsl, where you can use glsl built-in functions to eliminate aliasing.

Vec3 sdfCircle (vec2 uv,float rvector 3 value) {float d = length (uv)-r; return d > 0. ? Vec3 (0.3294, 0.3294, 0.9333): value; / / > 0 exceeds the circle range, less than 0} void main () {vec2 uv = gl_FragCoord.xy / iResolution.xy; uv-= 0.5; / / x:, y: uv.x * = iResolution.x/iResolution.y; / / x: * aspect ratio, y: vec3 circle = sdfCircle (uv,0.4,vec3 (1.)) Circle = mix (circle,sdfCircle (uv,0.3,vec3 (1.)), 0.5); circle = mix (circle,sdfCircle (uv,0.2,vec3 (1.)); gl_FragColor = vec4 (circle,.9);}

Introduction to smoothstep function

The result range of smoothstep (a, b, x) function:

Return value condition A value 0xb1xa returns a value between [0Jing 1] based on x within the range of [a < b] or [b < a].

The built-in function smoothstep can achieve the anti-aliasing effect of drawing circular graphics. May have used the built-in function step is also a step-by-step function, unlike the step function can be understood as if-else and smoothstep function is a smooth transition.

Anti-aliasing implementation

Using smoothstep to achieve anti-aliasing requires a modification of the original formula for drawing circles. Originally, you only need to use length (uv)-r to determine whether to choose the color of the drawing circle, but now you need to modify it to achieve a smooth transition to the color value of the drawing circle by using the calculated value of smoothstep (uv 0.002 uv) as the mixing coefficient value of the mix function, so that you can achieve anti-aliasing.

Vec2 uv = gl_FragCoord.xy / iResolution.xy; uv-= 0.5; / / x:, y: uv.x * = iResolution.x/iResolution.y; / / x: * aspect ratio, y: float m = 0.2; m = smoothstep (m = smoothstep); vec3 pixel = mix (vec3 (1.), vec3 (0.3294, 0.3294, 0.9333), m) Gl_FragColor = vec4 (pixel,1.0)

If you put the script in which MMI 0.002 masks 0.002 to modify the range of 0.002. For example, if you modify it to 0.02, you can find that the circle is blurred. This is why the smooth interval gradient range is visible to the naked eye because the interval is too large, so setting an appropriate transition interval can achieve a better anti-aliasing effect.

Expansion

After clearly realizing the anti-aliasing principle, we can implement a smooth transition function to achieve the anti-aliasing function according to the need. Similar to the following two homemade smooth transition functions, there is hardly much difference in the final implementation effect.

Self-made smoothstep function anti-aliasing float smootherstep (float edge0, float edge1, float x) {float t = (x-edge0) / (edge1-edge0); float T1 = tattlestict * (tcm 6.-15.) + 10.); return clamp (T1, 0.0,1.0);} void main () {vec2 uv = gl_FragCoord.xy / iResolution.xy; uv-= 0.5 / / x:, y: uv.x * = iResolution.x/iResolution.y; / / x: * aspect ratio, y: float m = 0.2; m = smootherstep (vec3 pixel 0.002 uv); vec3 pixel = mix (vec3 (1.), vec3 (0.3294, 0.3294, 0.9333), m); gl_FragColor = vec4 (pixel,1.0) } homemade linearstep function anti-aliasing float linearstep (float edge0, float edge1, float x) {float t = (x-edge0) / (edge1-edge0); return clamp (t, 0.0,1.0);} void main () {vec2 uv = gl_FragCoord.xy / iResolution.xy; uv-= 0.5; / / x:, y: uv.x * = iResolution.x/iResolution.y; / / x: * aspect ratio, y: float m = 0.2 M = linearstep; vec3 pixel = mix (vec3 (1.), vec3 (0.3294, 0.3294, 0.9333), m); gl_FragColor = vec4 (pixel,1.0);}

Smoothstep

Linearstep

These are all the contents of the article "how to achieve OpenGL Shader Anti-aliasing". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report