babylon.skeleton.js 16 KB

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