#![allow(clippy::type_complexity)]
#![deny(missing_docs)]
use bevy::{math::DVec3, prelude::*, transform::TransformSystem};
use std::marker::PhantomData;
pub mod camera;
pub mod precision;
#[cfg(feature = "debug")]
pub mod debug;
use precision::*;
#[derive(Default)]
pub struct FloatingOriginPlugin<P: GridPrecision> {
pub settings: FloatingOriginSettings,
phantom: PhantomData<P>,
}
impl<P: GridPrecision> FloatingOriginPlugin<P> {
pub fn new(grid_edge_length: f32, switching_threshold: f32) -> Self {
FloatingOriginPlugin {
settings: FloatingOriginSettings::new(grid_edge_length, switching_threshold),
..Default::default()
}
}
}
impl<P: GridPrecision> Plugin for FloatingOriginPlugin<P> {
fn build(&self, app: &mut App) {
app.insert_resource(self.settings.clone())
.register_type::<Transform>()
.register_type::<GlobalTransform>()
.register_type::<GridCell<P>>()
.add_plugin(ValidParentCheckPlugin::<GlobalTransform>::default())
.add_startup_system_to_stage(
StartupStage::PostStartup,
recenter_transform_on_grid::<P>
.label(TransformSystem::TransformPropagate)
.before(update_global_from_grid::<P>),
)
.add_startup_system_to_stage(
StartupStage::PostStartup,
update_global_from_grid::<P>
.label(TransformSystem::TransformPropagate)
.before(transform_propagate_system::<P>),
)
.add_startup_system_to_stage(
StartupStage::PostStartup,
transform_propagate_system::<P>.label(TransformSystem::TransformPropagate),
)
.add_system_to_stage(
CoreStage::PostUpdate,
recenter_transform_on_grid::<P>
.label(TransformSystem::TransformPropagate)
.before(update_global_from_grid::<P>),
)
.add_system_to_stage(
CoreStage::PostUpdate,
update_global_from_grid::<P>
.label(TransformSystem::TransformPropagate)
.before(transform_propagate_system::<P>),
)
.add_system_to_stage(
CoreStage::PostUpdate,
transform_propagate_system::<P>.label(TransformSystem::TransformPropagate),
);
}
}
#[derive(Reflect, Clone, Resource)]
pub struct FloatingOriginSettings {
grid_edge_length: f32,
maximum_distance_from_origin: f32,
}
impl FloatingOriginSettings {
fn new(grid_edge_length: f32, switching_threshold: f32) -> Self {
Self {
grid_edge_length,
maximum_distance_from_origin: grid_edge_length / 2.0 + switching_threshold,
}
}
pub fn grid_position_double<P: GridPrecision>(
&self,
pos: &GridCell<P>,
transform: &Transform,
) -> DVec3 {
DVec3 {
x: pos.x.as_f64() * self.grid_edge_length as f64 + transform.translation.x as f64,
y: pos.y.as_f64() * self.grid_edge_length as f64 + transform.translation.y as f64,
z: pos.z.as_f64() * self.grid_edge_length as f64 + transform.translation.z as f64,
}
}
pub fn grid_position<P: GridPrecision>(
&self,
pos: &GridCell<P>,
transform: &Transform,
) -> Vec3 {
Vec3 {
x: pos.x.as_f64() as f32 * self.grid_edge_length + transform.translation.x,
y: pos.y.as_f64() as f32 * self.grid_edge_length + transform.translation.y,
z: pos.z.as_f64() as f32 * self.grid_edge_length + transform.translation.z,
}
}
pub fn translation_to_grid<P: GridPrecision>(
&self,
input: impl Into<DVec3>,
) -> (GridCell<P>, Vec3) {
let l = self.grid_edge_length as f64;
let input = input.into();
let DVec3 { x, y, z } = input;
if input.abs().max_element() < self.maximum_distance_from_origin as f64 {
return (GridCell::default(), input.as_vec3());
}
let x_r = (x / l).round();
let y_r = (y / l).round();
let z_r = (z / l).round();
let t_x = x - x_r * l;
let t_y = y - y_r * l;
let t_z = z - z_r * l;
(
GridCell {
x: P::from_f32(x_r as f32),
y: P::from_f32(y_r as f32),
z: P::from_f32(z_r as f32),
},
Vec3::new(t_x as f32, t_y as f32, t_z as f32),
)
}
pub fn imprecise_translation_to_grid<P: GridPrecision>(
&self,
input: Vec3,
) -> (GridCell<P>, Vec3) {
self.translation_to_grid(input.as_dvec3())
}
}
impl Default for FloatingOriginSettings {
fn default() -> Self {
Self::new(10_000f32, 100f32)
}
}
#[derive(Bundle, Default)]
pub struct FloatingSpatialBundle<P: GridPrecision> {
pub visibility: Visibility,
pub computed: ComputedVisibility,
pub transform: Transform,
pub global_transform: GlobalTransform,
pub grid_position: GridCell<P>,
}
#[derive(Component, Default, Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Hash, Reflect)]
#[reflect(Component, Default, PartialEq)]
pub struct GridCell<P: GridPrecision> {
pub x: P,
pub y: P,
pub z: P,
}
impl<P: GridPrecision> GridCell<P> {
pub fn new(x: P, y: P, z: P) -> Self {
Self { x, y, z }
}
pub const ZERO: Self = GridCell {
x: P::ZERO,
y: P::ZERO,
z: P::ZERO,
};
pub const ONE: Self = GridCell {
x: P::ONE,
y: P::ONE,
z: P::ONE,
};
}
impl<P: GridPrecision> std::ops::Add for GridCell<P> {
type Output = GridCell<P>;
fn add(self, rhs: Self) -> Self::Output {
GridCell {
x: self.x.wrapping_add(rhs.x),
y: self.y.wrapping_add(rhs.y),
z: self.z.wrapping_add(rhs.z),
}
}
}
impl<P: GridPrecision> std::ops::Sub for GridCell<P> {
type Output = GridCell<P>;
fn sub(self, rhs: Self) -> Self::Output {
GridCell {
x: self.x.wrapping_sub(rhs.x),
y: self.y.wrapping_sub(rhs.y),
z: self.z.wrapping_sub(rhs.z),
}
}
}
impl<P: GridPrecision> std::ops::Add for &GridCell<P> {
type Output = GridCell<P>;
fn add(self, rhs: Self) -> Self::Output {
(*self).add(*rhs)
}
}
impl<P: GridPrecision> std::ops::Sub for &GridCell<P> {
type Output = GridCell<P>;
fn sub(self, rhs: Self) -> Self::Output {
(*self).sub(*rhs)
}
}
#[derive(Component, Reflect)]
pub struct FloatingOrigin;
pub fn recenter_transform_on_grid<P: GridPrecision>(
settings: Res<FloatingOriginSettings>,
mut query: Query<(&mut GridCell<P>, &mut Transform), (Changed<Transform>, Without<Parent>)>,
) {
query.par_for_each_mut(1024, |(mut grid_pos, mut transform)| {
if transform.as_ref().translation.abs().max_element()
> settings.maximum_distance_from_origin
{
let (grid_cell_delta, translation) =
settings.imprecise_translation_to_grid(transform.as_ref().translation);
*grid_pos = *grid_pos + grid_cell_delta;
transform.translation = translation;
}
});
}
pub fn update_global_from_grid<P: GridPrecision>(
settings: Res<FloatingOriginSettings>,
origin: Query<(&GridCell<P>, Changed<GridCell<P>>), With<FloatingOrigin>>,
mut entities: ParamSet<(
Query<
(&Transform, &mut GlobalTransform, &GridCell<P>),
Or<(Changed<GridCell<P>>, Changed<Transform>)>,
>,
Query<(&Transform, &mut GlobalTransform, &GridCell<P>)>,
)>,
) {
let (origin_cell, origin_grid_pos_changed) = origin.single();
if origin_grid_pos_changed {
let mut all_entities = entities.p1();
all_entities.par_for_each_mut(1024, |(local, global, entity_cell)| {
update_global_from_cell_local(&settings, entity_cell, origin_cell, local, global);
});
} else {
let mut moved_cell_entities = entities.p0();
moved_cell_entities.par_for_each_mut(1024, |(local, global, entity_cell)| {
update_global_from_cell_local(&settings, entity_cell, origin_cell, local, global);
});
}
}
fn update_global_from_cell_local<P: GridPrecision>(
settings: &FloatingOriginSettings,
entity_cell: &GridCell<P>,
origin_cell: &GridCell<P>,
local: &Transform,
mut global: Mut<GlobalTransform>,
) {
let grid_cell_delta = entity_cell - origin_cell;
*global = local
.with_translation(settings.grid_position(&grid_cell_delta, local))
.into();
}
pub fn transform_propagate_system<P: GridPrecision>(
origin_moved: Query<(), (Changed<GridCell<P>>, With<FloatingOrigin>)>,
mut root_query_no_grid: Query<
(
Option<(&Children, Changed<Children>)>,
&Transform,
Changed<Transform>,
&mut GlobalTransform,
Entity,
),
(Without<GridCell<P>>, Without<Parent>),
>,
mut root_query_grid: Query<
(
Option<(&Children, Changed<Children>)>,
Changed<Transform>,
Changed<GridCell<P>>,
&GlobalTransform,
Entity,
),
(With<GridCell<P>>, Without<Parent>),
>,
mut transform_query: Query<(
&Transform,
Changed<Transform>,
&mut GlobalTransform,
&Parent,
)>,
children_query: Query<(&Children, Changed<Children>), (With<Parent>, With<GlobalTransform>)>,
) {
let origin_cell_changed = !origin_moved.is_empty();
for (children, transform, transform_changed, mut global_transform, entity) in
root_query_no_grid.iter_mut()
{
let mut changed = transform_changed || origin_cell_changed;
if transform_changed {
*global_transform = GlobalTransform::from(*transform);
}
if let Some((children, changed_children)) = children {
changed |= changed_children;
for child in children {
let _ = propagate_recursive(
&global_transform,
&mut transform_query,
&children_query,
*child,
entity,
changed,
);
}
}
}
for (children, cell_changed, transform_changed, global_transform, entity) in
root_query_grid.iter_mut()
{
let mut changed = transform_changed || cell_changed || origin_cell_changed;
if let Some((children, changed_children)) = children {
changed |= changed_children;
for child in children {
let _ = propagate_recursive(
global_transform,
&mut transform_query,
&children_query,
*child,
entity,
changed,
);
}
}
}
}
fn propagate_recursive(
parent: &GlobalTransform,
transform_query: &mut Query<(
&Transform,
Changed<Transform>,
&mut GlobalTransform,
&Parent,
)>,
children_query: &Query<(&Children, Changed<Children>), (With<Parent>, With<GlobalTransform>)>,
entity: Entity,
expected_parent: Entity,
mut changed: bool,
) -> Result<(), ()> {
let global_matrix = {
let (transform, transform_changed, mut global_transform, child_parent) =
transform_query.get_mut(entity).map_err(drop)?;
assert_eq!(
child_parent.get(), expected_parent,
"Malformed hierarchy. This probably means that your hierarchy has been improperly maintained, or contains a cycle"
);
changed |= transform_changed;
if changed {
*global_transform = parent.mul_transform(*transform);
}
*global_transform
};
let (children, changed_children) = children_query.get(entity).map_err(drop)?;
changed |= changed_children;
for child in children {
let _ = propagate_recursive(
&global_matrix,
transform_query,
children_query,
*child,
entity,
changed,
);
}
Ok(())
}