Skip to content

Commit 26cbde5

Browse files
committed
configurable fadeout margin for particle systems
1 parent 7f6cab0 commit 26cbde5

File tree

1 file changed

+19
-11
lines changed
  • fyrox-impl/src/scene/particle_system

1 file changed

+19
-11
lines changed

fyrox-impl/src/scene/particle_system/mod.rs

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,6 @@
2121
//! Contains all structures and methods to create and manage particle systems. See [`ParticleSystem`] docs for more
2222
//! info and usage examples.
2323
24-
use crate::scene::node::constructor::NodeConstructor;
25-
use crate::scene::particle_system::emitter::base::BaseEmitterBuilder;
26-
use crate::scene::particle_system::emitter::sphere::SphereEmitterBuilder;
2724
use crate::{
2825
core::{
2926
algebra::{Point3, Vector2, Vector3},
@@ -45,16 +42,15 @@ use crate::{
4542
base::{Base, BaseBuilder},
4643
graph::Graph,
4744
mesh::{buffer::VertexTrait, RenderPath},
48-
node::{Node, NodeTrait, RdcControlFlow, UpdateContext},
45+
node::{constructor::NodeConstructor, Node, NodeTrait, RdcControlFlow, UpdateContext},
4946
particle_system::{
5047
draw::Vertex,
51-
emitter::{Emit, Emitter},
48+
emitter::{base::BaseEmitterBuilder, sphere::SphereEmitterBuilder, Emit, Emitter},
5249
particle::Particle,
5350
},
5451
},
5552
};
56-
use fyrox_graph::constructor::ConstructorProvider;
57-
use fyrox_graph::BaseSceneGraph;
53+
use fyrox_graph::{constructor::ConstructorProvider, BaseSceneGraph};
5854
use std::{
5955
cmp::Ordering,
6056
fmt::Debug,
@@ -258,6 +254,10 @@ pub struct ParticleSystem {
258254
/// detached from the particle system (smoke trails)
259255
coordinate_system: InheritableVariable<CoordinateSystem>,
260256

257+
/// A margin value in which the distance fading will occur.
258+
#[reflect(min_value = 0.0)]
259+
fadeout_margin: InheritableVariable<f32>,
260+
261261
rng: ParticleSystemRng,
262262
}
263263

@@ -313,6 +313,7 @@ impl Visit for ParticleSystem {
313313
let _ = self
314314
.coordinate_system
315315
.visit("CoordinateSystem", &mut region);
316+
let _ = self.fadeout_margin.visit("FadeoutMargin", &mut region);
316317

317318
// Backward compatibility.
318319
if region.is_reading() {
@@ -364,8 +365,6 @@ impl TypeUuidProvider for ParticleSystem {
364365
}
365366

366367
impl ParticleSystem {
367-
const FADEOUT_MARGIN: f32 = 1.5;
368-
369368
/// Returns current acceleration for particles in particle system.
370369
pub fn acceleration(&self) -> Vector3<f32> {
371370
*self.acceleration
@@ -533,7 +532,7 @@ impl ParticleSystem {
533532

534533
fn is_distance_clipped(&self, point: &Vector3<f32>) -> bool {
535534
point.metric_distance(&self.global_position())
536-
> (*self.visible_distance + Self::FADEOUT_MARGIN)
535+
> (*self.visible_distance + *self.fadeout_margin)
537536
}
538537
}
539538

@@ -599,7 +598,7 @@ impl NodeTrait for ParticleSystem {
599598
.metric_distance(&self.global_position());
600599

601600
let particle_alpha_factor = if distance_to_observer >= self.visible_distance() {
602-
1.0 - (distance_to_observer - self.visible_distance()) / Self::FADEOUT_MARGIN
601+
1.0 - (distance_to_observer - self.visible_distance()) / *self.fadeout_margin
603602
} else {
604603
1.0
605604
};
@@ -731,6 +730,7 @@ pub struct ParticleSystemBuilder {
731730
rng: ParticleSystemRng,
732731
visible_distance: f32,
733732
coordinate_system: CoordinateSystem,
733+
fadeout_margin: f32,
734734
}
735735

736736
impl ParticleSystemBuilder {
@@ -751,6 +751,7 @@ impl ParticleSystemBuilder {
751751
rng: ParticleSystemRng::default(),
752752
visible_distance: 30.0,
753753
coordinate_system: Default::default(),
754+
fadeout_margin: 1.5,
754755
}
755756
}
756757

@@ -810,6 +811,12 @@ impl ParticleSystemBuilder {
810811
self
811812
}
812813

814+
/// Sets a margin value in which the distance fading will occur.
815+
pub fn with_fadeout_margin(mut self, margin: f32) -> Self {
816+
self.fadeout_margin = margin;
817+
self
818+
}
819+
813820
fn build_particle_system(self) -> ParticleSystem {
814821
ParticleSystem {
815822
base: self.base_builder.build_base(),
@@ -823,6 +830,7 @@ impl ParticleSystemBuilder {
823830
rng: self.rng,
824831
visible_distance: self.visible_distance.into(),
825832
coordinate_system: self.coordinate_system.into(),
833+
fadeout_margin: self.fadeout_margin.into(),
826834
}
827835
}
828836

0 commit comments

Comments
 (0)