GlSL常用函数算法
GlSL常用函数算法
常用函数算法
- 一些平时自己经常用的函数算法
淡入淡出函数
1
2
3
4
float fadeInOut(lowp float t) {
// 淡入淡出效果
return smoothstep(0.0, 0.5, t) * (1.0 - smoothstep(0.5, 1.0, t));
}
UV旋转
1
2
3
4
5
6
7
8
9
10
11
12
13
//角度
vec2 Rotate_Degrees(vec2 UV, vec2 Center, float Rotation) {
Rotation = radians(Rotation); // 角度转弧度
UV -= Center;
float s = sin(Rotation);
float c = cos(Rotation);
UV = vec2(
UV.x * c - UV.y * s,
UV.x * s + UV.y * c
);
UV += Center;
return UV;
}
UV中心缩放
vertex shader
1
2
3
4
5
6
7
8
vec2 UVScale(vec2 uv, float scaleFactor, bool clampToEdge) {
vec2 center = vec2(0.5);
vec2 relativeUV = uv - center;
relativeUV *= scaleFactor;
vec2 result = relativeUV + center;
return result;
}
PS中的常见效果算法函数
叠加混合
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// 叠加混合模式的实现
vec3 overlayBlend(vec3 base, vec3 blend) {
// 根据基色值执行不同的混合计算
return mix(
2.0 * base * blend,
1.0 - 2.0 * (1.0 - base) * (1.0 - blend),
step(0.5, base)
);
}
// 带透明度的叠加混合模式
vec4 overlayBlendWithAlpha(vec4 base, vec4 blend) {
// 计算叠加混合后的RGB值
vec3 rgb = overlayBlend(base.rgb, blend.rgb);
// 计算最终的透明度
float alpha = base.a + blend.a * (1.0 - base.a);
// 考虑透明度的最终颜色
return vec4(mix(base.rgb, rgb, blend.a * (1.0 - base.a) / alpha), alpha);
}
本文由作者按照 CC BY 4.0 进行授权