成员逐一的三元运算符

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

glsl member-wise ?: operator

问题

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

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

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

  1. //c++, glm
  2. vec3 linear_to_sRGB(vec3 x)
  3. {
  4. return lessThan(v, vec3(0.0031308f)) == bvec3(true)
  5. ? (v * 12.9232102f)
  6. : 1.055f * glm::pow(v, vec3(1.0f / 2.4f)) - 0.055f
  7. ;
  8. }
英文:

does glsl support ?: on vectors, like this:

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

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

  1. //c++, glm
  2. vec3 linear_to_sRGB(vec3 x)
  3. {
  4. return lessThan(v, vec3(0.0031308f)) == bvec3(true)
  5. ? (v * 12.9232102f)
  6. : 1.055f * glm::pow(v, vec3(1.0f / 2.4f)) - 0.055f
  7. ;
  8. }

答案1

得分: 2

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

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

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

  1. return mix(
  2. 1.055 * pow(x, 1.0 / 2.4) - 0.055,
  3. x * 12.9232102,
  4. step(x, vec3(0.0031308f)));
  5. </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:

确定