编辑
2023-11-16
学习
00
请注意,本文编写于 340 天前,最后修改于 339 天前,其中某些信息可能已经过时。

参考地址:https://docs.godotengine.org/en/4.1/tutorials/shaders/shader_reference/shading_language.html

查找表(使用chatgpt辅助生成)

函数描述使用范例
radians将角度转换为弧度。vec_type angle_in_radians = radians(angle_in_degrees);
degrees将弧度转换为角度。vec_type angle_in_degrees = degrees(angle_in_radians);
sin正弦。vec_type sine_value = sin(angle);
cos余弦。vec_type cosine_value = cos(angle);
tan正切。vec_type tangent_value = tan(angle);
asin反正弦。vec_type angle = asin(value);
acos反余弦。vec_type angle = acos(value);
atan反正切。vec_type angle = atan(y_over_x);
atan (重载)反正切。vec_type angle = atan(y, x);
sinh双曲正弦。vec_type sinh_value = sinh(x);
cosh双曲余弦。vec_type cosh_value = cosh(x);
tanh双曲正切。vec_type tanh_value = tanh(x);
asinh反双曲正弦。vec_type x = asinh(value);
acosh反双曲余弦。vec_type x = acosh(value);
atanh反双曲正切。vec_type x = atanh(value);
pow幂运算(如果 x < 0 或 x == 0 且 y <= 0,则结果未定义)。vec_type result = pow(base, exponent);
expBase-e 指数函数。vec_type result = exp(x);
exp2Base-2 指数函数。vec_type result = exp2(x);
log自然对数函数。vec_type result = log(x);
log2Base-2 对数函数。vec_type result = log2(x);
sqrt平方根函数。vec_type result = sqrt(x);
inversesqrt平方根的倒数函数。vec_type result = inversesqrt(x);
abs绝对值函数(对于负数返回正值)。vec_type result = abs(x);
abs (整数版本)绝对值函数(对于负数返回正值)。ivec_type result = abs(x);
sign符号函数(正数返回1.0,负数返回-1.0,零返回0.0)。vec_type result = sign(x);
sign (整数版本)符号函数(正数返回1,负数返回-1,零返回0)。ivec_type result = sign(x);
floor向下取整函数。vec_type result = floor(x);
round四舍五入到最近的整数。vec_type result = round(x);
roundEven四舍五入到最近的偶数。vec_type result = roundEven(x);
trunc截断到整数。vec_type result = trunc(x);
ceil向上取整函数。vec_type result = ceil(x);
fract返回小数部分(等同于 x - floor(x))。vec_type result = fract(x);
mod取模运算,返回除法余数。vec_type result = mod(x, y);
mod (单个浮点数)取模运算,返回除法余数。vec_type result = mod(x, y);
modf返回小数部分和整数部分。vec_type fractional;
vec_type integral;
vec_type result = modf(x, integral);
min返回两个值中的较小值。vec_type result = min(a, b);
max返回两个值中的较大值。vec_type result = max(a, b);
clamp将值限制在指定范围内。vec_type result = clamp(x, min_value, max_value);
mix (浮点数版本)线性插值,取值范围为 [a, b] 之间的值,c 为插值因子。vec_type result = mix(a, b, c);
mix (向量版本)线性插值,取值范围为 [a, b] 之间的值,c 为插值因子。vec_type result = mix(a, b, condition_vector);
fma执行融合乘加操作:(a * b + c)。vec_type result = fma(a, b, c);
step如果 b[i] < a[i],返回 0.0,否则返回 1.0。vec_type result = step(a, b);
step (浮点数版本)如果 b < a,返回 0.0,否则返回 1.0。vec_type result = step(a, b);
smoothstepHermite 插值,根据 c 在 [a, b] 范围内的位置进行插值。vec_type result = smoothstep(a, b, c);
isnan检查标量或向量组件是否为 NaN。bvec_type result = isnan(x);
isinf检查标量或向量组件是否为 INF。bvec_type result = isinf(x);
floatBitsToInt浮点到整数的位复制,无转换。ivec_type result = floatBitsToInt(x);
floatBitsToUint浮点到无符号整数的位复制,无转换。uvec_type result = floatBitsToUint(x);
intBitsToFloat整数到浮点的位复制,无转换。vec_type result = intBitsToFloat(x);
uintBitsToFloat无符号整数到浮点的位复制,无转换。vec_type result = uintBitsToFloat(x);
length计算向量的长度。float result = length(x);
distance计算向量之间的距离,即 length(a - b)。float result = distance(a, b);
dot计算两个向量的点积。float result = dot(a, b);
cross计算两个三维向量的叉积。vec3 result = cross(a, b);
normalize将向量归一化为单位长度。vec_type result = normalize(x);
reflect计算向量相对于法线的反射。vec3 result = reflect(I, N);
refract计算向量相对于法线的折射。vec3 result = refract(I, N, eta);
faceforward如果 dot(Nref, I) < 0,则返回 N,否则返回 -N。vec_type result = faceforward(N, I, Nref);
matrixCompMult矩阵分量乘法。mat_type result = matrixCompMult(x, y);
outerProduct外积矩阵。mat_type result = outerProduct(column, row);
transpose转置矩阵。mat_type result = transpose(m);
determinant计算矩阵的行列式。float result = determinant(m);
inverse计算矩阵的逆矩阵。mat_type result = inverse(m);
lessThan向量比较,如果 x[i] < y[i],返回 true。bvec_type result = lessThan(x, y);
greaterThan向量比较,如果 x[i] > y[i],返回 true。bvec_type result = greaterThan(x, y);
lessThanEqual向量比较,如果 x[i] <= y[i],返回 true。bvec_type result = lessThanEqual(x, y);
greaterThanEqual向量比较,如果 x[i] >= y[i],返回 true。bvec_type result = greaterThanEqual(x, y);
equal向量比较,如果 x[i] == y[i],返回 true。bvec_type result = equal(x, y);
notEqual向量比较,如果 x[i] != y[i],返回 true。bvec_type result = notEqual(x, y);
any如果向量的任何组件为 true,则返回 true。bool result = any(x);
all如果向量的所有组件为 true,则返回 true。bool result = all(x);
not反转布尔向量。bvec_type result = not(x);
textureSize获取纹理的大小。ivec2 result = textureSize(s, lod);
textureSize获取纹理的大小。ivec3 result = textureSize(s, lod);
textureSize获取纹理的大小。ivec3 result = textureSize(s, lod);
textureSize获取纹理的大小。ivec2 result = textureSize(s, lod);
textureSize获取纹理的大小。ivec2 result = textureSize(s, lod);
textureQueryLod计算用于采样纹理的细节级别。vec2 result = textureQueryLod(s, p);
textureQueryLod计算用于采样纹理的细节级别。vec3 result = textureQueryLod(s, p);
textureQueryLod计算用于采样纹理的细节级别。vec2 result = textureQueryLod(s, p);
textureQueryLod计算用于采样纹理的细节级别。vec2 result = textureQueryLod(s, p);
textureQueryLevels获取纹理的可访问细节级别数。int result = textureQueryLevels(s);
textureQueryLevels获取纹理的可访问细节级别数。int result = textureQueryLevels(s);
textureQueryLevels获取纹理的可访问细节级别数。int result = textureQueryLevels(s);
textureQueryLevels获取纹理的可访问细节级别数。int result = textureQueryLevels(s);
texture执行纹理读取。gvec4_type result = texture(s, p, bias);
texture执行纹理读取。gvec4_type result = texture(s, p, bias);
texture执行纹理读取。gvec4_type result = texture(s, p, bias);
texture执行纹理读取。vec4 result = texture(s, p, bias);
texture执行纹理读取。vec4 result = texture(s, p, bias);
textureProj执行带投影的纹理读取。gvec4_type result = textureProj(s, p, bias);
textureProj执行带投影的纹理读取。gvec4_type result = textureProj(s, p, bias);
textureProj执行带投影的纹理读取。gvec4_type result = textureProj(s, p, bias);
textureLod在自定义细节级别处执行纹理读取。gvec4_type result = textureLod(s, p, lod);
textureLod在自定义细节级别处执行纹理读取。gvec4_type result = textureLod(s, p, lod);
textureLod在自定义细节级别处执行纹理读取。gvec4_type result = textureLod(s, p, lod);
textureLod在自定义细节级别处执行纹理读取。vec4 result = textureLod(s, p, lod);
textureLod在自定义细节级别处执行纹理读取。vec4 result = textureLod(s, p, lod);
textureProjLod带有投影和自定义细节级别的纹理读取。gvec4_type result = textureProjLod(s, p, lod);
textureProjLod带有投影和自定义细节级别的纹理读取。gvec4_type result = textureProjLod(s, p, lod);
textureProjLod带有投影和自定义细节级别的纹理读取。gvec4_type result = textureProjLod(s, p, lod);
textureGrad带有显式梯度的纹理读取。gvec4_type result = textureGrad(s, p, dPdx, dPdy);
textureGrad带有显式梯度的纹理读取。gvec4_type result = textureGrad(s, p, dPdx, dPdy);
textureGrad带有显式梯度的纹理读取。gvec4_type result = textureGrad(s, p, dPdx, dPdy);
textureGrad带有显式梯度的纹理读取。vec4 result = textureGrad(s, p, dPdx, dPdy);
textureGrad带有显式梯度的纹理读取。vec4 result = textureGrad(s, p, dPdx, dPdy);
textureProjGrad执行带有投影/LOD和显式梯度的纹理读取。gvec4_type result = textureProjGrad(s, p, dPdx, dPdy);
textureProjGrad执行带有投影/LOD和显式梯度的纹理读取。gvec4_type result = textureProjGrad(s, p, dPdx, dPdy);
textureProjGrad执行带有投影/LOD和显式梯度的纹理读取。gvec4_type result = textureProjGrad(s, p, dPdx, dPdy);
texelFetch使用整数坐标提取单个纹理元素。gvec4_type result = texelFetch(s, ivec2(p), lod);
texelFetch使用整数坐标提取单个纹理元素。gvec4_type result = texelFetch(s, ivec3(p), lod);
texelFetch使用整数坐标提取单个纹理元素。gvec4_type result = texelFetch(s, ivec3(p), lod);
textureGather从纹理中聚集四个纹素。gvec4_type result = textureGather(s, p, comps);
textureGather从纹理中聚集四个纹素。gvec4_type result = textureGather(s, p, comps);
textureGather从纹理中聚集四个纹素。vec4 result = textureGather(s, p, comps);
dFdx使用局部差分计算相对于 x 窗口坐标的导数。vec_type result = dFdx(p);
dFdxCoarse使用局部差分基于当前片元邻居的值计算相对于 x 窗口坐标的导数。vec_type result = dFdxCoarse(p);
dFdxFine使用局部差分基于当前片元及其相邻片元的值计算相对于 x 窗口坐标的导数。vec_type result = dFdxFine(p);
dFdy使用局部差分计算相对于 y 窗口坐标的导数。vec_type result = dFdy(p);
dFdyCoarse使用局部差分基于当前片元邻居的值计算相对于 y 窗口坐标的导数。vec_type result = dFdyCoarse(p);
dFdyFine使用局部差分基于当前片元及其相邻片元的值计算相对于 y 窗口坐标的导数。vec_type result = dFdyFine(p);
fwidthx 和 y 方向上的绝对导数之和。vec_type result = fwidth(p);
fwidthCoarsex 和 y 方向上的绝对导数之和(基于粗糙差分)。vec_type result = fwidthCoarse(p);
fwidthFinex 和 y 方向上的绝对导数之和(基于精细差分)。vec_type result = fwidthFine(p);
uint packHalf2x16 (vec2 v)将两个 32 位浮点数转换为 16 位并打包成一个 32 位无符号整数,反之亦然。uint packed = packHalf2x16(vec2(0.5, 0.75));
vec2 unpackHalf2x16 (uint v)将一个 32 位无符号整数解包成两个 16 位浮点数。vec2 unpacked = unpackHalf2x16(packed);
uint packUnorm2x16 (vec2 v)将两个 32 位浮点数(限制在 0 到 1 范围内)转换为 16 位并打包成一个 32 位无符号整数,反之亦然。uint packed = packUnorm2x16(vec2(0.5, 0.75));
vec2 unpackUnorm2x16 (uint v)将一个 32 位无符号整数解包成两个 16 位浮点数(范围在 0 到 1 之间)。vec2 unpacked = unpackUnorm2x16(packed);
uint packSnorm2x16 (vec2 v)将两个 32 位浮点数(限制在 -1 到 1 范围内)转换为 16 位并打包成一个 32 位无符号整数,反之亦然。uint packed = packSnorm2x16(vec2(0.5, -0.75));
vec2 unpackSnorm2x16 (uint v)将一个 32 位无符号整数解包成两个 16 位浮点数(范围在 -1 到 1 之间)。vec2 unpacked = unpackSnorm2x16(packed);
uint packUnorm4x8 (vec4 v)将四个 32 位浮点数(限制在 0 到 1 范围内)转换为 8 位并打包成一个 32 位无符号整数,反之亦然。uint packed = packUnorm4x8(vec4(0.1, 0.5, 0.8, 1.0));
vec4 unpackUnorm4x8 (uint v)将一个 32 位无符号整数解包成四个 8 位浮点数(范围在 0 到 1 之间)。vec4 unpacked = unpackUnorm4x8(packed);
uint packSnorm4x8 (vec4 v)将四个 32 位浮点数(限制在 -1 到 1 范围内)转换为 8 位并打包成一个 32 位无符号整数,反之亦然。uint packed = packSnorm4x8(vec4(0.1, -0.5, 0.8, 1.0));
vec4 unpackSnorm4x8 (uint v)将一个 32 位无符号整数解包成四个 8 位浮点数(范围在 -1 到 1 之间)。vec4 unpacked = unpackSnorm4x8(packed);
ivec_type bitfieldExtract (ivec_type value, int offset, int bits)从整数中提取一定范围的位。ivec_type extracted = bitfieldExtract(value, 2, 4);
ivec_type bitfieldInsert (ivec_type base, ivec_type insert, int offset, int bits)将一定范围的位插入到整数中。ivec_type result = bitfieldInsert(base, insert, 2, 4);
ivec_type bitfieldReverse (ivec_type value)反转整数中位的顺序。ivec_type reversed = bitfieldReverse(value);
ivec_type bitCount (ivec_type value)计算整数中值为 1 的位的数量。ivec_type count = bitCount(value);
ivec_type findLSB (ivec_type value)找到整数中最低有效位(值为 1)的索引。ivec_type lsb = findLSB(value);
ivec_type findMSB (ivec_type value)找到整数中最高有效位(值为 1)的索引。ivec_type msb = findMSB(value);
void imulExtended (ivec_type x, ivec_type y, out ivec_type msb, out ivec_type lsb)将两个 32 位数字相乘,生成一个 64 位结果。imulExtended(5, 3, msb, lsb);
void umulExtended (uvec_type x, uvec_type y, out uvec_type msb, out uvec_type lsb)将两个无符号整数相乘,生成一个 64 位结果。umulExtended(5u, 3u, msb, lsb);
uvec_type uaddCarry (uvec_type x, uvec_type y, out uvec_type carry)将两个无符号整数相加,并生成进位。uvec_type sum = uaddCarry(5u, 3u, carry);
uvec_type usubBorrow (uvec_type x, uvec_type y, out uvec_type borrow)将两个无符号整数相减,并生成借位。uvec_type difference = usubBorrow(5u, 3u, borrow);
vec_type ldexp (vec_type x, out ivec_type exp)从值和指数中组装浮点数。vec_type result = ldexp(0.75, exp);
vec_type frexp (vec_type x, out ivec_type exp)将浮点数拆分为尾数和整数指数。vec_type significand = frexp(0.75, exp);

本文作者:beiklive

本文链接:

版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!