成员逐一的三元运算符

huangapple go评论68阅读模式
英文:

glsl member-wise ?: operator

问题

GLSL支持向量上的? :吗,就像这样:

// hlsl
half3 linear_to_sRGB(half3 x)
{
    return (x <= 0.0031308 ? (x * 12.9232102) : 1.055 * pow(x, 1.0 / 2.4) - 0.055);
}

我尝试过这样(通过glm),但效果不如预期(我不认为它们是一样的):

//c++, glm
vec3 linear_to_sRGB(vec3 x)
{
	return lessThan(v, vec3(0.0031308f)) == bvec3(true)
		? (v * 12.9232102f)
        : 1.055f * glm::pow(v, vec3(1.0f / 2.4f)) - 0.055f
        ;
}
英文:

does glsl support ?: on vectors, like this:

// hlsl
half3 linear_to_sRGB(half3 x)
{
    return (x &lt;= 0.0031308 ? (x * 12.9232102) : 1.055 * pow(x, 1.0 / 2.4) - 0.055);
}

I tried like this (via glm), not working as expected (i don't think they are the same thing):

//c++, glm
vec3 linear_to_sRGB(vec3 x)
{
	return lessThan(v, vec3(0.0031308f)) == bvec3(true)
		? (v * 12.9232102f)
        : 1.055f * glm::pow(v, vec3(1.0f / 2.4f)) - 0.055f
        ;
}

答案1

得分: 2

你可以使用内置的 stepmix 函数来实现类似以下的效果:

return mix(
    1.055 * pow(x, 1.0 / 2.4) - 0.055,
    x * 12.9232102,
    step(x, vec3(0.0031308f)));
英文:

You can achieve something like this with the built-in step and mix functions:

return mix(
    1.055 * pow(x, 1.0 / 2.4) - 0.055,
    x * 12.9232102,
    step(x, vec3(0.0031308f)));

</details>



huangapple
  • 本文由 发表于 2023年5月7日 02:55:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76190592.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定