我正在尝试创建一个 OpenGL ES 2.0 片段着色器,它沿一个轴输出多个停止渐变。它应该在以百分比定义的点处的多种颜色之间进行插值。
我已经通过使用实现了这一点if
是片段着色器,如下所示:
float y = gl_FragCoord.y;
float step1 = resolution.y * 0.20;
float step2 = resolution.y * 0.50;
if (y < step1) {
color = white;
} else if (y < step2) {
float x = smoothstep(step1, step2, y);
color = mix(white, red, x);
} else {
float x = smoothstep(step2, resolution.y, y);
color = mix(red, green, x);
}
他们说片段着色器中的分支会降低性能。是否有一些巧妙的技巧可以用来在许多值之间进行插值而不使用if
是?它真的值得吗(我知道这是非常主观的,但作为经验法则)?
为了说明我的问题,完整源代码(虽然仍然很短)在此 GLSL Sandbox 链接中:http://glsl.heroku.com/e#8035.0 http://glsl.heroku.com/e#8035.0
如果你想消除分支,你可以执行以下操作(取自 Heroku);
color = mix(white, red, smoothstep(step1, step2, y));
color = mix(color, blue, smoothstep(step2, step3, y));
color = mix(color, green, smoothstep(step3, resolution.y, y));
但我完全不确定这是否比 if/elses 更快。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)