How To Properly Apply Matrix Of Node When Moving To Another Parent Node #1329
-
Hello! I am trying to apply the world matrix to it before moving it, hoping that it would resolve the math properly, Unfortunately, some of the rotations and positions are off. Steps:
This process isn't working. Another weird thing is, when I log the world matrix of the original Node and the local matrix, I am getting the same matrix. Am I missing something? I can't share the real code but I will do my best to imitate its functionality here. (newParentNode)=>{ // parent node is within the hierarchy somewhere
const originalNode:Node = refToOriginalNode // Original node is within the hierarchy somewhere else
const newNode:Node = document.createNode().copy(originalNode, resolve);
newNode.setMatrix(originalNode.getWorldMatrix());
newParentNode.addChild(newNode);
} Any help would be amazing! Thanks in advanced. |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 3 replies
-
Is this part of the same processing pipeline you mention in #1328? For the reasons mentioned in my comment there — transferring objects from one document to another is frankly error-prone currently and there's a bug fix A few ideas to perhaps narrow down the problem:
Transferring objects from another document is error-prone currently (see #924), and there's a bug fix related to computing world matrices in a cloned document in the v4 alpha release, so I just want to eliminate those as possible causes. Assuming it's not either of those things, this line ... newNode.setMatrix(originalNode.getWorldMatrix());
newParentNode.addChild(newNode); ... will only leave the original node in its original location if the new parent node does not have any transform of its own. You could multiply the inverse of the new parent's world matrix into the local matrix of the child, to counteract that. Or, one of these functions might be helpful to simplify the problem, if you don't mind having the scene hierarchy and transforms changed while keeping visual appearance the same: |
Beta Was this translation helpful? Give feedback.
-
As suspected, it was just a logging error. Ill will try using the gl-matrix lib. I know its alpha and prone to bugs, but just a heads up on this one. With 4.0.0 alpha, I ran into this I was able to rectify with a simple relative path, though I sure that could have something to do with my compiler. Just wanted to point it out if its helpful to you at all.
Thanks again for all of the help. As of now, the flatten and node transform clearing didn't seem to help. I suspect doing matrix math is the way to go. This is just an example but essentially there is no guarantee of the new structure as it could be almost anything. It will just include at least all of the nodes from the original in some way or another, visually the Gltf will look exactly the same. No new geometry is added, just informational nodes about hierarchical structure. |
Beta Was this translation helpful? Give feedback.
-
@donmccurdy just wanted to follow up, I think I was able to get 90% there. Seems I have some small issues but transforms are coming across much better now by multiplying the worldMatrix of the child by the inverse world matrix for the new parent. Additionally, I am no longer cloning the document. I just load it in and modify the original version. Though I didn't see the difference till the change above but it does save some computation time and resource. Thanks again for all of the help! |
Beta Was this translation helpful? Give feedback.
-
For anyone that happens upon this post. The answer was to multiply the movedNode.getWorldMatrix() multiplied by inv(newParentNode.getWorldMatrix()) import {mat4} from "gl-matrix"
(newParentNode)=>{ // parent node is within the hierarchy somewhere
let parentInvOfWorldMat:mat4 = newParentNode.getWorldMatrix();
glMatrix.mat4.invert(parentInvOfWorldMat,parentInvOfWorldMat);
const originalNode:Node = refToOriginalNode // Original node is within the hierarchy somewhere else
let newMatrix = originalNode.getWorldMatrix();
mat4.multiply(newMatrix, parentInvOfWorldMat, newMatrix);
const newNode:Node = document.createNode().copy(originalNode, resolve);
newNode.setMatrix(newMatrix);
newParentNode.addChild(newNode);
} Creds to @donmccurdy for carrying the solution. |
Beta Was this translation helpful? Give feedback.
For anyone that happens upon this post.
The answer was to multiply the movedNode.getWorldMatrix() multiplied by inv(newParentNode.getWorldMatrix())