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

MathUtils: Added .inverseLerp(). #21544

Merged
merged 4 commits into from
Mar 30, 2021
Merged

MathUtils: Added .inverseLerp(). #21544

merged 4 commits into from
Mar 30, 2021

Conversation

Hoodgail
Copy link
Contributor

Description

Calculates the linear parameter t that produces the interpolant value within the range [a, b]. The a and b values define the start and end of the line.
https://www.gamedev.net/tutorials/programming/general-and-gameplay-programming/inverse-lerp-a-super-useful-yet-often-overlooked-function-r5230/ for more information and to why its useful

This contribution is funded by Hoodgail.

src/math/MathUtils.js Outdated Show resolved Hide resolved
@Mugen87
Copy link
Collaborator

Mugen87 commented Mar 30, 2021

We should consider to implement the method like in Unity. If you do this:

const t = MathUtils.inverseLerp( 2, 2, 2 );

The method returns NaN because of the zero divide. I would use:

inverseLerp: function ( a, b, value ) {

    if ( a !== b ) {

        return MathUtils.clamp( ( value - a ) / ( b - a ) );

    } else {

        return 0;

    }

}

@Hoodgail
Copy link
Contributor Author

Resolved

Update variable names to match `lerp()`.
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.

@mrdoob This looks good to me.

I can enhance the docs and unit tests with a subsequent PR.

src/math/MathUtils.js Outdated Show resolved Hide resolved
@mrdoob mrdoob added this to the r127 milestone Mar 30, 2021
@mrdoob
Copy link
Owner

mrdoob commented Mar 30, 2021

@mrdoob Do you consider three.js a gaming engine?

I think it's a valid use case.

I wouldn't want it to focus too much on games but I'm interested in making it easier for people to do games with it:

https://threejs.org/examples/?q=game#games_fps

@mrdoob mrdoob merged commit 2916db9 into mrdoob:dev Mar 30, 2021
@mrdoob
Copy link
Owner

mrdoob commented Mar 30, 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