green-pack-cocos/assets/resources/effects/puzzle.effect
ruanwujing eb5bd5307d 拼图
2024-02-04 16:08:11 +08:00

139 lines
3.3 KiB
Plaintext

// Copyright (c) 2017-2020 Xiamen Yaji Software Co., Ltd.
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:
rows: {value: 4}
columns: {value: 8}
sizeExpand: {value: 0.6}
radius: {value: 0.15}
}%
CCProgram sprite-vs %{
precision highp float;
#include <builtin/uniforms/cc-global>
#if USE_LOCAL
#include <builtin/uniforms/cc-local>
#endif
#if SAMPLE_FROM_RT
#include <common/common-define>
#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 sprite-fs %{
precision highp float;
#include <builtin/internal/embedded-alpha>
#include "../chunks/sdf2d"
uniform Const {
float rows;
float columns;
float sizeExpand;
float radius;
};
in vec4 color;
in vec2 uv0;
float smin(float a,float b,float k){
float h = clamp(0.5+0.5*(a-b)/k,0.0,1.0);
return mix(a,b,h)-k*h*(1.0-h);
}
float smax(float a,float b,float k){
return -smin(-a,-b,k);
}
float isOdd(float x) {
return fract(x/2.0) * 2.0;
}
#pragma builtin(local)
layout(set = 2, binding = 12) uniform sampler2D cc_spriteTexture;
vec4 frag () {
vec4 o = vec4(1, 1, 1, 1);
vec2 id = floor(uv0 * vec2(columns, rows));
vec2 uv_center = (id + 0.5) / vec2(columns, rows);
float exp = sizeExpand + 1.0;
vec2 uv = (uv0 - uv_center) * vec2(columns, rows) * exp;
o *= CCSampleWithAlphaSeparated(cc_spriteTexture, (uv0 - uv_center) * exp + uv_center);
float d = sdBox(uv, vec2(0.5, 0.5));
float rad = radius;
float dd = 0.1;
float odd = isOdd(id.x + id.y);
float s = sign(odd - 0.5);
float dc_top = sdCircle(uv + vec2(0, 0.5 - dd * s), rad);
float dc_bottom = sdCircle(uv + vec2(0, -0.5 + dd * s), rad);
float dc_left = sdCircle(uv + vec2(0.5 + dd * s, 0), rad);
float dc_right = sdCircle(uv + vec2(-0.5 - dd * s, 0), rad);
float k = 0.02;
float nOdd = 1.0 - odd;
float dt = smax(d, -dc_top, k) * odd + smin(d, dc_top , k) * nOdd;
float edge = step(id.y, 0.5);
d = dt * (1.0 - edge) + d * edge;
float db = smax(d, -dc_bottom, k) * odd + smin(d, dc_bottom, k) * nOdd;
edge = step(rows - 1.5, id.y);
d = db * (1.0 - edge) + d * edge;
float dl = smin(d, dc_left, k) * odd + smax(d, -dc_left, k) * nOdd;
edge = step(id.x, 0.5);
d = dl * (1.0 - edge) + d * edge;
float dr = smin(d, dc_right, k) * odd + smax(d, -dc_right, k) * nOdd;
edge = step(columns - 1.5, id.x);
d = dr * (1.0 - edge) + d * edge;
float c = smoothstep(0.01, -0.01, d);
o *= c;
return o;
}
}%