GLSL Gif Dither Efecto: Optimización
Tengo un shader de fragmento que dice esencialmente el alfa del color y la traduce en un efecto de difuminado a través de los píxeles.
Sin embargo, es bastante intensivo en el procesador con todas las instrucciones mods y if. ¿Alguien tiene alguna recomendación para optimizar el código a continuación?
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, 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.001) + mod(gl_FragCoord.y, 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.001) + mod(gl_FragCoord.y + 2.0, 4.0)) > 6.00) {
gl_FragColor = color;
}
}
}
Aquí está la solución basada en la retroalimentación:
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, 0.0, 0.0);
}
tiene nombres muy genéricos para sus variables. Sería mucho más fácil averiguar cuál es tu algoritmo si usaras nombres más expresivos. –