sceneRecorder.js 12 KB

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