1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
//! Screen-space SDF shadow types and raymarching parameters.
//!
//! Provides the data structures needed to render screen-space signed distance
//! field shadows. Nodes cast shadows onto the layers below them by rendering
//! their shape into an SDF texture, then raymarching against it during the
//! compositing pass.
use crate::Rect;
/// Parameters for a single screen-space SDF shadow pass.
///
/// The shadow is computed by:
/// 1. Rendering occluder shapes into an SDF texture (distance to nearest edge).
/// 2. Raymarching from each pixel toward the light direction, checking SDF values.
/// 3. Accumulating shadow density based on occlusion distance.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct SdfShadowParams {
/// Direction the shadow is cast (unit vector pointing away from the light).
pub light_dir: [f32; 2],
/// Shadow color (RGB + alpha density).
pub shadow_color: [f32; 4],
/// Maximum raymarch distance in pixels.
pub max_distance: f32,
/// Number of raymarch steps (more = smoother, more expensive).
pub step_count: u32,
/// Softness of the shadow edge (higher = softer).
pub softness: f32,
/// Offset of the shadow from the occluder center.
pub offset: [f32; 2],
/// Opacity multiplier for the entire shadow.
pub opacity: f32,
}
impl SdfShadowParams {
/// Creates a default downward shadow (light from above).
pub fn downward() -> Self {
Self {
light_dir: [0.0, 1.0],
shadow_color: [0.0, 0.0, 0.0, 0.35],
max_distance: 64.0,
step_count: 32,
softness: 4.0,
offset: [0.0, 2.0],
opacity: 1.0,
}
}
/// Creates a default upward shadow (light from below).
pub fn upward() -> Self {
Self {
light_dir: [0.0, -1.0],
shadow_color: [0.0, 0.0, 0.0, 0.35],
max_distance: 64.0,
step_count: 32,
softness: 4.0,
offset: [0.0, -2.0],
opacity: 1.0,
}
}
/// Creates a directional shadow with a custom light angle (in radians).
pub fn with_angle(angle: f32) -> Self {
Self {
light_dir: [angle.sin(), angle.cos()],
..Self::downward()
}
}
/// Sets the shadow color.
pub fn with_color(mut self, r: f32, g: f32, b: f32, a: f32) -> Self {
self.shadow_color = [r, g, b, a];
self
}
/// Sets the maximum raymarch distance.
pub fn with_max_distance(mut self, d: f32) -> Self {
self.max_distance = d;
self
}
/// Sets the raymarch step count.
pub fn with_steps(mut self, n: u32) -> Self {
self.step_count = n;
self
}
/// Sets the shadow softness.
pub fn with_softness(mut self, s: f32) -> Self {
self.softness = s;
self
}
/// Sets the shadow offset from the occluder.
pub fn with_offset(mut self, dx: f32, dy: f32) -> Self {
self.offset = [dx, dy];
self
}
/// Sets the shadow opacity.
pub fn with_opacity(mut self, o: f32) -> Self {
self.opacity = o.clamp(0.0, 1.0);
self
}
}
impl Default for SdfShadowParams {
fn default() -> Self {
Self::downward()
}
}
/// An occluder shape that casts an SDF shadow.
///
/// Represents a single shape to be rendered into the SDF texture
/// before the raymarching pass.
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum SdfShape {
/// A rectangle occluder.
Rect {
x: f32,
y: f32,
width: f32,
height: f32,
},
/// A rounded rectangle occluder.
RoundedRect {
x: f32,
y: f32,
width: f32,
height: f32,
radius: f32,
},
/// A circle occluder.
Circle { cx: f32, cy: f32, radius: f32 },
}
impl SdfShape {
/// Returns the bounding box of this shape.
pub fn bounds(&self) -> Rect {
match *self {
SdfShape::Rect {
x,
y,
width,
height,
} => Rect::new(x, y, width, height),
SdfShape::RoundedRect {
x,
y,
width,
height,
..
} => Rect::new(x, y, width, height),
SdfShape::Circle { cx, cy, radius } => {
Rect::new(cx - radius, cy - radius, radius * 2.0, radius * 2.0)
}
}
}
/// Creates a rounded rectangle from a `Rect` and corner radius.
pub fn from_rect(rect: Rect, radius: f32) -> Self {
SdfShape::RoundedRect {
x: rect.x,
y: rect.y,
width: rect.width,
height: rect.height,
radius,
}
}
}
/// A single shadow instance to be rendered.
///
/// Combines an SDF shape with shadow parameters for one shadow-casting object.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct ShadowInstance {
/// The occluder shape.
pub shape: SdfShape,
/// Shadow rendering parameters.
pub params: SdfShadowParams,
}
impl ShadowInstance {
/// Creates a new shadow instance from a shape and parameters.
pub fn new(shape: SdfShape, params: SdfShadowParams) -> Self {
Self { shape, params }
}
/// Creates a shadow instance for a rectangle with default downward shadow.
pub fn rect_shadow(x: f32, y: f32, width: f32, height: f32) -> Self {
Self {
shape: SdfShape::Rect {
x,
y,
width,
height,
},
params: SdfShadowParams::downward(),
}
}
/// Creates a shadow instance for a rounded rectangle with default downward shadow.
pub fn rounded_rect_shadow(x: f32, y: f32, width: f32, height: f32, radius: f32) -> Self {
Self {
shape: SdfShape::RoundedRect {
x,
y,
width,
height,
radius,
},
params: SdfShadowParams::downward(),
}
}
/// Creates a shadow instance for a circle with default downward shadow.
pub fn circle_shadow(cx: f32, cy: f32, radius: f32) -> Self {
Self {
shape: SdfShape::Circle { cx, cy, radius },
params: SdfShadowParams::downward(),
}
}
/// Returns the bounding box of the shadow (occluder bounds + max distance + offset).
pub fn shadow_bounds(&self) -> Rect {
let bounds = self.shape.bounds();
let d = self.params.max_distance;
let ox = self.params.offset[0];
let oy = self.params.offset[1];
Rect::new(
bounds.x + ox - d,
bounds.y + oy - d,
bounds.width + d * 2.0,
bounds.height + d * 2.0,
)
}
}
/// A batch of shadow instances for efficient GPU submission.
#[derive(Debug, Clone, PartialEq)]
pub struct ShadowBatch {
pub instances: Vec<ShadowInstance>,
}
impl ShadowBatch {
/// Creates an empty shadow batch.
pub fn new() -> Self {
Self {
instances: Vec::new(),
}
}
/// Creates a batch pre-allocated for `capacity` instances.
pub fn with_capacity(capacity: usize) -> Self {
Self {
instances: Vec::with_capacity(capacity),
}
}
/// Adds a shadow instance to the batch.
pub fn push(&mut self, instance: ShadowInstance) {
self.instances.push(instance);
}
/// Returns the number of shadow instances.
pub fn len(&self) -> usize {
self.instances.len()
}
/// Returns true if the batch has no instances.
pub fn is_empty(&self) -> bool {
self.instances.is_empty()
}
/// Clears all instances while retaining capacity.
pub fn clear(&mut self) {
self.instances.clear();
}
/// Computes the combined bounding box of all shadow instances.
pub fn total_bounds(&self) -> Option<Rect> {
if self.instances.is_empty() {
return None;
}
let first = self.instances[0].shadow_bounds();
let mut min_x = first.x;
let mut min_y = first.y;
let mut max_x = first.x + first.width;
let mut max_y = first.y + first.height;
for inst in &self.instances[1..] {
let b = inst.shadow_bounds();
min_x = min_x.min(b.x);
min_y = min_y.min(b.y);
max_x = max_x.max(b.x + b.width);
max_y = max_y.max(b.y + b.height);
}
Some(Rect::new(min_x, min_y, max_x - min_x, max_y - min_y))
}
}
impl Default for ShadowBatch {
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn sdf_shadow_params_default() {
let p = SdfShadowParams::default();
assert_eq!(p.light_dir, [0.0, 1.0]);
assert_eq!(p.step_count, 32);
}
#[test]
fn sdf_shadow_params_with_angle() {
let p = SdfShadowParams::with_angle(std::f32::consts::PI / 4.0);
assert!((p.light_dir[0] - (std::f32::consts::PI / 4.0_f32).sin()).abs() < 0.001);
assert!((p.light_dir[1] - (std::f32::consts::PI / 4.0_f32).cos()).abs() < 0.001);
}
#[test]
fn sdf_shape_bounds() {
let rect = SdfShape::Rect {
x: 10.0,
y: 20.0,
width: 100.0,
height: 50.0,
};
let b = rect.bounds();
assert_eq!(b.x, 10.0);
assert_eq!(b.y, 20.0);
assert_eq!(b.width, 100.0);
assert_eq!(b.height, 50.0);
}
#[test]
fn sdf_shape_circle_bounds() {
let circle = SdfShape::Circle {
cx: 50.0,
cy: 50.0,
radius: 20.0,
};
let b = circle.bounds();
assert_eq!(b.x, 30.0);
assert_eq!(b.y, 30.0);
assert_eq!(b.width, 40.0);
assert_eq!(b.height, 40.0);
}
#[test]
fn shadow_instance_bounds() {
let inst = ShadowInstance::rect_shadow(0.0, 0.0, 100.0, 50.0);
let b = inst.shadow_bounds();
// Should extend by max_distance (64) in all directions
assert!(b.x < 0.0);
assert!(b.y < 0.0);
assert!(b.width > 100.0);
assert!(b.height > 50.0);
}
#[test]
fn shadow_batch_total_bounds() {
let mut batch = ShadowBatch::new();
assert!(batch.total_bounds().is_none());
batch.push(ShadowInstance::rect_shadow(0.0, 0.0, 100.0, 50.0));
batch.push(ShadowInstance::rect_shadow(200.0, 200.0, 50.0, 50.0));
let bounds = batch.total_bounds().unwrap();
assert!(bounds.x <= 0.0);
assert!(bounds.y <= 0.0);
assert!(bounds.width >= 250.0);
assert!(bounds.height >= 250.0);
}
#[test]
fn shadow_batch_clear() {
let mut batch = ShadowBatch::new();
batch.push(ShadowInstance::rect_shadow(0.0, 0.0, 10.0, 10.0));
assert_eq!(batch.len(), 1);
batch.clear();
assert!(batch.is_empty());
}
}