Needle Engine

Changes between version 3.19.8 and 3.19.9
Files changed (1) hide show
  1. src/engine-components/AnimatorController.ts +30 -33
src/engine-components/AnimatorController.ts CHANGED
@@ -61,7 +61,7 @@
61
61
 
62
62
  const state: State = {
63
63
  name: clip.name,
64
- hash: stringToHash(clip.name),
64
+ hash: i, // by using the index it's easy for users to call play(2) to play the clip at index 2
65
65
  motion: {
66
66
  name: clip.name,
67
67
  clip: clip,
@@ -685,11 +685,6 @@
685
685
  }
686
686
  };
687
687
 
688
- evaluate(time) {
689
- if (this.customEvaluate) {
690
- this.customEvaluate(time);
691
- }
692
- }
693
688
 
694
689
  dispose() {
695
690
  if (this.createdInterpolant && this.originalEvaluate) {
@@ -705,14 +700,13 @@
705
700
  class RootMotionAction {
706
701
 
707
702
  private static lastObjPosition: { [key: string]: Vector3 } = {};
708
- static lastObjRotation: { [key: string]: Quaternion } = {};
703
+ private static lastObjRotation: { [key: string]: Quaternion } = {};
709
704
 
710
705
  // we remove the first keyframe rotation from the space rotation when updating
711
706
  private static firstKeyframeRotation: { [key: string]: Quaternion } = {};
712
707
  // this is used to rotate the space on clip end / start (so the transform direction is correct)
713
708
  private static spaceRotation: { [key: string]: Quaternion } = {};
714
709
  private static effectiveSpaceRotation: { [key: string]: Quaternion } = {};
715
-
716
710
  private static clipOffsetRotation: { [key: string]: Quaternion } = {};
717
711
 
718
712
 
@@ -723,6 +717,10 @@
723
717
  return this._action;
724
718
  }
725
719
 
720
+ get cacheId() {
721
+ return this.root.uuid;
722
+ }
723
+
726
724
  private _action!: AnimationAction;
727
725
 
728
726
  private root: Object3D;
@@ -740,23 +738,23 @@
740
738
  this.root = root;
741
739
  this.clip = clip;
742
740
 
743
- if (!RootMotionAction.firstKeyframeRotation[clip.uuid])
744
- RootMotionAction.firstKeyframeRotation[clip.uuid] = new Quaternion();
741
+ if (!RootMotionAction.firstKeyframeRotation[this.cacheId])
742
+ RootMotionAction.firstKeyframeRotation[this.cacheId] = new Quaternion();
745
743
  if (rotationTrack) {
746
744
  const values = rotationTrack.values;
747
- RootMotionAction.firstKeyframeRotation[clip.uuid]
745
+ RootMotionAction.firstKeyframeRotation[this.cacheId]
748
746
  .set(values[0], values[1], values[2], values[3])
749
747
  }
750
748
 
751
- if (!RootMotionAction.spaceRotation[clip.uuid])
752
- RootMotionAction.spaceRotation[clip.uuid] = new Quaternion();
749
+ if (!RootMotionAction.spaceRotation[this.cacheId])
750
+ RootMotionAction.spaceRotation[this.cacheId] = new Quaternion();
753
751
 
754
- if (!RootMotionAction.effectiveSpaceRotation[clip.uuid])
755
- RootMotionAction.effectiveSpaceRotation[clip.uuid] = new Quaternion();
752
+ if (!RootMotionAction.effectiveSpaceRotation[this.cacheId])
753
+ RootMotionAction.effectiveSpaceRotation[this.cacheId] = new Quaternion();
756
754
 
757
- RootMotionAction.clipOffsetRotation[clip.uuid] = new Quaternion();
755
+ RootMotionAction.clipOffsetRotation[this.cacheId] = new Quaternion();
758
756
  if (rotationTrack) {
759
- RootMotionAction.clipOffsetRotation[clip.uuid]
757
+ RootMotionAction.clipOffsetRotation[this.cacheId]
760
758
  .set(rotationTrack.values[0], rotationTrack.values[1], rotationTrack.values[2], rotationTrack.values[3])
761
759
  .invert();
762
760
  }
@@ -768,13 +766,13 @@
768
766
  onStart(action: AnimationAction) {
769
767
  if (action.getClip() !== this.clip) return;
770
768
 
771
- if (!RootMotionAction.lastObjRotation[this.root.uuid]) {
772
- RootMotionAction.lastObjRotation[this.root.uuid] = this.root.quaternion.clone()
769
+ if (!RootMotionAction.lastObjRotation[this.cacheId]) {
770
+ RootMotionAction.lastObjRotation[this.cacheId] = this.root.quaternion.clone()
773
771
  }
774
- const lastRotation = RootMotionAction.lastObjRotation[this.root.uuid];
775
- // const firstKeyframe = RootMotionAction.firstKeyframeRotation[this.clip.uuid];
772
+ const lastRotation = RootMotionAction.lastObjRotation[this.cacheId];
773
+ // const firstKeyframe = RootMotionAction.firstKeyframeRotation[this.this.cacheId];
776
774
  // lastRotation.invert().premultiply(firstKeyframe).invert();
777
- RootMotionAction.spaceRotation[this.clip.uuid].copy(lastRotation);
775
+ RootMotionAction.spaceRotation[this.cacheId].copy(lastRotation);
778
776
 
779
777
  if (debugRootMotion) {
780
778
  const euler = new Euler().setFromQuaternion(lastRotation);
@@ -783,7 +781,7 @@
783
781
  }
784
782
 
785
783
  private getClipRotationOffset() {
786
- return RootMotionAction.clipOffsetRotation[this.clip.uuid];
784
+ return RootMotionAction.clipOffsetRotation[this.cacheId];
787
785
  }
788
786
 
789
787
  private _prevTime = 0;
@@ -794,9 +792,9 @@
794
792
  const root = this.root;
795
793
  if (debugRootMotion)
796
794
  root.add(new AxesHelper());
797
- if (!RootMotionAction.lastObjPosition[root.uuid])
798
- RootMotionAction.lastObjPosition[root.uuid] = this.root.position.clone();
799
-
795
+
796
+ if (!RootMotionAction.lastObjPosition[this.cacheId])
797
+ RootMotionAction.lastObjPosition[this.cacheId] = this.root.position.clone();
800
798
  const valuesDiff = new Vector3();
801
799
  const valuesPrev = new Vector3();
802
800
  // const rotation = new Quaternion();
@@ -818,7 +816,7 @@
818
816
  valuesDiff.applyQuaternion(this.getClipRotationOffset());
819
817
 
820
818
  // RootMotionAction.effectiveSpaceRotation[id].slerp(RootMotionAction.spaceRotation[id], weight);
821
- valuesDiff.applyQuaternion(RootMotionAction.spaceRotation[this.clip.uuid]);
819
+ valuesDiff.applyQuaternion(root.quaternion);
822
820
  this.positionChange.copy(valuesDiff);
823
821
 
824
822
  // this.root.position.add(valuesDiff);
@@ -850,8 +848,6 @@
850
848
  console.log(clip.name, track.name, "LAST ROTATION IN TRACK", Mathf.toDegrees(euler.y));
851
849
  }
852
850
 
853
-
854
- const root = this.root;
855
851
  // if (!RootMotionAction.lastObjRotation[root.uuid]) RootMotionAction.lastObjRotation[root.uuid] = new Quaternion();
856
852
  // const temp = new Quaternion();
857
853
  let prevTime: number = 0;
@@ -908,7 +904,8 @@
908
904
 
909
905
 
910
906
  private basePosition: Vector3 = new Vector3();
911
- private baseRotation: Quaternion = new Quaternion();
907
+ private baseQuaternion: Quaternion = new Quaternion();
908
+ private baseRotation: Euler = new Euler();
912
909
 
913
910
  constructor(controller: AnimatorController) {
914
911
  this.controller = controller;
@@ -943,7 +940,7 @@
943
940
  onBeforeUpdate() {
944
941
  // capture the position of the object
945
942
  this.basePosition.copy(this.root.position);
946
- this.baseRotation.copy(this.root.quaternion);
943
+ this.baseQuaternion.copy(this.root.quaternion);
947
944
 
948
945
  for (const hand of this.handler)
949
946
  hand.onBeforeUpdate();
@@ -955,7 +952,7 @@
955
952
  onAfterUpdate() {
956
953
  // apply the accumulated changes
957
954
  this.root.position.copy(this.basePosition);
958
- this.root.quaternion.copy(this.baseRotation);
955
+ this.root.quaternion.copy(this.baseQuaternion);
959
956
 
960
957
  this.summedPosition.set(0, 0, 0);
961
958
  this.summedRotation.set(0, 0, 0, 1);
@@ -967,7 +964,7 @@
967
964
  }
968
965
  this.root.position.add(this.summedPosition);
969
966
  this.root.quaternion.multiply(this.summedRotation);
970
- RootMotionAction.lastObjRotation[this.root.uuid].copy(this.root.quaternion);
967
+ // RootMotionAction.lastObjRotation[this.root.uuid].copy(this.root.quaternion);
971
968
  }
972
969
 
973
970
  private findRootTrack(clip: AnimationClip, name: string) {