时间:2023-9-23 作者:虣虣 分类: CocosCreator
我们线看一下这几个单词在这个语境下怎么翻译
所以SDF是用来判断一个点是否在一个区域内, 负数表示在区域内, 整数表示区域外
CCEffect %{
techniques:
- passes:
- vert: sprite-vs:vert
frag: sprite-fs:frag
depthStencilState:
depthTest: false
depthWrite: false
blendState:
targets:
- blend: true
blendSrc: src_alpha
blendDst: one_minus_src_alpha
blendDstAlpha: one_minus_src_alpha
rasterizerState:
cullMode: none
properties:
alphaThreshold: { value: 0.5 }
}%
CCProgram sprite-vs %{
precision highp float;
#include <builtin/uniforms/cc-global>
in vec3 a_position;
in vec2 a_texCoord;
in vec4 a_color;
out vec2 uv0;
vec4 vert () {
vec4 pos = vec4(a_position, 1);
pos = cc_matViewProj * pos;
uv0 = a_texCoord;
return pos;
}
}%
CCProgram sprite-fs %{
precision highp float;
#include <builtin/internal/embedded-alpha>
#include <builtin/internal/alpha-test>
in vec2 uv0;
float circleSDK(vec2 uv, float radius) {
float dis = length(uv - vec2(0.5, 0.5)) - radius;
return dis;
}
vec4 frag () {
float sdf = circleSDK(uv0, 0.2);
return vec4(sdf, sdf, sdf, 1);
}
}%
float circleSDK(vec2 uv, float radius) {
// 因为使用的是UVz坐标,想要对其屏幕的中心,所以要偏移0.5个坐标
float dis = length(uv - vec2(0.5, 0.5)) - radius;
// 减去半径(radius), 大于0则在圆外,小于0则在圆内
return dis;
}
vec4 frag () {
// step函数: 参数1 > 参数2 ? 0 : 1
float sdf = step(0.001, circleSDK(uv0, 0.2));
return vec4(sdf, sdf, sdf, 1);
}
vec4 frag () {
float sdf = circleSDK(uv0, 0.2);
float edge = step(0.001, abs(sdf));
// mix函数: 同lerp函数一样效果 线性插值运算
vec4 o = mix(vec4(0, 0, 0, 1), vec4(1, 1, 1, 1), edge);
return o;
}
vec4 frag () {
//连续增长段
float sdf = circleSDK(uv0, 0.2) * 30.0;
// 离散化增长段
float seg = floor(sdf);
//离散段绘制
vec4 o = mix(vec4(0, 0, 0, 1), vec4(1, 1, 1, 1), seg / 5.0);
return o;
}
properties:
centerColor: {value: [0.7, 0.5, 0.3, 1]}
edgeColor: {value: [0.4, 0.3, 0.3, 1]}
vec4 frag () {
float sdf = circleSDK(uv0, 0.2) * 30.0;
float seg = floor(sdf);
vec4 o = mix(centerColor, edgeColor, seg / 5.0);
return o;
}
vec4 frag () {
// 连续增长段
float sdf = circleSDK(uv0, 0.2) * 30.0;
// 离散化增长段
float seg = floor(sdf);
// 连续减离散区间,得到[0, 1]的取值范围
seg = sdf - seg;
vec4 o = mix(centerColor, edgeColor, seg / 5.0);
o = mix(vec4(0, 0, 0, 1), o, smoothstep(0.0, 0.1, 0.5 - abs(seg - 0.5)));
//查找边
float edge = step(0.1, abs(sdf));
// 绘制圆的编译
o = mix(vec4(1, 0, 0, 1), o, edge);
return o;
}
float squareSDF(vec2 uv, float width) {
uv = abs(uv - 0.3) - width;
float inner = min( max(uv.x, uv.y), 0.0); //inner
float outter = length(max(uv, vec2(0, 0))); // outerr
return inner + outter;
}
// 求并集
float unionSDF(float a, float b)
{
return min(a, b);
}
// 矩形和圆求并集运算
vec4 frag () {
float squareSdf = step(0.001, squareSDF(uv0, 0.2));
float circleSdf = step(0.001, circleSDK(uv0, 0.2));
float sdf = unionSDF(squareSdf, circleSdf);
return vec4(sdf, sdf, sdf, 1);
}
// 求交集
float subtractionSDF(float a, float b)
{
return max(a, b);
}
// 矩形和圆交集运算
vec4 frag () {
float squareSdf = step(0.001, squareSDF(uv0, 0.2));
float circleSdf = step(0.001, circleSDK(uv0, 0.2));
float sdf = subtractionSDF(squareSdf, circleSdf);
return vec4(sdf, sdf, sdf, 1);
}
更多有效干活请关注 微信公共号【游戏讲坛】