babylon.morphTargetManager.ts 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. module BABYLON {
  2. export class MorphTargetManager {
  3. private _targets = new Array<MorphTarget>();
  4. private _targetObservable = new Array<Nullable<Observer<boolean>>>();
  5. private _activeTargets = new SmartArray<MorphTarget>(16);
  6. private _scene: Nullable<Scene>;
  7. private _influences: Float32Array;
  8. private _supportsNormals = false;
  9. private _supportsTangents = false;
  10. private _vertexCount = 0;
  11. private _uniqueId = 0;
  12. private _tempInfluences = new Array<number>();
  13. public constructor(scene: Nullable<Scene> = null) {
  14. if (!scene) {
  15. scene = Engine.LastCreatedScene;
  16. }
  17. this._scene = scene;
  18. if (this._scene) {
  19. this._scene.morphTargetManagers.push(this);
  20. this._uniqueId = this._scene.getUniqueId();
  21. }
  22. }
  23. public get uniqueId(): number {
  24. return this._uniqueId;
  25. }
  26. public get vertexCount(): number {
  27. return this._vertexCount
  28. }
  29. public get supportsNormals(): boolean {
  30. return this._supportsNormals;
  31. }
  32. public get supportsTangents(): boolean {
  33. return this._supportsTangents;
  34. }
  35. public get numTargets(): number {
  36. return this._targets.length;
  37. }
  38. public get numInfluencers(): number {
  39. return this._activeTargets.length;
  40. }
  41. public get influences(): Float32Array {
  42. return this._influences;
  43. }
  44. public getActiveTarget(index: number): MorphTarget {
  45. return this._activeTargets.data[index];
  46. }
  47. public getTarget(index: number): MorphTarget {
  48. return this._targets[index];
  49. }
  50. public addTarget(target: MorphTarget): void {
  51. this._targets.push(target);
  52. this._targetObservable.push(target.onInfluenceChanged.add(needUpdate => {
  53. this._syncActiveTargets(needUpdate);
  54. }));
  55. this._syncActiveTargets(true);
  56. }
  57. public removeTarget(target: MorphTarget): void {
  58. var index = this._targets.indexOf(target);
  59. if (index >= 0) {
  60. this._targets.splice(index, 1);
  61. target.onInfluenceChanged.remove(this._targetObservable.splice(index, 1)[0]);
  62. this._syncActiveTargets(true);
  63. }
  64. }
  65. /**
  66. * Serializes the current manager into a Serialization object.
  67. * Returns the serialized object.
  68. */
  69. public serialize(): any {
  70. var serializationObject:any = {};
  71. serializationObject.id = this.uniqueId;
  72. serializationObject.targets = [];
  73. for (var target of this._targets) {
  74. serializationObject.targets.push(target.serialize());
  75. }
  76. return serializationObject;
  77. }
  78. private _syncActiveTargets(needUpdate: boolean): void {
  79. let influenceCount = 0;
  80. this._activeTargets.reset();
  81. this._supportsNormals = true;
  82. this._supportsTangents = true;
  83. this._vertexCount = 0;
  84. for (var target of this._targets) {
  85. if (target.influence > 0) {
  86. this._activeTargets.push(target);
  87. this._tempInfluences[influenceCount++] = target.influence;
  88. this._supportsNormals = this._supportsNormals && target.hasNormals;
  89. this._supportsTangents = this._supportsTangents && target.hasTangents;
  90. const positions = target.getPositions();
  91. if (!positions) {
  92. Tools.Error("Invalid target. Target must positions.");
  93. return;
  94. }
  95. const vertexCount = positions.length / 3;
  96. if (this._vertexCount === 0) {
  97. this._vertexCount = vertexCount;
  98. }
  99. else if (this._vertexCount !== vertexCount) {
  100. Tools.Error("Incompatible target. Targets must all have the same vertices count.");
  101. return;
  102. }
  103. }
  104. }
  105. if (!this._influences || this._influences.length !== influenceCount) {
  106. this._influences = new Float32Array(influenceCount);
  107. }
  108. for (var index = 0; index < influenceCount; index++) {
  109. this._influences[index] = this._tempInfluences[index];
  110. }
  111. if (needUpdate && this._scene) {
  112. // Flag meshes as dirty to resync with the active targets
  113. for (var mesh of this._scene.meshes) {
  114. if ((<any>mesh).morphTargetManager === this) {
  115. (<Mesh>mesh)._syncGeometryWithMorphTargetManager();
  116. }
  117. }
  118. }
  119. }
  120. // Statics
  121. public static Parse(serializationObject: any, scene: Scene): MorphTargetManager {
  122. var result = new MorphTargetManager(scene);
  123. result._uniqueId = serializationObject.id;
  124. for (var targetData of serializationObject.targets) {
  125. result.addTarget(MorphTarget.Parse(targetData));
  126. }
  127. return result;
  128. }
  129. }
  130. }