From 96cf3bac508d706836ff5d0651a4b2a0d868bef2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Espeute?= Date: Thu, 30 Jan 2025 14:28:06 +0100 Subject: [PATCH] [animgraph] Added KeepSync param for anim nodes --- hide/view/animgraph/BlendSpace2DEditor.hx | 1 + hrt/animgraph/BlendSpace2D.hx | 2 +- hrt/animgraph/nodes/BlendSpace2D.hx | 49 ++++++++++++++++------- 3 files changed, 37 insertions(+), 15 deletions(-) diff --git a/hide/view/animgraph/BlendSpace2DEditor.hx b/hide/view/animgraph/BlendSpace2DEditor.hx index abc5ca04d..e410c2e72 100644 --- a/hide/view/animgraph/BlendSpace2DEditor.hx +++ b/hide/view/animgraph/BlendSpace2DEditor.hx @@ -432,6 +432,7 @@ class BlendSpace2DEditor extends hide.view.FileView {
X
Y
Anim speed
+
Keep Sync
'); diff --git a/hrt/animgraph/BlendSpace2D.hx b/hrt/animgraph/BlendSpace2D.hx index 61330b083..150983a79 100644 --- a/hrt/animgraph/BlendSpace2D.hx +++ b/hrt/animgraph/BlendSpace2D.hx @@ -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 { diff --git a/hrt/animgraph/nodes/BlendSpace2D.hx b/hrt/animgraph/nodes/BlendSpace2D.hx index c265d3c06..c478962d5 100644 --- a/hrt/animgraph/nodes/BlendSpace2D.hx +++ b/hrt/animgraph/nodes/BlendSpace2D.hx @@ -12,6 +12,8 @@ typedef AnimInfo = { anim: h3d.anim.Animation, proxy: hrt.animgraph.nodes.Input.AnimProxy, indexRemap: Array>, + keepSync: Bool, + selfSpeed: Float, } @:access(hrt.animgraph.BlendSpace2D) @@ -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(); @@ -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]; } @@ -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; } } @@ -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]; } @@ -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; } } }