babylon.morphTargetManager.ts 5.0 KB

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