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

Nodes: RotateNode and remapping functions #27525

Merged
merged 1 commit into from
Jan 8, 2024

Conversation

RenaudRohlinger
Copy link
Collaborator

@RenaudRohlinger RenaudRohlinger commented Jan 8, 2024

Added a new node RotateNode useful to rotate a positionLocal in 3D for example. Also added some remapping functions from Inigo Quilez. and PI PI2 Math nodes.

This contribution is funded by Utsubo

@mrdoob mrdoob added this to the r161 milestone Jan 8, 2024
@sunag sunag merged commit bdb5b06 into mrdoob:dev Jan 8, 2024
11 checks passed
AdaRoseCannon pushed a commit to AdaRoseCannon/three.js that referenced this pull request Jan 15, 2024
@@ -253,6 +253,8 @@ export default MathNode;

export const EPSILON = float( 1e-6 );
export const INFINITY = float( 1e6 );
export const PI = float( Math.PI );
Copy link
Contributor

Choose a reason for hiding this comment

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

Why do we need this? AFAIR, Math.PI and Math.PI2 are already cached when generating constants, so float( Math.PI or Math.PI2 ) (or even just them without float -- most places in the Nodes system autoconvert values to nodes) should already be efficient. /ping @sunag @RenaudRohlinger

Copy link
Collaborator Author

@RenaudRohlinger RenaudRohlinger Jan 20, 2024

Choose a reason for hiding this comment

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

I was following the logic of constants such as EPSILON and still thinking with a GLSL mind but now I recon that all these constants might just be doable with js Math. Also when thinking about a visual node editor I believe that it's where they can be relevant.

const { rotationNode, positionNode } = this;

const rotation = rotationNode;
const rotationXMatrix = mat4( vec4( 1.0, 0.0, 0.0, 0.0 ), vec4( 0.0, cos( rotation.x ), sin( rotation.x ).negate(), 0.0 ), vec4( 0.0, sin( rotation.x ), cos( rotation.x ), 0.0 ), vec4( 0.0, 0.0, 0.0, 1.0 ) );
Copy link
Contributor

Choose a reason for hiding this comment

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

IIRC (not sure, if not, then we should definitely add this) something like mat4( a1, a2, ..., a16 ) should work even without using vec4.

const rotationZMatrix = mat4( vec4( cos( rotation.z ), sin( rotation.z ).negate(), 0.0, 0.0 ), vec4( sin( rotation.z ), cos( rotation.z ), 0.0, 0.0 ), vec4( 0.0, 0.0, 1.0, 0.0 ), vec4( 0.0, 0.0, 0.0, 1.0 ) );


return rotationXMatrix.mul( rotationYMatrix ).mul( rotationZMatrix ).mul( vec4( positionNode, 1.0 ) ).xyz;
Copy link
Contributor

Choose a reason for hiding this comment

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

We can simplify this to rotationXMatrix.mul( rotationYMatrrix, rotationZMatrix, positionNode ). Note that we don't need to worry about converting positionNode to vec4 (this happens automatically because of mat4) or result to vec3 (this happens automatically because nodeType is specified as vec3).

@@ -301,6 +303,12 @@ export const pow3 = nodeProxy( MathNode, MathNode.POW, 3 );
export const pow4 = nodeProxy( MathNode, MathNode.POW, 4 );
export const transformDirection = nodeProxy( MathNode, MathNode.TRANSFORM_DIRECTION );

// remapping functions https://iquilezles.org/articles/functions/
export const parabola = ( x, k ) => pow( mul( 4.0, x.mul( sub( 1.0, x ) ) ), k );
Copy link
Contributor

Choose a reason for hiding this comment

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

Hm.. Maybe we can put these functions into a different node file? MathNode feels like the place for the most basic maths (functions supported OOTB in GLSL/WGSL mostly), not more complex functions like these...

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