babylon.morphTarget.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. module BABYLON {
  2. export class MorphTarget {
  3. public animations = new Array<Animation>();
  4. private _positions: Nullable<FloatArray> = null;
  5. private _normals: Nullable<FloatArray> = null;
  6. private _tangents: Nullable<FloatArray> = null;
  7. private _influence: number;
  8. public onInfluenceChanged = new Observable<boolean>();
  9. public get influence(): number {
  10. return this._influence;
  11. }
  12. public set influence(influence: number) {
  13. if (this._influence === influence) {
  14. return;
  15. }
  16. var previous = this._influence;
  17. this._influence = influence;
  18. if (this.onInfluenceChanged.hasObservers) {
  19. this.onInfluenceChanged.notifyObservers(previous === 0 || influence === 0);
  20. }
  21. }
  22. public constructor(public name: string, influence = 0) {
  23. this.influence = influence;
  24. }
  25. public get hasPositions(): boolean {
  26. return !!this._positions;
  27. }
  28. public get hasNormals(): boolean {
  29. return !!this._normals;
  30. }
  31. public get hasTangents(): boolean {
  32. return !!this._tangents;
  33. }
  34. public setPositions(data: Nullable<FloatArray>) {
  35. this._positions = data;
  36. }
  37. public getPositions(): Nullable<FloatArray> {
  38. return this._positions;
  39. }
  40. public setNormals(data: Nullable<FloatArray>) {
  41. this._normals = data;
  42. }
  43. public getNormals(): Nullable<FloatArray> {
  44. return this._normals;
  45. }
  46. public setTangents(data: Nullable<FloatArray>) {
  47. this._tangents = data;
  48. }
  49. public getTangents(): Nullable<FloatArray> {
  50. return this._tangents;
  51. }
  52. /**
  53. * Serializes the current target into a Serialization object.
  54. * Returns the serialized object.
  55. */
  56. public serialize(): any {
  57. var serializationObject:any = {};
  58. serializationObject.name = this.name;
  59. serializationObject.influence = this.influence;
  60. serializationObject.positions = Array.prototype.slice.call(this.getPositions());
  61. if (this.hasNormals) {
  62. serializationObject.normals = Array.prototype.slice.call(this.getNormals());
  63. }
  64. if (this.hasTangents) {
  65. serializationObject.tangents = Array.prototype.slice.call(this.getTangents());
  66. }
  67. // Animations
  68. Animation.AppendSerializedAnimations(this, serializationObject);
  69. return serializationObject;
  70. }
  71. // Statics
  72. public static Parse(serializationObject: any): MorphTarget {
  73. var result = new MorphTarget(serializationObject.name , serializationObject.influence);
  74. result.setPositions(serializationObject.positions);
  75. if (serializationObject.normals) {
  76. result.setNormals(serializationObject.normals);
  77. }
  78. if (serializationObject.tangents) {
  79. result.setTangents(serializationObject.tangents);
  80. }
  81. // Animations
  82. if (serializationObject.animations) {
  83. for (var animationIndex = 0; animationIndex < serializationObject.animations.length; animationIndex++) {
  84. var parsedAnimation = serializationObject.animations[animationIndex];
  85. result.animations.push(Animation.Parse(parsedAnimation));
  86. }
  87. }
  88. return result;
  89. }
  90. public static FromMesh(mesh: AbstractMesh, name?: string, influence?: number): MorphTarget {
  91. if (!name) {
  92. name = mesh.name;
  93. }
  94. var result = new MorphTarget(name, influence);
  95. result.setPositions(<FloatArray>mesh.getVerticesData(VertexBuffer.PositionKind));
  96. if (mesh.isVerticesDataPresent(VertexBuffer.NormalKind)) {
  97. result.setNormals(<FloatArray>mesh.getVerticesData(VertexBuffer.NormalKind));
  98. }
  99. if (mesh.isVerticesDataPresent(VertexBuffer.TangentKind)) {
  100. result.setTangents(<FloatArray>mesh.getVerticesData(VertexBuffer.TangentKind));
  101. }
  102. return result;
  103. }
  104. }
  105. }