-
Notifications
You must be signed in to change notification settings - Fork 121
RotateOverTimeModifier #552
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
hukasu
wants to merge
3
commits into
djeedai:main
Choose a base branch
from
hukasu:rotate-over-time
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| //! An example using the [`RotateOverTimeModifier`]. | ||
|
|
||
| use std::f32::consts::FRAC_PI_2; | ||
|
|
||
| use bevy::{core_pipeline::tonemapping::Tonemapping, prelude::*}; | ||
| use bevy_hanabi::prelude::*; | ||
|
|
||
| mod utils; | ||
| use utils::*; | ||
|
|
||
| const DEMO_DESC: &str = include_str!("rotate_over_time.txt"); | ||
| const COLOR: Vec4 = Vec4::new(0.7, 0.7, 1.0, 1.0); | ||
| const SIZE: Vec3 = Vec3::splat(0.1); | ||
|
|
||
| fn main() -> Result<(), Box<dyn std::error::Error>> { | ||
| let app_exit = utils::DemoApp::new("box") | ||
| .with_desc(DEMO_DESC) | ||
| .build() | ||
| .add_systems(Startup, setup) | ||
| .add_systems(Update, rotate_camera) | ||
| .run(); | ||
| app_exit.into_result() | ||
| } | ||
|
|
||
| fn setup( | ||
| mut commands: Commands, | ||
| mut effects: ResMut<Assets<EffectAsset>>, | ||
| mut meshes: ResMut<Assets<Mesh>>, | ||
| mut materials: ResMut<Assets<StandardMaterial>>, | ||
| ) { | ||
| commands.spawn(( | ||
| Transform::from_xyz(3.0, 3.0, 3.0).looking_at(Vec3::ZERO, Vec3::Y), | ||
| Camera3d::default(), | ||
| Projection::Perspective(PerspectiveProjection { | ||
| fov: 120.0, | ||
| ..default() | ||
| }), | ||
| Tonemapping::None, | ||
| )); | ||
|
|
||
| // The ground | ||
| commands.spawn(( | ||
| Transform::from_xyz(0.0, -0.5, 0.0) | ||
| * Transform::from_rotation(Quat::from_rotation_x(-FRAC_PI_2)), | ||
| Mesh3d(meshes.add(Rectangle { | ||
| half_size: Vec2::splat(2.0), | ||
| })), | ||
| MeshMaterial3d(materials.add(utils::COLOR_BLUE)), | ||
| Name::new("ground"), | ||
| )); | ||
|
|
||
| let writer = ExprWriter::new(); | ||
|
|
||
| let init_pos = SetPositionCircleModifier { | ||
| center: writer.lit(Vec3::Y * 0.1).expr(), | ||
| axis: writer.lit(Vec3::Y).expr(), | ||
| radius: writer.lit(1.).expr(), | ||
| dimension: ShapeDimension::Volume, | ||
| }; | ||
|
|
||
| let init_axis_x = SetAttributeModifier::new(Attribute::AXIS_X, writer.lit(Vec3::X).expr()); | ||
| let init_axis_y = SetAttributeModifier::new(Attribute::AXIS_Y, writer.lit(Vec3::Y).expr()); | ||
| let init_axis_z = SetAttributeModifier::new(Attribute::AXIS_Z, writer.lit(Vec3::Z).expr()); | ||
|
|
||
| // Particle will complete 3/4 of a rotation around Y axis per second | ||
| let rotate_over_time = RotateOverTimeModifier { | ||
| rotation: writer | ||
| .lit(Vec3::new(0., 270.0f32.to_radians(), 190.0f32.to_radians())) | ||
| .expr(), | ||
| }; | ||
|
|
||
| let module = writer.finish(); | ||
|
|
||
| let effect = effects.add( | ||
| EffectAsset::new(32768, SpawnerSettings::once(64.0.into()), module) | ||
| .with_name("rotate_over_time") | ||
| // Disable motion integration; in this demo particles don't move. This silences some warning | ||
| // about missing the VELOCITY attribute. | ||
| .with_motion_integration(MotionIntegration::None) | ||
| .with_simulation_space(SimulationSpace::Local) | ||
| .init(init_pos) | ||
| .init(init_axis_x) | ||
| .init(init_axis_y) | ||
| .init(init_axis_z) | ||
| .update(rotate_over_time) | ||
| .render(SetColorModifier::new(COLOR)) | ||
| .render(SetSizeModifier { size: SIZE.into() }), | ||
| ); | ||
|
|
||
| commands.spawn(( | ||
| Transform::from_translation(Vec3::new(0., 1., 0.)), | ||
| ParticleEffect::new(effect), | ||
| Name::new("Rotate Over Time"), | ||
| )); | ||
| } | ||
|
|
||
| fn rotate_camera(time: Res<Time>, mut camera_transform: Single<&mut Transform, With<Camera3d>>) { | ||
| let radius_xz = 18_f32.sqrt(); | ||
| let a = (time.elapsed_secs() * 0.3).sin(); | ||
| let (s, c) = a.sin_cos(); | ||
| **camera_transform = | ||
| Transform::from_xyz(c * radius_xz, 3.0, s * radius_xz).looking_at(Vec3::ZERO, Vec3::Y) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| This example shows the behavior of the RotateOverTimeModifier. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| use bevy::reflect::Reflect; | ||
|
|
||
| use crate::{ | ||
| Attribute, BoxedModifier, BuiltInExpr, EvalContext, ExprError, ExprHandle, Modifier, | ||
| ModifierContext, Module, ShaderWriter, | ||
| }; | ||
|
|
||
| /// Rotates particles over time. | ||
| #[derive(Clone, Copy, Reflect)] | ||
| pub struct RotateOverTimeModifier { | ||
| /// Rotation that the particle will have in a second. | ||
| /// | ||
| /// The rotation is defined as a Euler rotation, applied | ||
| /// in XYZ order. Angles must be in radians. | ||
| /// | ||
| /// Expr type: Vec3 | ||
| pub rotation: ExprHandle, | ||
| } | ||
|
|
||
| impl Modifier for RotateOverTimeModifier { | ||
| fn context(&self) -> ModifierContext { | ||
| ModifierContext::Update | ||
| } | ||
|
|
||
| fn attributes(&self) -> &[Attribute] { | ||
| &[Attribute::AXIS_X, Attribute::AXIS_Y, Attribute::AXIS_Z] | ||
| } | ||
|
|
||
| fn boxed_clone(&self) -> BoxedModifier { | ||
| Box::new(*self) | ||
| } | ||
|
|
||
| fn apply(&self, module: &mut Module, context: &mut ShaderWriter) -> Result<(), ExprError> { | ||
| let rotation = context.eval(module, self.rotation)?; | ||
| let dt = BuiltInExpr::new(crate::graph::BuiltInOperator::DeltaTime).eval(context)?; | ||
| context.main_code += &format!( | ||
| r#" {{ | ||
| let euler_angles = {rotation} * {dt}; | ||
| let cx = cos(euler_angles.x); | ||
| let sx = sin(euler_angles.x); | ||
| let cy = cos(euler_angles.y); | ||
| let sy = sin(euler_angles.y); | ||
| let cz = cos(euler_angles.z); | ||
| let sz = sin(euler_angles.z); | ||
|
|
||
| // Individual axes matrices (Column-major format) | ||
| let rx = mat3x3<f32>( | ||
| 1.0, 0.0, 0.0, | ||
| 0.0, cx, sx, | ||
| 0.0, -sx, cx | ||
| ); | ||
|
|
||
| let ry = mat3x3<f32>( | ||
| cy, 0.0, -sy, | ||
| 0.0, 1.0, 0.0, | ||
| sy, 0.0, cy | ||
| ); | ||
|
|
||
| let rz = mat3x3<f32>( | ||
| cz, sz, 0.0, | ||
| -sz, cz, 0.0, | ||
| 0.0, 0.0, 1.0 | ||
| ); | ||
|
|
||
| // Combines rotations (Applies X, then Y, then Z) | ||
| let rotation = rz * ry * rx; | ||
| particle.{0} = particle.{0} * rotation; | ||
| particle.{1} = particle.{1} * rotation; | ||
| particle.{2} = particle.{2} * rotation; | ||
| }} | ||
| "#, | ||
| Attribute::AXIS_X.name(), | ||
| Attribute::AXIS_Y.name(), | ||
| Attribute::AXIS_Z.name(), | ||
| ); | ||
| Ok(()) | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In general please avoid adding examples unless there's a non-obvious new feature or there's really no other example this could fit in. The reason is that the maintenance cost of examples is quite high; not only are examples used as behavior benchmark to validate behavior for all major changes (need to run them all one by one for visual check), they also require extra work in
wasm(add new webpage, extra compile time, etc.). Ideally all of that should be automated or covered by unit/feature tests, but until then it's quite the burden. Thanks!There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
which example would it be ok to include this modifier then? if any?