glTFLoaderAnimation.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import { Animation } from "@babylonjs/core/Animations/animation.js";
  2. import { Quaternion, Vector3 } from "@babylonjs/core/Maths/math.vector.js";
  3. /** @internal */
  4. export function getVector3(_target, source, offset, scale) {
  5. return Vector3.FromArray(source, offset).scaleInPlace(scale);
  6. }
  7. /** @internal */
  8. export function getQuaternion(_target, source, offset, scale) {
  9. return Quaternion.FromArray(source, offset).scaleInPlace(scale);
  10. }
  11. /** @internal */
  12. export function getWeights(target, source, offset, scale) {
  13. const value = new Array(target._numMorphTargets);
  14. for (let i = 0; i < value.length; i++) {
  15. value[i] = source[offset++] * scale;
  16. }
  17. return value;
  18. }
  19. /** @internal */
  20. export class AnimationPropertyInfo {
  21. /** @internal */
  22. constructor(type, name, getValue, getStride) {
  23. this.type = type;
  24. this.name = name;
  25. this.getValue = getValue;
  26. this.getStride = getStride;
  27. }
  28. _buildAnimation(name, fps, keys) {
  29. const babylonAnimation = new Animation(name, this.name, fps, this.type);
  30. babylonAnimation.setKeys(keys);
  31. return babylonAnimation;
  32. }
  33. }
  34. /** @internal */
  35. export class TransformNodeAnimationPropertyInfo extends AnimationPropertyInfo {
  36. /** @internal */
  37. buildAnimations(target, name, fps, keys, callback) {
  38. callback(target._babylonTransformNode, this._buildAnimation(name, fps, keys));
  39. }
  40. }
  41. /** @internal */
  42. export class WeightAnimationPropertyInfo extends AnimationPropertyInfo {
  43. buildAnimations(target, name, fps, keys, callback) {
  44. if (target._numMorphTargets) {
  45. for (let targetIndex = 0; targetIndex < target._numMorphTargets; targetIndex++) {
  46. const babylonAnimation = new Animation(`${name}_${targetIndex}`, this.name, fps, this.type);
  47. babylonAnimation.setKeys(keys.map((key) => ({
  48. frame: key.frame,
  49. inTangent: key.inTangent ? key.inTangent[targetIndex] : undefined,
  50. value: key.value[targetIndex],
  51. outTangent: key.outTangent ? key.outTangent[targetIndex] : undefined,
  52. interpolation: key.interpolation,
  53. })));
  54. if (target._primitiveBabylonMeshes) {
  55. for (const babylonMesh of target._primitiveBabylonMeshes) {
  56. if (babylonMesh.morphTargetManager) {
  57. const morphTarget = babylonMesh.morphTargetManager.getTarget(targetIndex);
  58. const babylonAnimationClone = babylonAnimation.clone();
  59. morphTarget.animations.push(babylonAnimationClone);
  60. callback(morphTarget, babylonAnimationClone);
  61. }
  62. }
  63. }
  64. }
  65. }
  66. }
  67. }
  68. /** @internal */
  69. export const nodeAnimationData = {
  70. translation: [new TransformNodeAnimationPropertyInfo(Animation.ANIMATIONTYPE_VECTOR3, "position", getVector3, () => 3)],
  71. rotation: [new TransformNodeAnimationPropertyInfo(Animation.ANIMATIONTYPE_QUATERNION, "rotationQuaternion", getQuaternion, () => 4)],
  72. scale: [new TransformNodeAnimationPropertyInfo(Animation.ANIMATIONTYPE_VECTOR3, "scaling", getVector3, () => 3)],
  73. weights: [new WeightAnimationPropertyInfo(Animation.ANIMATIONTYPE_FLOAT, "influence", getWeights, (target) => target._numMorphTargets)],
  74. };
  75. //# sourceMappingURL=glTFLoaderAnimation.js.map