godot着色器

三渲二

基础

  • 自己改的
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
shader_type spatial;
render_mode ambient_light_disabled;


uniform sampler2D base:source_color;
uniform sampler2D sss:source_color;
uniform sampler2D ilm:source_color;

uniform float shinese= 32.0f; //高光范围
uniform float shade_threshold:hint_range(0,1)=0.3;
uniform float spec_threshold:hint_range(0,0.1)=0.05f;
uniform float test:hint_range(0,1)=0.5f; //亮度

varying vec4 v_color;


void vertex(){
v_color = COLOR; //获取顶的色
}


void light(){
float fade_ao_weight = v_color.r; // 顶点色提供的环境光遮蔽
vec3 base_color = texture(base,UV).rgb; //基础颜色
vec3 shade_color = texture(sss,UV).rgb; //阴影颜色

float shadow_weight = texture(ilm, UV).g; //阴影权重,发梢

float LdotN = dot(LIGHT, NORMAL);
float shade = LdotN*fade_ao_weight;
float factor = LdotN*shadow_weight*fade_ao_weight; //假ao的用法 通过贴图AO和法线AO重叠

float is_shade = step(shade_threshold, shade); //调整阴影范围
if(is_shade>0.0f){
DIFFUSE_LIGHT += base_color*(LIGHT_COLOR*test);
}else{
DIFFUSE_LIGHT += (base_color+fade_ao_weight)*shade_color*(LIGHT_COLOR*test);
}

//本村轮廓线,添加线条细节
vec3 outline_color = texture(ilm, UV).aaa;
DIFFUSE_LIGHT *= outline_color;

float spec_mask = texture(ilm,UV).r; //ilm.r 高光mask 高光范围
float spec_shape = texture(ilm,UV).b; //高光形状mask
//float shininess = 100.f;

vec3 halfwayDir = normalize(LIGHT + VIEW); //获取法向?
float spec = pow(max(dot(NORMAL, halfwayDir), 0.0), shinese);

spec = spec*spec_mask*spec_shape;
float sm_spec = step(spec_threshold,spec);

SPECULAR_LIGHT += base_color* sm_spec;
}

//void fragment()
//{
//vec4 albedo_tex = texture(base, UV);
//ALBEDO=albedo_tex.rgb;
//}
  • 教程改的
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
shader_type spatial;
render_mode ambient_light_disabled;


uniform sampler2D base:source_color;
uniform sampler2D sss:source_color;
uniform sampler2D ilm:source_color;

uniform float shinese= 32.0f;

uniform float shade_threshold:hint_range(0,1)=0.3;
uniform float spec_threshold:hint_range(0,0.01)=0.05f;

varying vec4 v_color;


void vertex(){
v_color = COLOR;
}


void light(){
float LdotN = dot(LIGHT, NORMAL);

vec3 base_color = texture(base,UV).rgb; //基础颜色
vec3 shade_color = texture(sss,UV).rgb; //阴影颜色
vec3 outline_color = texture(ilm, UV).aaa; //本村轮廓线

float shadow_weight = texture(ilm, UV).g; //阴影权重
float fade_ao_weight = v_color.r; // 顶点色提供的环境光遮蔽

float shade = LdotN*shadow_weight*fade_ao_weight;
float is_shade = step(shade_threshold, shade);
if(is_shade>0.0f){
DIFFUSE_LIGHT += base_color;
}else{
DIFFUSE_LIGHT += (base_color+fade_ao_weight)*shade_color;
}
DIFFUSE_LIGHT *= outline_color;
//
//高光计算
//布林高光
float spec_mask = texture(ilm,UV).r;
float spec_shape = texture(ilm,UV).b;
float shininess = 32.0f;

vec3 halfwayDir = normalize(LIGHT + VIEW);
float spec = pow(max(dot(NORMAL, halfwayDir), 0.0), shininess);

spec = spec*spec_mask;

float sm_spec = step(spec_threshold,spec*spec_shape);

SPECULAR_LIGHT += base_color* sm_spec;


}
  • 轮廓
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
shader_type spatial;
render_mode unshaded, cull_front; //无光照,前面剔除

uniform float strength:hint_range(0,01) = 0.002; //轮廓粗细

void vertex(){
vec3 offset = NORMAL; // 我们要向法线方向膨胀
//计算顶点到摄像机的距离
float v2c_dist = clamp((MODELVIEW_MATRIX * vec4(VERTEX, 1.0)).z * -1.0,0.0,50.0);
offset.z *= COLOR.b; //顶点颜色.b 是法线z值偏移系数。值大就越往镜头,值小就越远离尽头陷入模型。

//COLOR.a 是精细控制轮廓线粗细
VERTEX += offset*strength*COLOR.a*v2c_dist*COLOR.g;
}

void fragment(){
ALBEDO = vec3(0.0f); //直接弄成黑色
}
  • 贴画decal
1
2
3
4
5
6
7
8
9
10
11
12
shader_type spatial;
render_mode ambient_light_disabled;

uniform sampler2D decal;
uniform sampler2D base;


void light(){
vec4 decal_color = texture(decal,UV);
ALPHA = (decal_color.r-0.5)/0.5f;
DIFFUSE_LIGHT += decal_color.rgb/0.5f;
}

兰伯特光照

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
shader_type spatial;
render_mode ambient_light_disabled;


uniform sampler2D base;
uniform sampler2D sss;
uniform sampler2D ilm;

uniform float shinese= 32.0f; //高光范围
uniform float shade_threshold:hint_range(0,1)=0.3;
uniform float spec_threshold:hint_range(0,0.1)=0.05f;

varying vec4 v_color;


void vertex(){
v_color = COLOR;
}


void light(){


vec3 base_color = texture(base,UV).rgb; //基础颜色
vec3 shade_color = texture(sss,UV).rgb; //阴影颜色

float shadow_weight = texture(ilm, UV).g; //阴影权重
float fade_ao_weight = v_color.r; // 顶点色提供的环境光遮蔽

float LdotN = dot(LIGHT, NORMAL);
float shade = LdotN*shadow_weight*fade_ao_weight;
float is_shade = step(shade_threshold, shade);
if(is_shade>0.0f){
DIFFUSE_LIGHT += base_color;
}else{
DIFFUSE_LIGHT += (base_color+fade_ao_weight)*shade_color;
}
}

void fragment()
{
vec4 albedo_tex = texture(base, UV);
ALBEDO=albedo_tex.rgb;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
//接收灯光颜色
shader_type spatial;
render_mode ambient_light_disabled;


uniform sampler2D base:source_color;
uniform sampler2D sss:source_color;
uniform sampler2D ilm:source_color;

uniform float shinese= 32.0f;

uniform float shade_threshold:hint_range(0,1)=0.3;
uniform float spec_threshold:hint_range(0,0.01)=0.05f;

varying vec4 v_color;



void vertex(){
v_color = COLOR;
}


void light(){
float LdotN = dot(LIGHT, NORMAL);

vec3 base_color = texture(base, UV).rgb; // 基础颜色
vec3 shade_color = texture(sss, UV).rgb; // 阴影颜色
vec3 outline_color = texture(ilm, UV).aaa; // 轮廓线颜色

float shadow_weight = texture(ilm, UV).g; // 阴影权重
float fade_ao_weight = v_color.r; // 顶点色提供的环境光遮蔽

float shade = LdotN * shadow_weight * fade_ao_weight;
float is_shade = step(shade_threshold, shade);

if (is_shade > 0.0f) {
DIFFUSE_LIGHT += base_color * LIGHT_COLOR; // 漫反射光乘以灯光颜色
} else {
DIFFUSE_LIGHT += (base_color + fade_ao_weight) * shade_color * LIGHT_COLOR; // 漫反射光乘以灯光颜色
}
DIFFUSE_LIGHT *= outline_color;

// 高光计算
float spec_mask = texture(ilm, UV).r;
float spec_shape = texture(ilm, UV).b;
float shininess = shinese;

vec3 halfwayDir = normalize(LIGHT + VIEW);
float spec = pow(max(dot(NORMAL, halfwayDir), 0.0), shininess);

spec = spec * spec_mask;

float sm_spec = step(spec_threshold, spec * spec_shape);

SPECULAR_LIGHT += base_color * sm_spec * LIGHT_COLOR; // 高光乘以灯光颜色
}