我正在尝试获取值低于0.89的像素的数量。由于WebGL着色器变量不持续,我只是尝试了一个在上次迭代中计算计数或TexCoord为1.0,1.0的黑客。
目前我正在尝试以下代码,这是正确的,我们对此有更好的方法吗?
`
void main() {
//gl_FragColor = vec4(texture2D(u_texture, vec2(mytexcoord.x, mytexcoord.y)).rgb, 1.0);
vec2 uv = mytexcoord;
float ins = texture2D(u_texture, vec2(uv.x, 1.0 - uv.y)).r;
float neglectIntensityValue = 0.89453125;
float blueVal = 0.00000000;
vec2 onePixel = vec2(1.0, 1.0) / u_resolution;
int pixelCount = 0;
if (vec2(1.0) == mytexcoord) {
//last iteration
//loop through all the pixels to know the count
const int totalPixels = 100000;//int(u_resolution.x * u_resolution.y);
for (int i = 0; i < totalPixels; i++) {
vec2 uv = vec2((float(i)) / 100000.0, 0.5);
vec4 colorData = vec4(texture2D(u_texture, uv).rgb, 1.0);
if (colorData.r < neglectIntensityValue) {
pixelCount++;
}
}
}
if (pixelCount > 1000) {
gl_FragColor = vec4(ins, ins, ins, 1.0);
} else {
ins = 1.0 - ins;
gl_FragColor = vec4(ins, ins, ins, 1.0);
}
}
`
仅使用片段着色器就无法更有效地执行此操作。您的像素着色器可能会在大纹理上崩溃,因为它不打算长时间运行。如果足够的话,您可以随机对像素的子集作为近似。
为了进行有效的实现,您可以将着色器输出1或0到渲染纹理中,然后使用其他像素着色器的log(n)通过,将其汇总为2x2 pixels(或4x4或更多)每次通过1个像素。显然,实施将要复杂得多。