Skip to content

Commit 12c9c41

Browse files
author
Arksource
committed
feat: implement dynamic unit shadow rendering system with configurable shaders and light source detection
1 parent 91d4d61 commit 12c9c41

11 files changed

Lines changed: 584 additions & 339 deletions

‎assets/shaders/blur.frag‎

Lines changed: 0 additions & 24 deletions
This file was deleted.

‎assets/shaders/shadow.frag‎

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
varying vec4 v_color;
2+
varying vec2 v_texCoord;
3+
4+
uniform sampler2D u_texture;
5+
uniform vec2 u_resolution;
6+
uniform vec2 u_cameraPos;
7+
uniform vec2 u_camSize;
8+
9+
uniform float u_radius;
10+
uniform vec2 u_blurDir;
11+
uniform float u_sunElevation;
12+
uniform float u_time;
13+
uniform float u_edgeNoise;
14+
uniform float u_shadowTint;
15+
uniform float u_contactShadow;
16+
17+
// ── Noise helpers ─────────────────────────────────────────────────────────────
18+
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+
}
47+
48+
// ── Main ──────────────────────────────────────────────────────────────────────
49+
50+
void main() {
51+
vec2 texel = 1.0 / u_resolution;
52+
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.
56+
float elevFactor = 1.0 - clamp(u_sunElevation, 0.0, 1.0); // 0 at noon, 1 at horizon
57+
float dynamicRad = u_radius * (1.0 + elevFactor * 1.4); // up to 2.4× at horizon
58+
59+
// Gaussian weights for kernel [0, ±1r, ±2r, ±3r, ±4r]
60+
// Sum ≈ 1.0: 0.227 + 2×0.1945 + 2×0.1216 + 2×0.054 + 2×0.013
61+
vec2 step = u_blurDir * texel * dynamicRad;
62+
vec4 col = texture2D(u_texture, v_texCoord) * 0.2270;
63+
col += texture2D(u_texture, v_texCoord + step ) * 0.1945;
64+
col += texture2D(u_texture, v_texCoord - step ) * 0.1945;
65+
col += texture2D(u_texture, v_texCoord + step * 2.0 ) * 0.1216;
66+
col += texture2D(u_texture, v_texCoord - step * 2.0 ) * 0.1216;
67+
col += texture2D(u_texture, v_texCoord + step * 3.0 ) * 0.054;
68+
col += texture2D(u_texture, v_texCoord - step * 3.0 ) * 0.054;
69+
col += texture2D(u_texture, v_texCoord + step * 4.0 ) * 0.013;
70+
col += texture2D(u_texture, v_texCoord - step * 4.0 ) * 0.013;
71+
72+
float alpha = col.a;
73+
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);
81+
82+
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+
87+
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.
91+
float contactBoost = smoothstep(0.55, 1.0, alpha) * u_contactShadow;
92+
93+
// ── 6. Dynamic tint from solar elevation ─────────────────────────────────
94+
// sunElevation: -1=midnight, 0=horizon, +1=noon
95+
float sunE = u_sunElevation; // -1 .. +1
96+
97+
// Day: soft blue-grey (sky scatter fills shadow colour)
98+
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
102+
vec3 horizonTint = vec3(0.16, 0.06, 0.01) * horizonFactor;
103+
// Night: deep indigo-violet (moonlight tint)
104+
float nightFactor = clamp(-sunE, 0.0, 1.0);
105+
vec3 nightTint = vec3(0.03, 0.02, 0.09) * nightFactor;
106+
107+
vec3 rawTint = dayTint + horizonTint + nightTint;
108+
vec3 shadowColor = mix(vec3(0.0), rawTint, u_shadowTint);
109+
110+
// Warm halo at the very edge (physics: diffracted warm light outlines shadow)
111+
shadowColor += vec3(0.025, 0.010, 0.0) * edgeFactor * u_shadowTint * (1.0 - nightFactor);
112+
113+
// ── Combine ───────────────────────────────────────────────────────────────
114+
float finalAlpha = alpha * distFade * (1.0 + contactBoost);
115+
finalAlpha = clamp(finalAlpha, 0.0, 1.0);
116+
117+
gl_FragColor = vec4(shadowColor, finalAlpha) * v_color;
118+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ varying vec4 v_color;
77
varying vec2 v_texCoord;
88

99
void main() {
10-
v_color = a_color;
10+
v_color = a_color;
1111
v_texCoord = a_texCoord0;
1212
gl_Position = u_projTrans * a_position;
1313
}

‎src/dynamicShadows/AnyBlocksShadows.java‎

Lines changed: 50 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,97 @@
11
package 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;
56
import mindustry.core.World;
67
import mindustry.world.Block;
78
import mindustry.world.blocks.power.*;
89

9-
/**
10-
* Sombreado Dinamico para cualquier tipo de bloque
11-
@author Arksource
12-
*/
1310
public 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
});

‎src/dynamicShadows/BlurShader.java‎

Lines changed: 0 additions & 21 deletions
This file was deleted.

0 commit comments

Comments
 (0)