问题描述
假设您有一个从vertex shader
到frag shader
的vec3 colourIn
,是否可以测试一个值并将其覆盖呢?
Say you have a vec3 colourIn
going from a vertex shader
to a frag shader
, is there a way to test a value and overwrite it if you want to?
例如,将蓝色值大于0.5的任何片段设置为白色吗?
For example, set any fragment that has a blue value larger than 0.5 to the colour white?
在我的Shader.frag
中,我实现了此测试:
In my Shader.frag
I implemented this test:
if(colourIn.b>0.5){ //or if(greaterThan(colourIn.b,0.5))
colourIn.b=0.0;
}
它可以编译并渲染场景,但是由于色盲(lol),我无法确定它是否有效……我是否正确理解了理论并正确地实现了它?
It compiles and renders the scene, but I can't tell if it's worked because I'm colourblind (lol)... Have I got the theory right and implemented it correctly?
推荐答案
如果需要,您可以直接编写条件,示例应该是正确的,但是更明智的做法可能是:
You could directly write the conditional if you wanted, and your example should be correct, but a smarter move might be something like:
float mixValue = clamp(floor(colourIn.b * 2.0), 0.0, 1.0);
colourIn.b = mix(colourIn.b, 0.0, mixValue);
// the floor will be:
// 0.0 for [0.0 — 0.5);
// 1.0 for [0.5, 1.0)
// 2.0 1.0
//
// so the clamp will make mixValue:
// 0.0 for [0.0, 0.5]
// 1.0 for (0.5, 1.0]
//
// if you were to multiply by 1.99 then you could
// get rid of the clamp but if the input is a
// GLubyte then that'd move 128 into the low group
// instead of the high one
这避免了有条件的,因此避免了任何相关的管道停顿或并行化故障.
Which avoids the conditional and therefore any related pipeline stalls or parallelisation breakdowns.
这篇关于GLSL-测试片段值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!