sceneRecorder.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. import { Scene } from '../scene';
  2. import { Nullable } from '../types';
  3. import { SceneSerializer } from './sceneSerializer';
  4. import { Mesh } from '../Meshes/mesh';
  5. import { Light } from '../Lights/light';
  6. import { Camera } from '../Cameras/camera';
  7. import { Skeleton } from '../Bones/skeleton';
  8. import { Material } from '../Materials/material';
  9. import { MultiMaterial } from '../Materials/multiMaterial';
  10. import { TransformNode } from '../Meshes/transformNode';
  11. import { ParticleSystem } from '../Particles/particleSystem';
  12. import { MorphTargetManager } from '../Morph/morphTargetManager';
  13. import { ShadowGenerator } from '../Lights/Shadows/shadowGenerator';
  14. import { PostProcess } from '../PostProcesses/postProcess';
  15. /**
  16. * Class used to record delta files between 2 scene states
  17. */
  18. export class SceneRecorder {
  19. private _trackedScene: Nullable<Scene> = null;
  20. private _savedJSON: any;
  21. /**
  22. * Track a given scene. This means the current scene state will be considered the original state
  23. * @param scene defines the scene to track
  24. */
  25. public track(scene: Scene) {
  26. this._trackedScene = scene;
  27. this._savedJSON = SceneSerializer.Serialize(scene);
  28. }
  29. /**
  30. * Get the delta between current state and original state
  31. * @returns a string containing the delta
  32. */
  33. public getDelta() {
  34. if (!this._trackedScene) {
  35. return null;
  36. }
  37. let newJSON = SceneSerializer.Serialize(this._trackedScene);
  38. let deltaJSON: any = {};
  39. for (var node in newJSON) {
  40. this._compareCollections(node, this._savedJSON[node], newJSON[node], deltaJSON);
  41. }
  42. return deltaJSON;
  43. }
  44. private _compareArray(key: string, original: any[], current: any[], deltaJSON: any) {
  45. if (original.length === 0 && current.length === 0) {
  46. return true;
  47. }
  48. // Numbers?
  49. if (original.length && !isNaN(original[0]) || current.length && !isNaN(current[0])) {
  50. if (original.length !== current.length) {
  51. return false;
  52. }
  53. if (original.length === 0) {
  54. return true;
  55. }
  56. for (var index = 0; index < original.length; index++) {
  57. if (original[index] !== current[index]) {
  58. deltaJSON[key] = current;
  59. return false;
  60. }
  61. }
  62. return true;
  63. }
  64. // let's use uniqueId to find similar objects
  65. let originalUniqueIds: number[] = [];
  66. for (var index = 0; index < original.length; index++) {
  67. let originalObject = original[index];
  68. let originalUniqueId = originalObject.uniqueId;
  69. originalUniqueIds.push(originalUniqueId);
  70. // Look for that object in current state
  71. let currentObjects = current.filter((c) => c.uniqueId === originalUniqueId);
  72. if (currentObjects.length) { // We have a candidate
  73. let currentObject = currentObjects[0];
  74. let newObject: any = {};
  75. if (!this._compareObjects(originalObject, currentObject, newObject)) {
  76. if (!deltaJSON[key]) {
  77. deltaJSON[key] = [];
  78. }
  79. newObject.__state = {
  80. id: currentObject.id || currentObject.name
  81. };
  82. deltaJSON[key].push(newObject);
  83. }
  84. } else {
  85. // We need to delete
  86. let newObject: any = {
  87. __state: {
  88. deleteId: originalObject.id || originalObject.name
  89. }
  90. };
  91. deltaJSON[key].push(newObject);
  92. }
  93. }
  94. // Checking for new objects
  95. for (var index = 0; index < current.length; index++) {
  96. let currentObject = current[index];
  97. let currentUniqueId = currentObject.uniqueId;
  98. // Object was added
  99. if (originalUniqueIds.indexOf(currentUniqueId) === -1) {
  100. if (!deltaJSON[key]) {
  101. deltaJSON[key] = [];
  102. }
  103. deltaJSON[key].push(currentObject);
  104. }
  105. }
  106. return true;
  107. }
  108. private _compareObjects(originalObjet: any, currentObject: any, deltaJSON: any) {
  109. let aDifferenceWasFound = false;
  110. for (var prop in originalObjet) {
  111. if (!originalObjet.hasOwnProperty(prop)) {
  112. continue;
  113. }
  114. var originalValue = originalObjet[prop];
  115. var currentValue = currentObject[prop];
  116. var diffFound = false;
  117. if (Array.isArray(originalValue)) {
  118. diffFound = (JSON.stringify(originalValue) !== JSON.stringify(currentValue));
  119. } else if (!isNaN(originalValue) || Object.prototype.toString.call(originalValue) == '[object String]') {
  120. diffFound = (originalValue !== currentValue);
  121. }
  122. if (diffFound) {
  123. aDifferenceWasFound = true;
  124. deltaJSON[prop] = currentValue;
  125. }
  126. }
  127. return !aDifferenceWasFound;
  128. }
  129. private _compareCollections(key: string, original: any[], current: any[], deltaJSON: any) {
  130. // Same ?
  131. if (original === current) {
  132. return;
  133. }
  134. if (original && current) {
  135. // Array?
  136. if (Array.isArray(original) && Array.isArray(current)) {
  137. if (this._compareArray(key, original, current, deltaJSON)) {
  138. return;
  139. }
  140. } else if (typeof original === "object" && typeof current === "object") { // Object
  141. let newObject = {};
  142. if (!this._compareObjects(original, current, newObject)) {
  143. deltaJSON[key] = newObject;
  144. }
  145. return;
  146. }
  147. }
  148. }
  149. private static GetShadowGeneratorById(scene: Scene, id: string) {
  150. var generators = scene.lights.map((l) => l.getShadowGenerator());
  151. for (var generator of generators) {
  152. if (generator && generator.id === id) {
  153. return generator;
  154. }
  155. }
  156. return null;
  157. }
  158. /**
  159. * Apply a given delta to a given scene
  160. * @param deltaJSON defines the JSON containing the delta
  161. * @param scene defines the scene to apply the delta to
  162. */
  163. public static ApplyDelta(deltaJSON: any | string, scene: Scene) {
  164. if (typeof deltaJSON === 'string') {
  165. deltaJSON = JSON.parse(deltaJSON);
  166. }
  167. // Scene
  168. let anyScene = scene as any;
  169. for (var prop in deltaJSON) {
  170. var source = deltaJSON[prop];
  171. var property = anyScene[prop];
  172. if (Array.isArray(property) || prop === "shadowGenerators") { // Restore array
  173. switch (prop) {
  174. case "cameras":
  175. this._ApplyDeltaForEntity(source, scene, scene.getCameraByID.bind(scene), (data) => Camera.Parse(data, scene));
  176. break;
  177. case "lights":
  178. this._ApplyDeltaForEntity(source, scene, scene.getLightByID.bind(scene), (data) => Light.Parse(data, scene));
  179. break;
  180. case "shadowGenerators":
  181. this._ApplyDeltaForEntity(source, scene, (id) => this.GetShadowGeneratorById(scene, id), (data) => ShadowGenerator.Parse(data, scene));
  182. break;
  183. case "meshes":
  184. this._ApplyDeltaForEntity(source, scene, scene.getMeshByID.bind(scene), (data) => Mesh.Parse(data, scene, ""));
  185. break;
  186. case "skeletons":
  187. this._ApplyDeltaForEntity(source, scene, scene.getSkeletonById.bind(scene), (data) => Skeleton.Parse(data, scene));
  188. break;
  189. case "materials":
  190. this._ApplyDeltaForEntity(source, scene, scene.getMaterialByID.bind(scene), (data) => Material.Parse(data, scene, ""));
  191. break;
  192. case "multiMaterials":
  193. this._ApplyDeltaForEntity(source, scene, scene.getMaterialByID.bind(scene), (data) => MultiMaterial.Parse(data, scene, ""));
  194. break;
  195. case "transformNodes":
  196. this._ApplyDeltaForEntity(source, scene, scene.getTransformNodeByID.bind(scene), (data) => TransformNode.Parse(data, scene, ""));
  197. break;
  198. case "particleSystems":
  199. this._ApplyDeltaForEntity(source, scene, scene.getParticleSystemByID.bind(scene), (data) => ParticleSystem.Parse(data, scene, ""));
  200. break;
  201. case "morphTargetManagers":
  202. this._ApplyDeltaForEntity(source, scene, scene.getMorphTargetById.bind(scene), (data) => MorphTargetManager.Parse(data, scene));
  203. break;
  204. case "postProcesses":
  205. this._ApplyDeltaForEntity(source, scene, scene.getPostProcessByName.bind(scene), (data) => PostProcess.Parse(data, scene, ""));
  206. break;
  207. }
  208. } else if (!isNaN(property)) {
  209. anyScene[prop] = source;
  210. } else if (property.fromArray) {
  211. property.fromArray(source);
  212. }
  213. }
  214. }
  215. private static _ApplyPropertiesToEntity(deltaJSON: any, entity: any) {
  216. for (var prop in deltaJSON) {
  217. var source = deltaJSON[prop];
  218. var property = entity[prop];
  219. if (property === undefined) {
  220. continue;
  221. }
  222. if (!isNaN(property) || Array.isArray(property)) {
  223. entity[prop] = source;
  224. } else if (property.fromArray) {
  225. property.fromArray(source);
  226. }
  227. }
  228. }
  229. private static _ApplyDeltaForEntity(sources: any[], scene: Scene, finder: (id: string) => any, addNew: (data: any) => void) {
  230. for (var source of sources) {
  231. // Update
  232. if (source.__state && source.__state.id !== undefined) {
  233. let targetEntity = finder(source.__state.id);
  234. if (targetEntity) {
  235. this._ApplyPropertiesToEntity(source, targetEntity);
  236. }
  237. } else if (source.__state && source.__state.deleteId !== undefined) {
  238. let target = finder(source.__state.deleteId);
  239. target?.dispose();
  240. } else {
  241. // New
  242. addNew(source);
  243. }
  244. }
  245. }
  246. }