Skip to content

Commit

Permalink
[animgraph] Added KeepSync param for anim nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
EspeuteClement committed Jan 30, 2025
1 parent 91ea483 commit 96cf3ba
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 15 deletions.
1 change: 1 addition & 0 deletions hide/view/animgraph/BlendSpace2DEditor.hx
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,7 @@ class BlendSpace2DEditor extends hide.view.FileView {
<dt>X</dt><dd><input type="range" min="0.0" max="1.0" field="x"/></dd>
<dt>Y</dt><dd><input type="range" min="0.0" max="1.0" field="y"/></dd>
<dt>Anim speed</dt><dd><input type="range" min="0.1" max="2.0" field="speed"/></dd>
<dt>Keep Sync</dt><dd><input type="checkbox" field="keepSync"/></dd>
</dl>
</div>
');
Expand Down
2 changes: 1 addition & 1 deletion hrt/animgraph/BlendSpace2D.hx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class BlendSpacePoint {
@:s public var y : Float = 0.0;
@:s public var speed: Float = 1.0;
@:s public var animPath: String = null;

@:s public var keepSync: Bool = true; // If true, the anim will be kept in sync with all the other anims in the graph marked as keepSync
}

class BlendSpace2D extends hrt.prefab.Prefab {
Expand Down
49 changes: 35 additions & 14 deletions hrt/animgraph/nodes/BlendSpace2D.hx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ typedef AnimInfo = {
anim: h3d.anim.Animation,
proxy: hrt.animgraph.nodes.Input.AnimProxy,
indexRemap: Array<Null<Int>>,
keepSync: Bool,
selfSpeed: Float,
}

@:access(hrt.animgraph.BlendSpace2D)
Expand Down Expand Up @@ -73,7 +75,8 @@ class BlendSpace2D extends AnimNode {
{
var path = ctx.resolver(blendSpacePoint.animPath);
if (path != null) {
var animIndex = animMap.getOrPut(path, {

function makeAnim() : Int {
// Create a new animation
var index = animInfos.length;
var animBase = hxd.res.Loader.currentInstance.load(path).toModel().toHmd().loadAnimation();
Expand All @@ -88,9 +91,18 @@ class BlendSpace2D extends AnimNode {
indexRemap[ourId] = boneId;
}

animInfos.push({anim: animInstance, proxy: proxy, indexRemap: indexRemap});
index;
});
animInfos.push({anim: animInstance, proxy: proxy, indexRemap: indexRemap, selfSpeed: 1.0, keepSync: blendSpacePoint.keepSync});
return index;
}

var animIndex = if (blendSpacePoint.keepSync) {
animMap.getOrPut(path, makeAnim());
} else {
// All anims not kept in sync are unique, so we bypass the animMap
var i = makeAnim();
animInfos[i].selfSpeed = blendSpacePoint.speed;
i;
}

point.animInfo = animInfos[animIndex];
}
Expand Down Expand Up @@ -125,13 +137,19 @@ class BlendSpace2D extends AnimNode {
override function tick(dt:Float) {
super.tick(dt);

if (currentAnimLenght > 0) {
for (animInfo in animInfos) {
// keep all the animations in sync
var scale = (animInfo.anim.getDuration()) / currentAnimLenght;
animInfo.anim.update(dt * scale);
@:privateAccess animInfo.anim.isSync = false;
for (animInfo in animInfos) {
// keep all the animations in sync
var scale = animInfo.selfSpeed;

if (animInfo.keepSync) {
if (currentAnimLenght <= 0) {
continue;
}
scale *= (animInfo.anim.getDuration()) / currentAnimLenght;
}

animInfo.anim.update(dt * scale);
@:privateAccess animInfo.anim.isSync = false;
}
}

Expand Down Expand Up @@ -197,11 +215,11 @@ class BlendSpace2D extends AnimNode {

currentAnimLenght = 0.0;

// Compensate for null animations that don't have lenght
// Compensate for null animations that don't have length
var nulls = 0;
var nullWeights: Float = 0;
for (i => pt in triangles[currentTriangle]) {
if (pt.animInfo == null) {
if (pt.animInfo == null || !pt.animInfo.keepSync) {
nulls ++;
nullWeights += weights[i];
}
Expand All @@ -211,9 +229,12 @@ class BlendSpace2D extends AnimNode {
nullWeights /= (3 - nulls);
}

trace(nullWeights, weights);

for (i => pt in triangles[currentTriangle]) {
if(pt.animInfo != null) {
currentAnimLenght += pt.animInfo.anim.getDuration()/pt.speed * weights[i] + nullWeights;
if(pt.animInfo != null && pt.animInfo.keepSync) {
var blendLength = pt.animInfo.anim.getDuration()/pt.speed * (weights[i] + nullWeights);
currentAnimLenght += blendLength;
}
}
}
Expand Down

0 comments on commit 96cf3ba

Please sign in to comment.