use super::*;
impl PointLight3D {
#[must_use]
pub fn new(position: Vec3, color: Color, intensity: f32, radius: f32) -> Self {
Self {
position,
color: Vec3::from_color(color),
intensity: intensity.max(0.0),
radius: radius.max(0.25),
cast_shadows: false,
}
}
#[must_use]
pub fn from_linear(position: Vec3, color: Vec3, intensity: f32, range: f32) -> Self {
Self {
position,
color,
intensity: intensity.max(0.0),
radius: range.max(0.25),
cast_shadows: false,
}
}
#[cfg_attr(not(target_arch = "wasm32"), inline)]
#[must_use]
pub fn khr_attenuation(distance_sq: f32, range: f32) -> f32 {
let safe_dist_sq: f32 = distance_sq.max(1.0e-4);
if range <= 0.0 || !range.is_finite() {
return 1.0 / safe_dist_sq;
}
let inv_range_sq: f32 = 1.0 / (range * range).max(1.0e-4);
let ratio_sq: f32 = safe_dist_sq * inv_range_sq;
let window: f32 = (1.0 - ratio_sq * ratio_sq).clamp(0.0, 1.0);
(window * window) / safe_dist_sq
}
}
impl SpotLight3D {
#[must_use]
pub fn new(
position: Vec3,
direction: Vec3,
color: Color,
intensity: f32,
range: f32,
cone_deg: (f32, f32),
) -> Self {
let (inner_deg, outer_deg): (f32, f32) = cone_deg;
let clamped_inner: f32 = inner_deg.clamp(1.0, 80.0);
let clamped_outer: f32 = outer_deg.clamp(clamped_inner + 1.0, 88.0);
Self {
position,
direction: direction.normalize(),
color: Vec3::from_color(color),
intensity: intensity.max(0.0),
range: range.max(0.5),
inner_cos: clamped_inner.to_radians().cos(),
outer_cos: clamped_outer.to_radians().cos(),
}
}
#[must_use]
pub fn from_linear(
position: Vec3,
direction: Vec3,
color: Vec3,
intensity: f32,
range: f32,
cone_rad: (f32, f32),
) -> Self {
let (inner_rad, outer_rad): (f32, f32) = cone_rad;
let clamped_inner: f32 = inner_rad.clamp(0.0, FRAC_PI_2 - 0.02);
let clamped_outer: f32 = outer_rad.clamp(clamped_inner + 0.005, FRAC_PI_2 - 0.01);
Self {
position,
direction: direction.normalize(),
color,
intensity: intensity.max(0.0),
range: range.max(0.5),
inner_cos: clamped_inner.cos(),
outer_cos: clamped_outer.cos(),
}
}
}
impl AreaLight3D {
#[must_use]
pub fn new_rect(
position: Vec3,
direction: Vec3,
color: Vec3,
intensity: f32,
dimensions: (f32, f32),
range: f32,
) -> Self {
let dir_unit: Vec3 = direction.normalize();
let ref_up: Vec3 = if dir_unit.get_y().abs() > 0.95 {
Vec3::new(0.0, 0.0, 1.0)
} else {
Vec3::new(0.0, 1.0, 0.0)
};
let right: Vec3 = ref_up.cross(dir_unit).normalize();
let up: Vec3 = dir_unit.cross(right).normalize();
Self {
position,
direction: dir_unit,
right,
up,
color,
intensity: intensity.max(0.0),
width: dimensions.0.max(0.05),
height: dimensions.1.max(0.05),
range: range.max(1.0),
shape: AreaLightShape3D::Rectangle,
two_sided: false,
}
}
#[must_use]
pub fn new_disk(
position: Vec3,
direction: Vec3,
color: Vec3,
intensity: f32,
diameter: f32,
range: f32,
) -> Self {
let mut light: Self = Self::new_rect(
position,
direction,
color,
intensity,
(diameter, diameter),
range,
);
light.set_shape(AreaLightShape3D::Disk);
light
}
#[must_use]
pub fn evaluate_pbr(
&self,
world_pos: Vec3,
normal: Vec3,
view_dir: Vec3,
material_terms: (Vec3, Vec3, f32, f32),
) -> Vec3 {
let intensity: f32 = self.get_intensity();
if intensity <= 1.0e-5 {
return Vec3::zero();
}
let light_pos: Vec3 = self.get_position();
let light_dir: Vec3 = self.get_direction();
let light_right: Vec3 = self.get_right();
let light_up: Vec3 = self.get_up();
let half_w: f32 = self.get_width() * 0.5;
let half_h: f32 = self.get_height() * 0.5;
let from_light: Vec3 = world_pos.sub(light_pos);
let plane_dist: f32 = from_light.dot(light_dir);
if !self.get_two_sided() && plane_dist <= 0.0 {
return Vec3::zero();
}
let center_dist_sq: f32 = from_light.length_sq();
let range_val: f32 = self.get_range();
if center_dist_sq >= range_val * range_val {
return Vec3::zero();
}
let (albedo, f0, roughness, diffuse_scale): (Vec3, Vec3, f32, f32) = material_terms;
let clamped_rough: f32 = roughness.clamp(0.04, 1.0);
let alpha: f32 = clamped_rough * clamped_rough;
let refl_dir: Vec3 = view_dir.scale(-1.0).reflect(normal).normalize();
let r_dot_ldir: f32 = refl_dir.dot(light_dir);
let hit_local: Vec3 = if r_dot_ldir.abs() > 1.0e-4 {
let ray_t: f32 = (-plane_dist / r_dot_ldir).max(0.0);
let plane_hit: Vec3 = world_pos.add(refl_dir.scale(ray_t));
plane_hit.sub(light_pos)
} else {
from_light
};
let raw_u: f32 = hit_local.dot(light_right);
let raw_v: f32 = hit_local.dot(light_up);
let (clamped_u, clamped_v): (f32, f32) = match self.get_shape() {
AreaLightShape3D::Rectangle => {
(raw_u.clamp(-half_w, half_w), raw_v.clamp(-half_h, half_h))
}
AreaLightShape3D::Disk => {
let norm_u: f32 = raw_u / half_w.max(1.0e-4);
let norm_v: f32 = raw_v / half_h.max(1.0e-4);
let rad_sq: f32 = norm_u * norm_u + norm_v * norm_v;
if rad_sq > 1.0 {
let inv_rad: f32 = 1.0 / rad_sq.sqrt();
(raw_u * inv_rad, raw_v * inv_rad)
} else {
(raw_u, raw_v)
}
}
};
let rep_point: Vec3 = light_pos
.add(light_right.scale(clamped_u))
.add(light_up.scale(clamped_v));
let to_rep: Vec3 = rep_point.sub(world_pos);
let rep_dist_sq: f32 = to_rep.length_sq().max(1.0e-4);
let rep_dist: f32 = rep_dist_sq.sqrt();
let spec_l: Vec3 = to_rep.scale(1.0 / rep_dist);
let n_dot_l_spec: f32 = normal.dot(spec_l).max(0.0);
let to_center: Vec3 = light_pos.sub(world_pos);
let center_dist: f32 = center_dist_sq.max(1.0e-4).sqrt();
let diff_l: Vec3 = to_center.scale(1.0 / center_dist);
let emitter_facing: f32 = if self.get_two_sided() {
diff_l.dot(light_dir).abs()
} else {
(-diff_l.dot(light_dir)).max(0.0)
};
let n_dot_l_diff: f32 = normal.dot(diff_l).max(0.0);
let emitter_area: f32 = match self.get_shape() {
AreaLightShape3D::Rectangle => self.get_width() * self.get_height(),
AreaLightShape3D::Disk => PI * half_w * half_h,
};
let solid_angle: f32 =
(emitter_area * emitter_facing) / (center_dist_sq + emitter_area / PI).max(1.0e-4);
let range_ratio_sq: f32 = center_dist_sq / (range_val * range_val).max(1.0e-4);
let range_win: f32 = (1.0 - range_ratio_sq * range_ratio_sq).clamp(0.0, 1.0);
let window_sq: f32 = range_win * range_win;
let diffuse_rgb: Vec3 =
albedo.scale((diffuse_scale / PI) * n_dot_l_diff * solid_angle.min(PI) * window_sq);
let spec_rgb: Vec3 = if n_dot_l_spec > 0.0 {
let equiv_rad: f32 = (emitter_area / PI).sqrt();
let alpha_prime: f32 = (alpha + equiv_rad / (2.0 * rep_dist)).clamp(0.04, 1.0);
let alpha_p2: f32 = alpha_prime * alpha_prime;
let energy_norm: f32 = (alpha / alpha_prime) * (alpha / alpha_prime);
let half_vec: Vec3 = spec_l.add(view_dir).normalize();
let n_dot_h: f32 = normal.dot(half_vec).max(0.0);
let n_dot_v: f32 = normal.dot(view_dir).max(0.04);
let v_dot_h: f32 = view_dir.dot(half_vec).clamp(0.0, 1.0);
let ggx_den: f32 = n_dot_h * n_dot_h * (alpha_p2 - 1.0) + 1.0;
let ggx_d: f32 = (alpha_p2 * energy_norm) / (PI * ggx_den * ggx_den).max(1.0e-4);
let om_vh: f32 = 1.0 - v_dot_h;
let pow5: f32 = om_vh * om_vh * om_vh * om_vh * om_vh;
let fresnel: Vec3 = f0.add(Vec3::splat(1.0).sub(f0).scale(pow5));
let spec_atten: f32 = PointLight3D::khr_attenuation(rep_dist_sq, range_val);
let spec_scalar: f32 = ((ggx_d * 0.25) / (n_dot_l_spec.max(0.08) + n_dot_v)).min(6.0);
fresnel.scale(spec_scalar * n_dot_l_spec * spec_atten * emitter_area.max(0.1))
} else {
Vec3::zero()
};
let radiance: Vec3 = self.get_color().scale(intensity);
diffuse_rgb.add(spec_rgb).mul(radiance)
}
}
impl Default for LightClusterGrid3D {
fn default() -> Self {
let total_froxels: usize =
LIGHT_CLUSTER_GRID_X * LIGHT_CLUSTER_GRID_Y * LIGHT_CLUSTER_GRID_Z;
Self {
near_plane: 0.3,
far_plane: 420.0,
log_depth_scale: (LIGHT_CLUSTER_GRID_Z as f32) / (420.0_f32 / 0.3_f32).ln(),
depth_slice_max: [420.0; LIGHT_CLUSTER_GRID_Z],
active_light_count: 0,
total_assignments: 0,
packed_lights: Vec::with_capacity(LIGHT_CLUSTER_MAX_LIGHTS),
cluster_counts: vec![0_u16; total_froxels],
cluster_indices: vec![0_u16; total_froxels * LIGHT_CLUSTER_MAX_PER_FROXEL],
}
}
}
impl LightClusterGrid3D {
#[cfg_attr(not(target_arch = "wasm32"), inline)]
#[must_use]
pub fn has_active_lights(&self) -> bool {
self.get_active_light_count() > 0
}
pub fn build_clusters(
&mut self,
camera: &Camera3D,
aspect: f32,
point_lights: &[PointLight3D],
spot_lights: &[SpotLight3D],
) {
let near_p: f32 = camera.get_near().max(0.15);
let far_p: f32 = camera.get_far().max(near_p + 1.0);
let log_scale: f32 = (LIGHT_CLUSTER_GRID_Z as f32) / (far_p / near_p).ln().max(1.0e-4);
self.set_near_plane(near_p);
self.set_far_plane(far_p);
self.set_log_depth_scale(log_scale);
let depth_ratio: f32 = far_p / near_p;
{
let Self {
depth_slice_max, ..
} = self;
for (slice_idx, slot) in depth_slice_max.iter_mut().enumerate() {
let frac: f32 = ((slice_idx + 1) as f32) / (LIGHT_CLUSTER_GRID_Z as f32);
*slot = near_p * depth_ratio.powf(frac);
}
}
let cam_pos: Vec3 = camera.get_position();
let cam_fwd: Vec3 = camera.forward();
let cam_right: Vec3 = camera.right();
let cam_up: Vec3 = cam_right.cross(cam_fwd).normalize();
let half_h: f32 = (camera.get_fov_deg().to_radians() * 0.5).tan().max(0.05);
let half_w: f32 = half_h * aspect.max(0.1);
let inv_half_w: f32 = 1.0 / half_w;
let inv_half_h: f32 = 1.0 / half_h;
let mut assignments: usize = 0;
let active_count: usize = {
let Self {
packed_lights,
cluster_counts,
cluster_indices,
..
} = self;
packed_lights.clear();
if point_lights.is_empty() && spot_lights.is_empty() {
cluster_counts.fill(0);
0
} else {
cluster_counts.fill(0);
for point_light in point_lights.iter() {
if packed_lights.len() >= LIGHT_CLUSTER_MAX_LIGHTS {
break;
}
let rad: f32 = point_light.get_radius().max(0.25);
let intensity: f32 = point_light.get_intensity();
if intensity <= 1.0e-4 {
continue;
}
let light_pos: Vec3 = point_light.get_position();
let col: Vec3 = point_light.get_color().scale(intensity);
let light_idx: u16 = packed_lights.len() as u16;
packed_lights.push(PackedLocalLight {
pos_inv_r2: [
light_pos.get_x(),
light_pos.get_y(),
light_pos.get_z(),
1.0 / (rad * rad),
],
radiance_radius: [col.get_x(), col.get_y(), col.get_z(), rad],
dir_outer_cos: [0.0, -1.0, 0.0, -2.0],
inv_cone_span: [0.0; 4],
});
assignments += Self::bin_sphere_into_froxels(
(cam_pos, cam_right, cam_up, cam_fwd),
(inv_half_w, inv_half_h, near_p, far_p, log_scale),
(light_pos, rad, light_idx),
cluster_counts,
cluster_indices,
);
}
for spot_light in spot_lights.iter() {
if packed_lights.len() >= LIGHT_CLUSTER_MAX_LIGHTS {
break;
}
let range: f32 = spot_light.get_range().max(0.5);
let intensity: f32 = spot_light.get_intensity();
if intensity <= 1.0e-4 {
continue;
}
let light_pos: Vec3 = spot_light.get_position();
let light_dir: Vec3 = spot_light.get_direction().normalize();
let col: Vec3 = spot_light.get_color().scale(intensity);
let outer_cos: f32 = spot_light.get_outer_cos();
let span: f32 = (spot_light.get_inner_cos() - outer_cos).max(SPOT_CONE_MIN_EPS);
let light_idx: u16 = packed_lights.len() as u16;
packed_lights.push(PackedLocalLight {
pos_inv_r2: [
light_pos.get_x(),
light_pos.get_y(),
light_pos.get_z(),
1.0 / (range * range),
],
radiance_radius: [col.get_x(), col.get_y(), col.get_z(), range],
dir_outer_cos: [
light_dir.get_x(),
light_dir.get_y(),
light_dir.get_z(),
outer_cos,
],
inv_cone_span: [1.0 / span, 0.0, 0.0, 0.0],
});
let cone_center: Vec3 = light_pos.add(light_dir.scale(range * 0.45));
let cone_bound_rad: f32 = range * 0.45;
assignments += Self::bin_sphere_into_froxels(
(cam_pos, cam_right, cam_up, cam_fwd),
(inv_half_w, inv_half_h, near_p, far_p, log_scale),
(cone_center, cone_bound_rad, light_idx),
cluster_counts,
cluster_indices,
);
}
packed_lights.len()
}
};
self.set_active_light_count(active_count);
self.set_total_assignments(assignments);
}
fn bin_sphere_into_froxels(
cam_basis: (Vec3, Vec3, Vec3, Vec3),
frustum_params: (f32, f32, f32, f32, f32),
sphere: (Vec3, f32, u16),
cluster_counts: &mut [u16],
cluster_indices: &mut [u16],
) -> usize {
let (cam_pos, cam_right, cam_up, cam_fwd): (Vec3, Vec3, Vec3, Vec3) = cam_basis;
let (inv_half_w, inv_half_h, near_p, far_p, log_scale): (f32, f32, f32, f32, f32) =
frustum_params;
let (center, radius, light_idx): (Vec3, f32, u16) = sphere;
let rel: Vec3 = center.sub(cam_pos);
let vz: f32 = rel.dot(cam_fwd);
let z_min: f32 = vz - radius;
let z_max: f32 = vz + radius;
if z_max <= near_p || z_min >= far_p {
return 0;
}
let vx: f32 = rel.dot(cam_right);
let vy: f32 = rel.dot(cam_up);
let z_proj: f32 = vz.max(near_p);
let inv_z: f32 = 1.0 / z_proj;
let ndc_cx: f32 = vx * inv_z * inv_half_w;
let ndc_cy: f32 = vy * inv_z * inv_half_h;
let pad_x: f32 = if z_min <= near_p {
(radius / vz.max(2.0) * inv_half_w).clamp(0.35, 1.35)
} else {
(radius / z_min) * inv_half_w
};
let pad_y: f32 = if z_min <= near_p {
(radius / vz.max(2.0) * inv_half_h).clamp(0.35, 1.35)
} else {
(radius / z_min) * inv_half_h
};
let min_u: f32 = (ndc_cx - pad_x) * 0.5 + 0.5;
let max_u: f32 = (ndc_cx + pad_x) * 0.5 + 0.5;
let min_v: f32 = 0.5 - (ndc_cy + pad_y) * 0.5;
let max_v: f32 = 0.5 - (ndc_cy - pad_y) * 0.5;
let out_u: bool = max_u < 0.0 || min_u > 1.0;
let out_v: bool = max_v < 0.0 || min_v > 1.0;
if out_u || out_v {
return 0;
}
let gx_max: usize = LIGHT_CLUSTER_GRID_X - 1;
let gy_max: usize = LIGHT_CLUSTER_GRID_Y - 1;
let gz_max: usize = LIGHT_CLUSTER_GRID_Z - 1;
let cx0: usize =
((min_u.clamp(0.0, 1.0) * (LIGHT_CLUSTER_GRID_X as f32)).floor() as usize).min(gx_max);
let cx1: usize =
((max_u.clamp(0.0, 1.0) * (LIGHT_CLUSTER_GRID_X as f32)).floor() as usize).min(gx_max);
let cy0: usize =
((min_v.clamp(0.0, 1.0) * (LIGHT_CLUSTER_GRID_Y as f32)).floor() as usize).min(gy_max);
let cy1: usize =
((max_v.clamp(0.0, 1.0) * (LIGHT_CLUSTER_GRID_Y as f32)).floor() as usize).min(gy_max);
let cz0: usize = ((((z_min.max(near_p) / near_p).ln()) * log_scale)
.floor()
.max(0.0) as usize)
.min(gz_max);
let cz1: usize = ((((z_max.min(far_p) / near_p).ln()) * log_scale)
.floor()
.max(0.0) as usize)
.min(gz_max);
let slice_stride: usize = LIGHT_CLUSTER_GRID_X * LIGHT_CLUSTER_GRID_Y;
let mut count_added: usize = 0;
for cz in cz0..=cz1 {
let z_off: usize = cz * slice_stride;
for cy in cy0..=cy1 {
let y_off: usize = z_off + cy * LIGHT_CLUSTER_GRID_X;
for cx in cx0..=cx1 {
let froxel: usize = y_off + cx;
let cur: usize = cluster_counts[froxel] as usize;
if cur < LIGHT_CLUSTER_MAX_PER_FROXEL {
cluster_indices[froxel * LIGHT_CLUSTER_MAX_PER_FROXEL + cur] = light_idx;
cluster_counts[froxel] = (cur + 1) as u16;
count_added += 1;
}
}
}
}
count_added
}
#[cfg_attr(not(target_arch = "wasm32"), inline)]
#[must_use]
pub fn evaluate_pixel(
&self,
world_pos: Vec3,
normal: Vec3,
view_dir: Vec3,
material_terms: (Vec3, Vec3, f32, f32),
screen_depth: (f32, f32, f32),
) -> Vec3 {
if self.get_active_light_count() == 0 {
return Vec3::zero();
}
let (uv_x, uv_y, linear_depth): (f32, f32, f32) = screen_depth;
let near_p: f32 = self.get_near_plane();
if linear_depth < near_p || linear_depth > self.get_far_plane() {
return Vec3::zero();
}
let cx: usize = ((uv_x.clamp(0.0, 0.9999) * (LIGHT_CLUSTER_GRID_X as f32)) as usize)
.min(LIGHT_CLUSTER_GRID_X - 1);
let cy: usize = ((uv_y.clamp(0.0, 0.9999) * (LIGHT_CLUSTER_GRID_Y as f32)) as usize)
.min(LIGHT_CLUSTER_GRID_Y - 1);
let cz: usize = ((((linear_depth / near_p).ln()) * self.get_log_depth_scale()).max(0.0)
as usize)
.min(LIGHT_CLUSTER_GRID_Z - 1);
let froxel: usize = (cz * LIGHT_CLUSTER_GRID_Y + cy) * LIGHT_CLUSTER_GRID_X + cx;
let Self {
packed_lights,
cluster_counts,
cluster_indices,
..
} = self;
let light_count: usize = cluster_counts[froxel] as usize;
if light_count == 0 {
return Vec3::zero();
}
let (albedo, f0, alpha2, diffuse_scale): (Vec3, Vec3, f32, f32) = material_terms;
let n_dot_v: f32 = normal.dot(view_dir).max(0.04);
let idx_base: usize = froxel * LIGHT_CLUSTER_MAX_PER_FROXEL;
let mut accum: Vec3 = Vec3::zero();
for slot in 0..light_count {
let light: PackedLocalLight = packed_lights[cluster_indices[idx_base + slot] as usize];
let to_light: Vec3 = Vec3::new(
light.pos_inv_r2[0] - world_pos.get_x(),
light.pos_inv_r2[1] - world_pos.get_y(),
light.pos_inv_r2[2] - world_pos.get_z(),
);
let dist_sq: f32 = to_light.length_sq();
let ratio_sq: f32 = dist_sq * light.pos_inv_r2[3];
if ratio_sq >= 1.0 {
continue;
}
let inv_dist: f32 = 1.0 / dist_sq.max(1.0e-4).sqrt();
let light_dir: Vec3 = to_light.scale(inv_dist);
let n_dot_l: f32 = normal.dot(light_dir);
if n_dot_l <= 0.0 {
continue;
}
let mut spot_factor: f32 = 1.0;
if light.dir_outer_cos[3] > -1.5 {
let beam_dir: Vec3 = Vec3::new(
light.dir_outer_cos[0],
light.dir_outer_cos[1],
light.dir_outer_cos[2],
);
let cos_theta: f32 = (-light_dir.get_x()) * beam_dir.get_x()
+ (-light_dir.get_y()) * beam_dir.get_y()
+ (-light_dir.get_z()) * beam_dir.get_z();
if cos_theta <= light.dir_outer_cos[3] {
continue;
}
let raw_spot: f32 =
((cos_theta - light.dir_outer_cos[3]) * light.inv_cone_span[0]).clamp(0.0, 1.0);
spot_factor = raw_spot * raw_spot;
}
let window: f32 = (1.0 - ratio_sq * ratio_sq).clamp(0.0, 1.0);
let atten: f32 = (window * window * spot_factor) / dist_sq.max(1.0e-4);
let half_vec: Vec3 = light_dir.add(view_dir).normalize();
let n_dot_h: f32 = normal.dot(half_vec).max(0.0);
let v_dot_h: f32 = view_dir.dot(half_vec).clamp(0.0, 1.0);
let denom: f32 = n_dot_h * n_dot_h * (alpha2 - 1.0) + 1.0;
let ggx_d: f32 = alpha2 / (PI * denom * denom).max(1.0e-4);
let one_minus_vh: f32 = 1.0 - v_dot_h;
let pow5: f32 =
one_minus_vh * one_minus_vh * one_minus_vh * one_minus_vh * one_minus_vh;
let fresnel: Vec3 = f0.add(Vec3::splat(1.0).sub(f0).scale(pow5));
let spec_scalar: f32 = (ggx_d * 0.25) / (n_dot_l.max(0.08) + n_dot_v);
let diffuse_rgb: Vec3 = albedo.scale(diffuse_scale * (1.0 / PI));
let brdf_rgb: Vec3 = diffuse_rgb.add(fresnel.scale(spec_scalar.min(4.0)));
let light_rad: Vec3 = Vec3::new(
light.radiance_radius[0],
light.radiance_radius[1],
light.radiance_radius[2],
);
accum = accum.add(brdf_rgb.mul(light_rad).scale(n_dot_l * atten));
}
accum
}
#[must_use]
pub fn evaluate_irradiance_fast(
&self,
world_pos: Vec3,
normal: Vec3,
screen_depth: (f32, f32, f32),
) -> Vec3 {
if self.get_active_light_count() == 0 {
return Vec3::zero();
}
let (uv_x, uv_y, linear_depth): (f32, f32, f32) = screen_depth;
if linear_depth < self.get_near_plane() || linear_depth > self.get_far_plane() {
return Vec3::zero();
}
let cx: usize = ((uv_x.clamp(0.0, 0.9999) * (LIGHT_CLUSTER_GRID_X as f32)) as usize)
.min(LIGHT_CLUSTER_GRID_X - 1);
let cy: usize = ((uv_y.clamp(0.0, 0.9999) * (LIGHT_CLUSTER_GRID_Y as f32)) as usize)
.min(LIGHT_CLUSTER_GRID_Y - 1);
let Self {
depth_slice_max,
packed_lights,
cluster_counts,
cluster_indices,
..
} = self;
let mut cz: usize = 0;
if linear_depth > depth_slice_max[7] {
cz = 8;
}
if linear_depth > depth_slice_max[cz + 3] {
cz += 4;
}
if linear_depth > depth_slice_max[cz + 1] {
cz += 2;
}
if linear_depth > depth_slice_max[cz] {
cz += 1;
}
let froxel: usize = (cz * LIGHT_CLUSTER_GRID_Y + cy) * LIGHT_CLUSTER_GRID_X + cx;
let light_count: usize = cluster_counts[froxel] as usize;
if light_count == 0 {
return Vec3::zero();
}
let idx_base: usize = froxel * LIGHT_CLUSTER_MAX_PER_FROXEL;
let mut accum_r: f32 = 0.0;
let mut accum_g: f32 = 0.0;
let mut accum_b: f32 = 0.0;
let wx: f32 = world_pos.get_x();
let wy: f32 = world_pos.get_y();
let wz: f32 = world_pos.get_z();
let nx: f32 = normal.get_x();
let ny: f32 = normal.get_y();
let nz: f32 = normal.get_z();
for slot in 0..light_count {
let light: PackedLocalLight = packed_lights[cluster_indices[idx_base + slot] as usize];
let dx: f32 = light.pos_inv_r2[0] - wx;
let dy: f32 = light.pos_inv_r2[1] - wy;
let dz: f32 = light.pos_inv_r2[2] - wz;
let dist_sq: f32 = dx.mul_add(dx, dy.mul_add(dy, dz * dz));
let ratio_sq: f32 = dist_sq * light.pos_inv_r2[3];
if ratio_sq >= 1.0 {
continue;
}
let ndotl_raw: f32 = dx.mul_add(nx, dy.mul_add(ny, dz * nz));
if ndotl_raw <= 0.0 {
continue;
}
let inv_dist: f32 = 1.0 / dist_sq.max(1.0e-4).sqrt();
let mut spot_factor: f32 = 1.0;
if light.dir_outer_cos[3] > -1.5 {
let cos_theta: f32 = -(dx * light.dir_outer_cos[0]
+ dy * light.dir_outer_cos[1]
+ dz * light.dir_outer_cos[2])
* inv_dist;
if cos_theta <= light.dir_outer_cos[3] {
continue;
}
let raw_spot: f32 =
((cos_theta - light.dir_outer_cos[3]) * light.inv_cone_span[0]).clamp(0.0, 1.0);
spot_factor = raw_spot * raw_spot;
}
let window: f32 = (1.0 - ratio_sq * ratio_sq).max(0.0);
let scale: f32 =
(window * window * spot_factor * ndotl_raw * inv_dist) / (dist_sq + 1.0);
accum_r = light.radiance_radius[0].mul_add(scale, accum_r);
accum_g = light.radiance_radius[1].mul_add(scale, accum_g);
accum_b = light.radiance_radius[2].mul_add(scale, accum_b);
}
Vec3::new(accum_r, accum_g, accum_b)
}
#[must_use]
pub fn froxel_light_count(&self, cx: usize, cy: usize, cz: usize) -> usize {
let out_xy: bool = cx >= LIGHT_CLUSTER_GRID_X || cy >= LIGHT_CLUSTER_GRID_Y;
if out_xy || cz >= LIGHT_CLUSTER_GRID_Z {
return 0;
}
let Self { cluster_counts, .. } = self;
let froxel: usize = (cz * LIGHT_CLUSTER_GRID_Y + cy) * LIGHT_CLUSTER_GRID_X + cx;
cluster_counts[froxel] as usize
}
}
impl VsmCascade {
#[must_use]
pub fn new(resolution: usize, half_extent_m: f32) -> Self {
let res: usize = resolution.max(32);
let total: usize = res * res;
Self {
resolution: res,
half_extent_m: half_extent_m.max(8.0),
snapped_center: Vec3::zero(),
light_right: Vec3::new(1.0, 0.0, 0.0),
light_up: Vec3::new(0.0, 0.0, 1.0),
light_forward: Vec3::new(0.0, 1.0, 0.0),
depth_min: -160.0,
depth_max: 160.0,
static_valid: false,
static_depth: vec![1.0; total],
active_depth: vec![1.0; total],
}
}
#[must_use]
fn compute_snapped_basis(&self, focus_pos: Vec3, sun_dir: Vec3) -> (Vec3, Vec3, Vec3, Vec3) {
let fwd: Vec3 = sun_dir.normalize();
let ref_up: Vec3 = if fwd.get_y().abs() > 0.95 {
Vec3::new(0.0, 0.0, 1.0)
} else {
Vec3::new(0.0, 1.0, 0.0)
};
let right: Vec3 = ref_up.cross(fwd).normalize();
let up: Vec3 = fwd.cross(right).normalize();
let texel_world: f32 = (self.get_half_extent_m() * 2.0) / (self.get_resolution() as f32);
let snap_step: f32 = (texel_world * 4.0).max(0.25);
let proj_r: f32 = (focus_pos.dot(right) / snap_step).floor() * snap_step;
let proj_u: f32 = (focus_pos.dot(up) / snap_step).floor() * snap_step;
let proj_f: f32 = (focus_pos.dot(fwd) / snap_step).floor() * snap_step;
let snapped: Vec3 = right
.scale(proj_r)
.add(up.scale(proj_u))
.add(fwd.scale(proj_f));
(snapped, right, up, fwd)
}
#[must_use]
pub fn light_view_proj(&self) -> Mat4 {
let right: Vec3 = self.get_light_right();
let up: Vec3 = self.get_light_up();
let fwd: Vec3 = self.get_light_forward();
let center: Vec3 = self.get_snapped_center();
let inv_extent: f32 = 1.0 / self.get_half_extent_m().max(1.0);
let depth_max: f32 = self.get_depth_max();
let inv_depth_span: f32 = 1.0 / (depth_max - self.get_depth_min()).max(1.0);
let rx: f32 = right.get_x() * inv_extent;
let ry: f32 = right.get_y() * inv_extent;
let rz: f32 = right.get_z() * inv_extent;
let ux: f32 = up.get_x() * inv_extent;
let uy: f32 = up.get_y() * inv_extent;
let uz: f32 = up.get_z() * inv_extent;
let zx: f32 = -fwd.get_x() * inv_depth_span;
let zy: f32 = -fwd.get_y() * inv_depth_span;
let zz: f32 = -fwd.get_z() * inv_depth_span;
let tx: f32 = -center.dot(right) * inv_extent;
let ty: f32 = -center.dot(up) * inv_extent;
let tz: f32 = (depth_max + center.dot(fwd)) * inv_depth_span;
Mat4::from_cols_array([
rx, ux, zx, 0.0, ry, uy, zy, 0.0, rz, uz, zz, 0.0, tx, ty, tz, 1.0,
])
}
#[cfg_attr(not(target_arch = "wasm32"), inline)]
#[must_use]
pub fn project_world_point(&self, world_pos: Vec3) -> (f32, f32, f32) {
let rel: Vec3 = world_pos.sub(self.get_snapped_center());
let inv_span: f32 = 0.5 / self.get_half_extent_m().max(1.0);
let coord_u: f32 = rel.dot(self.get_light_right()) * inv_span + 0.5;
let coord_v: f32 = rel.dot(self.get_light_up()) * inv_span + 0.5;
let light_z: f32 = rel.dot(self.get_light_forward());
let depth_span: f32 = (self.get_depth_max() - self.get_depth_min()).max(1.0);
let depth_norm: f32 = (self.get_depth_max() - light_z) / depth_span;
(coord_u, coord_v, depth_norm)
}
#[cfg_attr(not(target_arch = "wasm32"), inline)]
fn rasterize_shadow_tri(depth_buf: &mut [f32], res: usize, verts: [(f32, f32, f32); 3]) {
let res_f: f32 = res as f32;
let x0: f32 = verts[0].0 * res_f;
let y0: f32 = verts[0].1 * res_f;
let z0: f32 = verts[0].2;
let x1: f32 = verts[1].0 * res_f;
let y1: f32 = verts[1].1 * res_f;
let z1: f32 = verts[1].2;
let x2: f32 = verts[2].0 * res_f;
let y2: f32 = verts[2].1 * res_f;
let z2: f32 = verts[2].2;
let area: f32 = (x1 - x0) * (y2 - y0) - (y1 - y0) * (x2 - x0);
if area.abs() < 0.05 {
return;
}
let max_i: i32 = (res as i32) - 1;
let min_x: i32 = (x0.min(x1).min(x2).floor() as i32).max(0);
let max_x: i32 = (x0.max(x1).max(x2).ceil() as i32).min(max_i);
let min_y: i32 = (y0.min(y1).min(y2).floor() as i32).max(0);
let max_y: i32 = (y0.max(y1).max(y2).ceil() as i32).min(max_i);
if min_x > max_x || min_y > max_y {
return;
}
let inv_area: f32 = 1.0 / area;
for py in min_y..=max_y {
let fy: f32 = (py as f32) + 0.5;
let row_off: usize = (py as usize) * res;
for px in min_x..=max_x {
let fx: f32 = (px as f32) + 0.5;
let w0: f32 = ((x1 - fx) * (y2 - fy) - (y1 - fy) * (x2 - fx)) * inv_area;
let w1: f32 = ((x2 - fx) * (y0 - fy) - (y2 - fy) * (x0 - fx)) * inv_area;
let w2: f32 = 1.0 - w0 - w1;
if w0 >= -1.0e-4 && w1 >= -1.0e-4 && w2 >= -1.0e-4 {
let depth_val: f32 = (w0 * z0 + w1 * z1 + w2 * z2).clamp(0.0, 1.0);
let slot: &mut f32 = &mut depth_buf[row_off + (px as usize)];
if depth_val < *slot {
*slot = depth_val;
}
}
}
}
}
fn rebuild_static_depth(&mut self, world: &World3D) {
let objects: &[SceneObject3D] = world.get_objects().as_slice();
let mesh_lib: &[MeshLodChain] = world.get_mesh_library().as_slice();
let res: usize = self.get_resolution();
let center: Vec3 = self.get_snapped_center();
let reach: f32 = self.get_half_extent_m() * 1.5;
let right: Vec3 = self.get_light_right();
let up: Vec3 = self.get_light_up();
let fwd: Vec3 = self.get_light_forward();
let inv_span: f32 = 0.5 / self.get_half_extent_m().max(1.0);
let depth_max: f32 = self.get_depth_max();
let inv_depth_span: f32 = 1.0 / (depth_max - self.get_depth_min()).max(1.0);
{
let Self { static_depth, .. } = self;
static_depth.fill(1.0);
for (obj_idx, obj) in objects.iter().enumerate() {
if !obj.get_ray_trace_caster()
|| obj.get_is_dynamic()
|| !world.is_object_visible(obj_idx)
{
continue;
}
let wb: AxisAlignedBox3D = obj.get_world_bounds();
let obj_center: Vec3 = wb.center();
let rel_c: Vec3 = obj_center.sub(center);
let limit: f32 = reach + wb.radius();
if rel_c.get_x().abs() > limit && rel_c.get_z().abs() > limit {
continue;
}
let Some(mesh) = world.deformed_mesh_for_object(obj_idx).or_else(|| {
mesh_lib
.get(obj.get_mesh_id())
.and_then(|chain: &MeshLodChain| chain.get_levels().first())
}) else {
continue;
};
let verts: &[Vertex3D] = mesh.get_vertices().as_slice();
let indices: &[[u32; 3]] = mesh.get_indices().as_slice();
let model: Mat4 = obj.get_transform().to_mat4();
for tri in indices.iter() {
let (i0, i1, i2): (usize, usize, usize) =
(tri[0] as usize, tri[1] as usize, tri[2] as usize);
let out_01: bool = i0 >= verts.len() || i1 >= verts.len();
if out_01 || i2 >= verts.len() {
continue;
}
let w0: Vec3 = model.mul_point3(verts[i0].get_position()).sub(center);
let w1: Vec3 = model.mul_point3(verts[i1].get_position()).sub(center);
let w2: Vec3 = model.mul_point3(verts[i2].get_position()).sub(center);
let p0: (f32, f32, f32) = (
w0.dot(right) * inv_span + 0.5,
w0.dot(up) * inv_span + 0.5,
(depth_max - w0.dot(fwd)) * inv_depth_span,
);
let p1: (f32, f32, f32) = (
w1.dot(right) * inv_span + 0.5,
w1.dot(up) * inv_span + 0.5,
(depth_max - w1.dot(fwd)) * inv_depth_span,
);
let p2: (f32, f32, f32) = (
w2.dot(right) * inv_span + 0.5,
w2.dot(up) * inv_span + 0.5,
(depth_max - w2.dot(fwd)) * inv_depth_span,
);
Self::rasterize_shadow_tri(static_depth.as_mut_slice(), res, [p0, p1, p2]);
}
}
}
self.set_static_valid(true);
}
fn overlay_dynamic_depth(&mut self, world: &World3D) {
let objects: &[SceneObject3D] = world.get_objects().as_slice();
let mesh_lib: &[MeshLodChain] = world.get_mesh_library().as_slice();
let res: usize = self.get_resolution();
let center: Vec3 = self.get_snapped_center();
let right: Vec3 = self.get_light_right();
let up: Vec3 = self.get_light_up();
let fwd: Vec3 = self.get_light_forward();
let inv_span: f32 = 0.5 / self.get_half_extent_m().max(1.0);
let depth_max: f32 = self.get_depth_max();
let inv_depth_span: f32 = 1.0 / (depth_max - self.get_depth_min()).max(1.0);
let Self {
static_depth,
active_depth,
..
} = self;
active_depth.copy_from_slice(static_depth.as_slice());
for (obj_idx, obj) in objects.iter().enumerate() {
if !obj.get_ray_trace_caster()
|| !obj.get_is_dynamic()
|| !world.is_object_visible(obj_idx)
{
continue;
}
let Some(mesh) = world.deformed_mesh_for_object(obj_idx).or_else(|| {
mesh_lib
.get(obj.get_mesh_id())
.and_then(|chain: &MeshLodChain| chain.get_levels().first())
}) else {
continue;
};
let verts: &[Vertex3D] = mesh.get_vertices().as_slice();
let indices: &[[u32; 3]] = mesh.get_indices().as_slice();
let model: Mat4 = obj.get_transform().to_mat4();
for tri in indices.iter() {
let (i0, i1, i2): (usize, usize, usize) =
(tri[0] as usize, tri[1] as usize, tri[2] as usize);
let out_01: bool = i0 >= verts.len() || i1 >= verts.len();
if out_01 || i2 >= verts.len() {
continue;
}
let w0: Vec3 = model.mul_point3(verts[i0].get_position()).sub(center);
let w1: Vec3 = model.mul_point3(verts[i1].get_position()).sub(center);
let w2: Vec3 = model.mul_point3(verts[i2].get_position()).sub(center);
let p0: (f32, f32, f32) = (
w0.dot(right) * inv_span + 0.5,
w0.dot(up) * inv_span + 0.5,
(depth_max - w0.dot(fwd)) * inv_depth_span,
);
let p1: (f32, f32, f32) = (
w1.dot(right) * inv_span + 0.5,
w1.dot(up) * inv_span + 0.5,
(depth_max - w1.dot(fwd)) * inv_depth_span,
);
let p2: (f32, f32, f32) = (
w2.dot(right) * inv_span + 0.5,
w2.dot(up) * inv_span + 0.5,
(depth_max - w2.dot(fwd)) * inv_depth_span,
);
Self::rasterize_shadow_tri(active_depth.as_mut_slice(), res, [p0, p1, p2]);
}
}
}
#[cfg_attr(not(target_arch = "wasm32"), inline)]
#[must_use]
pub fn sample_pcf(&self, world_pos: Vec3, normal: Vec3, sun_dir: Vec3) -> Option<f32> {
if !self.get_static_valid() {
return None;
}
let texel_m: f32 = (self.get_half_extent_m() * 2.0) / (self.get_resolution() as f32);
let biased_pos: Vec3 = world_pos.add(normal.scale(texel_m * 0.65));
let (coord_u, coord_v, depth_norm): (f32, f32, f32) = self.project_world_point(biased_pos);
if !(0.02..=0.98).contains(&coord_u)
|| !(0.02..=0.98).contains(&coord_v)
|| !(0.0..=1.0).contains(&depth_norm)
{
return None;
}
let res: usize = self.get_resolution();
let res_f: f32 = res as f32;
let max_idx: usize = res - 1;
let n_dot_l: f32 = normal.dot(sun_dir).clamp(0.05, 1.0);
let slope_bias: f32 = (0.0022 * (1.0 - n_dot_l) + 0.0014).clamp(0.0012, 0.008);
let cmp_depth: f32 = depth_norm - slope_bias;
let base_tx: f32 = coord_u * res_f;
let base_ty: f32 = coord_v * res_f;
let hash_seed: f32 = world_pos.get_x().mul_add(
12.9898,
world_pos
.get_z()
.mul_add(78.233, world_pos.get_y() * 37.719),
);
let rot_angle: f32 = (hash_seed.sin() * 43_758.547).fract() * TAU;
let cos_rot: f32 = rot_angle.cos() * VSM_VOGEL_FILTER_RADIUS_TEXELS;
let sin_rot: f32 = rot_angle.sin() * VSM_VOGEL_FILTER_RADIUS_TEXELS;
let Self { active_depth, .. } = self;
let mut lit_taps: f32 = 0.0;
for &(tap_x, tap_y) in VSM_VOGEL_TAPS_16.iter() {
let rot_x: f32 = tap_x * cos_rot - tap_y * sin_rot;
let rot_y: f32 = tap_x * sin_rot + tap_y * cos_rot;
let tx: usize = ((base_tx + rot_x).floor().max(0.0) as usize).min(max_idx);
let ty: usize = ((base_ty + rot_y).floor().max(0.0) as usize).min(max_idx);
if cmp_depth <= active_depth[ty * res + tx] {
lit_taps += 1.0 / 16.0;
}
}
Some(VSM_SHADOW_FLOOR + (1.0 - VSM_SHADOW_FLOOR) * lit_taps)
}
}
impl Default for VirtualShadowMap {
fn default() -> Self {
Self::with_resolution(VSM_DEFAULT_CASCADE_RES)
}
}
impl VirtualShadowMap {
#[must_use]
pub fn compute_practical_splits(
near_plane: f32,
far_plane: f32,
lambda: f32,
) -> [f32; VSM_CASCADE_COUNT] {
let near_val: f32 = near_plane.max(0.1);
let far_val: f32 = far_plane.max(near_val + 4.0);
let blend_lambda: f32 = lambda.clamp(0.0, 1.0);
let ratio: f32 = far_val / near_val;
let mut splits: [f32; VSM_CASCADE_COUNT] = [0.0; VSM_CASCADE_COUNT];
for (cascade_idx, split_slot) in splits.iter_mut().enumerate().take(VSM_CASCADE_COUNT) {
let frac: f32 = ((cascade_idx + 1) as f32) / (VSM_CASCADE_COUNT as f32);
let log_split: f32 = near_val * ratio.powf(frac);
let lin_split: f32 = near_val + (far_val - near_val) * frac;
*split_slot = lin_split + (log_split - lin_split) * blend_lambda;
}
splits
}
#[must_use]
pub fn with_resolution(resolution: usize) -> Self {
let splits: [f32; VSM_CASCADE_COUNT] =
Self::compute_practical_splits(0.25, VSM_CSM_MAX_DISTANCE_M, VSM_CSM_LAMBDA);
let extents: [f32; VSM_CASCADE_COUNT] = [
VSM_CASCADE_NEAR_EXTENT_M,
(splits[1] * 0.72).max(28.0),
(splits[2] * 0.78).max(64.0),
VSM_CASCADE_FAR_EXTENT_M,
];
Self {
cascades: [
VsmCascade::new(resolution, extents[0]),
VsmCascade::new(resolution, extents[1]),
VsmCascade::new(resolution, extents[2]),
VsmCascade::new(resolution, extents[3]),
],
cascade_split_depths: splits,
cached_sun_dir: Vec3::zero(),
cached_static_epoch: u64::MAX,
static_cache_hits: 0,
static_rebuilds: 0,
}
}
#[must_use]
pub fn get_cascade_near(&self) -> &VsmCascade {
&self.get_cascades()[0]
}
#[must_use]
pub fn get_cascade_far(&self) -> &VsmCascade {
&self.get_cascades()[VSM_CASCADE_COUNT - 1]
}
#[must_use]
pub fn cascade_light_view_proj(&self, cascade_idx: usize) -> Mat4 {
let clamped_idx: usize = cascade_idx.min(VSM_CASCADE_COUNT - 1);
self.get_cascades()[clamped_idx].light_view_proj()
}
pub fn invalidate(&mut self) {
for cascade in self.get_mut_cascades().iter_mut() {
cascade.set_static_valid(false);
}
}
pub fn update_from_world(&mut self, world: &World3D) {
let sun_dir: Vec3 = world.get_sun().get_direction().normalize();
let cam: Camera3D = world.get_camera();
let cam_pos: Vec3 = cam.get_position();
let cam_fwd: Vec3 = cam.forward();
let near_p: f32 = cam.get_near().max(0.15);
let far_p: f32 = cam.get_far().min(VSM_CSM_MAX_DISTANCE_M).max(near_p + 12.0);
let splits: [f32; VSM_CASCADE_COUNT] =
Self::compute_practical_splits(near_p, far_p, VSM_CSM_LAMBDA);
self.set_cascade_split_depths(splits);
let static_epoch: u64 = world.get_static_bvh_rebuilds();
let sun_changed: bool = self.get_cached_sun_dir().dot(sun_dir) < 0.9998;
let epoch_changed: bool = self.get_cached_static_epoch() != static_epoch;
let mut rebuilt_any: bool = false;
let mut hit_any: bool = false;
let mut prev_split: f32 = near_p;
for (cascade_idx, &split_far) in splits.iter().enumerate().take(VSM_CASCADE_COUNT) {
let mid_dist: f32 = (prev_split + split_far) * 0.5;
let focus_pt: Vec3 = cam_pos.add(cam_fwd.scale(mid_dist));
prev_split = split_far;
let cascade: &mut VsmCascade = &mut self.get_mut_cascades()[cascade_idx];
let (snap_c, r_b, u_b, f_b): (Vec3, Vec3, Vec3, Vec3) =
cascade.compute_snapped_basis(focus_pt, sun_dir);
let moved: bool = cascade.get_snapped_center().sub(snap_c).length_sq() > 1.0e-3;
let dirty_light: bool = !cascade.get_static_valid() || sun_changed;
let dirty_world: bool = epoch_changed || moved;
if dirty_light || dirty_world {
cascade.set_snapped_center(snap_c);
cascade.set_light_right(r_b);
cascade.set_light_up(u_b);
cascade.set_light_forward(f_b);
cascade.rebuild_static_depth(world);
rebuilt_any = true;
} else {
hit_any = true;
}
cascade.overlay_dynamic_depth(world);
}
self.set_cached_sun_dir(sun_dir);
self.set_cached_static_epoch(static_epoch);
if rebuilt_any {
self.set_static_rebuilds(self.get_static_rebuilds().saturating_add(1));
} else if hit_any {
self.set_static_cache_hits(self.get_static_cache_hits().saturating_add(1));
}
}
#[cfg_attr(not(target_arch = "wasm32"), inline)]
#[must_use]
pub fn sample_visibility(&self, world_pos: Vec3, normal: Vec3, sun_dir: Vec3) -> f32 {
let cascades: &[VsmCascade; VSM_CASCADE_COUNT] = self.get_cascades();
for cascade_idx in 0..VSM_CASCADE_COUNT {
let cascade: &VsmCascade = &cascades[cascade_idx];
if let Some(vis_cur) = cascade.sample_pcf(world_pos, normal, sun_dir) {
if cascade_idx + 1 < VSM_CASCADE_COUNT {
let (coord_u, coord_v, _): (f32, f32, f32) =
cascade.project_world_point(world_pos);
let edge_dist: f32 = coord_u.min(1.0 - coord_u).min(coord_v.min(1.0 - coord_v));
if edge_dist < 0.08
&& let Some(vis_next) =
cascades[cascade_idx + 1].sample_pcf(world_pos, normal, sun_dir)
{
let blend_t: f32 = ((0.08 - edge_dist) / 0.06).clamp(0.0, 1.0);
return vis_cur + (vis_next - vis_cur) * blend_t;
}
}
return vis_cur;
}
}
1.0
}
}