opengl – GLSL Gif抖动效果:优化

opengl – GLSL Gif抖动效果:优化,第1张

概述我有一个片段着色器,它基本上读取颜色alpha并将其转换为像素上的抖动效果. 但是,对于所有mod和if语句来说,它是处理器密集型的. 有没有人对优化下面的代码有任何建议? varying vec2 the_uv;varying vec4 color;void main(){ // The pixel color will correspond // to the uv 我有一个片段着色器,它基本上读取颜色Alpha并将其转换为像素上的抖动效果.

但是,对于所有mod和if语句来说,它是处理器密集型的.
有没有人对优化下面的代码有任何建议?

varying vec2 the_uv;varying vec4 color;voID main(){    // The pixel color will correspond    //  to the uv coords of the texture    //  for the given vertice,retrIEved    //  by the Vertex shader through varying vec2 the_uv    gl_Fragcolor = vec4(0.0,0.0,0.0);    vec4 tex = texture2D(_MainTex,the_uv);    tex = tex * color ;    float r = tex.a;    if ( r > 0.1 ) {        if ( ( mod(gl_FragCoord.x,4.001) + mod(gl_FragCoord.y,4.0) ) > 6.00 ) {            gl_Fragcolor = color;        }    }    if ( r > 0.5 ) {        if ( ( mod(gl_FragCoord.x + 2.0,4.0) ) > 6.00 ) {            gl_Fragcolor = color;        }    }    if ( r > 0.7 ) {        if ( ( mod(gl_FragCoord.x,4.001) + mod(gl_FragCoord.y + 2.0,4.0) ) > 6.00 ) {            gl_Fragcolor = color;        }    }    if ( r > 0.9 ) {        if ( ( mod(gl_FragCoord.x + 1.0,4.001) + mod(gl_FragCoord.y + 1.0,4.0) ) > 6.00 ) {            gl_Fragcolor = color;        }    }    if ( r > 0.3 ) {        if ( ( mod(gl_FragCoord.x + 2.0,4.0) ) > 6.00 ) {            gl_Fragcolor = color;        }    }}

以下是基于反馈的解决方案:

varying vec2 the_uv;        varying vec4 color;        voID main()        {            color = gl_color;            gl_position = gl_ModelVIEwProjectionMatrix * gl_Vertex;            the_uv = gl_MultiTexCoord0.st;        }        #endif        #ifdef FRAGMENT        uniform sampler2D _MainTex;        uniform sampler2D _GrIDTex;        varying vec2 the_uv;        varying vec4 color;        voID main()        {            if (texture2D(_MainTex,the_uv).a * color.a > texture2D(_GrIDTex,vec2(gl_FragCoord.x,gl_FragCoord.y)*.25).a) gl_Fragcolor = color;             else gl_Fragcolor = vec4(0.0,0.0);           }
解决方法 您要做的是选择是否应根据源Alpha在4×4网格上点亮每个像素.最简单的方法就是做到这一点.

首先使用像素通过所需的相应Alpha来初始化4×4纹理(我选择1.0作为从未在此处显示的Alpha)

1.0 0.5 1.0 0.11.0 1.0 0.9 1.01.0 0.3 1.0 0.71.0 1.0 1.0 1.0

使用repeat设置此纹理以完全避免mod,并使用最近的滤镜来避免线性过滤.

然后,使用该纹理的采样来决定是否点亮像素.

vec4 dither = texture2D(_my4x4,gl_FragCoord / 4.); // 4 is the size of the textureif (r > dither) { gl_Fragcolor = color; }

这假设r永远不是1.有简单的方法可以解决这个问题,例如:除了1.0之外,将4×4纹理中的值除以2,然后在if测试之前将抖动乘以2,但是在获取之后.

一些进一步的潜在优化:

>你可以通过使用带比较的纹理(阴影纹理)完全避免if测试.>您可以使用textureFetch指令来避免/ 4

总结

以上是内存溢出为你收集整理的opengl – GLSL Gif抖动效果:优化全部内容,希望文章能够帮你解决opengl – GLSL Gif抖动效果:优化所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://www.outofmemory.cn/web/1077090.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存