Skip to content
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

StandardMaterial easings? #8

Open
joncol opened this issue Dec 28, 2020 · 2 comments
Open

StandardMaterial easings? #8

joncol opened this issue Dec 28, 2020 · 2 comments

Comments

@joncol
Copy link

joncol commented Dec 28, 2020

Any plan on adding StandardMaterial as an available easing target? I'm fumbling around with trying it myself, but I'm running into problems due to lack of skills.

Current attempt:

#[derive(Debug, Default)]
struct MyEaseValue<T>(pub T);

impl Lerp for MyEaseValue<StandardMaterial> {
    type Scalar = f32;

    fn lerp(&self, other: &Self, scalar: &Self::Scalar) -> Self {
        MyEaseValue(StandardMaterial {
            albedo: self.0.albedo
                + (other.0.albedo + (self.0.albedo * -1.)) * *scalar,
            albedo_texture: None,
            ..Default::default()
        })
    }
}

Which is more or less copied from your implementation for ColorMaterial. Getting problems with how to add easings for my materials with this.

@joncol
Copy link
Author

joncol commented Dec 30, 2020

OK, figured out how to use custom easings finally. Just thought it would be a good idea to summarize what I did, if anyone else stumbles upon this:

#[derive(Debug, Default)]
struct MyEaseValue<T>(pub T);

impl Lerp for MyEaseValue<Color> {
    type Scalar = f32;

    fn lerp(&self, other: &Self, scalar: &Self::Scalar) -> Self {
        MyEaseValue(self.0 + (other.0 + (self.0 * -1.)) * *scalar)
    }
}

And then created a helper function to add an EasingComponent to a specified entity:

#[derive(Debug)]
struct Fade;

fn fade_to(
    commands: &mut Commands,
    entity: Entity,
    from: Color,
    to: Color,
    millis: u64,
) {
    commands.set_current_entity(entity);
    commands.with(Fade).with(MyEaseValue(from)).with(
        MyEaseValue(from).ease_to(
            MyEaseValue(to),
            EaseFunction::QuadraticOut,
            EasingType::Once {
                duration: Duration::from_millis(millis),
            },
        ),
    );
}

Fade in the above is just a component for my fading system to recognize that it should handle this entity:

fn fade(
    mut materials: ResMut<Assets<StandardMaterial>>,
    mut query: Query<
        (&MyEaseValue<Color>, &Handle<StandardMaterial>),
        With<Fade>,
    >,
) {
    for (color, mat_handle) in query.iter_mut() {
        materials.set(mat_handle, color.0.into());
    }
}

Finally, the systems should be added:

app
    .add_system(fade.system())
    .add_system(custom_ease_system::<MyEaseValue<Color>>.system())

Feel free to close this issue. I don't know if it's a good idea to have StandardMaterial easings? Maybe that would not work so well with textures etc...

@mockersf
Copy link
Member

great! thank you for digging into this!
doing easing on something that is behind a Handle is not something simple, it depends on which part you want to change... your solution for that is nice!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants