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

GraphNode setPositionAndRotation #6625

Merged
merged 11 commits into from
Jun 4, 2024
Merged

GraphNode setPositionAndRotation #6625

merged 11 commits into from
Jun 4, 2024

Conversation

kpal81xd
Copy link
Contributor

@kpal81xd kpal81xd commented May 24, 2024

Fixes #6317

Adds a method to graph node class for apply both position and rotation together.

Performance Testing

Average of 5
1e6 Iterations
Random Positions [-5, 5]
Random Rotations [0, 360]

setPos + setRot setTRS setPosRot
178ms 18.6ms 105ms

Using entity.getWorldTransform().setTRS directly is the fastest however the local positions and rotations are not updated and so neither is the local transform.

@kpal81xd kpal81xd self-assigned this May 24, 2024
@kpal81xd kpal81xd added area: graphics Graphics related issue performance Relating to load times or frame rate labels May 24, 2024
@kpal81xd kpal81xd requested review from a team and mvaligursky May 24, 2024 14:53
@mvaligursky
Copy link
Contributor

Should the table compare

  • setPos + setRot times
  • setPosRotCombined times?

I do not see any timing of setRot to conclude the combined functionality is not an improvement. Would you have some numbers here @kpal81xd ?

@mvaligursky mvaligursky reopened this Jun 3, 2024
@kpal81xd
Copy link
Contributor Author

kpal81xd commented Jun 3, 2024

Should the table compare

  • setPos + setRot times
  • setPosRotCombined times?

I do not see any timing of setRot to conclude the combined functionality is not an improvement. Would you have some numbers here @kpal81xd ?

The screenshot names are actuallly incorrect the setPos should be setPos + setRot. Ill updated the screenshot

@mvaligursky
Copy link
Contributor

So in this case, the combined functionality has a nice performance benefit (34% faster), and we should definitely consider including this in the engine.

@kpal81xd
Copy link
Contributor Author

kpal81xd commented Jun 3, 2024

So in this case, the combined functionality has a nice performance benefit (34% faster), and we should definitely consider including this in the engine.

This api of having setPositionAndRotation is similar to Unity's so should we also include getPositionAndRotation likewise? https://docs.unity3d.com/ScriptReference/Transform.GetPositionAndRotation.html

@mvaligursky
Copy link
Contributor

By a quick look at the code, I'd say that this is not likely to win much performance, so perhaps not, as those are a simple functions.

@mvaligursky
Copy link
Contributor

Looking at the new function, it does not seem to work the same way the two separate functions work though, which is undesirable?

@kpal81xd
Copy link
Contributor Author

kpal81xd commented Jun 3, 2024

Looking at the new function, it does not seem to work the same way the two separate functions work though, which is undesirable?

Wdym? The end result of the transformation? Or the fact that the function uses the transform of the node as opposed to the parent transform directly?

@mvaligursky
Copy link
Contributor

The individual functions transform the provided world space position and rotation to the parent space.
But the new function pretty much assigns those to local space position and rotation, ignoring the parent transform.
So the resulting local space position / rotation will be different between the functions.

Add a unit test for it and compare the results with old and new functions.

@mvaligursky
Copy link
Contributor

And the performance benefit is smaller now, around 8.5%. Any way to optimize this?
One way would be to avoid doing the expensive Mat4 inverse, as these matrixes are affine and so the last column (or row?) is always (0, 0, 0, 1). Would could likely do something with the Mat3.invertMat4 and just handle the translation separately. Or add Mat4.invertAffine (similarly to mulAffine2 it has). But perhaps at least we can avoid inverting the rotation, and extract it from the inverted matrix that we already have?

But perhaps a separate PR for the affine part, if we ge to it.

@kpal81xd
Copy link
Contributor Author

kpal81xd commented Jun 3, 2024

And the performance benefit is smaller now, around 8.5%. Any way to optimize this? One way would be to avoid doing the expensive Mat4 inverse, as these matrixes are affine and so the last column (or row?) is always (0, 0, 0, 1). Would could likely do something with the Mat3.invertMat4 and just handle the translation separately. Or add Mat4.invertAffine (similarly to mulAffine2 it has). But perhaps at least we can avoid inverting the rotation, and extract it from the inverted matrix that we already have?

But perhaps a separate PR for the affine part, if we ge to it.

I modified the code to use the existing inverse matrix instead of calling getRotation which re generates the parent world transform. Looks like a ~40% speedup

Copy link
Contributor

@mvaligursky mvaligursky left a comment

Choose a reason for hiding this comment

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

Nice!

@kpal81xd kpal81xd merged commit 5c44889 into main Jun 4, 2024
8 checks passed
@kpal81xd kpal81xd deleted the pos-rot branch June 4, 2024 08:57
@willeastcott
Copy link
Contributor

Presumably we can now swap out individual calls to setPosition and setRotation to this function across the engine codebase.

@AlexAPPi
Copy link
Contributor

This method is not included in v1 of the engine, can you include it in the build?

mvaligursky pushed a commit that referenced this pull request Dec 20, 2024
* added new method on graph node for setting position and rotation together

* set transform on entity directly instead of using parent

* added back the same position and rotation logic and test for setPositionAndRotation

* removed extra call to get ptm

* removed mat3 and used inv parent matrix directly

* revereted example changes

* Updated description of setPositionAndRotation
# Conflicts:
#	src/scene/graph-node.js
@mvaligursky
Copy link
Contributor

I just cherry picked it to v1, this will get released in the future.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: graphics Graphics related issue performance Relating to load times or frame rate
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add method for Node
4 participants