# 着色语言¶

## 前言¶

Godot 使用类似于 GLSL ES 3.0 的着色语言。支持大多数数据类型和函数，并且可能会随着时间的推移添加剩余的几种类型和函数。

## 数据类型¶

void

Void数据类型, 只对不返回任何内容的函数有用.

bool

bvec2

bvec3

bvec4

int

ivec2

ivec3

ivec4

uint

uvec2

uvec3

uvec4

float

vec2

vec3

vec4

mat2

2x2矩阵, 按主要顺序排列.

mat3

3x3矩阵, 在列的主要顺序.

mat4

4x4矩阵, 按主要顺序排列.

sampler2D

isampler2D

usampler2D

sampler2DArray

isampler2DArray

usampler2DArray

sampler3D

isampler3D

usampler3D

samplerCube

### 转换¶

```float a = 2; // invalid
float a = 2.0; // valid
float a = float(2); // valid
```

```int a = 2; // valid
uint a = 2; // invalid
uint a = uint(2); // valid
```

### 构建¶

```// The required amount of scalars
vec4 a = vec4(0.0, 1.0, 2.0, 3.0);
// Complementary vectors and/or scalars
vec4 a = vec4(vec2(0.0, 1.0), vec2(2.0, 3.0));
vec4 a = vec4(vec3(0.0, 1.0, 2.0), 3.0);
// A single scalar for the whole vector
vec4 a = vec4(0.0);
```

```mat2 m2 = mat2(vec2(1.0, 0.0), vec2(0.0, 1.0));
mat3 m3 = mat3(vec3(1.0, 0.0, 0.0), vec3(0.0, 1.0, 0.0), vec3(0.0, 0.0, 1.0));
mat4 identity = mat4(1.0);
```

```mat3 basis = mat3(WORLD_MATRIX);
mat4 m4 = mat4(basis);
mat2 m2 = mat2(m4);
```

### 混写Swizzling¶

```vec4 a = vec4(0.0, 1.0, 2.0, 3.0);
vec3 b = a.rgb; // Creates a vec3 with vec4 components.
vec3 b = a.ggg; // Also valid; creates a vec3 and fills it with a single vec4 component.
vec3 b = a.bgr; // "b" will be vec3(2.0, 1.0, 0.0).
vec3 b = a.xyz; // Also rgba, xyzw are equivalent.
vec3 b = a.stp; // And stpq (for texture coordinates).
float c = b.w; // Invalid, because "w" is not present in vec3 b.
vec3 c = b.xrt; // Invalid, mixing different styles is forbidden.
b.rrr = a.rgb; // Invalid, assignment with duplication.
b.bgr = a.rgb; // Valid assignment. "b"'s "blue" component will be "a"'s "red" and vice versa.
```

### 精度¶

```lowp vec4 a = vec4(0.0, 1.0, 2.0, 3.0); // low precision, usually 8 bits per component mapped to 0-1
mediump vec4 a = vec4(0.0, 1.0, 2.0, 3.0); // medium precision, usually 16 bits or half float
highp vec4 a = vec4(0.0, 1.0, 2.0, 3.0); // high precision, uses full float or integer range (default)
```

## 数组¶

### 局部数组¶

```void fragment() {
float arr[3];
}
```

```float float_arr[3] = float[3] (1.0, 0.5, 0.0); // first constructor

int int_arr[3] = int[] (2, 1, 0); // second constructor

vec2 vec2_arr[3] = { vec2(1.0, 1.0), vec2(0.5, 0.5), vec2(0.0, 0.0) }; // third constructor

bool bool_arr[] = { true, true, false }; // fourth constructor - size is defined automatically from the element count
```

```float a[3] = float[3] (1.0, 0.5, 0.0),
b[2] = { 1.0, 0.5 },
c[] = { 0.7 },
d = 0.0,
e[5];
```

```float arr[3];

arr[0] = 1.0; // setter

COLOR.r = arr[0]; // getter
```

```float arr[] = { 0.0, 1.0, 0.5, -1.0 };
for (int i = 0; i < arr.length(); i++) {
// ...
}
```

## 常量¶

```const vec2 a = vec2(0.0, 1.0);
vec2 b;

a = b; // invalid
b = a; // valid
```

```const vec2 V1 = vec2(1, 1), V2 = vec2(2, 2);
```

```const float arr[] = { 1.0, 0.5, 0.0 };

arr[0] = 1.0; // invalid

COLOR.r = arr[0]; // valid
```

```shader_type spatial;

const float PI = 3.14159265358979323846;
```

## 运算符¶

Godot 着色器语言支持与GLSL ES 3.0相同的操作符集. 下面是它们的优先级列表:

 优先级 类 操作符 1（最高） 括号分组 () 2 单目 +, -, !, ~ 3 乘除法 /, *, % 4 加减法 +, - 5 移位 <<, >> 6 关系比较 <, >, <=, >= 7 相等比较 ==, != 8 按位与 & 9 按位异或 ^ 10 按位或 | 11 逻辑与 && 12（最低） 逻辑或 ||

## 流控制¶

Godot 着色器语言支持最常见的控制流类型：

```// if and else
if (cond) {

} else {

}

// switch
switch(i) { // signed integer expression
case -1:
break;
case 0:
return; // break or return
case 1: // pass-through
case 2:
break;
//...
default: // optional
break;
}

// for loops
for (int i = 0; i < 10; i++) {

}

// while
while (true) {

}

// do while
do {

} while(true);
```

## 函数¶

```ret_type func_name(args) {
return ret_type; // if returning a value
}

// a more specific example:

int sum2(int a, int b) {
return a + b;
}
```

• in : 表示参数仅用于读取(默认).

• out : 表示该参数只用于写入.

• inout : 表示该参数以引用传递.

```void sum2(int a, int b, inout int result) {
result = a + b;
}
```

### Varying¶

```shader_type spatial;

varying vec3 some_color;

void vertex() {
some_color = NORMAL; // Make the normal the color.
}

void fragment() {
ALBEDO = some_color;
}

void light() {
DIFFUSE_LIGHT = some_color * 100; // optionally
}
```

Varying 也可以是数组：

```shader_type spatial;

varying float var_arr[3];

void vertex() {
var_arr[0] = 1.0;
var_arr[1] = 0.0;
}

void fragment() {
ALBEDO = vec3(var_arr[0], var_arr[1], var_arr[2]); // red color
}
```

```shader_type spatial;

varying vec3 some_light;

void fragment() {
some_light = ALBEDO * 100.0; // Make a shining light.
}

void light() {
DIFFUSE_LIGHT = some_light;
}
```

```shader_type spatial;

varying float test;

void foo() {
test = 0.0; // Error.
}

void vertex() {
test = 0.0;
}

void light() {
test = 0.0; // Error too.
}
```

### 插值限定符¶

```shader_type spatial;

varying flat vec3 our_color;

void vertex() {
our_color = COLOR.rgb;
}

void fragment() {
ALBEDO = our_color;
}
```

flat

smooth

### Uniform¶

```shader_type spatial;

uniform float some_value;
```

```material.set_shader_param("some_value", some_value)
```

`set_shader_param` 的第一个参数是着色器中的 uniform 名称。它必须与着色器中的 uniform 名称完全一致，否则将无法被识别。

```shader_type spatial;

uniform vec4 color : hint_color;
uniform float amount : hint_range(0, 1);
uniform vec4 other_color : hint_color = vec4(1.0);
```

vec4

hint_color

int、float

hint_range(min,max [,step] )

sampler2D

hint_albedo

sampler2D

hint_black_albedo

sampler2D

hint_normal

sampler2D

hint_white

sampler2D

hint_black

sampler2D

hint_aniso

GDScript 使用的变量类型与 GLSL 不同，所以当把变量从 GDScript 传递到着色器时，Godot 会自动转换类型。以下是相应类型的表格：

GDScript 类型

GLSL 类型

bool

bool

int

int

float

float

Vector2

vec2

Vector3

vec3

Color

vec4

Transform

mat4

Transform2D

mat4

Uniform 也可以分配默认值：

```shader_type spatial;

uniform vec4 some_vector = vec4(0.0);
uniform vec4 some_color : hint_color = vec4(1.0);
```

## 内置函数¶

vec_type sin (vec_type x)

vec_type cos (vec_type x)

vec_type tan (vec_type x)

vec_type asin (vec_type x)

vec_type acos (vec_type x)

vec_type atan (vec_type y_over_x)

vec_type atan (vec_type y, vec_type x)

vec_type sinh (vec_type x)

vec_type cosh (vec_type x)

vec_type tanh (vec_type x)

vec_type asinh (vec_type x)

vec_type acosh (vec_type x)

vec_type atanh (vec_type x)

vec_type pow (vec_type x, vec_type y)

vec_type exp (vec_type x)

vec_type exp2 (vec_type x)

vec_type log (vec_type x)

vec_type log2 (vec_type x)

vec_type sqrt (vec_type x)

vec_type inversesqrt (vec_type x)

vec_type abs (vec_type x)

ivec_type abs (ivec_type x)

vec_type sign ( vec_type )

ivec_type sign (ivec_type x)

vec_type floor (vec_type x)

vec_type round (vec_type x)

vec_type roundEven (vec_type x)

vec_type trunc (vec_type x)

vec_type ceil (vec_type x)

vec_type fract (vec_type x)

vec_type mod (vec_type x, vec_type y)

vec_type mod (vec_type x , float y)

vec_type modf (vec_type x, out vec_type i)

`x` 为小数部分, `i` 为整数部分

vec_type min (vec_type a, vec_type b)

vec_type max (vec_type a, vec_type b)

vec_type clamp (vec_type x, vec_type min, vec_type max)

float mix (float a, float b, float c)

vec_type mix (vec_type a, vec_type b, float c)

vec_type mix (vec_type a, vec_type b, vec_type c)

vec_type mix (vec_type a, vec_type b, bvec_type c)

vec_type step (vec_type a, vec_type b)

`b[i] < a[i] ? 0.0 : 1.0`

vec_type step (float a, vec_type b)

`b[i] < a ? 0.0 : 1.0`

vec_type smoothstep (vec_type a, vec_type b, vec_type c)

Hermite 插值

vec_type smoothstep (float a, float b, vec_type c)

Hermite 插值

bvec_type isnan (vec_type x)

bvec_type isinf (vec_type x)

ivec_type floatBitsToInt (vec_type x)

Float->Int 位复制，无转换

uvec_type floatBitsToUint (vec_type x)

Float->UInt 位复制，无转换

vec_type intBitsToFloat (ivec_type x)

Int-> Float 位复制，无转换

vec_type uintBitsToFloat (uvec_type x)

UInt->Float 位复制，无转换

float length (vec_type x)

float distance (vec_type a, vec_type b)

float dot (vec_type a, vec_type b)

vec3 cross (vec3 a, vec3 b)

vec_type normalize (vec_type x)

vec3 reflect (vec3 I, vec3 N)

vec3 refract (vec3 I, vec3 N, float eta)

vec_type faceforward (vec_type N, vec_type I, vec_type Nref)

mat_type matrixCompMult (mat_type x, mat_type y)

mat_type outerProduct (vec_type column, vec_type row)

mat_type transpose (mat_type m)

float determinant (mat_type m)

mat_type inverse (mat_type m)

bvec_type lessThan (vec_type x, vec_type y)

Bool vector 对比 < int/uint/float vectors

bvec_type greaterThan (vec_type x, vec_type y)

Bool vector 对比 > int/uint/float vectors

bvec_type lessThanEqual (vec_type x, vec_type y)

Bool vector 对比 <= int/uint/float vectors

bvec_type greaterThanEqual (vec_type x, vec_type y)

Bool vector 对比 >= int/uint/float vectors

bvec_type equal (vec_type x, vec_type y)

Bool vector 对比 == int/uint/float vectors

bvec_type notEqual (vec_type x, vec_type y)

Bool vector 对比 != int/uint/float vectors

bool any (bvec_type x)

bool all (bvec_type x)

bvec_type not (bvec_type x)

ivec2 textureSize (sampler2D_type s, int lod)

ivec3 textureSize (sampler2DArray_type s, int lod)

ivec3 textureSize (sampler3D s, int lod)

ivec2 textureSize (samplerCube s, int lod)

vec4_type texture (sampler2D_type s, vec2 uv [, float bias])

vec4_type texture (sampler2DArray_type s, vec3 uv [, float bias])

vec4_type texture (sampler3D_type s, vec3 uv [, float bias])

vec4 texture (samplerCube s, vec3 uv [, float bias])

vec4_type textureProj (sampler2D_type s, vec3 uv [, float bias])

vec4_type textureProj (sampler2D_type s, vec4 uv [, float bias])

vec4_type textureProj (sampler3D_type s, vec4 uv [, float bias])

vec4_type textureLod (sampler2D_type s, vec2 uv, float lod)

vec4_type textureLod (sampler2DArray_type s, vec3 uv, float lod)

vec4_type textureLod (sampler3D_type s, vec3 uv, float lod)

vec4 textureLod (samplerCube s, vec3 uv, float lod)

vec4_type textureProjLod (sampler2D_type s, vec3 uv, float lod)

vec4_type textureProjLod (sampler2D_type s, vec4 uv, float lod)

vec4_type textureProjLod (sampler3D_type s, vec4 uv, float lod)

vec4_type texelFetch (sampler2D_type s, ivec2 uv, int lod)

vec4_type texelFetch (sampler2DArray_type s, ivec3 uv, int lod)

vec4_type texelFetch (sampler3D_type s, ivec3 uv, int lod)

vec_type dFdx (vec_type p)

vec_type dFdy (vec_type p)

vec_type fwidth (vec_type p)

`x``y` 的绝对导数之和