zaycev / bevy-magic-light-2d

Experiment with computing 2D shading, lighting and shadows with Bevy Engine
Apache License 2.0
478 stars 41 forks source link

LightOccluder2D ignores transformation of parent entity. #44

Closed GlummixX closed 6 months ago

GlummixX commented 8 months ago

Hi, I like this project, its exactly what i've been searching for. But it seems that ocluders ignore parent transformations. Attaching a Rapier collider as children works jsut fine, they share the parents transformation, augmenting its own. But the LightOccluder2D just doesnt seem to like it. To make it work, I had to get the current translation using query, and then set the SpatialBundle transform to the values of the parent.

Could that be a bug? Iam still learning my way around bevy, but it seems like the parents transformation should be applied to child as well. Many thanks. MK

The parent:

pub struct MapBundle {
    pub tiled_map: Handle<TiledMap>, // tiled 
    pub storage: LayersStorage, // tiled
    pub visibility: Visibility, // bevy
    pub computed_visibility: ComputedVisibility, //bevy 
    pub transform: Transform, // bevy
    pub global_transform: GlobalTransform, //bevy
    pub module: MapModule, // my stuff
}

And my simplified functions for attaching a coliders to the MapBundle entity using my calculated collider rectangle as input.

// rapier
let id = world.spawn(Collider::cuboid(rect.half_size().x, rect.half_size().y))
    .insert(TransformBundle::from(Transform::from_xyz(
        rect.center().x,
        rect.center().y,
        2.
    )))
    .id();
world.entity_mut(map_id).add_child(id);

let t = world.entity(map_id).get::<Transform>().unwrap().translation;  // the modification I had to do
// ocluders
let id = world.spawn(LightOccluder2D {h_size: rect.half_size()})
    .insert(SpatialBundle::from_transform(Transform::from_xyz( // I had to use Spatial bundle, since simple transform bundle didnt work
        rect.center().x + t.x,
        rect.center().y + t.y,
        2. + t.z
    )))
    .id();
world.entity_mut(map_id).add_child(id);
zaycev commented 8 months ago

Yep, this is a bug, we should use GlobalTransform instead of Transform:

https://github.com/zaycev/bevy-magic-light-2d/blob/main/src/gi/pipeline_assets.rs#L63-L64

luan commented 7 months ago

52

zaycev commented 6 months ago

Fixed in #52