|
|
@@ -18,7 +18,7 @@ CCEffect %{
|
|
|
properties:
|
|
|
edgeColor: { value: [1.0, 1.0, 1.0, 1.0] }
|
|
|
textureSize: { value: [512.0, 512.0] }
|
|
|
- edgeWidth: { value: 40.0 }
|
|
|
+ edgeWidth: { value: 50.0 }
|
|
|
alphaThreshold: { value: 0.5 }
|
|
|
}%
|
|
|
|
|
|
@@ -67,7 +67,7 @@ CCProgram sprite-fs %{
|
|
|
precision highp float;
|
|
|
#include <builtin/internal/embedded-alpha>
|
|
|
#include <builtin/internal/alpha-test>
|
|
|
- #define MAX_EDGE_WIDTH 15.0 // 最大边缘宽度(可根据需求调整)
|
|
|
+ #define MAX_EDGE_WIDTH 50.0 // 最大边缘宽度(可根据需求调整)
|
|
|
in vec4 color;
|
|
|
|
|
|
#if USE_TEXTURE
|
|
|
@@ -96,19 +96,41 @@ CCProgram sprite-fs %{
|
|
|
float edgeDetected = 0.0;
|
|
|
if (currentAlpha > 0.01) {
|
|
|
// Check if we're at the edge by sampling neighboring pixels
|
|
|
- for (float x = -MAX_EDGE_WIDTH; x <= MAX_EDGE_WIDTH; x += 1.0) {
|
|
|
- for (float y = -MAX_EDGE_WIDTH; y <= MAX_EDGE_WIDTH; y += 1.0) {
|
|
|
- if (x == 0.0 && y == 0.0) continue;
|
|
|
-
|
|
|
- vec2 sampleUV = uv0 + vec2(x, y) * texelSize;
|
|
|
- float sampleAlpha = CCSampleWithAlphaSeparated(cc_spriteTexture, sampleUV).a;
|
|
|
-
|
|
|
- // If we find a transparent pixel nearby, we're at an edge
|
|
|
- if (sampleAlpha < 0.01) {
|
|
|
- float distance = length(vec2(x, y));
|
|
|
- if (distance <= edgeWidth) {
|
|
|
- float edgeStrength = 1.0 - (distance / edgeWidth);
|
|
|
- edgeDetected = max(edgeDetected, edgeStrength);
|
|
|
+ float maxDistance = min(edgeWidth, MAX_EDGE_WIDTH);
|
|
|
+ bool isEdge = false;
|
|
|
+
|
|
|
+ // Check boundary conditions first (image edges)
|
|
|
+ vec2 pixelPos = uv0 * textureSize;
|
|
|
+ float distToLeft = pixelPos.x;
|
|
|
+ float distToRight = textureSize.x - pixelPos.x;
|
|
|
+ float distToTop = pixelPos.y;
|
|
|
+ float distToBottom = textureSize.y - pixelPos.y;
|
|
|
+
|
|
|
+ float minDistToBorder = min(min(distToLeft, distToRight), min(distToTop, distToBottom));
|
|
|
+
|
|
|
+ if (minDistToBorder <= maxDistance) {
|
|
|
+ isEdge = true;
|
|
|
+ edgeDetected = max(edgeDetected, 1.0 - (minDistToBorder / maxDistance));
|
|
|
+ }
|
|
|
+
|
|
|
+ // Also check for alpha transitions
|
|
|
+ if (!isEdge) {
|
|
|
+ // 改为固定范围循环(-MAX_EDGE_WIDTH 到 MAX_EDGE_WIDTH)
|
|
|
+ for (float x = -MAX_EDGE_WIDTH; x <= MAX_EDGE_WIDTH; x += 1.0) {
|
|
|
+ for (float y = -MAX_EDGE_WIDTH; y <= MAX_EDGE_WIDTH; y += 1.0) {
|
|
|
+ if (x == 0.0 && y == 0.0) continue;
|
|
|
+
|
|
|
+ vec2 sampleUV = uv0 + vec2(x, y) * texelSize;
|
|
|
+ float sampleAlpha = CCSampleWithAlphaSeparated(cc_spriteTexture, sampleUV).a;
|
|
|
+
|
|
|
+ // If we find a transparent pixel nearby, we're at an edge
|
|
|
+ if (sampleAlpha < 0.01) {
|
|
|
+ float distance = length(vec2(x, y));
|
|
|
+ if (distance <= maxDistance) {
|
|
|
+ float edgeStrength = 1.0 - (distance / maxDistance);
|
|
|
+ edgeDetected = max(edgeDetected, edgeStrength);
|
|
|
+ isEdge = true;
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
}
|