私は基本的に色のアルファを読み込み、各画素間でディザリング効果に変換し、フラグメントシェーダを持っています。
しかし、すべての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, 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;
}
}
}
ここでは、フィードバックに基づいてソリューションです:すべての
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);
}
あなたは、変数のための非常に一般的な名前を持っているを使用して/ 4を回避することができます。より表現力のある名前を使用すると、アルゴリズムがどのようなものかを簡単に把握することができます。 –