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

Add MathUtils.pingpong() #21091

Merged
merged 4 commits into from
Jan 16, 2021
Merged

Add MathUtils.pingpong() #21091

merged 4 commits into from
Jan 16, 2021

Conversation

marcofugaro
Copy link
Contributor

@marcofugaro marcofugaro commented Jan 15, 2021

Related issue: -

Description

One function I find useful is the pingPong function. It alternates between two values, you can use it to alternate between two colors for example.
It also can be combined with the in-out eases from Robert Penner's Easing Functions to create a differently shaped alternating function.

Here is how the function looks like:
https://www.desmos.com/calculator/vcsjnyz7x4

image

@gkjohnson
Copy link
Collaborator

gkjohnson commented Jan 15, 2021

It looks like the current implementation doesn't work as expected with negative numbers:

MathUtils.pingPong( - 1.5 ); // - 1.5
MathUtils.pingPong( - 2.5 ); // - 0.5

@marcofugaro
Copy link
Contributor Author

@gkjohnson missed that, thanks for testing. It is fixed by using the euclideanModulo instead of the javascript modulo.

I added the correct negative case to the unit test.

Copy link
Collaborator

@Mugen87 Mugen87 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@marcofugaro
Copy link
Contributor Author

marcofugaro commented Jan 16, 2021

Thanks, I was aware, I put the unity link in the description.

From the unity implementation, I renamed the parameter t to x to better match the rest of the MathUtils functions.

@Mugen87
Copy link
Collaborator

Mugen87 commented Jan 16, 2021

Thanks, I was aware, I put the unity link in the description.

Sorry, did not realize that^^. I know the method from Unity and I think it's a useful addition to MathUtils.

@mrdoob
Copy link
Owner

mrdoob commented Jan 16, 2021

Maybe pingpong instead of pingPong in this case? We have smoothstep and smootherstep ...

@marcofugaro
Copy link
Contributor Author

Sure, it makes sense. Done!

@marcofugaro marcofugaro changed the title Add MathUtils.pingPong() Add MathUtils.pingpong() Jan 16, 2021
@mrdoob mrdoob added this to the r125 milestone Jan 16, 2021
@mrdoob mrdoob merged commit c4d3bbe into mrdoob:dev Jan 16, 2021
@mrdoob
Copy link
Owner

mrdoob commented Jan 16, 2021

Thanks!

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

Successfully merging this pull request may close these issues.

4 participants