decorators.ts 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. import { Tags } from "Tools/tags";
  2. import { Nullable } from "types";
  3. import { Color4, Quaternion, Color3, Vector2, Vector3 } from "Math/math";
  4. declare type Scene = import("scene").Scene;
  5. declare type Camera = import("Cameras/camera").Camera;
  6. declare type ImageProcessingConfiguration = import("Materials/imageProcessingConfiguration").ImageProcessingConfiguration;
  7. declare type FresnelParameters = import("Materials/fresnelParameters").FresnelParameters;
  8. declare type ColorCurves = import("Materials/colorCurves").ColorCurves;
  9. declare type BaseTexture = import("Materials/Textures/baseTexture").BaseTexture;
  10. var __decoratorInitialStore = {};
  11. var __mergedStore = {};
  12. var _copySource = function <T>(creationFunction: () => T, source: T, instanciate: boolean): T {
  13. var destination = creationFunction();
  14. // Tags
  15. if (Tags) {
  16. Tags.AddTagsTo(destination, (<any>source).tags);
  17. }
  18. var classStore = getMergedStore(destination);
  19. // Properties
  20. for (var property in classStore) {
  21. var propertyDescriptor = classStore[property];
  22. var sourceProperty = (<any>source)[property];
  23. var propertyType = propertyDescriptor.type;
  24. if (sourceProperty !== undefined && sourceProperty !== null) {
  25. switch (propertyType) {
  26. case 0: // Value
  27. case 6: // Mesh reference
  28. case 11: // Camera reference
  29. (<any>destination)[property] = sourceProperty;
  30. break;
  31. case 1: // Texture
  32. (<any>destination)[property] = (instanciate || sourceProperty.isRenderTarget) ? sourceProperty : sourceProperty.clone();
  33. break;
  34. case 2: // Color3
  35. case 3: // FresnelParameters
  36. case 4: // Vector2
  37. case 5: // Vector3
  38. case 7: // Color Curves
  39. case 10: // Quaternion
  40. (<any>destination)[property] = instanciate ? sourceProperty : sourceProperty.clone();
  41. break;
  42. }
  43. }
  44. }
  45. return destination;
  46. };
  47. function getDirectStore(target: any): any {
  48. var classKey = target.getClassName();
  49. if (!(<any>__decoratorInitialStore)[classKey]) {
  50. (<any>__decoratorInitialStore)[classKey] = {};
  51. }
  52. return (<any>__decoratorInitialStore)[classKey];
  53. }
  54. /**
  55. * Return the list of properties flagged as serializable
  56. * @param target: host object
  57. */
  58. function getMergedStore(target: any): any {
  59. let classKey = target.getClassName();
  60. if ((<any>__mergedStore)[classKey]) {
  61. return (<any>__mergedStore)[classKey];
  62. }
  63. (<any>__mergedStore)[classKey] = {};
  64. let store = (<any>__mergedStore)[classKey];
  65. let currentTarget = target;
  66. let currentKey = classKey;
  67. while (currentKey) {
  68. let initialStore = (<any>__decoratorInitialStore)[currentKey];
  69. for (var property in initialStore) {
  70. store[property] = initialStore[property];
  71. }
  72. let parent: any;
  73. let done = false;
  74. do {
  75. parent = Object.getPrototypeOf(currentTarget);
  76. if (!parent.getClassName) {
  77. done = true;
  78. break;
  79. }
  80. if (parent.getClassName() !== currentKey) {
  81. break;
  82. }
  83. currentTarget = parent;
  84. }
  85. while (parent);
  86. if (done) {
  87. break;
  88. }
  89. currentKey = parent.getClassName();
  90. currentTarget = parent;
  91. }
  92. return store;
  93. }
  94. function generateSerializableMember(type: number, sourceName?: string) {
  95. return (target: any, propertyKey: string | symbol) => {
  96. var classStore = getDirectStore(target);
  97. if (!classStore[propertyKey]) {
  98. classStore[propertyKey] = { type: type, sourceName: sourceName };
  99. }
  100. };
  101. }
  102. function generateExpandMember(setCallback: string, targetKey: Nullable<string> = null) {
  103. return (target: any, propertyKey: string) => {
  104. var key = targetKey || ("_" + propertyKey);
  105. Object.defineProperty(target, propertyKey, {
  106. get: function(this: any) {
  107. return this[key];
  108. },
  109. set: function(this: any, value) {
  110. if (this[key] === value) {
  111. return;
  112. }
  113. this[key] = value;
  114. target[setCallback].apply(this);
  115. },
  116. enumerable: true,
  117. configurable: true
  118. });
  119. };
  120. }
  121. export function expandToProperty(callback: string, targetKey: Nullable<string> = null) {
  122. return generateExpandMember(callback, targetKey);
  123. }
  124. export function serialize(sourceName?: string) {
  125. return generateSerializableMember(0, sourceName); // value member
  126. }
  127. export function serializeAsTexture(sourceName?: string) {
  128. return generateSerializableMember(1, sourceName); // texture member
  129. }
  130. export function serializeAsColor3(sourceName?: string) {
  131. return generateSerializableMember(2, sourceName); // color3 member
  132. }
  133. export function serializeAsFresnelParameters(sourceName?: string) {
  134. return generateSerializableMember(3, sourceName); // fresnel parameters member
  135. }
  136. export function serializeAsVector2(sourceName?: string) {
  137. return generateSerializableMember(4, sourceName); // vector2 member
  138. }
  139. export function serializeAsVector3(sourceName?: string) {
  140. return generateSerializableMember(5, sourceName); // vector3 member
  141. }
  142. export function serializeAsMeshReference(sourceName?: string) {
  143. return generateSerializableMember(6, sourceName); // mesh reference member
  144. }
  145. export function serializeAsColorCurves(sourceName?: string) {
  146. return generateSerializableMember(7, sourceName); // color curves
  147. }
  148. export function serializeAsColor4(sourceName?: string) {
  149. return generateSerializableMember(8, sourceName); // color 4
  150. }
  151. export function serializeAsImageProcessingConfiguration(sourceName?: string) {
  152. return generateSerializableMember(9, sourceName); // image processing
  153. }
  154. export function serializeAsQuaternion(sourceName?: string) {
  155. return generateSerializableMember(10, sourceName); // quaternion member
  156. }
  157. /**
  158. * Decorator used to define property that can be serialized as reference to a camera
  159. * @param sourceName defines the name of the property to decorate
  160. */
  161. export function serializeAsCameraReference(sourceName?: string) {
  162. return generateSerializableMember(11, sourceName); // camera reference member
  163. }
  164. /**
  165. * Class used to help serialization objects
  166. */
  167. export class SerializationHelper {
  168. /** hidden */
  169. public static _ImageProcessingConfigurationParser = (sourceProperty: any): ImageProcessingConfiguration => {
  170. throw "ImageProcessingConfiguration needs to be imported before being deserialized.";
  171. }
  172. /** hidden */
  173. public static _FresnelParametersParser = (sourceProperty: any): FresnelParameters => {
  174. throw "FresnelParameters needs to be imported before being deserialized.";
  175. }
  176. /** hidden */
  177. public static _ColorCurvesParser = (sourceProperty: any): ColorCurves => {
  178. throw "ColorCurves needs to be imported before being deserialized.";
  179. }
  180. /** hidden */
  181. public static _TextureParser = (sourceProperty: any, scene: Scene, rootUrl: string): Nullable<BaseTexture> => {
  182. throw "Texture needs to be imported before being deserialized.";
  183. }
  184. /**
  185. * Static function used to serialized a specific entity
  186. * @param entity defines the entity to serialize
  187. * @param serializationObject defines the optional target obecjt where serialization data will be stored
  188. * @returns a JSON compatible object representing the serialization of the entity
  189. */
  190. public static Serialize<T>(entity: T, serializationObject?: any): any {
  191. if (!serializationObject) {
  192. serializationObject = {};
  193. }
  194. // Tags
  195. if (Tags) {
  196. serializationObject.tags = Tags.GetTags(entity);
  197. }
  198. var serializedProperties = getMergedStore(entity);
  199. // Properties
  200. for (var property in serializedProperties) {
  201. var propertyDescriptor = serializedProperties[property];
  202. var targetPropertyName = propertyDescriptor.sourceName || property;
  203. var propertyType = propertyDescriptor.type;
  204. var sourceProperty = (<any>entity)[property];
  205. if (sourceProperty !== undefined && sourceProperty !== null) {
  206. switch (propertyType) {
  207. case 0: // Value
  208. serializationObject[targetPropertyName] = sourceProperty;
  209. break;
  210. case 1: // Texture
  211. serializationObject[targetPropertyName] = sourceProperty.serialize();
  212. break;
  213. case 2: // Color3
  214. serializationObject[targetPropertyName] = sourceProperty.asArray();
  215. break;
  216. case 3: // FresnelParameters
  217. serializationObject[targetPropertyName] = sourceProperty.serialize();
  218. break;
  219. case 4: // Vector2
  220. serializationObject[targetPropertyName] = sourceProperty.asArray();
  221. break;
  222. case 5: // Vector3
  223. serializationObject[targetPropertyName] = sourceProperty.asArray();
  224. break;
  225. case 6: // Mesh reference
  226. serializationObject[targetPropertyName] = sourceProperty.id;
  227. break;
  228. case 7: // Color Curves
  229. serializationObject[targetPropertyName] = sourceProperty.serialize();
  230. break;
  231. case 8: // Color 4
  232. serializationObject[targetPropertyName] = (<Color4>sourceProperty).asArray();
  233. break;
  234. case 9: // Image Processing
  235. serializationObject[targetPropertyName] = (<ImageProcessingConfiguration>sourceProperty).serialize();
  236. break;
  237. case 10: // Quaternion
  238. serializationObject[targetPropertyName] = (<Quaternion>sourceProperty).asArray();
  239. break;
  240. case 11: // Camera reference
  241. serializationObject[targetPropertyName] = (<Camera>sourceProperty).id;
  242. break;
  243. }
  244. }
  245. }
  246. return serializationObject;
  247. }
  248. /**
  249. * Creates a new entity from a serialization data object
  250. * @param creationFunction defines a function used to instanciated the new entity
  251. * @param source defines the source serialization data
  252. * @param scene defines the hosting scene
  253. * @param rootUrl defines the root url for resources
  254. * @returns a new entity
  255. */
  256. public static Parse<T>(creationFunction: () => T, source: any, scene: Nullable<Scene>, rootUrl: Nullable<string> = null): T {
  257. var destination = creationFunction();
  258. if (!rootUrl) {
  259. rootUrl = "";
  260. }
  261. // Tags
  262. if (Tags) {
  263. Tags.AddTagsTo(destination, source.tags);
  264. }
  265. var classStore = getMergedStore(destination);
  266. // Properties
  267. for (var property in classStore) {
  268. var propertyDescriptor = classStore[property];
  269. var sourceProperty = source[propertyDescriptor.sourceName || property];
  270. var propertyType = propertyDescriptor.type;
  271. if (sourceProperty !== undefined && sourceProperty !== null) {
  272. var dest = <any>destination;
  273. switch (propertyType) {
  274. case 0: // Value
  275. dest[property] = sourceProperty;
  276. break;
  277. case 1: // Texture
  278. if (scene) {
  279. dest[property] = SerializationHelper._TextureParser(sourceProperty, scene, rootUrl);
  280. }
  281. break;
  282. case 2: // Color3
  283. dest[property] = Color3.FromArray(sourceProperty);
  284. break;
  285. case 3: // FresnelParameters
  286. dest[property] = SerializationHelper._FresnelParametersParser(sourceProperty);
  287. break;
  288. case 4: // Vector2
  289. dest[property] = Vector2.FromArray(sourceProperty);
  290. break;
  291. case 5: // Vector3
  292. dest[property] = Vector3.FromArray(sourceProperty);
  293. break;
  294. case 6: // Mesh reference
  295. if (scene) {
  296. dest[property] = scene.getLastMeshByID(sourceProperty);
  297. }
  298. break;
  299. case 7: // Color Curves
  300. dest[property] = SerializationHelper._ColorCurvesParser(sourceProperty);
  301. break;
  302. case 8: // Color 4
  303. dest[property] = Color4.FromArray(sourceProperty);
  304. break;
  305. case 9: // Image Processing
  306. dest[property] = SerializationHelper._ImageProcessingConfigurationParser(sourceProperty);
  307. break;
  308. case 10: // Quaternion
  309. dest[property] = Quaternion.FromArray(sourceProperty);
  310. break;
  311. case 11: // Camera reference
  312. if (scene) {
  313. dest[property] = scene.getCameraByID(sourceProperty);
  314. }
  315. break;
  316. }
  317. }
  318. }
  319. return destination;
  320. }
  321. /**
  322. * Clones an object
  323. * @param creationFunction defines the function used to instanciate the new object
  324. * @param source defines the source object
  325. * @returns the cloned object
  326. */
  327. public static Clone<T>(creationFunction: () => T, source: T): T {
  328. return _copySource(creationFunction, source, false);
  329. }
  330. /**
  331. * Instanciates a new object based on a source one (some data will be shared between both object)
  332. * @param creationFunction defines the function used to instanciate the new object
  333. * @param source defines the source object
  334. * @returns the new object
  335. */
  336. public static Instanciate<T>(creationFunction: () => T, source: T): T {
  337. return _copySource(creationFunction, source, true);
  338. }
  339. }