11package dynamicShadows ;
22
3- import arc .graphics .g2d .Fill ;
4- import arc .struct .ObjectFloatMap ;
3+ import arc .graphics .g2d .*;
4+ import arc .math .Mathf ;
5+ import arc .struct .ObjectMap ;
56import mindustry .core .World ;
67import mindustry .world .Block ;
78import mindustry .world .blocks .power .*;
89
9- /**
10- * Sombreado Dinamico para cualquier tipo de bloque
11- @author Arksource
12- */
1310public class AnyBlocksShadows {
14- private static final ObjectFloatMap <Block > modCache = new ObjectFloatMap <>(128 );
15- private static final arc . struct . ObjectMap <mindustry .type .UnitType , arc . graphics . g2d . TextureRegion > unitShadowCache = new arc . struct . ObjectMap <>();
11+ private static final ObjectMap <Block , Float > modCache = new ObjectMap <>();
12+ private static final ObjectMap <mindustry .type .UnitType , TextureRegion > unitShadowCache = new ObjectMap <>();
1613
17- /** @return El multiplicador de longitud de sombra para un bloque específico. */
1814 public static float getModifier (Block blk ) {
1915 if (modCache .containsKey (blk ))
2016 return modCache .get (blk , 1f );
2117
22- float mod = 0.8f ;
18+ float mod ;
2319 if (blk instanceof PowerNode || blk instanceof LightBlock ) {
2420 mod = 0.0f ;
25- }
26- else if (blk .isStatic () && blk .solid ) {
27- mod = (blk .size == 1 ) ? 0.2f : 0.20f ;
28- }
29- //Based on the size of the block (Artificial Structures)
30- else {
21+ } else if (blk .isStatic () && blk .solid ) {
22+ mod = 0.20f ;
23+ } else {
3124 switch (blk .size ) {
3225 case 1 :
33- mod = 0.013f ;
26+ mod = 0.06f ;
3427 break ;
3528 case 2 :
36- mod = 0.02f ;
29+ mod = 0.08f ;
3730 break ;
3831 case 3 :
39- mod = 0.03f ;
32+ mod = 0.10f ;
4033 break ;
4134 case 4 :
42- mod = 0.04f ;
35+ mod = 0.15f ;
4336 break ;
4437 case 5 :
45- mod = 0.05f ;
38+ mod = 0.22f ;
4639 break ;
4740 case 6 :
48- mod = 0.06f ;
41+ mod = 0.30f ;
4942 break ;
5043 default :
51- mod = 0.07f ;
44+ mod = 0.35f ;
5245 break ;
5346 }
47+
5448 }
5549
5650 modCache .put (blk , mod );
5751 return mod ;
5852 }
5953
54+ /**
55+ * Draws a shadow volume using UNIFORM alpha on every quad.
56+ * KEY FIX: All quads share the same Draw color/alpha so overlapping regions
57+ * do NOT produce darker triangles. The shadow.frag shader handles soft edge :P
58+ */
6059 public static void draw (float cx , float cy , float size , float len , float cosA , float sinA ) {
6160 float hs = size * 0.5f ;
6261 float sdx = cosA * len ;
6362 float sdy = sinA * len ;
63+
6464 float xL = cx - hs , xR = cx + hs ;
6565 float yB = cy - hs , yT = cy + hs ;
6666
67- // 1. Capas base
68- Fill .quad (xL , yB , xR , yB , xR , yT , xL , yT );
69- Fill .quad (xL + sdx , yB + sdy , xR + sdx , yB + sdy , xR + sdx , yT + sdy , xL + sdx , yT + sdy );
67+ float js = size * 0.14f ;
7068
71- // 2. Paredes laterales
72- Fill .quad (xL , yB , xR , yB , xR + sdx , yB + sdy , xL + sdx , yB + sdy );
73- Fill .quad (xL , yT , xR , yT , xR + sdx , yT + sdy , xL + sdx , yT + sdy );
74- Fill .quad (xL , yB , xL , yT , xL + sdx , yT + sdy , xL + sdx , yB + sdy );
75- Fill .quad (xR , yB , xR , yT , xR + sdx , yT + sdy , xR + sdx , yB + sdy );
69+ float txBL = (xL + sdx ) + jitter (xL , yB ) * js ;
70+ float tyBL = (yB + sdy ) + jitter (yB , xL + 1f ) * js ;
71+ float txBR = (xR + sdx ) + jitter (xR , yB + 1f ) * js ;
72+ float tyBR = (yB + sdy ) + jitter (yB , xR ) * js ;
73+ float txTL = (xL + sdx ) + jitter (xL , yT + 1f ) * js ;
74+ float tyTL = (yT + sdy ) + jitter (yT , xL ) * js ;
75+ float txTR = (xR + sdx ) + jitter (xR + 1f , yT ) * js ;
76+ float tyTR = (yT + sdy ) + jitter (yT + 1f , xR ) * js ;
7677
77- // 3. Relleno Cúbico Interno
78- Fill .quad (xL , yB , xR , yT , xR + sdx , yT + sdy , xL + sdx , yB + sdy );
79- Fill .quad (xR , yB , xL , yT , xL + sdx , yT + sdy , xR + sdx , yB + sdy );
78+ Fill .quad (xL , yB , xR , yB , xR , yT , xL , yT );
79+ Fill .quad (txBL , tyBL , txBR , tyBR , txTR , tyTR , txTL , tyTL );
80+ Fill .quad (xL , yB , xR , yB , txBR , tyBR , txBL , tyBL );
81+ Fill .quad (xL , yT , xR , yT , txTR , tyTR , txTL , tyTL );
82+ Fill .quad (xL , yB , xL , yT , txTL , tyTL , txBL , tyBL );
83+ Fill .quad (xR , yB , xR , yT , txTR , tyTR , txBR , tyBR );
84+ Fill .quad (xL , yB , xR , yT , txTR , tyTR , txBL , tyBL );
85+ Fill .quad (xR , yB , xL , yT , txTL , tyTL , txBR , tyBR );
86+ }
87+
88+ private static float jitter (float x , float y ) {
89+ int ix = Mathf .round (x * 0.01f );
90+ int iy = Mathf .round (y * 0.01f );
91+ int h = ix * 374761393 + iy * 668265263 ;
92+ h = (h ^ (h >> 13 )) * 1274126177 ;
93+ h = h ^ (h >> 16 );
94+ return ((h & 0xFFFF ) / 32767.5f ) - 1.0f ;
8095 }
8196
8297 public static void drawOccludedLight (float lx , float ly , float radius ) {
@@ -86,15 +101,12 @@ public static void drawOccludedLight(float lx, float ly, float radius) {
86101 for (int i = 0 ; i < segments ; i ++) {
87102 float a1 = i * angleStep ;
88103 float a2 = (i + 1 ) * angleStep ;
89-
90104 float d1 = cast (lx , ly , a1 , radius );
91105 float d2 = cast (lx , ly , a2 , radius );
92-
93106 float x1 = lx + arc .math .Mathf .cosDeg (a1 ) * d1 ;
94107 float y1 = ly + arc .math .Mathf .sinDeg (a1 ) * d1 ;
95108 float x2 = lx + arc .math .Mathf .cosDeg (a2 ) * d2 ;
96109 float y2 = ly + arc .math .Mathf .sinDeg (a2 ) * d2 ;
97-
98110 Fill .tri (lx , ly , x1 , y1 , x2 , y2 );
99111 }
100112 }
@@ -109,11 +121,10 @@ private static float cast(float lx, float ly, float angle, float radius) {
109121 if (tile != null && tile .block ().solid ) {
110122 if (Math .abs (tile .worldx () - lx ) < 1f && Math .abs (tile .worldy () - ly ) < 1f )
111123 return false ;
112-
113124 float d = arc .math .Mathf .dst (lx , ly , tile .worldx (), tile .worldy ());
114125 if (d < result [0 ])
115126 result [0 ] = d ;
116- return true ; // Stop
127+ return true ;
117128 }
118129 return false ;
119130 });
0 commit comments