// Copyright (c) 2017-2020 Xiamen Yaji Software Co., Ltd. CCEffect %{ techniques: - passes: - vert: sprite-vs:vert frag: circle-hole-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 } circles: { value: [0.5, 0.5, 0.5, 0.5], editor: { type: array } } maskColor: { value: [0.5, 0.5, 0.5, 0.5], editor: { type: color } } textureSize: { value: [300, 300] } }% CCProgram sprite-vs %{ precision highp float; #include #if USE_LOCAL #include #endif #if SAMPLE_FROM_RT #include #endif in vec3 a_position; in vec2 a_texCoord; in vec4 a_color; out vec4 color; out vec2 uv0; vec4 vert() { vec4 pos = vec4(a_position, 1); #if USE_LOCAL pos = cc_matWorld * pos; #endif #if USE_PIXEL_ALIGNMENT pos = cc_matView * pos; pos.xyz = floor(pos.xyz); pos = cc_matProj * pos; #else pos = cc_matViewProj * pos; #endif uv0 = a_texCoord; #if SAMPLE_FROM_RT CC_HANDLE_RT_SAMPLE_FLIP(uv0); #endif color = a_color; return pos; } }% CCProgram circle-hole-fs %{ precision highp float; #include #include in vec4 color; in vec2 uv0; #pragma builtin(local) layout(set = 2, binding = 12)uniform sampler2D cc_spriteTexture; uniform ufs { vec4 circles[20]; // 存储多个圆的信息,每个元素包含圆心坐标、半径、渐变宽度 vec4 maskColor; vec2 textureSize; }; vec4 frag() { vec4 o = maskColor; // 遍历每个圆的信息来判断像素是否在孔洞内并应用效果 for(int i = 0; i < 10; ++ i) { // 同样这里10根据实际设置的最大圆数量来定,可优化成根据circles实际传入的有效数量来循环 float radius = circles[i].z; radius = circles[i].z / max(textureSize.x, textureSize.y); float dis = distance(uv0, circles[i].xy); if (dis < radius) { float gradient = smoothstep(radius - circles[i].w , radius, dis); o.a = min(gradient * maskColor.a, o.a); // 取多个圆作用后的最小透明度,可根据需求调整合成方式 } } #if USE_TEXTURE o *= texture(cc_spriteTexture, uv0); #endif o *= color; ALPHA_TEST(o); return o; } }%