Skip to content

Instantly share code, notes, and snippets.

@richard3d
Created May 16, 2019 23:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save richard3d/b495fe7c748cf98ce49469dfb9faa0d6 to your computer and use it in GitHub Desktop.
Save richard3d/b495fe7c748cf98ce49469dfb9faa0d6 to your computer and use it in GitHub Desktop.
Calculating world-space transform for debugging model nodes in Cesium
calcNodeDebugTransform(nodeName) {
let animation_set = this.animation_player.animation_set;
let currParentNode = animation_set.nodes[nodeName];
let worldSpaceQuat = new Cesium.Quaternion();
let node_stack = new Array();
let transform_stack = new Array();
let animation_transform_stack = new Array();
//gather the chain of transformations
while(typeof currParentNode != 'undefined' && typeof currParentNode.name != 'undefined' && currParentNode.name != "") {
if(typeof currParentNode.translation === 'undefined' || typeof currParentNode.rotation === 'undefined' || typeof currParentNode.scale === 'undefined')
continue;
let parentQuat = new Cesium.Quaternion(currParentNode.rotation[0], currParentNode.rotation[1], currParentNode.rotation[2], currParentNode.rotation[3]);
let angle = Cesium.Quaternion.computeAngle(parentQuat);
//don't try to alter the quaternion if the angle is zero
if(Math.abs(angle) > Cesium.Math.EPSILON5) {
let axis = new Cesium.Cartesian3();
Cesium.Quaternion.computeAxis(parentQuat, axis);
let newAxis = new Cesium.Cartesian3(axis.z, axis.x, axis.y);
parentQuat = Cesium.Quaternion.fromAxisAngle(newAxis, angle);
}
let parentTrans = new Cesium.Cartesian3(currParentNode.translation[2], currParentNode.translation[0], currParentNode.translation[1]);
let parentScale = new Cesium.Cartesian3(currParentNode.scale[0], currParentNode.scale[1], currParentNode.scale[2]);
let scaleMag = Cesium.Cartesian3.magnitude(parentScale);
if(scaleMag < Math.EPSILON5 || scaleMag == 0.0) {
parentScale = new Cesium.Cartesian3(1,1,1);
}
let parentTransform = new Cesium.Matrix4();
Cesium.Matrix4.fromTranslationQuaternionRotationScale(parentTrans, parentQuat, parentScale, parentTransform);
//compute the animation transform
let nodeAnimTransform = this.entity.model.nodeTransformations[currParentNode.name];
//console.log(currParentNode.name);
let nodeAnimTrans = new Cesium.Cartesian3(nodeAnimTransform.translation._value.z, nodeAnimTransform.translation._value.x, nodeAnimTransform.translation._value.y);
let nodeAnimRot = nodeAnimTransform.rotation._value;
let nodeAnimScale = nodeAnimTransform.scale._value;
angle = Cesium.Quaternion.computeAngle(nodeAnimRot);
//don't try to alter the quaternion if the angle is zero
if(Math.abs(angle) > Cesium.Math.EPSILON5) {
let axis = new Cesium.Cartesian3();
Cesium.Quaternion.computeAxis(nodeAnimRot, axis);
let newAxis = new Cesium.Cartesian3(axis.z, axis.x, axis.y);
nodeAnimRot = Cesium.Quaternion.fromAxisAngle(newAxis, angle);
}
let nodeAnimMatrix = Cesium.Matrix4.fromTranslationQuaternionRotationScale(nodeAnimTrans, nodeAnimRot, nodeAnimScale);
Cesium.Matrix4.multiply(parentTransform, nodeAnimMatrix, parentTransform);
transform_stack.push(parentTransform);
currParentNode = animation_set.nodes[currParentNode.parent];
}
let modelMatrix = new Cesium.Matrix4();
Cesium.Matrix4.fromTranslationQuaternionRotationScale(this.entity.position._value, this.entity.orientation._value, new Cesium.Cartesian3(1,1,1), modelMatrix);
while(transform_stack.length > 0) {
Cesium.Matrix4.multiply(modelMatrix, transform_stack.pop(), modelMatrix);
}
return modelMatrix;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment