1
1
use std:: collections:: HashSet ;
2
2
3
3
use bevy_ecs:: prelude:: * ;
4
- use bevy_math:: { Mat4 , Quat , UVec2 , UVec3 , Vec2 , Vec3 , Vec3A , Vec3Swizzles , Vec4 , Vec4Swizzles } ;
4
+ use bevy_math:: { Mat4 , UVec2 , UVec3 , Vec2 , Vec3 , Vec3A , Vec3Swizzles , Vec4 , Vec4Swizzles } ;
5
5
use bevy_reflect:: prelude:: * ;
6
6
use bevy_render:: {
7
7
camera:: { Camera , CameraProjection , OrthographicProjection } ,
@@ -12,7 +12,7 @@ use bevy_render::{
12
12
renderer:: RenderDevice ,
13
13
view:: { ComputedVisibility , RenderLayers , VisibleEntities } ,
14
14
} ;
15
- use bevy_transform:: components:: GlobalTransform ;
15
+ use bevy_transform:: { components:: GlobalTransform , prelude :: Transform } ;
16
16
use bevy_utils:: tracing:: warn;
17
17
18
18
use crate :: {
@@ -755,13 +755,21 @@ pub(crate) fn point_light_order(
755
755
// data required for assigning lights to clusters
756
756
pub ( crate ) struct PointLightAssignmentData {
757
757
entity : Entity ,
758
- translation : Vec3 ,
759
- rotation : Quat ,
758
+ transform : GlobalTransform ,
760
759
range : f32 ,
761
760
shadows_enabled : bool ,
762
761
spot_light_angle : Option < f32 > ,
763
762
}
764
763
764
+ impl PointLightAssignmentData {
765
+ pub fn sphere ( & self ) -> Sphere {
766
+ Sphere {
767
+ center : self . transform . translation_vec3a ( ) ,
768
+ radius : self . range ,
769
+ }
770
+ }
771
+ }
772
+
765
773
#[ derive( Default ) ]
766
774
pub struct GlobalVisiblePointLights {
767
775
entities : HashSet < Entity > ,
@@ -815,8 +823,7 @@ pub(crate) fn assign_lights_to_clusters(
815
823
. map (
816
824
|( entity, transform, point_light, _visibility) | PointLightAssignmentData {
817
825
entity,
818
- translation : transform. translation ,
819
- rotation : Quat :: default ( ) ,
826
+ transform : GlobalTransform :: from_translation ( transform. translation ( ) ) ,
820
827
shadows_enabled : point_light. shadows_enabled ,
821
828
range : point_light. range ,
822
829
spot_light_angle : None ,
@@ -830,8 +837,7 @@ pub(crate) fn assign_lights_to_clusters(
830
837
. map (
831
838
|( entity, transform, spot_light, _visibility) | PointLightAssignmentData {
832
839
entity,
833
- translation : transform. translation ,
834
- rotation : transform. rotation ,
840
+ transform : * transform,
835
841
shadows_enabled : spot_light. shadows_enabled ,
836
842
range : spot_light. range ,
837
843
spot_light_angle : Some ( spot_light. outer_angle ) ,
@@ -872,11 +878,7 @@ pub(crate) fn assign_lights_to_clusters(
872
878
if lights_in_view_count == MAX_UNIFORM_BUFFER_POINT_LIGHTS + 1 {
873
879
false
874
880
} else {
875
- let light_sphere = Sphere {
876
- center : Vec3A :: from ( light. translation ) ,
877
- radius : light. range ,
878
- } ;
879
-
881
+ let light_sphere = light. sphere ( ) ;
880
882
let light_in_view = frusta
881
883
. iter ( )
882
884
. any ( |frustum| frustum. intersects_sphere ( & light_sphere, true ) ) ;
@@ -932,7 +934,8 @@ pub(crate) fn assign_lights_to_clusters(
932
934
lights
933
935
. iter ( )
934
936
. map ( |light| {
935
- -inverse_view_row_2. dot ( light. translation . extend ( 1.0 ) ) + light. range
937
+ -inverse_view_row_2. dot ( light. transform . translation ( ) . extend ( 1.0 ) )
938
+ + light. range
936
939
} )
937
940
. reduce ( f32:: max)
938
941
. unwrap_or ( 0.0 )
@@ -966,10 +969,7 @@ pub(crate) fn assign_lights_to_clusters(
966
969
if config. dynamic_resizing ( ) {
967
970
let mut cluster_index_estimate = 0.0 ;
968
971
for light in lights. iter ( ) {
969
- let light_sphere = Sphere {
970
- center : Vec3A :: from ( light. translation ) ,
971
- radius : light. range ,
972
- } ;
972
+ let light_sphere = light. sphere ( ) ;
973
973
974
974
// Check if the light is within the view frustum
975
975
if !frustum. intersects_sphere ( & light_sphere, true ) {
@@ -1124,10 +1124,7 @@ pub(crate) fn assign_lights_to_clusters(
1124
1124
1125
1125
let mut update_from_light_intersections = |visible_lights : & mut Vec < Entity > | {
1126
1126
for light in lights. iter ( ) {
1127
- let light_sphere = Sphere {
1128
- center : Vec3A :: from ( light. translation ) ,
1129
- radius : light. range ,
1130
- } ;
1127
+ let light_sphere = light. sphere ( ) ;
1131
1128
1132
1129
// Check if the light is within the view frustum
1133
1130
if !frustum. intersects_sphere ( & light_sphere, true ) {
@@ -1177,8 +1174,7 @@ pub(crate) fn assign_lights_to_clusters(
1177
1174
let spot_light_dir_sin_cos = light. spot_light_angle . map ( |angle| {
1178
1175
let ( angle_sin, angle_cos) = angle. sin_cos ( ) ;
1179
1176
(
1180
- ( inverse_view_transform * ( light. rotation * Vec3 :: Z ) . extend ( 0.0 ) )
1181
- . truncate ( ) ,
1177
+ ( inverse_view_transform * light. transform . back ( ) . extend ( 0.0 ) ) . truncate ( ) ,
1182
1178
angle_sin,
1183
1179
angle_cos,
1184
1180
)
@@ -1432,7 +1428,7 @@ pub fn update_directional_light_frusta(
1432
1428
* transform. compute_matrix ( ) . inverse ( ) ;
1433
1429
* frustum = Frustum :: from_view_projection (
1434
1430
& view_projection,
1435
- & transform. translation ,
1431
+ & transform. translation ( ) ,
1436
1432
& transform. back ( ) ,
1437
1433
directional_light. shadow_projection . far ( ) ,
1438
1434
) ;
@@ -1451,7 +1447,7 @@ pub fn update_point_light_frusta(
1451
1447
Mat4 :: perspective_infinite_reverse_rh ( std:: f32:: consts:: FRAC_PI_2 , 1.0 , POINT_LIGHT_NEAR_Z ) ;
1452
1448
let view_rotations = CUBE_MAP_FACES
1453
1449
. iter ( )
1454
- . map ( |CubeMapFace { target, up } | GlobalTransform :: identity ( ) . looking_at ( * target, * up) )
1450
+ . map ( |CubeMapFace { target, up } | Transform :: identity ( ) . looking_at ( * target, * up) )
1455
1451
. collect :: < Vec < _ > > ( ) ;
1456
1452
1457
1453
for ( entity, transform, point_light, mut cubemap_frusta) in & mut views {
@@ -1467,7 +1463,7 @@ pub fn update_point_light_frusta(
1467
1463
// ignore scale because we don't want to effectively scale light radius and range
1468
1464
// by applying those as a view transform to shadow map rendering of objects
1469
1465
// and ignore rotation because we want the shadow map projections to align with the axes
1470
- let view_translation = GlobalTransform :: from_translation ( transform. translation ) ;
1466
+ let view_translation = Transform :: from_translation ( transform. translation ( ) ) ;
1471
1467
let view_backward = transform. back ( ) ;
1472
1468
1473
1469
for ( view_rotation, frustum) in view_rotations. iter ( ) . zip ( cubemap_frusta. iter_mut ( ) ) {
@@ -1476,7 +1472,7 @@ pub fn update_point_light_frusta(
1476
1472
1477
1473
* frustum = Frustum :: from_view_projection (
1478
1474
& view_projection,
1479
- & transform. translation ,
1475
+ & transform. translation ( ) ,
1480
1476
& view_backward,
1481
1477
point_light. range ,
1482
1478
) ;
@@ -1503,7 +1499,6 @@ pub fn update_spot_light_frusta(
1503
1499
1504
1500
// ignore scale because we don't want to effectively scale light radius and range
1505
1501
// by applying those as a view transform to shadow map rendering of objects
1506
- let view_translation = GlobalTransform :: from_translation ( transform. translation ) ;
1507
1502
let view_backward = transform. back ( ) ;
1508
1503
1509
1504
let spot_view = spot_light_view_matrix ( transform) ;
@@ -1512,7 +1507,7 @@ pub fn update_spot_light_frusta(
1512
1507
1513
1508
* frustum = Frustum :: from_view_projection (
1514
1509
& view_projection,
1515
- & view_translation . translation ,
1510
+ & transform . translation ( ) ,
1516
1511
& view_backward,
1517
1512
spot_light. range ,
1518
1513
) ;
@@ -1623,7 +1618,7 @@ pub fn check_light_mesh_visibility(
1623
1618
1624
1619
let view_mask = maybe_view_mask. copied ( ) . unwrap_or_default ( ) ;
1625
1620
let light_sphere = Sphere {
1626
- center : Vec3A :: from ( transform. translation ) ,
1621
+ center : Vec3A :: from ( transform. translation ( ) ) ,
1627
1622
radius : point_light. range ,
1628
1623
} ;
1629
1624
@@ -1686,7 +1681,7 @@ pub fn check_light_mesh_visibility(
1686
1681
1687
1682
let view_mask = maybe_view_mask. copied ( ) . unwrap_or_default ( ) ;
1688
1683
let light_sphere = Sphere {
1689
- center : Vec3A :: from ( transform. translation ) ,
1684
+ center : Vec3A :: from ( transform. translation ( ) ) ,
1690
1685
radius : point_light. range ,
1691
1686
} ;
1692
1687
0 commit comments