babylon.skeleton.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. module BABYLON {
  2. export class Skeleton {
  3. public bones = new Array<Bone>();
  4. public needInitialSkinMatrix = false;
  5. private _scene: Scene;
  6. private _isDirty = true;
  7. private _transformMatrices: Float32Array;
  8. private _meshesWithPoseMatrix = new Array<AbstractMesh>();
  9. private _animatables: IAnimatable[];
  10. private _identity = Matrix.Identity();
  11. private _ranges: { [name: string]: AnimationRange; } = {};
  12. constructor(public name: string, public id: string, scene: Scene) {
  13. this.bones = [];
  14. this._scene = scene;
  15. scene.skeletons.push(this);
  16. //make sure it will recalculate the matrix next time prepare is called.
  17. this._isDirty = true;
  18. }
  19. // Members
  20. public getTransformMatrices(mesh: AbstractMesh): Float32Array {
  21. if (this.needInitialSkinMatrix && mesh._bonesTransformMatrices) {
  22. return mesh._bonesTransformMatrices;
  23. }
  24. return this._transformMatrices;
  25. }
  26. public getScene(): Scene {
  27. return this._scene;
  28. }
  29. // Methods
  30. /**
  31. * @param {boolean} fullDetails - support for multiple levels of logging within scene loading
  32. */
  33. public toString(fullDetails? : boolean) : string {
  34. var ret = "Name: " + this.name + ", nBones: " + this.bones.length;
  35. ret += ", nAnimationRanges: " + (this._ranges ? Object.keys(this._ranges).length : "none");
  36. if (fullDetails){
  37. ret += ", Ranges: {"
  38. var first = true;
  39. for (var name in this._ranges) {
  40. if (!first){
  41. ret + ", ";
  42. first = false;
  43. }
  44. ret += name;
  45. }
  46. ret += "}";
  47. }
  48. return ret;
  49. }
  50. public createAnimationRange(name: string, from: number, to: number): void {
  51. // check name not already in use
  52. if (!this._ranges[name]) {
  53. this._ranges[name] = new AnimationRange(name, from, to);
  54. for (var i = 0, nBones = this.bones.length; i < nBones; i++) {
  55. if (this.bones[i].animations[0]) {
  56. this.bones[i].animations[0].createRange(name, from, to);
  57. }
  58. }
  59. }
  60. }
  61. public deleteAnimationRange(name: string, deleteFrames = true): void {
  62. for (var i = 0, nBones = this.bones.length; i < nBones; i++) {
  63. if (this.bones[i].animations[0]) {
  64. this.bones[i].animations[0].deleteRange(name, deleteFrames);
  65. }
  66. }
  67. this._ranges[name] = undefined; // said much faster than 'delete this._range[name]'
  68. }
  69. public getAnimationRange(name: string): AnimationRange {
  70. return this._ranges[name];
  71. }
  72. /**
  73. * Returns as an Array, all AnimationRanges defined on this skeleton
  74. */
  75. public getAnimationRanges(): AnimationRange[] {
  76. var animationRanges : AnimationRange[] = [];
  77. var name : string;
  78. var i: number = 0;
  79. for (name in this._ranges){
  80. animationRanges[i] = this._ranges[name];
  81. i++;
  82. }
  83. return animationRanges;
  84. }
  85. /**
  86. * get a index on skeleton by name
  87. * @param {string} the bone's name
  88. * @return {number} the indice of bone on
  89. */
  90. public getIndexOnSkeletonByName(name: string): number {
  91. for (var boneIndex = 0, cache = this.bones.length; boneIndex < cache; boneIndex++) {
  92. if (this.bones[boneIndex].name === name) {
  93. return boneIndex;
  94. }
  95. }
  96. return null;
  97. }
  98. /**
  99. * note: This is not for a complete retargeting, only between very similar skeleton's with only possible bone length differences
  100. */
  101. public copyAnimationRange(source: Skeleton, name: string, rescaleAsRequired = false): boolean {
  102. if (this._ranges[name] || !source.getAnimationRange(name)) {
  103. return false;
  104. }
  105. var ret = true;
  106. var frameOffset = this._getHighestAnimationFrame() + 1;
  107. // make a dictionary of source skeleton's bones, so exact same order or doublely nested loop is not required
  108. var boneDict = {};
  109. var sourceBones = source.bones;
  110. for (var i = 0, nBones = sourceBones.length; i < nBones; i++) {
  111. boneDict[sourceBones[i].name] = sourceBones[i];
  112. }
  113. if (this.bones.length !== sourceBones.length){
  114. BABYLON.Tools.Warn("copyAnimationRange: this rig has " + this.bones.length + " bones, while source as " + sourceBones.length);
  115. ret = false;
  116. }
  117. for (var i = 0, nBones = this.bones.length; i < nBones; i++) {
  118. var boneName = this.bones[i].name;
  119. var sourceBone = boneDict[boneName];
  120. if (sourceBone) {
  121. ret = ret && this.bones[i].copyAnimationRange(sourceBone, name, frameOffset, rescaleAsRequired);
  122. } else {
  123. BABYLON.Tools.Warn("copyAnimationRange: not same rig, missing source bone " + boneName);
  124. ret = false;
  125. }
  126. }
  127. // do not call createAnimationRange(), since it also is done to bones, which was already done
  128. var range = source.getAnimationRange(name);
  129. this._ranges[name] = new AnimationRange(name, range.from + frameOffset, range.to + frameOffset);
  130. return ret;
  131. }
  132. public returnToRest(): void {
  133. for (var index = 0; index < this.bones.length; index++) {
  134. this.bones[index].returnToRest();
  135. }
  136. }
  137. private _getHighestAnimationFrame(): number {
  138. var ret = 0;
  139. for (var i = 0, nBones = this.bones.length; i < nBones; i++) {
  140. if (this.bones[i].animations[0]) {
  141. var highest = this.bones[i].animations[0].getHighestFrame();
  142. if (ret < highest) {
  143. ret = highest;
  144. }
  145. }
  146. }
  147. return ret;
  148. }
  149. public beginAnimation(name: string, loop?: boolean, speedRatio?: number, onAnimationEnd?: () => void): void {
  150. var range = this.getAnimationRange(name);
  151. if (!range) {
  152. return null;
  153. }
  154. this._scene.beginAnimation(this, range.from, range.to, loop, speedRatio, onAnimationEnd);
  155. }
  156. public _markAsDirty(): void {
  157. this._isDirty = true;
  158. }
  159. public _registerMeshWithPoseMatrix(mesh: AbstractMesh): void {
  160. this._meshesWithPoseMatrix.push(mesh);
  161. }
  162. public _unregisterMeshWithPoseMatrix(mesh: AbstractMesh): void {
  163. var index = this._meshesWithPoseMatrix.indexOf(mesh);
  164. if (index > -1) {
  165. this._meshesWithPoseMatrix.splice(index, 1);
  166. }
  167. }
  168. public _computeTransformMatrices(targetMatrix: Float32Array, initialSkinMatrix: Matrix): void {
  169. for (var index = 0; index < this.bones.length; index++) {
  170. var bone = this.bones[index];
  171. var parentBone = bone.getParent();
  172. if (parentBone) {
  173. bone.getLocalMatrix().multiplyToRef(parentBone.getWorldMatrix(), bone.getWorldMatrix());
  174. } else {
  175. if (initialSkinMatrix) {
  176. bone.getLocalMatrix().multiplyToRef(initialSkinMatrix, bone.getWorldMatrix());
  177. } else {
  178. bone.getWorldMatrix().copyFrom(bone.getLocalMatrix());
  179. }
  180. }
  181. bone.getInvertedAbsoluteTransform().multiplyToArray(bone.getWorldMatrix(), targetMatrix, index * 16);
  182. }
  183. this._identity.copyToArray(targetMatrix, this.bones.length * 16);
  184. }
  185. public prepare(): void {
  186. if (!this._isDirty) {
  187. return;
  188. }
  189. if (this.needInitialSkinMatrix) {
  190. for (var index = 0; index < this._meshesWithPoseMatrix.length; index++) {
  191. var mesh = this._meshesWithPoseMatrix[index];
  192. if (!mesh._bonesTransformMatrices || mesh._bonesTransformMatrices.length !== 16 * (this.bones.length + 1)) {
  193. mesh._bonesTransformMatrices = new Float32Array(16 * (this.bones.length + 1));
  194. }
  195. var poseMatrix = mesh.getPoseMatrix();
  196. // Prepare bones
  197. for (var boneIndex = 0; boneIndex < this.bones.length; boneIndex++) {
  198. var bone = this.bones[boneIndex];
  199. if (!bone.getParent()) {
  200. var matrix = bone.getBaseMatrix();
  201. matrix.multiplyToRef(poseMatrix, Tmp.Matrix[0]);
  202. bone._updateDifferenceMatrix(Tmp.Matrix[0]);
  203. }
  204. }
  205. this._computeTransformMatrices(mesh._bonesTransformMatrices, poseMatrix);
  206. }
  207. } else {
  208. if (!this._transformMatrices || this._transformMatrices.length !== 16 * (this.bones.length + 1)) {
  209. this._transformMatrices = new Float32Array(16 * (this.bones.length + 1));
  210. }
  211. this._computeTransformMatrices(this._transformMatrices, null);
  212. }
  213. this._isDirty = false;
  214. this._scene._activeBones += this.bones.length;
  215. }
  216. public getAnimatables(): IAnimatable[] {
  217. if (!this._animatables || this._animatables.length !== this.bones.length) {
  218. this._animatables = [];
  219. for (var index = 0; index < this.bones.length; index++) {
  220. this._animatables.push(this.bones[index]);
  221. }
  222. }
  223. return this._animatables;
  224. }
  225. public clone(name: string, id: string): Skeleton {
  226. var result = new Skeleton(name, id || name, this._scene);
  227. result.needInitialSkinMatrix = this.needInitialSkinMatrix;
  228. for (var index = 0; index < this.bones.length; index++) {
  229. var source = this.bones[index];
  230. var parentBone = null;
  231. if (source.getParent()) {
  232. var parentIndex = this.bones.indexOf(source.getParent());
  233. parentBone = result.bones[parentIndex];
  234. }
  235. var bone = new Bone(source.name, result, parentBone, source.getBaseMatrix().clone(), source.getRestPose().clone());
  236. Tools.DeepCopy(source.animations, bone.animations);
  237. }
  238. if (this._ranges) {
  239. result._ranges = {};
  240. for (var rangeName in this._ranges) {
  241. result._ranges[rangeName] = this._ranges[rangeName].clone();
  242. }
  243. }
  244. this._isDirty = true;
  245. return result;
  246. }
  247. public dispose() {
  248. this._meshesWithPoseMatrix = [];
  249. // Animations
  250. this.getScene().stopAnimation(this);
  251. // Remove from scene
  252. this.getScene().removeSkeleton(this);
  253. }
  254. public serialize(): any {
  255. var serializationObject: any = {};
  256. serializationObject.name = this.name;
  257. serializationObject.id = this.id;
  258. serializationObject.bones = [];
  259. serializationObject.needInitialSkinMatrix = this.needInitialSkinMatrix;
  260. for (var index = 0; index < this.bones.length; index++) {
  261. var bone = this.bones[index];
  262. var serializedBone: any = {
  263. parentBoneIndex: bone.getParent() ? this.bones.indexOf(bone.getParent()) : -1,
  264. name: bone.name,
  265. matrix: bone.getLocalMatrix().toArray(),
  266. rest: bone.getRestPose().toArray()
  267. };
  268. serializationObject.bones.push(serializedBone);
  269. if (bone.length) {
  270. serializedBone.length = bone.length;
  271. }
  272. if (bone.animations && bone.animations.length > 0) {
  273. serializedBone.animation = bone.animations[0].serialize();
  274. }
  275. serializationObject.ranges = [];
  276. for (var name in this._ranges) {
  277. var range: any = {};
  278. range.name = name;
  279. range.from = this._ranges[name].from;
  280. range.to = this._ranges[name].to;
  281. serializationObject.ranges.push(range);
  282. }
  283. }
  284. return serializationObject;
  285. }
  286. public static Parse(parsedSkeleton: any, scene: Scene): Skeleton {
  287. var skeleton = new Skeleton(parsedSkeleton.name, parsedSkeleton.id, scene);
  288. skeleton.needInitialSkinMatrix = parsedSkeleton.needInitialSkinMatrix;
  289. for (var index = 0; index < parsedSkeleton.bones.length; index++) {
  290. var parsedBone = parsedSkeleton.bones[index];
  291. var parentBone = null;
  292. if (parsedBone.parentBoneIndex > -1) {
  293. parentBone = skeleton.bones[parsedBone.parentBoneIndex];
  294. }
  295. var rest: Matrix = parsedBone.rest ? Matrix.FromArray(parsedBone.rest) : null;
  296. var bone = new Bone(parsedBone.name, skeleton, parentBone, Matrix.FromArray(parsedBone.matrix), rest);
  297. if (parsedBone.length) {
  298. bone.length = parsedBone.length;
  299. }
  300. if (parsedBone.animation) {
  301. bone.animations.push(Animation.Parse(parsedBone.animation));
  302. }
  303. }
  304. // placed after bones, so createAnimationRange can cascade down
  305. if (parsedSkeleton.ranges) {
  306. for (var index = 0; index < parsedSkeleton.ranges.length; index++) {
  307. var data = parsedSkeleton.ranges[index];
  308. skeleton.createAnimationRange(data.name, data.from, data.to);
  309. }
  310. }
  311. return skeleton;
  312. }
  313. }
  314. }