Skip to content

Commit 1a9479a

Browse files
authored
Add files via upload
1 parent 9a6f87a commit 1a9479a

6 files changed

Lines changed: 557 additions & 257 deletions

File tree

‎assets/shaders/shadow.frag‎

Lines changed: 4 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -14,45 +14,13 @@ uniform float u_edgeNoise;
1414
uniform float u_shadowTint;
1515
uniform float u_contactShadow;
1616

17-
// ── Noise helpers ─────────────────────────────────────────────────────────────
1817

19-
float hash(vec2 p) {
20-
p = fract(p * vec2(127.1, 311.7));
21-
p += dot(p, p + 17.43);
22-
return fract(p.x * p.y);
23-
}
24-
25-
float valueNoise(vec2 p) {
26-
vec2 i = floor(p);
27-
vec2 f = fract(p);
28-
vec2 u = f * f * (3.0 - 2.0 * f);
29-
float a = hash(i);
30-
float b = hash(i + vec2(1.0, 0.0));
31-
float c = hash(i + vec2(0.0, 1.0));
32-
float d = hash(i + vec2(1.0, 1.0));
33-
return mix(mix(a, b, u.x), mix(c, d, u.x), u.y);
34-
}
35-
36-
// Two-octave fBm for richer, natural edge irregularity
37-
float fbm(vec2 p) {
38-
float v = 0.0;
39-
float amp = 0.5;
40-
for (int i = 0; i < 2; i++) {
41-
v += valueNoise(p) * amp;
42-
p *= 2.1;
43-
amp *= 0.5;
44-
}
45-
return v;
46-
}
4718

48-
// ── Main ──────────────────────────────────────────────────────────────────────
19+
// Main
4920

5021
void main() {
5122
vec2 texel = 1.0 / u_resolution;
5223

53-
// ── 1. Separable Gaussian blur (9-tap, one direction per pass) ───────────
54-
// When sun is near horizon (low elevation), shadows are longer and softer →
55-
// scale the blur radius up so penumbra widens at dawn/dusk.
5624
float elevFactor = 1.0 - clamp(u_sunElevation, 0.0, 1.0); // 0 at noon, 1 at horizon
5725
float dynamicRad = u_radius * (1.0 + elevFactor * 1.4); // up to 2.4× at horizon
5826

@@ -71,46 +39,25 @@ void main() {
7139

7240
float alpha = col.a;
7341

74-
// ── 2. Distance-based fade — shadow dissipates at the tip ────────────────
75-
float distFade = smoothstep(0.0, 0.50, alpha);
76-
77-
// ── 3. Procedural fBm edge noise ─────────────────────────────────────────
78-
vec2 worldPos = v_texCoord * u_camSize + (u_cameraPos - u_camSize * 0.5);
79-
vec2 noiseUV = worldPos * 0.016;
80-
float noise = fbm(noiseUV);
42+
float distFade = smoothstep(0.0, 0.15, alpha);
8143

8244
float edgeFactor = smoothstep(0.0, 0.40, alpha) * (1.0 - smoothstep(0.50, 1.0, alpha));
83-
float noiseDisp = (noise - 0.5) * 2.0 * u_edgeNoise * edgeFactor;
84-
alpha = clamp(alpha + noiseDisp, 0.0, 1.0);
85-
86-
8745

88-
// ── 5. Contact-shadow darkening (root zone is always deepest) ────────────
89-
// High-alpha areas (near caster base) get an extra multiplicative darkening.
90-
// This pushes the "grounded" zone darker without touching the tip.
9146
float contactBoost = smoothstep(0.55, 1.0, alpha) * u_contactShadow;
9247

93-
// ── 6. Dynamic tint from solar elevation ─────────────────────────────────
94-
// sunElevation: -1=midnight, 0=horizon, +1=noon
95-
float sunE = u_sunElevation; // -1 .. +1
48+
float sunE = u_sunElevation;
9649

97-
// Day: soft blue-grey (sky scatter fills shadow colour)
9850
vec3 dayTint = vec3(0.06, 0.07, 0.12);
99-
// Dawn / dusk: warm orange-red (low sun bakes the shadow in amber)
100-
// Peaks at elevation ≈ 0 (horizon crossing)
101-
float horizonFactor = exp(-sunE * sunE * 8.0); // Gaussian peak at sunE=0
51+
float horizonFactor = exp(-sunE * sunE * 8.0);
10252
vec3 horizonTint = vec3(0.16, 0.06, 0.01) * horizonFactor;
103-
// Night: deep indigo-violet (moonlight tint)
10453
float nightFactor = clamp(-sunE, 0.0, 1.0);
10554
vec3 nightTint = vec3(0.03, 0.02, 0.09) * nightFactor;
10655

10756
vec3 rawTint = dayTint + horizonTint + nightTint;
10857
vec3 shadowColor = mix(vec3(0.0), rawTint, u_shadowTint);
10958

110-
// Warm halo at the very edge (physics: diffracted warm light outlines shadow)
11159
shadowColor += vec3(0.025, 0.010, 0.0) * edgeFactor * u_shadowTint * (1.0 - nightFactor);
11260

113-
// ── Combine ───────────────────────────────────────────────────────────────
11461
float finalAlpha = alpha * distFade * (1.0 + contactBoost);
11562
finalAlpha = clamp(finalAlpha, 0.0, 1.0);
11663

‎bundles/bundle.properties‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
11
setting.dynamic_shadows_enabled.name=Enabled
22
setting.day_night_cycle.name=Day/Night Cycle
3+
setting.dynamic_unit_shadows.name=Dynamic Unit Shadows
4+
setting.graphics_quality.name=Graphics Quality
5+
setting.graphics_quality.val.0=Low
6+
setting.graphics_quality.val.1=Medium
7+
setting.graphics_quality.val.2=High
38
setting.shadow_length.name=Shadow Length
9+
setting.shadow_length.tiles=tiles
10+
setting.prop_shadow_scale.name=Prop Shadow Scale
11+
setting.prop_shadow_scale.old=Mindustry (Old)
412
setting.shadow_opacity_percent.name=Shadow Opacity
513
setting.blur_radius.name=Blur Radius
614
setting.shadow_tint_percent.name=Shadow Tint
715
setting.contact_shadow_percent.name=Contact Shadow
816
setting.dark_fade_percent.name=Dark Fade
17+
setting.anti_aliasing.name=Anti-Aliasing (FXAA)
918
settings.dynamic_shadows.header=Dynamic Shadows
10-
settings.dynamic_shadows.category=Shaders
19+
settings.dynamic_shadows.category=Shaders

‎bundles/bundle_es.properties‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
11
setting.dynamic_shadows_enabled.name=Activado
22
setting.day_night_cycle.name=Ciclo Día/Noche
3+
setting.dynamic_unit_shadows.name=Sombras Dinámicas de Unidades
4+
setting.graphics_quality.name=Calidad Gráfica
5+
setting.graphics_quality.val.0=Baja
6+
setting.graphics_quality.val.1=Media
7+
setting.graphics_quality.val.2=Alta
38
setting.shadow_length.name=Longitud de Sombra
9+
setting.shadow_length.tiles=bloques
10+
setting.prop_shadow_scale.name=Escala de Sombra de Props
11+
setting.prop_shadow_scale.old=Mindustry (Antiguas)
412
setting.shadow_opacity_percent.name=Opacidad de Sombra
513
setting.blur_radius.name=Radio de Desenfoque
614
setting.shadow_tint_percent.name=Tinte de Sombra
715
setting.contact_shadow_percent.name=Sombra de Contacto
816
setting.dark_fade_percent.name=Desvanecimiento Nocturno
17+
setting.anti_aliasing.name=Antialiasing (FXAA)
918
settings.dynamic_shadows.header=Sombras Dinámicas
10-
settings.dynamic_shadows.category=Shaders
19+
settings.dynamic_shadows.category=Sombras

‎src/dynamicShadows/AnyBlocksShadows.java‎

Lines changed: 119 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
import arc.graphics.g2d.*;
44
import arc.math.Mathf;
55
import arc.struct.ObjectMap;
6-
import mindustry.core.World;
6+
import mindustry.Vars;
77
import mindustry.world.Block;
8+
import mindustry.world.blocks.environment.TreeBlock;
89
import mindustry.world.blocks.power.*;
910

1011
public class AnyBlocksShadows {
@@ -51,6 +52,11 @@ public static float getModifier(Block blk) {
5152
return mod;
5253
}
5354

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+
*/
5460
public static void draw(float cx, float cy, float size, float len, float cosA, float sinA) {
5561
float hs = size * 0.5f;
5662
float sdx = cosA * len;
@@ -89,48 +95,124 @@ private static float jitter(float x, float y) {
8995
return ((h & 0xFFFF) / 32767.5f) - 1.0f;
9096
}
9197

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-
});
12698

127-
return result[0];
128-
}
12999

130100
public static arc.graphics.g2d.TextureRegion getOriginalShadow(mindustry.type.UnitType type) {
131101
if (!unitShadowCache.containsKey(type)) {
132102
unitShadowCache.put(type, type.shadowRegion);
133103
}
134104
return unitShadowCache.get(type);
135105
}
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+
}
136218
}

0 commit comments

Comments
 (0)