瀏覽代碼

优化边缘变色

181404010226 4 月之前
父節點
當前提交
312fb2ec40

+ 1 - 4
assets/Scenes/GameLevel.scene

@@ -17222,10 +17222,7 @@
     },
     "_enabled": true,
     "__prefab": null,
-    "_customMaterial": {
-      "__uuid__": "76a76f1d-d1a0-4d66-b032-e1b0a442bf0c",
-      "__expectedType__": "cc.Material"
-    },
+    "_customMaterial": null,
     "_srcBlendFactor": 2,
     "_dstBlendFactor": 4,
     "_color": {

+ 4 - 1
assets/assets/Prefabs/Block001.prefab

@@ -428,7 +428,10 @@
     "__prefab": {
       "__id__": 19
     },
-    "_customMaterial": null,
+    "_customMaterial": {
+      "__uuid__": "76a76f1d-d1a0-4d66-b032-e1b0a442bf0c",
+      "__expectedType__": "cc.Material"
+    },
     "_srcBlendFactor": 2,
     "_dstBlendFactor": 4,
     "_color": {

+ 4 - 1
assets/assets/Prefabs/Block002.prefab

@@ -428,7 +428,10 @@
     "__prefab": {
       "__id__": 19
     },
-    "_customMaterial": null,
+    "_customMaterial": {
+      "__uuid__": "76a76f1d-d1a0-4d66-b032-e1b0a442bf0c",
+      "__expectedType__": "cc.Material"
+    },
     "_srcBlendFactor": 2,
     "_dstBlendFactor": 4,
     "_color": {

+ 4 - 1
assets/assets/Prefabs/Block003.prefab

@@ -519,7 +519,10 @@
     "__prefab": {
       "__id__": 23
     },
-    "_customMaterial": null,
+    "_customMaterial": {
+      "__uuid__": "76a76f1d-d1a0-4d66-b032-e1b0a442bf0c",
+      "__expectedType__": "cc.Material"
+    },
     "_srcBlendFactor": 2,
     "_dstBlendFactor": 4,
     "_color": {

+ 4 - 1
assets/assets/Prefabs/Block004.prefab

@@ -610,7 +610,10 @@
     "__prefab": {
       "__id__": 27
     },
-    "_customMaterial": null,
+    "_customMaterial": {
+      "__uuid__": "76a76f1d-d1a0-4d66-b032-e1b0a442bf0c",
+      "__expectedType__": "cc.Material"
+    },
     "_srcBlendFactor": 2,
     "_dstBlendFactor": 4,
     "_color": {

+ 4 - 1
assets/assets/Prefabs/Block005.prefab

@@ -610,7 +610,10 @@
     "__prefab": {
       "__id__": 27
     },
-    "_customMaterial": null,
+    "_customMaterial": {
+      "__uuid__": "76a76f1d-d1a0-4d66-b032-e1b0a442bf0c",
+      "__expectedType__": "cc.Material"
+    },
     "_srcBlendFactor": 2,
     "_dstBlendFactor": 4,
     "_color": {

+ 37 - 15
assets/resources/shaders/builtin-sprite.effect

@@ -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;
+                }
               }
             }
           }