1use bevy::camera::visibility::VisibilitySystems;
66use bevy::light::NotShadowCaster;
67use bevy::prelude::*;
68use bevy::transform::TransformSystems;
69
70use crate::noise::hash3;
71use crate::world::ViewerPosition;
72
73#[derive(Debug, Clone, PartialEq, Default)]
82pub enum AmbientParticleShape {
83 #[default]
85 Cube,
86 Sphere,
88 Custom(Handle<Mesh>),
90}
91
92#[derive(Resource, Debug, Clone, PartialEq)]
102pub struct AmbientParticlesConfig {
103 pub enabled: bool,
107 pub count: u32,
113 pub volume_radius: f32,
118 pub size_range: (f32, f32),
123 pub color: Color,
128 pub shape: AmbientParticleShape,
130 pub edge_fade: f32,
136 pub drift_speed: f32,
142 pub seed: u32,
147}
148
149impl Default for AmbientParticlesConfig {
150 fn default() -> Self {
151 Self {
152 enabled: true,
153 count: 96,
154 volume_radius: 24.0,
155 size_range: (0.03, 0.08),
156 color: Color::WHITE,
157 shape: AmbientParticleShape::Cube,
158 edge_fade: 0.25,
159 drift_speed: 0.0,
160 seed: 0,
161 }
162 }
163}
164
165impl AmbientParticlesConfig {
166 pub fn active_count(&self) -> u32 {
184 if self.enabled && self.radius().is_some() {
185 self.count
186 } else {
187 0
188 }
189 }
190
191 fn radius(&self) -> Option<f32> {
193 (self.volume_radius.is_finite() && self.volume_radius > 0.0).then_some(self.volume_radius)
194 }
195}
196
197#[derive(Debug, Clone, Copy, PartialEq)]
203pub struct AmbientParticleState {
204 pub translation: Vec3,
206 pub scale: f32,
209 pub visible: bool,
211}
212
213impl AmbientParticleState {
214 const HIDDEN: Self = Self {
216 translation: Vec3::ZERO,
217 scale: 0.0,
218 visible: false,
219 };
220}
221
222const CHANNEL_HOME_X: i32 = 0;
224const CHANNEL_HOME_Y: i32 = 1;
225const CHANNEL_HOME_Z: i32 = 2;
226const CHANNEL_SIZE: i32 = 3;
227const CHANNEL_DRIFT_HEIGHT: i32 = 4;
228const CHANNEL_DRIFT_ANGLE: i32 = 5;
229
230#[inline]
232fn particle_hash(index: u32, channel: i32, seed: u32) -> f32 {
233 hash3(index as i32, channel, 0, seed)
234}
235
236pub fn ambient_particle_size(index: u32, config: &AmbientParticlesConfig) -> f32 {
253 let sanitize = |value: f32| {
254 if value.is_finite() {
255 value.max(0.0)
256 } else {
257 0.0
258 }
259 };
260 let a = sanitize(config.size_range.0);
261 let b = sanitize(config.size_range.1);
262 let (low, high) = (a.min(b), a.max(b));
263 low + (high - low) * particle_hash(index, CHANNEL_SIZE, config.seed)
264}
265
266fn drift_direction(index: u32, seed: u32) -> Vec3 {
268 let height = particle_hash(index, CHANNEL_DRIFT_HEIGHT, seed) * 2.0 - 1.0;
270 let angle = particle_hash(index, CHANNEL_DRIFT_ANGLE, seed) * std::f32::consts::TAU;
271 let ring = (1.0 - height * height).max(0.0).sqrt();
272 Vec3::new(ring * angle.cos(), height, ring * angle.sin())
273}
274
275#[inline]
283fn wrap_axis(value: f32, center: f32, radius: f32) -> f32 {
284 let period = 2.0 * radius;
285 value + ((center - value) / period).round() * period
286}
287
288pub fn ambient_particle_state(
324 index: u32,
325 viewer: Vec3,
326 elapsed_secs: f32,
327 config: &AmbientParticlesConfig,
328) -> AmbientParticleState {
329 let Some(radius) = config.radius() else {
330 return AmbientParticleState::HIDDEN;
331 };
332 if !viewer.is_finite() {
333 return AmbientParticleState::HIDDEN;
334 }
335
336 let seed = config.seed;
337 let period = 2.0 * radius;
338 let mut home = Vec3::new(
340 particle_hash(index, CHANNEL_HOME_X, seed),
341 particle_hash(index, CHANNEL_HOME_Y, seed),
342 particle_hash(index, CHANNEL_HOME_Z, seed),
343 ) * period;
344
345 let drift = config.drift_speed * elapsed_secs;
346 if drift.is_finite() && drift != 0.0 {
347 home += drift_direction(index, seed) * drift;
348 }
349
350 let translation = Vec3::new(
351 wrap_axis(home.x, viewer.x, radius),
352 wrap_axis(home.y, viewer.y, radius),
353 wrap_axis(home.z, viewer.z, radius),
354 );
355
356 let distance = translation.distance(viewer);
357 if !distance.is_finite() || distance >= radius {
358 return AmbientParticleState {
359 translation,
360 scale: 0.0,
361 visible: false,
362 };
363 }
364
365 let fade = if config.edge_fade.is_finite() {
366 config.edge_fade.clamp(0.0, 1.0)
367 } else {
368 0.0
369 };
370 let band = fade * radius;
371 let factor = if band > 0.0 {
372 ((radius - distance) / band).clamp(0.0, 1.0)
373 } else {
374 1.0
375 };
376 let scale = ambient_particle_size(index, config) * factor;
377
378 AmbientParticleState {
379 translation,
380 scale,
381 visible: scale > 0.0,
382 }
383}
384
385#[derive(Component, Debug, Clone, Copy, PartialEq, Eq)]
394pub struct AmbientParticle {
395 pub index: u32,
398}
399
400#[derive(Resource, Debug, Clone)]
406pub struct AmbientParticleAssets {
407 pub mesh: Handle<Mesh>,
410 pub material: Handle<StandardMaterial>,
412 shape: AmbientParticleShape,
414}
415
416#[derive(SystemSet, Debug, Clone, Copy, PartialEq, Eq, Hash)]
423pub struct AmbientParticleSystems;
424
425fn shape_mesh(shape: &AmbientParticleShape, meshes: &mut Assets<Mesh>) -> Handle<Mesh> {
429 match shape {
430 AmbientParticleShape::Cube => meshes.add(Cuboid::new(1.0, 1.0, 1.0)),
431 AmbientParticleShape::Sphere => meshes.add(Sphere::new(0.5)),
432 AmbientParticleShape::Custom(handle) => handle.clone(),
433 }
434}
435
436fn particle_material(color: Color) -> StandardMaterial {
438 StandardMaterial {
439 base_color: color,
440 unlit: true,
441 alpha_mode: if color.to_linear().alpha < 1.0 {
442 AlphaMode::Blend
443 } else {
444 AlphaMode::Opaque
445 },
446 ..default()
447 }
448}
449
450fn sync_ambient_particle_assets(
453 mut commands: Commands,
454 config: Res<AmbientParticlesConfig>,
455 assets: Option<ResMut<AmbientParticleAssets>>,
456 mut meshes: ResMut<Assets<Mesh>>,
457 mut materials: ResMut<Assets<StandardMaterial>>,
458 mut particles: Query<&mut Mesh3d, With<AmbientParticle>>,
459) {
460 let Some(mut assets) = assets else {
461 if config.active_count() == 0 {
462 return;
463 }
464 let mesh = shape_mesh(&config.shape, &mut meshes);
465 let material = materials.add(particle_material(config.color));
466 commands.insert_resource(AmbientParticleAssets {
467 mesh,
468 material,
469 shape: config.shape.clone(),
470 });
471 return;
472 };
473
474 if !config.is_changed() {
475 return;
476 }
477
478 if assets.shape != config.shape {
479 let mesh = shape_mesh(&config.shape, &mut meshes);
480 for mut particle_mesh in &mut particles {
481 particle_mesh.0 = mesh.clone();
482 }
483 assets.mesh = mesh;
484 assets.shape = config.shape.clone();
485 }
486
487 let color_changed = materials
489 .get(&assets.material)
490 .is_some_and(|material| material.base_color != config.color);
491 if color_changed && let Some(material) = materials.get_mut(&assets.material) {
492 *material = particle_material(config.color);
493 }
494}
495
496fn sync_ambient_particle_pool(
498 mut commands: Commands,
499 config: Res<AmbientParticlesConfig>,
500 assets: Option<Res<AmbientParticleAssets>>,
501 particles: Query<(Entity, &AmbientParticle)>,
502) {
503 let target = config.active_count();
504
505 let mut kept = 0u32;
506 for (entity, particle) in &particles {
507 if particle.index < target {
508 kept += 1;
509 } else {
510 commands.entity(entity).despawn();
511 }
512 }
513 if kept == target {
514 return;
515 }
516 let Some(assets) = assets else {
517 return;
518 };
519
520 let mut taken = vec![false; target as usize];
523 for (entity, particle) in &particles {
524 let Some(slot) = taken.get_mut(particle.index as usize) else {
525 continue;
526 };
527 if *slot {
528 commands.entity(entity).despawn();
529 } else {
530 *slot = true;
531 }
532 }
533
534 for (index, slot) in taken.iter().enumerate() {
535 if *slot {
536 continue;
537 }
538 commands.spawn((
540 AmbientParticle {
541 index: index as u32,
542 },
543 Mesh3d(assets.mesh.clone()),
544 MeshMaterial3d(assets.material.clone()),
545 Transform::from_scale(Vec3::ZERO),
546 Visibility::Hidden,
547 NotShadowCaster,
548 ));
549 }
550}
551
552fn update_ambient_particles(
554 viewer: Res<ViewerPosition>,
555 time: Res<Time>,
556 config: Res<AmbientParticlesConfig>,
557 mut particles: Query<(&AmbientParticle, &mut Transform, &mut Visibility)>,
558) {
559 let elapsed = time.elapsed_secs();
560
561 for (particle, mut transform, mut visibility) in &mut particles {
562 let state = ambient_particle_state(particle.index, viewer.position, elapsed, &config);
563
564 let scale = Vec3::splat(state.scale);
567 if transform.translation != state.translation || transform.scale != scale {
568 transform.translation = state.translation;
569 transform.scale = scale;
570 }
571 let wanted = if state.visible {
572 Visibility::Inherited
573 } else {
574 Visibility::Hidden
575 };
576 if *visibility != wanted {
577 *visibility = wanted;
578 }
579 }
580}
581
582pub struct AmbientParticlesPlugin;
628
629impl Plugin for AmbientParticlesPlugin {
630 fn build(&self, app: &mut App) {
631 app.init_resource::<AmbientParticlesConfig>()
632 .init_resource::<ViewerPosition>()
633 .add_systems(
634 PostUpdate,
635 (
636 sync_ambient_particle_assets,
637 sync_ambient_particle_pool,
638 update_ambient_particles,
639 )
640 .chain()
641 .in_set(AmbientParticleSystems)
642 .before(TransformSystems::Propagate)
643 .before(VisibilitySystems::VisibilityPropagate)
644 .before(VisibilitySystems::CalculateBounds),
645 );
646 }
647}
648
649#[cfg(test)]
652mod tests {
653 use super::*;
654
655 const EPS: f32 = 1e-3;
656
657 fn config() -> AmbientParticlesConfig {
658 AmbientParticlesConfig {
659 count: 200,
660 volume_radius: 10.0,
661 size_range: (0.1, 0.3),
662 ..default()
663 }
664 }
665
666 #[test]
669 fn defaults_are_documented_values() {
670 let config = AmbientParticlesConfig::default();
671 assert!(config.enabled);
672 assert_eq!(config.count, 96);
673 assert_eq!(config.volume_radius, 24.0);
674 assert_eq!(config.size_range, (0.03, 0.08));
675 assert_eq!(config.color, Color::WHITE);
676 assert_eq!(config.shape, AmbientParticleShape::Cube);
677 assert_eq!(config.edge_fade, 0.25);
678 assert_eq!(config.drift_speed, 0.0);
679 assert_eq!(config.seed, 0);
680 }
681
682 #[test]
683 fn state_is_deterministic() {
684 let config = config();
685 let viewer = Vec3::new(3.5, -120.25, 77.0);
686 for index in 0..config.count {
687 assert_eq!(
688 ambient_particle_state(index, viewer, 1.5, &config),
689 ambient_particle_state(index, viewer, 1.5, &config)
690 );
691 }
692 }
693
694 #[test]
695 fn particles_stay_within_the_volume_on_every_axis() {
696 let config = config();
697 for viewer in [
698 Vec3::ZERO,
699 Vec3::new(1234.5, -987.25, 42.0),
700 Vec3::new(-5000.0, 5000.0, -0.5),
701 ] {
702 for index in 0..config.count {
703 let state = ambient_particle_state(index, viewer, 0.0, &config);
704 let offset = (state.translation - viewer).abs();
705 assert!(
707 offset.max_element() <= config.volume_radius + 1e-2,
708 "particle {index} at {offset:?} from viewer {viewer:?}"
709 );
710 }
711 }
712 }
713
714 #[test]
715 fn visible_particles_are_inside_the_sphere() {
716 let config = config();
717 let viewer = Vec3::new(10.0, 20.0, 30.0);
718 let mut visible = 0;
719 for index in 0..config.count {
720 let state = ambient_particle_state(index, viewer, 0.0, &config);
721 let distance = state.translation.distance(viewer);
722 if state.visible {
723 visible += 1;
724 assert!(distance < config.volume_radius);
725 assert!(state.scale > 0.0);
726 } else {
727 assert_eq!(state.scale, 0.0);
728 }
729 }
730 assert!(
732 (60..=150).contains(&visible),
733 "{visible} of 200 particles visible"
734 );
735 }
736
737 #[test]
738 fn resting_particles_keep_their_world_position_until_recycled() {
739 let config = config();
740 let before = Vec3::ZERO;
741 let after = Vec3::new(1.0, 0.0, 0.0);
742 let mut unchanged = 0;
743 let mut recycled = 0;
744 for index in 0..config.count {
745 let a = ambient_particle_state(index, before, 0.0, &config).translation;
746 let b = ambient_particle_state(index, after, 5.0, &config).translation;
747 if a.distance(b) < EPS {
748 unchanged += 1;
749 } else {
750 recycled += 1;
751 assert!((b.x - a.x - 2.0 * config.volume_radius).abs() < EPS);
753 assert!((b.y - a.y).abs() < EPS);
754 assert!((b.z - a.z).abs() < EPS);
755 }
756 }
757 assert!(unchanged > recycled);
758 assert!(unchanged + recycled == config.count);
759 }
760
761 #[test]
762 fn moving_one_full_period_gives_the_same_relative_field() {
763 let config = config();
764 let shift = Vec3::new(2.0 * config.volume_radius, 0.0, 0.0);
765 for index in 0..config.count {
766 let a = ambient_particle_state(index, Vec3::ZERO, 0.0, &config);
767 let b = ambient_particle_state(index, shift, 0.0, &config);
768 assert!((b.translation - shift).distance(a.translation) < EPS);
769 }
770 }
771
772 #[test]
773 fn sizes_stay_in_range_whatever_the_order() {
774 let forward = config();
775 let reversed = AmbientParticlesConfig {
776 size_range: (0.3, 0.1),
777 ..config()
778 };
779 let mut smallest = f32::MAX;
780 let mut largest = f32::MIN;
781 for index in 0..forward.count {
782 let size = ambient_particle_size(index, &forward);
783 assert!((0.1..=0.3).contains(&size));
784 assert_eq!(size, ambient_particle_size(index, &reversed));
785 smallest = smallest.min(size);
786 largest = largest.max(size);
787 }
788 assert!(largest - smallest > 0.1, "sizes should vary");
789 }
790
791 #[test]
792 fn unusable_sizes_become_zero() {
793 let config = AmbientParticlesConfig {
794 size_range: (f32::NAN, -1.0),
795 ..config()
796 };
797 assert_eq!(ambient_particle_size(0, &config), 0.0);
798 assert!(!ambient_particle_state(0, Vec3::ZERO, 0.0, &config).visible);
799 }
800
801 #[test]
802 fn edge_fade_shrinks_particles_near_the_surface() {
803 let faded = AmbientParticlesConfig {
804 edge_fade: 0.5,
805 ..config()
806 };
807 let hard = AmbientParticlesConfig {
808 edge_fade: 0.0,
809 ..config()
810 };
811 let band_start = faded.volume_radius * 0.5;
812 let mut shrunk = 0;
813 for index in 0..faded.count {
814 let state = ambient_particle_state(index, Vec3::ZERO, 0.0, &faded);
815 let full = ambient_particle_size(index, &faded);
816 let distance = state.translation.length();
817 if !state.visible {
818 continue;
819 }
820 let unfaded = ambient_particle_state(index, Vec3::ZERO, 0.0, &hard);
822 assert_eq!(unfaded.scale, full);
823 if distance <= band_start {
824 assert_eq!(state.scale, full);
825 } else {
826 assert!(state.scale <= full);
827 if state.scale < full {
828 shrunk += 1;
829 }
830 }
831 }
832 assert!(shrunk > 0);
833 }
834
835 #[test]
836 fn drift_moves_particles_at_the_configured_speed() {
837 let config = AmbientParticlesConfig {
838 drift_speed: 0.5,
839 ..config()
840 };
841 let mut checked = 0;
842 for index in 0..config.count {
843 let a = ambient_particle_state(index, Vec3::ZERO, 0.0, &config).translation;
844 let b = ambient_particle_state(index, Vec3::ZERO, 1.0, &config).translation;
845 let moved = a.distance(b);
846 if moved < 1.0 {
848 assert!((moved - 0.5).abs() < EPS, "particle {index} moved {moved}");
849 checked += 1;
850 }
851 }
852 assert!(checked > config.count / 2);
853 }
854
855 #[test]
856 fn drift_directions_are_unit_length_and_varied() {
857 let first = drift_direction(0, 0);
858 let mut varied = false;
859 for index in 0..64 {
860 let direction = drift_direction(index, 0);
861 assert!((direction.length() - 1.0).abs() < EPS);
862 varied |= direction.distance(first) > 0.5;
863 }
864 assert!(varied);
865 }
866
867 #[test]
868 fn seed_changes_the_field() {
869 let a = config();
870 let b = AmbientParticlesConfig {
871 seed: 1,
872 ..config()
873 };
874 let differing = (0..a.count)
875 .filter(|&index| {
876 ambient_particle_state(index, Vec3::ZERO, 0.0, &a).translation
877 != ambient_particle_state(index, Vec3::ZERO, 0.0, &b).translation
878 })
879 .count();
880 assert!(differing > 190);
881 }
882
883 #[test]
884 fn unusable_configs_and_viewers_hide_everything() {
885 for volume_radius in [0.0, -4.0, f32::NAN, f32::INFINITY] {
886 let config = AmbientParticlesConfig {
887 volume_radius,
888 ..config()
889 };
890 assert_eq!(config.active_count(), 0);
891 let state = ambient_particle_state(0, Vec3::ZERO, 0.0, &config);
892 assert_eq!(state, AmbientParticleState::HIDDEN);
893 }
894 let disabled = AmbientParticlesConfig {
895 enabled: false,
896 ..config()
897 };
898 assert_eq!(disabled.active_count(), 0);
899
900 let state = ambient_particle_state(0, Vec3::splat(f32::NAN), 0.0, &config());
901 assert_eq!(state, AmbientParticleState::HIDDEN);
902 }
903
904 fn app(config: AmbientParticlesConfig) -> App {
907 let mut app = App::new();
908 app.init_resource::<Time>()
909 .init_resource::<Assets<Mesh>>()
910 .init_resource::<Assets<StandardMaterial>>()
911 .insert_resource(config)
912 .add_plugins(AmbientParticlesPlugin);
913 app
914 }
915
916 fn particles(app: &mut App) -> Vec<(u32, Transform, Visibility)> {
917 let world = app.world_mut();
918 let mut query = world.query::<(&AmbientParticle, &Transform, &Visibility)>();
919 let mut found: Vec<_> = query
920 .iter(world)
921 .map(|(particle, transform, visibility)| (particle.index, *transform, *visibility))
922 .collect();
923 found.sort_by_key(|(index, _, _)| *index);
924 found
925 }
926
927 #[test]
928 fn plugin_keeps_an_inserted_config_and_initializes_the_viewer() {
929 let mut app = app(config());
930 app.update();
931 assert_eq!(app.world().resource::<AmbientParticlesConfig>().count, 200);
932 assert!(app.world().get_resource::<ViewerPosition>().is_some());
933 }
934
935 #[test]
936 fn pool_matches_count_and_follows_the_pure_placement() {
937 let mut app = app(config());
938 app.world_mut().resource_mut::<ViewerPosition>().position = Vec3::new(50.0, 0.0, 0.0);
939 app.update();
940 app.update();
941
942 let config = config();
943 let found = particles(&mut app);
944 assert_eq!(found.len(), 200);
945 for (slot, (index, transform, visibility)) in found.iter().enumerate() {
946 assert_eq!(*index, slot as u32);
947 let state = ambient_particle_state(*index, Vec3::new(50.0, 0.0, 0.0), 0.0, &config);
948 assert_eq!(transform.translation, state.translation);
949 assert_eq!(transform.scale, Vec3::splat(state.scale));
950 assert_eq!(*visibility == Visibility::Inherited, state.visible);
951 }
952 }
953
954 #[test]
955 fn particles_share_one_mesh_and_one_material() {
956 let mut app = app(config());
957 app.update();
958 app.update();
959
960 let assets = app.world().resource::<AmbientParticleAssets>().clone();
961 let world = app.world_mut();
962 let mut query = world
963 .query_filtered::<(&Mesh3d, &MeshMaterial3d<StandardMaterial>), With<AmbientParticle>>(
964 );
965 let mut seen = 0;
966 for (mesh, material) in query.iter(world) {
967 assert_eq!(mesh.0, assets.mesh);
968 assert_eq!(material.0, assets.material);
969 seen += 1;
970 }
971 assert_eq!(seen, 200);
972 assert_eq!(world.resource::<Assets<Mesh>>().len(), 1);
973 assert_eq!(world.resource::<Assets<StandardMaterial>>().len(), 1);
974
975 let material = world
976 .resource::<Assets<StandardMaterial>>()
977 .get(&assets.material)
978 .expect("shared material exists");
979 assert!(material.unlit);
980 assert_eq!(material.base_color, Color::WHITE);
981 }
982
983 #[test]
984 fn pool_follows_count_and_enabled_changes() {
985 let mut app = app(config());
986 app.update();
987 app.update();
988 assert_eq!(particles(&mut app).len(), 200);
989
990 app.world_mut()
991 .resource_mut::<AmbientParticlesConfig>()
992 .count = 50;
993 app.update();
994 app.update();
995 let found = particles(&mut app);
996 assert_eq!(found.len(), 50);
997 assert!(found.iter().all(|(index, _, _)| *index < 50));
998
999 app.world_mut()
1000 .resource_mut::<AmbientParticlesConfig>()
1001 .count = 80;
1002 app.update();
1003 app.update();
1004 let found = particles(&mut app);
1005 assert_eq!(found.len(), 80);
1006 assert!(
1007 found
1008 .iter()
1009 .enumerate()
1010 .all(|(slot, (index, _, _))| *index == slot as u32)
1011 );
1012
1013 app.world_mut()
1014 .resource_mut::<AmbientParticlesConfig>()
1015 .enabled = false;
1016 app.update();
1017 app.update();
1018 assert!(particles(&mut app).is_empty());
1019 }
1020
1021 #[test]
1022 fn disabled_plugin_allocates_nothing() {
1023 let mut app = app(AmbientParticlesConfig {
1024 enabled: false,
1025 ..config()
1026 });
1027 app.update();
1028 app.update();
1029 assert!(particles(&mut app).is_empty());
1030 assert!(
1031 app.world()
1032 .get_resource::<AmbientParticleAssets>()
1033 .is_none()
1034 );
1035 assert_eq!(app.world().resource::<Assets<Mesh>>().len(), 0);
1036 }
1037
1038 #[test]
1039 fn a_missing_slot_is_refilled() {
1040 let mut app = app(config());
1041 app.update();
1042 app.update();
1043
1044 let world = app.world_mut();
1045 let mut query = world.query::<(Entity, &AmbientParticle)>();
1046 let victim = query
1047 .iter(world)
1048 .find(|(_, particle)| particle.index == 7)
1049 .map(|(entity, _)| entity)
1050 .expect("slot 7 exists");
1051 world.despawn(victim);
1052
1053 app.update();
1054 app.update();
1055 let found = particles(&mut app);
1056 assert_eq!(found.len(), 200);
1057 assert_eq!(found[7].0, 7);
1058 }
1059
1060 #[test]
1061 fn color_and_shape_changes_update_the_shared_assets() {
1062 let mut app = app(config());
1063 app.update();
1064 app.update();
1065 let before = app.world().resource::<AmbientParticleAssets>().clone();
1066
1067 let translucent = Color::srgba(1.0, 0.5, 0.25, 0.5);
1068 {
1069 let mut config = app.world_mut().resource_mut::<AmbientParticlesConfig>();
1070 config.color = translucent;
1071 config.shape = AmbientParticleShape::Sphere;
1072 }
1073 app.update();
1074 app.update();
1075
1076 let after = app.world().resource::<AmbientParticleAssets>().clone();
1077 assert_eq!(
1078 after.material, before.material,
1079 "material is edited in place"
1080 );
1081 assert_ne!(after.mesh, before.mesh, "a new shape needs a new mesh");
1082
1083 let world = app.world_mut();
1084 let material = world
1085 .resource::<Assets<StandardMaterial>>()
1086 .get(&after.material)
1087 .expect("shared material exists");
1088 assert_eq!(material.base_color, translucent);
1089 assert_eq!(material.alpha_mode, AlphaMode::Blend);
1090
1091 let mut query = world.query_filtered::<&Mesh3d, With<AmbientParticle>>();
1092 assert!(query.iter(world).all(|mesh| mesh.0 == after.mesh));
1093 }
1094
1095 #[test]
1096 fn particles_stream_past_a_moving_viewer() {
1097 let mut app = app(config());
1098 app.update();
1099 app.update();
1100 let before = particles(&mut app);
1101
1102 app.world_mut().resource_mut::<ViewerPosition>().position = Vec3::new(0.5, 0.0, 0.0);
1103 app.update();
1104 let after = particles(&mut app);
1105
1106 let still = before
1109 .iter()
1110 .zip(&after)
1111 .filter(|(a, b)| a.1.translation == b.1.translation)
1112 .count();
1113 assert!(still > 180, "{still} of 200 particles kept their position");
1114 }
1115}