我正在努力将我们的应用程序从 Windows 移植到 Linux,我需要了解运算符 [] 在 HLSL 中做什么以及如何移植到 GLSL。
我有这样的代码:
red = texture.Sample(sampler, uv)[x];
示例函数应该返回一个像素值,对吧? x 是一种偏移量吗?
最佳答案
[]-运算符是 array member selection operator,也可用于访问向量。
与
等效的 glsl 代码red = texture.Sample(sampler, uv)[x];
是
float red = texture(sampler, uv)[x];
texture
返回类型为 vec4
的值(例如 sampler2D
)。向量的分量可以通过索引运算符访问。由于 x
是索引,因此它必须是具有整型数据类型的变量或常量。该向量有 4 个分量( .x
、 .y
、 .z
、 .w
分别为 .r
、 .g
、 .b
、 .a
),因此x
必须是 0、1、2 或 3。
texture(sampler, uv)[0]
与 texture(sampler, uv).r
相同。另请参阅 GLSL- Swizzling 。
关于shader - 方括号运算符在 HLSL 中的作用是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60156485/