Skip to content

Commit

Permalink
[animgraph] Code review cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
EspeuteClement committed Jan 10, 2025
1 parent 006338f commit 37f6576
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 77 deletions.
1 change: 0 additions & 1 deletion hide.hxml
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,4 @@ common.hxml
--macro include("hrt")
-dce no
-debug
-D dump=pretty
#-D shader_debug_dump
42 changes: 0 additions & 42 deletions hrt/animgraph/AnimGraph.hx
Original file line number Diff line number Diff line change
Expand Up @@ -159,48 +159,6 @@ class AnimGraph extends hrt.prefab.Prefab {

override function copy(other: hrt.prefab.Prefab) {
load(other.save());
// super.copy(other);
// var other : AnimGraph = cast other;

// var nodeCopy: Map<{}, Node> = [];
// var parameterCopy: Map<{}, Parameter> = [];

// this.nodes = [];
// this.parameters = [];

// for (parameter in other.parameters) {
// var copy = new Parameter();
// @:privateAccess copy.copyFromOther(parameter);
// this.parameters.push(copy);
// parameterCopy.set(parameter, copy);
// }

// for (node in other.nodes) {
// var copy = Node.createFromDynamic(node.serializeToDynamic());
// this.nodes.push(copy);
// nodeCopy.set(node, copy);

// var copyParam = Std.downcast(copy, hrt.animgraph.nodes.FloatParameter);
// var nodeParam = Std.downcast(node, hrt.animgraph.nodes.FloatParameter);
// if (copyParam != null) {
// copyParam.parameter = parameterCopy.get(nodeParam.parameter);
// }
// }

// // restore edges
// for (node in other.nodes) {
// var ours = nodeCopy.get(node);
// for (id => edge in node.inputEdges) {
// if (edge == null)
// continue;
// ours.inputEdges[id] = {
// target: nodeCopy.get(edge.target),
// outputIndex: edge.outputIndex,
// };
// }
// }

// this.parameters = haxe.Json.parse(haxe.Json.stringify(other.parameters));
}

#if editor
Expand Down
35 changes: 5 additions & 30 deletions hrt/animgraph/AnimGraphInstance.hx
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,6 @@ class AnimGraphInstance extends h3d.anim.Animation {

var inst = new AnimGraphInstance(outputNode, animGraph.name, 1000, 1/60.0);

// for (param in animGraph.parameters) {
// inst.parameterMap.set(param.name, param);
// param.runtimeValue = param.defaultValue;
// }

return inst;
}

Expand All @@ -52,7 +47,10 @@ class AnimGraphInstance extends h3d.anim.Animation {
}

public function setParam(name: String, value: Float) {
parameterMap.get(name)?.runtimeValue = value;
var param = parameterMap.get(name);
if (param != null) {
param.runtimeValue = value;
}
}

public function getParam(name: String) : Null<Float> {
Expand Down Expand Up @@ -158,39 +156,16 @@ class AnimGraphInstance extends h3d.anim.Animation {
}

if (!decompose) {
decomposeMatrix(workMatrix, targetMatrix);
Tools.recomposeMatrix(workMatrix, targetMatrix);
if (obj.targetSkin != null) {
var def = obj.targetSkin.getSkinData().allJoints[obj.targetJoint].defMat;
// targetMatrix._41 = def._41;
// targetMatrix._42 = def._42;
// targetMatrix._43 = def._43;
}
} else {
targetMatrix.load(workMatrix);
}
}
}

static function decomposeMatrix(inMatrix: h3d.Matrix, outMatrix: h3d.Matrix) {
var quat = inline new h3d.Quat(inMatrix._12, inMatrix._13, inMatrix._21, inMatrix._23);
inline quat.toMatrix(outMatrix);

outMatrix._11 *= inMatrix._11;
outMatrix._12 *= inMatrix._11;
outMatrix._13 *= inMatrix._11;
outMatrix._21 *= inMatrix._22;
outMatrix._22 *= inMatrix._22;
outMatrix._23 *= inMatrix._22;
outMatrix._31 *= inMatrix._33;
outMatrix._32 *= inMatrix._33;
outMatrix._33 *= inMatrix._33;

outMatrix._41 = inMatrix._41;
outMatrix._42 = inMatrix._42;
outMatrix._43 = inMatrix._43;

}

function updateNodeInputs(node: Node) : Void {
var inputs = node.getInputs();
for (inputId => edge in node.inputEdges) {
Expand Down
32 changes: 30 additions & 2 deletions hrt/animgraph/Tools.hx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package hrt.animgraph;

class Tools {
@:haxe.warning("-WInlineOptimizedField")

// from https://theorangeduck.com/page/quaternion-weighted-average
@:haxe.warning("-WInlineOptimizedField")
static public function weightedBlend(inRotations: Array<h3d.Quat>, inReference: h3d.Quat, inWeights: Array<Float>, outRotation: h3d.Quat) {
outRotation.set(0,0,0,0);

Expand Down Expand Up @@ -35,7 +35,12 @@ class Tools {


static var workMatrix = new h3d.Matrix();
static public function splitMatrix(inMatrix: h3d.Matrix, outMatrix: h3d.Matrix) {

/**
Decompose a 3d matrix so the rotation quaternion is stored in [m12,m13,m21,m23] instead of mixed with the scale
**/
@:haxe.warning("-WInlineOptimizedField")
static public function decomposeMatrix(inMatrix: h3d.Matrix, outMatrix: h3d.Matrix) {
workMatrix.load(inMatrix);
var scale = inline workMatrix.getScale();
workMatrix.prependScale(1.0/scale.x, 1.0/scale.y, 1.0/scale.z);
Expand All @@ -55,4 +60,27 @@ class Tools {
outMatrix.ty = inMatrix.ty;
outMatrix.tz = inMatrix.tz;
}

/**
Transform a decomposed matrix into a normal one
**/
@:haxe.warning("-WInlineOptimizedField")
static public function recomposeMatrix(inMatrix: h3d.Matrix, outMatrix: h3d.Matrix) {
var quat = inline new h3d.Quat(inMatrix._12, inMatrix._13, inMatrix._21, inMatrix._23);
inline quat.toMatrix(outMatrix);

outMatrix._11 *= inMatrix._11;
outMatrix._12 *= inMatrix._11;
outMatrix._13 *= inMatrix._11;
outMatrix._21 *= inMatrix._22;
outMatrix._22 *= inMatrix._22;
outMatrix._23 *= inMatrix._22;
outMatrix._31 *= inMatrix._33;
outMatrix._32 *= inMatrix._33;
outMatrix._33 *= inMatrix._33;

outMatrix._41 = inMatrix._41;
outMatrix._42 = inMatrix._42;
outMatrix._43 = inMatrix._43;
}
}
2 changes: 1 addition & 1 deletion hrt/animgraph/nodes/AnimNode.hx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class GetBoneTransformContext {
} else {
targetObj.targetObject.defaultTransform ?? @:privateAccess h3d.anim.SmoothTransition.MZERO;
}
Tools.splitMatrix(m, tmpDefMatrix);
Tools.decomposeMatrix(m, tmpDefMatrix);

defMatrix = tmpDefMatrix;
return defMatrix;
Expand Down
2 changes: 1 addition & 1 deletion hrt/animgraph/nodes/DefaultPose.hx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class DefaultPose extends AnimNode {
var m = bone.skin != null ? bone.skin.getSkinData().allJoints[bone.joint].defMat : bone.object.defaultTransform;
if (m != null) {
bone.matDecomposed = new h3d.Matrix();
Tools.splitMatrix(m, bone.matDecomposed);
Tools.decomposeMatrix(m, bone.matDecomposed);
} else {
bone.matDecomposed = @:privateAccess h3d.anim.SmoothTransition.MZERO;
}
Expand Down

0 comments on commit 37f6576

Please sign in to comment.