GLSL - 测试片段值

时间:2013-11-07 19:08:45

标签: glsl

假设您vec3 colourInvertex shader转到frag shader,是否可以测试值并覆盖它?

例如,将蓝色值大于0.5的任何片段设置为白色?

在我的Shader.frag我实施了这项测试:

    if(colourIn.b>0.5){ //or if(greaterThan(colourIn.b,0.5))
     colourIn.b=0.0;
    }

它编译并呈现场景,但我不知道它是否有效,因为我是colourblind(笑)...我是否理解并正确实施了理论?

cubes

1 个答案:

答案 0 :(得分:0)

如果你愿意,你可以直接写条件,你的例子应该是正确的,但更聪明的举动可能是这样的:

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

这避免了条件,因此避免了任何相关的管道停顿或并行化故障。