fyrox_impl/scene/particle_system/
particle.rs

1// Copyright (c) 2019-present Dmitry Stepanov and Fyrox Engine contributors.
2//
3// Permission is hereby granted, free of charge, to any person obtaining a copy
4// of this software and associated documentation files (the "Software"), to deal
5// in the Software without restriction, including without limitation the rights
6// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7// copies of the Software, and to permit persons to whom the Software is
8// furnished to do so, subject to the following conditions:
9//
10// The above copyright notice and this permission notice shall be included in all
11// copies or substantial portions of the Software.
12//
13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19// SOFTWARE.
20
21//! Particle is a quad with texture and various other parameters, such as
22//! position, velocity, size, lifetime, etc.
23
24use crate::core::{algebra::Vector3, color::Color, visitor::prelude::*};
25use std::cell::Cell;
26
27/// See module docs.
28#[derive(Clone, Debug, Visit)]
29pub struct Particle {
30    /// Position of particle in local coordinates.
31    #[visit(rename = "Pos")]
32    pub position: Vector3<f32>,
33    /// Velocity of particle in local coordinates.
34    #[visit(rename = "Vel")]
35    pub velocity: Vector3<f32>,
36    /// Size of particle.
37    pub size: f32,
38    /// Modifier for size which will be added to size each update tick.
39    pub size_modifier: f32,
40    /// Lifetime at the moment when particle was created.
41    pub initial_lifetime: f32,
42    /// Rotation speed of particle in radians per second.
43    pub rotation_speed: f32,
44    /// Rotation angle in radians.
45    pub rotation: f32,
46    /// Color of particle.
47    pub color: Color,
48
49    pub(super) alive: bool,
50    pub(super) emitter_index: u32,
51    /// Particle is alive if lifetime > 0
52    #[visit(rename = "LifeTime")]
53    pub(super) lifetime: f32,
54    #[visit(skip)]
55    pub(super) sqr_distance_to_camera: Cell<f32>,
56}
57
58impl Default for Particle {
59    fn default() -> Self {
60        Self {
61            position: Default::default(),
62            velocity: Default::default(),
63            size: 1.0,
64            alive: true,
65            size_modifier: 0.0,
66            lifetime: 0.0,
67            initial_lifetime: 2.0,
68            rotation_speed: 0.0,
69            rotation: 0.0,
70            emitter_index: 0,
71            color: Color::WHITE,
72            sqr_distance_to_camera: Cell::new(0.0),
73        }
74    }
75}
76
77impl Particle {
78    /// Sets new position in builder manner.
79    pub fn with_position(mut self, position: Vector3<f32>) -> Self {
80        self.position = position;
81        self
82    }
83
84    /// Sets new velocity in builder manner.
85    pub fn with_velocity(mut self, velocity: Vector3<f32>) -> Self {
86        self.velocity = velocity;
87        self
88    }
89
90    /// Sets new size in builder manner.
91    pub fn with_size(mut self, size: f32) -> Self {
92        self.size = size;
93        self
94    }
95
96    /// Sets new size modifier in builder manner.
97    pub fn with_size_modifier(mut self, size_modifier: f32) -> Self {
98        self.size_modifier = size_modifier;
99        self
100    }
101
102    /// Sets new initial lifetime in builder manner.
103    pub fn with_initial_lifetime(mut self, initial_lifetime: f32) -> Self {
104        self.initial_lifetime = initial_lifetime;
105        self
106    }
107
108    /// Sets new rotation in builder manner.
109    pub fn with_rotation(mut self, rotation: f32) -> Self {
110        self.rotation = rotation;
111        self
112    }
113
114    /// Sets new rotation speed in builder manner.
115    pub fn with_rotation_speed(mut self, rotation_speed: f32) -> Self {
116        self.rotation_speed = rotation_speed;
117        self
118    }
119
120    /// Sets new color in builder manner.
121    pub fn with_color(mut self, color: Color) -> Self {
122        self.color = color;
123        self
124    }
125}