2
0
Эх сурвалжийг харах

[animgraph] Added KeepSync param for anim nodes

Clément Espeute 7 сар өмнө
parent
commit
b85e768a13

+ 1 - 0
hide/view/animgraph/BlendSpace2DEditor.hx

@@ -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>
 			');

+ 1 - 1
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 {

+ 35 - 14
hrt/animgraph/nodes/BlendSpace2D.hx

@@ -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)
@@ -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;
 				}
 			}
 		}