babylon.morphTargetManager.ts 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. this._activeTargets.push(target);
  86. this._tempInfluences[influenceCount++] = target.influence;
  87. const positions = target.getPositions();
  88. if (positions) {
  89. this._supportsNormals = this._supportsNormals && target.hasNormals;
  90. this._supportsTangents = this._supportsTangents && target.hasTangents;
  91. const vertexCount = positions.length / 3;
  92. if (this._vertexCount === 0) {
  93. this._vertexCount = vertexCount;
  94. }
  95. else if (this._vertexCount !== vertexCount) {
  96. Tools.Error("Incompatible target. Targets must all have the same vertices count.");
  97. return;
  98. }
  99. }
  100. }
  101. if (!this._influences || this._influences.length !== influenceCount) {
  102. this._influences = new Float32Array(influenceCount);
  103. }
  104. for (var index = 0; index < influenceCount; index++) {
  105. this._influences[index] = this._tempInfluences[index];
  106. }
  107. if (needUpdate && this._scene) {
  108. // Flag meshes as dirty to resync with the active targets
  109. for (var mesh of this._scene.meshes) {
  110. if ((<any>mesh).morphTargetManager === this) {
  111. (<Mesh>mesh)._syncGeometryWithMorphTargetManager();
  112. }
  113. }
  114. }
  115. }
  116. // Statics
  117. public static Parse(serializationObject: any, scene: Scene): MorphTargetManager {
  118. var result = new MorphTargetManager(scene);
  119. result._uniqueId = serializationObject.id;
  120. for (var targetData of serializationObject.targets) {
  121. result.addTarget(MorphTarget.Parse(targetData));
  122. }
  123. return result;
  124. }
  125. }
  126. }