GLSL Gif Dither Tác dụng: Tối ưu hóa
Tôi đã có một Shader đoạn mà chủ yếu đọc alpha màu sắc và chuyển nó thành một hiệu ứng phối màu trên các pixel.
Tuy nhiên, đó là bộ xử lý khá chuyên sâu với tất cả các mod và nếu câu lệnh. Có ai có bất kỳ đề xuất nào về tối ưu hóa mã bên dưới không?
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;
}
}
}
Dưới đây là giải pháp dựa trên những phản hồi:
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);
}
Bạn có tên rất chung chung cho các biến của mình. Nó sẽ là một _lot_ dễ dàng hơn để tìm ra thuật toán của bạn là gì nếu bạn sử dụng nhiều tên biểu cảm hơn. –