Skip to content

Commit

Permalink
[animgraph] Added anim speed parameter to blendSpace points
Browse files Browse the repository at this point in the history
  • Loading branch information
EspeuteClement committed Jan 13, 2025
1 parent d81d952 commit 40fa78e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 15 deletions.
14 changes: 8 additions & 6 deletions hide/view/animgraph/BlendSpace2DEditor.hx
Original file line number Diff line number Diff line change
Expand Up @@ -223,11 +223,13 @@ class BlendSpace2DEditor extends hide.view.FileView {
label: "Add point",
click: () -> {
var pt = getPointPos(x, y, !ctrl);
addPoint({
var pt2 : hrt.animgraph.BlendSpace2D.BlendSpacePoint = {
x: pt.x,
y: pt.y,
speed: 1.0,
animPath: "",
});
};
addPoint(pt2);
}
});

Expand All @@ -247,8 +249,7 @@ class BlendSpace2DEditor extends hide.view.FileView {
e.preventDefault();

var pos = getPointPos(e.clientX, e.clientY, true);
var newPoint = {x: pos.x, y: pos.y, animPath: e.dataTransfer.getData(AnimList.dragEventKey)};
addPoint(newPoint, true);
addPoint({x: pos.x, y: pos.y, animPath: e.dataTransfer.getData(AnimList.dragEventKey)}, true);
}
}
}
Expand Down Expand Up @@ -362,7 +363,7 @@ class BlendSpace2DEditor extends hide.view.FileView {
<dl>
<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>Anim</dt><dd><input type="fileselect" extensions="fbx" field="animPath"/></dd>
</dl>
</div>
Expand Down Expand Up @@ -402,7 +403,7 @@ class BlendSpace2DEditor extends hide.view.FileView {
if (ide.mouseX >= rect.x && ide.mouseX <= rect.x + rect.width && ide.mouseY >= rect.y && ide.mouseY <= rect.y + rect.height) {
if (isDrop) {
var pos = getPointPos(ide.mouseX, ide.mouseY, true);
var newPoint = {x: pos.x, y: pos.y, animPath: items[0]};
var newPoint : hrt.animgraph.BlendSpace2D.BlendSpacePoint = {x: pos.x, y: pos.y, speed: 1.0, animPath: items[0]};
addPoint(newPoint, true);
}
return true;
Expand Down Expand Up @@ -453,6 +454,7 @@ class BlendSpace2DEditor extends hide.view.FileView {
setSelection(prevSelection);
}
refreshGraph();
refreshPreviewAnimation();
}
exec(false);
undo.change(Custom(exec));
Expand Down
14 changes: 9 additions & 5 deletions hrt/animgraph/BlendSpace2D.hx
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package hrt.animgraph;

typedef BlendSpacePoint = {
x : Float,
y : Float,
animPath: String,
};
@:structInit
@:build(hrt.prefab.Macros.buildSerializable())
class BlendSpacePoint {
@:s public var x : Float = 0.0;
@:s public var y : Float = 0.0;
@:s public var speed: Float = 1.0;
@:s public var animPath: String = null;

}

class BlendSpace2D extends hrt.prefab.Prefab {
@:s var points : Array<BlendSpacePoint> = [];
Expand Down
11 changes: 7 additions & 4 deletions hrt/animgraph/nodes/BlendSpace2D.hx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ typedef BlendSpaceInstancePoint = {
typedef AnimInfo = {
anim: h3d.anim.Animation,
proxy: hrt.animgraph.nodes.Input.AnimProxy,
speed: Float,
indexRemap: Array<Null<Int>>,
}

Expand Down Expand Up @@ -66,11 +67,13 @@ class BlendSpace2D extends AnimNode {
var animMap : Map<String, Int> = [];

for (blendSpacePoint in blendSpace.points) {
var speedInt = Math.round(blendSpacePoint.speed * 100.0);
var speed = speedInt / 100.0;
var point : BlendSpaceInstancePoint = {x: blendSpacePoint.x, y: blendSpacePoint.y};
if (blendSpacePoint.animPath != null && blendSpacePoint.animPath.length > 0) {
try
{
var animIndex = animMap.getOrPut(blendSpacePoint.animPath, {
var animIndex = animMap.getOrPut('${blendSpacePoint.animPath}_$speedInt}', {
// Create a new animation
var index = animInfos.length;
var animBase = hxd.res.Loader.currentInstance.load(blendSpacePoint.animPath).toModel().toHmd().loadAnimation();
Expand All @@ -85,7 +88,7 @@ class BlendSpace2D extends AnimNode {
indexRemap[ourId] = boneId;
}

animInfos.push({anim: animInstance, proxy: proxy, indexRemap: indexRemap});
animInfos.push({anim: animInstance, proxy: proxy, speed: speed, indexRemap: indexRemap});
index;
});

Expand Down Expand Up @@ -122,7 +125,7 @@ class BlendSpace2D extends AnimNode {

for (animInfo in animInfos) {
// keep all the animations in sync
var scale = animInfo.anim.getDuration() / currentAnimLenght;
var scale = (animInfo.anim.getDuration()) / currentAnimLenght;
animInfo.anim.update(dt * scale);
@:privateAccess animInfo.anim.isSync = false;
}
Expand Down Expand Up @@ -190,7 +193,7 @@ class BlendSpace2D extends AnimNode {

currentAnimLenght = 0.0;
for (i => pt in triangles[currentTriangle]) {
currentAnimLenght += pt.animInfo.anim.getDuration() * weights[i];
currentAnimLenght += pt.animInfo.anim.getDuration()/pt.animInfo.speed * weights[i];
}
}

Expand Down

0 comments on commit 40fa78e

Please sign in to comment.