|
3 | 3 | import arc.graphics.g2d.*; |
4 | 4 | import arc.math.Mathf; |
5 | 5 | import arc.struct.ObjectMap; |
6 | | -import mindustry.core.World; |
| 6 | +import mindustry.Vars; |
7 | 7 | import mindustry.world.Block; |
| 8 | +import mindustry.world.blocks.environment.TreeBlock; |
8 | 9 | import mindustry.world.blocks.power.*; |
9 | 10 |
|
10 | 11 | public class AnyBlocksShadows { |
@@ -51,6 +52,11 @@ public static float getModifier(Block blk) { |
51 | 52 | return mod; |
52 | 53 | } |
53 | 54 |
|
| 55 | + /** |
| 56 | + * Draws a shadow volume using UNIFORM alpha on every quad. |
| 57 | + * KEY FIX: All quads share the same Draw color/alpha so overlapping regions |
| 58 | + * do NOT produce darker triangles. The shadow.frag shader handles soft edge :P |
| 59 | + */ |
54 | 60 | public static void draw(float cx, float cy, float size, float len, float cosA, float sinA) { |
55 | 61 | float hs = size * 0.5f; |
56 | 62 | float sdx = cosA * len; |
@@ -89,48 +95,124 @@ private static float jitter(float x, float y) { |
89 | 95 | return ((h & 0xFFFF) / 32767.5f) - 1.0f; |
90 | 96 | } |
91 | 97 |
|
92 | | - public static void drawOccludedLight(float lx, float ly, float radius) { |
93 | | - int segments = 40; |
94 | | - float angleStep = 360f / segments; |
95 | | - |
96 | | - for (int i = 0; i < segments; i++) { |
97 | | - float a1 = i * angleStep; |
98 | | - float a2 = (i + 1) * angleStep; |
99 | | - float d1 = cast(lx, ly, a1, radius); |
100 | | - float d2 = cast(lx, ly, a2, radius); |
101 | | - float x1 = lx + arc.math.Mathf.cosDeg(a1) * d1; |
102 | | - float y1 = ly + arc.math.Mathf.sinDeg(a1) * d1; |
103 | | - float x2 = lx + arc.math.Mathf.cosDeg(a2) * d2; |
104 | | - float y2 = ly + arc.math.Mathf.sinDeg(a2) * d2; |
105 | | - Fill.tri(lx, ly, x1, y1, x2, y2); |
106 | | - } |
107 | | - } |
108 | | - |
109 | | - private static float cast(float lx, float ly, float angle, float radius) { |
110 | | - final float[] result = { radius }; |
111 | | - float dx = arc.math.Mathf.cosDeg(angle); |
112 | | - float dy = arc.math.Mathf.sinDeg(angle); |
113 | | - |
114 | | - World.raycastEachWorld(lx, ly, lx + dx * radius, ly + dy * radius, (x, y) -> { |
115 | | - mindustry.world.Tile tile = mindustry.Vars.world.tile(x, y); |
116 | | - if (tile != null && tile.block().solid) { |
117 | | - if (Math.abs(tile.worldx() - lx) < 1f && Math.abs(tile.worldy() - ly) < 1f) |
118 | | - return false; |
119 | | - float d = arc.math.Mathf.dst(lx, ly, tile.worldx(), tile.worldy()); |
120 | | - if (d < result[0]) |
121 | | - result[0] = d; |
122 | | - return true; |
123 | | - } |
124 | | - return false; |
125 | | - }); |
126 | 98 |
|
127 | | - return result[0]; |
128 | | - } |
129 | 99 |
|
130 | 100 | public static arc.graphics.g2d.TextureRegion getOriginalShadow(mindustry.type.UnitType type) { |
131 | 101 | if (!unitShadowCache.containsKey(type)) { |
132 | 102 | unitShadowCache.put(type, type.shadowRegion); |
133 | 103 | } |
134 | 104 | return unitShadowCache.get(type); |
135 | 105 | } |
| 106 | + |
| 107 | + // Prop shadow system |
| 108 | + |
| 109 | + /** Shadow rendering category for environment props. */ |
| 110 | + public enum PropShadowType { TREE, ORB, SPIKE, GENERIC } |
| 111 | + |
| 112 | + // Per-block caches — filled once on first encounter, never GC-pressured |
| 113 | + private static final ObjectMap<Block, PropShadowType> propTypeCache = new ObjectMap<>(64); |
| 114 | + private static final ObjectMap<Block, Float> propElevCache = new ObjectMap<>(64); |
| 115 | + |
| 116 | + /** |
| 117 | + * Classifies a prop into a shadow rendering category. |
| 118 | + * Result is cached per block — zero cost on subsequent calls. |
| 119 | + */ |
| 120 | + public static PropShadowType getPropType(Block block, TextureRegion region) { |
| 121 | + if (propTypeCache.containsKey(block)) return propTypeCache.get(block); |
| 122 | + |
| 123 | + PropShadowType type; |
| 124 | + String name = block.name.toLowerCase(); |
| 125 | + |
| 126 | + if (block instanceof TreeBlock) { |
| 127 | + // Mindustry tree class tall plant shadow |
| 128 | + type = PropShadowType.TREE; |
| 129 | + } else if (name.contains("orb") || name.contains("sphere") || name.contains("ball")) { |
| 130 | + type = PropShadowType.ORB; |
| 131 | + } else if (name.contains("spike") || name.contains("thorn") || name.contains("needle")) { |
| 132 | + type = PropShadowType.SPIKE; |
| 133 | + } else if (region != null && region.found()) { |
| 134 | + float ar = region.height / (float) Math.max(region.width, 1); |
| 135 | + type = (ar > 1.4f) ? PropShadowType.TREE : PropShadowType.GENERIC; |
| 136 | + } else { |
| 137 | + type = PropShadowType.GENERIC; |
| 138 | + } |
| 139 | + |
| 140 | + propTypeCache.put(block, type); |
| 141 | + return type; |
| 142 | + } |
| 143 | + |
| 144 | + /** |
| 145 | + * Estimates the "height" of a prop from its sprite dimensions. |
| 146 | + * Taller sprites produce longer shadows at the same sun angle. |
| 147 | + * Result is cached per block — zero cost on subsequent calls. |
| 148 | + */ |
| 149 | + public static float getPropElevation(Block block, TextureRegion region) { |
| 150 | + if (propElevCache.containsKey(block)) return propElevCache.get(block, 0.3f); |
| 151 | + float propH = region.height * Draw.scl; |
| 152 | + // Normalize by 2× tile height so a 2-tile-tall sprite = elevation 1.0 |
| 153 | + float elev = Mathf.clamp(propH / (Vars.tilesize * 2f), 0.08f, 1.80f); |
| 154 | + propElevCache.put(block, elev); |
| 155 | + return elev; |
| 156 | + } |
| 157 | + |
| 158 | + /** |
| 159 | + * Draws a realistic prop shadow: |
| 160 | + * 1. A contact disk at the prop base (shows it is grounded). |
| 161 | + * 2. A body shadow displaced in the sun direction, shaped by PropShadowType. |
| 162 | + * Caller must set Draw.color to the desired shadow color before calling. |
| 163 | + * This method may temporarily change Draw.color for the contact disk, |
| 164 | + * and restores it before drawing the body shadow. |
| 165 | + * |
| 166 | + * @param cx prop world X |
| 167 | + * @param cy prop world Y |
| 168 | + * @param region sprite region (used for width/height reference) |
| 169 | + * @param type shadow shape category |
| 170 | + * @param propLen world-space shadow body length |
| 171 | + * @param cosA cos of sun direction angle |
| 172 | + * @param sinA sin of sun direction angle |
| 173 | + * @param angle sun direction angle in degrees (for Draw.rect rotation) |
| 174 | + * @param contactAlpha opacity of contact disk (0 = skip) |
| 175 | + */ |
| 176 | + public static void drawPropShadow(float cx, float cy, TextureRegion region, |
| 177 | + PropShadowType type, float propLen, float cosA, float sinA, float angle, |
| 178 | + float contactAlpha) { |
| 179 | + |
| 180 | + float propW = region.width * Draw.scl; |
| 181 | + |
| 182 | + if (contactAlpha > 0.005f) { |
| 183 | + Draw.color(0.02f, 0.015f, 0.04f, contactAlpha); |
| 184 | + Fill.circle(cx, cy, propW * 0.40f); |
| 185 | + // Restore body-shadow color |
| 186 | + Draw.color(0.04f, 0.03f, 0.08f, 1f); |
| 187 | + } |
| 188 | + switch (type) { |
| 189 | + case ORB: |
| 190 | + Fill.circle(cx + cosA * propLen * 0.35f, |
| 191 | + cy + sinA * propLen * 0.35f, |
| 192 | + propW * 0.30f); |
| 193 | + break; |
| 194 | + |
| 195 | + case SPIKE: { |
| 196 | + float bx = cx + cosA * propLen * 0.49f; |
| 197 | + float by = cy + sinA * propLen * 0.49f; |
| 198 | + Draw.rect(region, bx, by, propW * 0.40f, propLen, angle - 90f); |
| 199 | + break; |
| 200 | + } |
| 201 | + |
| 202 | + case TREE: { |
| 203 | + float bx = cx + cosA * propLen * 0.49f; |
| 204 | + float by = cy + sinA * propLen * 0.49f; |
| 205 | + Draw.rect(region, bx, by, propW * 0.85f, propLen, angle - 90f); |
| 206 | + break; |
| 207 | + } |
| 208 | + |
| 209 | + default: { |
| 210 | + float actualLen = propLen * 0.65f; |
| 211 | + float bx = cx + cosA * actualLen * 0.49f; |
| 212 | + float by = cy + sinA * actualLen * 0.49f; |
| 213 | + Draw.rect(region, bx, by, propW * 0.85f, actualLen, angle - 90f); |
| 214 | + break; |
| 215 | + } |
| 216 | + } |
| 217 | + } |
136 | 218 | } |
0 commit comments