morphTarget.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. import { IAnimatable } from '../Animations/animatable.interface';
  2. import { Observable } from "../Misc/observable";
  3. import { Nullable, FloatArray } from "../types";
  4. import { Scene } from "../scene";
  5. import { EngineStore } from "../Engines/engineStore";
  6. import { AbstractMesh } from "../Meshes/abstractMesh";
  7. import { VertexBuffer } from "../Meshes/buffer";
  8. import { AnimationPropertiesOverride } from "../Animations/animationPropertiesOverride";
  9. import { serialize, SerializationHelper } from "../Misc/decorators";
  10. import { _TypeStore } from '../Misc/typeStore';
  11. declare type Animation = import("../Animations/animation").Animation;
  12. /**
  13. * Defines a target to use with MorphTargetManager
  14. * @see http://doc.babylonjs.com/how_to/how_to_use_morphtargets
  15. */
  16. export class MorphTarget implements IAnimatable {
  17. /**
  18. * Gets or sets the list of animations
  19. */
  20. public animations = new Array<Animation>();
  21. private _scene: Nullable<Scene>;
  22. private _positions: Nullable<FloatArray> = null;
  23. private _normals: Nullable<FloatArray> = null;
  24. private _tangents: Nullable<FloatArray> = null;
  25. private _uvs: Nullable<FloatArray> = null;
  26. private _influence: number;
  27. private _uniqueId = 0;
  28. /**
  29. * Observable raised when the influence changes
  30. */
  31. public onInfluenceChanged = new Observable<boolean>();
  32. /** @hidden */
  33. public _onDataLayoutChanged = new Observable<void>();
  34. /**
  35. * Gets or sets the influence of this target (ie. its weight in the overall morphing)
  36. */
  37. public get influence(): number {
  38. return this._influence;
  39. }
  40. public set influence(influence: number) {
  41. if (this._influence === influence) {
  42. return;
  43. }
  44. var previous = this._influence;
  45. this._influence = influence;
  46. if (this.onInfluenceChanged.hasObservers()) {
  47. this.onInfluenceChanged.notifyObservers(previous === 0 || influence === 0);
  48. }
  49. }
  50. /**
  51. * Gets or sets the id of the morph Target
  52. */
  53. @serialize()
  54. public id: string;
  55. private _animationPropertiesOverride: Nullable<AnimationPropertiesOverride> = null;
  56. /**
  57. * Gets or sets the animation properties override
  58. */
  59. public get animationPropertiesOverride(): Nullable<AnimationPropertiesOverride> {
  60. if (!this._animationPropertiesOverride && this._scene) {
  61. return this._scene.animationPropertiesOverride;
  62. }
  63. return this._animationPropertiesOverride;
  64. }
  65. public set animationPropertiesOverride(value: Nullable<AnimationPropertiesOverride>) {
  66. this._animationPropertiesOverride = value;
  67. }
  68. /**
  69. * Creates a new MorphTarget
  70. * @param name defines the name of the target
  71. * @param influence defines the influence to use
  72. * @param scene defines the scene the morphtarget belongs to
  73. */
  74. public constructor(
  75. /** defines the name of the target */
  76. public name: string, influence = 0, scene: Nullable<Scene> = null) {
  77. this._scene = scene || EngineStore.LastCreatedScene;
  78. this.influence = influence;
  79. if (this._scene) {
  80. this._uniqueId = this._scene.getUniqueId();
  81. }
  82. }
  83. /**
  84. * Gets the unique ID of this manager
  85. */
  86. public get uniqueId(): number {
  87. return this._uniqueId;
  88. }
  89. /**
  90. * Gets a boolean defining if the target contains position data
  91. */
  92. public get hasPositions(): boolean {
  93. return !!this._positions;
  94. }
  95. /**
  96. * Gets a boolean defining if the target contains normal data
  97. */
  98. public get hasNormals(): boolean {
  99. return !!this._normals;
  100. }
  101. /**
  102. * Gets a boolean defining if the target contains tangent data
  103. */
  104. public get hasTangents(): boolean {
  105. return !!this._tangents;
  106. }
  107. /**
  108. * Gets a boolean defining if the target contains texture coordinates data
  109. */
  110. public get hasUVs(): boolean {
  111. return !!this._uvs;
  112. }
  113. /**
  114. * Affects position data to this target
  115. * @param data defines the position data to use
  116. */
  117. public setPositions(data: Nullable<FloatArray>) {
  118. const hadPositions = this.hasPositions;
  119. this._positions = data;
  120. if (hadPositions !== this.hasPositions) {
  121. this._onDataLayoutChanged.notifyObservers(undefined);
  122. }
  123. }
  124. /**
  125. * Gets the position data stored in this target
  126. * @returns a FloatArray containing the position data (or null if not present)
  127. */
  128. public getPositions(): Nullable<FloatArray> {
  129. return this._positions;
  130. }
  131. /**
  132. * Affects normal data to this target
  133. * @param data defines the normal data to use
  134. */
  135. public setNormals(data: Nullable<FloatArray>) {
  136. const hadNormals = this.hasNormals;
  137. this._normals = data;
  138. if (hadNormals !== this.hasNormals) {
  139. this._onDataLayoutChanged.notifyObservers(undefined);
  140. }
  141. }
  142. /**
  143. * Gets the normal data stored in this target
  144. * @returns a FloatArray containing the normal data (or null if not present)
  145. */
  146. public getNormals(): Nullable<FloatArray> {
  147. return this._normals;
  148. }
  149. /**
  150. * Affects tangent data to this target
  151. * @param data defines the tangent data to use
  152. */
  153. public setTangents(data: Nullable<FloatArray>) {
  154. const hadTangents = this.hasTangents;
  155. this._tangents = data;
  156. if (hadTangents !== this.hasTangents) {
  157. this._onDataLayoutChanged.notifyObservers(undefined);
  158. }
  159. }
  160. /**
  161. * Gets the tangent data stored in this target
  162. * @returns a FloatArray containing the tangent data (or null if not present)
  163. */
  164. public getTangents(): Nullable<FloatArray> {
  165. return this._tangents;
  166. }
  167. /**
  168. * Affects texture coordinates data to this target
  169. * @param data defines the texture coordinates data to use
  170. */
  171. public setUVs(data: Nullable<FloatArray>) {
  172. const hadUVs = this.hasUVs;
  173. this._uvs = data;
  174. if (hadUVs !== this.hasUVs) {
  175. this._onDataLayoutChanged.notifyObservers(undefined);
  176. }
  177. }
  178. /**
  179. * Gets the texture coordinates data stored in this target
  180. * @returns a FloatArray containing the texture coordinates data (or null if not present)
  181. */
  182. public getUVs(): Nullable<FloatArray> {
  183. return this._uvs;
  184. }
  185. /**
  186. * Clone the current target
  187. * @returns a new MorphTarget
  188. */
  189. public clone(): MorphTarget {
  190. let newOne = SerializationHelper.Clone(() => new MorphTarget(this.name, this.influence, this._scene), this);
  191. newOne._positions = this._positions;
  192. newOne._normals = this._normals;
  193. newOne._tangents = this._tangents;
  194. newOne._uvs = this._uvs;
  195. return newOne;
  196. }
  197. /**
  198. * Serializes the current target into a Serialization object
  199. * @returns the serialized object
  200. */
  201. public serialize(): any {
  202. var serializationObject: any = {};
  203. serializationObject.name = this.name;
  204. serializationObject.influence = this.influence;
  205. serializationObject.positions = Array.prototype.slice.call(this.getPositions());
  206. if (this.id != null) {
  207. serializationObject.id = this.id;
  208. }
  209. if (this.hasNormals) {
  210. serializationObject.normals = Array.prototype.slice.call(this.getNormals());
  211. }
  212. if (this.hasTangents) {
  213. serializationObject.tangents = Array.prototype.slice.call(this.getTangents());
  214. }
  215. if (this.hasUVs) {
  216. serializationObject.uvs = Array.prototype.slice.call(this.getUVs());
  217. }
  218. // Animations
  219. SerializationHelper.AppendSerializedAnimations(this, serializationObject);
  220. return serializationObject;
  221. }
  222. /**
  223. * Returns the string "MorphTarget"
  224. * @returns "MorphTarget"
  225. */
  226. public getClassName(): string {
  227. return "MorphTarget";
  228. }
  229. // Statics
  230. /**
  231. * Creates a new target from serialized data
  232. * @param serializationObject defines the serialized data to use
  233. * @returns a new MorphTarget
  234. */
  235. public static Parse(serializationObject: any): MorphTarget {
  236. var result = new MorphTarget(serializationObject.name, serializationObject.influence);
  237. result.setPositions(serializationObject.positions);
  238. if (serializationObject.id != null) {
  239. result.id = serializationObject.id;
  240. }
  241. if (serializationObject.normals) {
  242. result.setNormals(serializationObject.normals);
  243. }
  244. if (serializationObject.tangents) {
  245. result.setTangents(serializationObject.tangents);
  246. }
  247. if (serializationObject.uvs) {
  248. result.setUVs(serializationObject.uvs);
  249. }
  250. // Animations
  251. if (serializationObject.animations) {
  252. for (var animationIndex = 0; animationIndex < serializationObject.animations.length; animationIndex++) {
  253. var parsedAnimation = serializationObject.animations[animationIndex];
  254. const internalClass = _TypeStore.GetClass("BABYLON.Animation");
  255. if (internalClass) {
  256. result.animations.push(internalClass.Parse(parsedAnimation));
  257. }
  258. }
  259. }
  260. return result;
  261. }
  262. /**
  263. * Creates a MorphTarget from mesh data
  264. * @param mesh defines the source mesh
  265. * @param name defines the name to use for the new target
  266. * @param influence defines the influence to attach to the target
  267. * @returns a new MorphTarget
  268. */
  269. public static FromMesh(mesh: AbstractMesh, name?: string, influence?: number): MorphTarget {
  270. if (!name) {
  271. name = mesh.name;
  272. }
  273. var result = new MorphTarget(name, influence, mesh.getScene());
  274. result.setPositions(<FloatArray>mesh.getVerticesData(VertexBuffer.PositionKind));
  275. if (mesh.isVerticesDataPresent(VertexBuffer.NormalKind)) {
  276. result.setNormals(<FloatArray>mesh.getVerticesData(VertexBuffer.NormalKind));
  277. }
  278. if (mesh.isVerticesDataPresent(VertexBuffer.TangentKind)) {
  279. result.setTangents(<FloatArray>mesh.getVerticesData(VertexBuffer.TangentKind));
  280. }
  281. if (mesh.isVerticesDataPresent(VertexBuffer.UVKind)) {
  282. result.setUVs(<FloatArray>mesh.getVerticesData(VertexBuffer.UVKind));
  283. }
  284. return result;
  285. }
  286. }