@babylonjs/smart-filters 1.0.13 → 8.19.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (259) hide show
  1. package/dist/blockFoundation/aggregateBlock.d.ts +3 -3
  2. package/dist/blockFoundation/aggregateBlock.d.ts.map +1 -1
  3. package/dist/blockFoundation/aggregateBlock.js +1 -2
  4. package/dist/blockFoundation/aggregateBlock.js.map +1 -1
  5. package/dist/blockFoundation/baseBlock.d.ts +2 -2
  6. package/dist/blockFoundation/baseBlock.d.ts.map +1 -1
  7. package/dist/blockFoundation/baseBlock.js +5 -6
  8. package/dist/blockFoundation/baseBlock.js.map +1 -1
  9. package/dist/blockFoundation/customAggregateBlock.d.ts.map +1 -1
  10. package/dist/blockFoundation/customAggregateBlock.js +2 -2
  11. package/dist/blockFoundation/customAggregateBlock.js.map +1 -1
  12. package/dist/blockFoundation/customShaderBlock.d.ts +9 -0
  13. package/dist/blockFoundation/customShaderBlock.d.ts.map +1 -1
  14. package/dist/blockFoundation/customShaderBlock.js +47 -7
  15. package/dist/blockFoundation/customShaderBlock.js.map +1 -1
  16. package/dist/blockFoundation/disableableShaderBlock.d.ts.map +1 -1
  17. package/dist/blockFoundation/disableableShaderBlock.js +4 -4
  18. package/dist/blockFoundation/disableableShaderBlock.js.map +1 -1
  19. package/dist/blockFoundation/index.js +0 -2
  20. package/dist/blockFoundation/index.js.map +1 -1
  21. package/dist/blockFoundation/inputBlock.d.ts +8 -8
  22. package/dist/blockFoundation/inputBlock.d.ts.map +1 -1
  23. package/dist/blockFoundation/inputBlock.deserializer.d.ts +1 -1
  24. package/dist/blockFoundation/inputBlock.deserializer.d.ts.map +1 -1
  25. package/dist/blockFoundation/inputBlock.deserializer.js +1 -1
  26. package/dist/blockFoundation/inputBlock.deserializer.js.map +1 -1
  27. package/dist/blockFoundation/inputBlock.js +7 -7
  28. package/dist/blockFoundation/inputBlock.js.map +1 -1
  29. package/dist/blockFoundation/inputBlock.serialization.types.d.ts.map +1 -1
  30. package/dist/blockFoundation/inputBlock.serializer.d.ts +1 -1
  31. package/dist/blockFoundation/inputBlock.serializer.js +26 -28
  32. package/dist/blockFoundation/inputBlock.serializer.js.map +1 -1
  33. package/dist/blockFoundation/outputBlock.d.ts +4 -9
  34. package/dist/blockFoundation/outputBlock.d.ts.map +1 -1
  35. package/dist/blockFoundation/outputBlock.js +13 -14
  36. package/dist/blockFoundation/outputBlock.js.map +1 -1
  37. package/dist/blockFoundation/shaderBlock.d.ts +4 -4
  38. package/dist/blockFoundation/shaderBlock.d.ts.map +1 -1
  39. package/dist/blockFoundation/shaderBlock.js +13 -19
  40. package/dist/blockFoundation/shaderBlock.js.map +1 -1
  41. package/dist/blockFoundation/textureOptions.d.ts +1 -1
  42. package/dist/blockFoundation/textureOptions.js +1 -1
  43. package/dist/command/command.d.ts +1 -1
  44. package/dist/command/command.js +1 -1
  45. package/dist/command/commandBuffer.d.ts +1 -1
  46. package/dist/command/commandBuffer.d.ts.map +1 -1
  47. package/dist/command/commandBuffer.js +2 -2
  48. package/dist/command/commandBufferDebugger.d.ts +2 -2
  49. package/dist/command/commandBufferDebugger.d.ts.map +1 -1
  50. package/dist/command/commandBufferDebugger.js +1 -1
  51. package/dist/command/commandBufferDebugger.js.map +1 -1
  52. package/dist/command/index.d.ts +3 -1
  53. package/dist/command/index.d.ts.map +1 -1
  54. package/dist/command/index.js +5 -1
  55. package/dist/command/index.js.map +1 -1
  56. package/dist/connection/connectionPoint.d.ts +3 -3
  57. package/dist/connection/connectionPoint.d.ts.map +1 -1
  58. package/dist/connection/connectionPoint.js +2 -2
  59. package/dist/connection/connectionPoint.js.map +1 -1
  60. package/dist/connection/connectionPointCompatibilityState.d.ts +1 -1
  61. package/dist/connection/connectionPointCompatibilityState.js +1 -1
  62. package/dist/connection/connectionPointType.d.ts +3 -3
  63. package/dist/connection/connectionPointType.d.ts.map +1 -1
  64. package/dist/connection/connectionPointWithDefault.d.ts +3 -3
  65. package/dist/connection/connectionPointWithDefault.d.ts.map +1 -1
  66. package/dist/connection/connectionPointWithDefault.js.map +1 -1
  67. package/dist/connection/index.d.ts +2 -1
  68. package/dist/connection/index.d.ts.map +1 -1
  69. package/dist/connection/index.js +3 -3
  70. package/dist/connection/index.js.map +1 -1
  71. package/dist/editorUtils/editableInPropertyPage.d.ts +2 -2
  72. package/dist/editorUtils/editableInPropertyPage.d.ts.map +1 -1
  73. package/dist/editorUtils/editableInPropertyPage.js +2 -2
  74. package/dist/editorUtils/editableInPropertyPage.js.map +1 -1
  75. package/dist/editorUtils/index.d.ts +1 -0
  76. package/dist/editorUtils/index.d.ts.map +1 -1
  77. package/dist/editorUtils/index.js +2 -0
  78. package/dist/editorUtils/index.js.map +1 -1
  79. package/dist/index.d.ts.map +1 -1
  80. package/dist/index.js +1 -1
  81. package/dist/index.js.map +1 -1
  82. package/dist/optimization/dependencyGraph.js.map +1 -1
  83. package/dist/optimization/optimizedShaderBlock.d.ts +4 -4
  84. package/dist/optimization/optimizedShaderBlock.d.ts.map +1 -1
  85. package/dist/optimization/optimizedShaderBlock.js.map +1 -1
  86. package/dist/optimization/smartFilterOptimizer.d.ts +2 -2
  87. package/dist/optimization/smartFilterOptimizer.d.ts.map +1 -1
  88. package/dist/optimization/smartFilterOptimizer.js +34 -57
  89. package/dist/optimization/smartFilterOptimizer.js.map +1 -1
  90. package/dist/runtime/index.d.ts +2 -1
  91. package/dist/runtime/index.d.ts.map +1 -1
  92. package/dist/runtime/index.js +3 -3
  93. package/dist/runtime/index.js.map +1 -1
  94. package/dist/runtime/renderTargetGenerator.d.ts +1 -1
  95. package/dist/runtime/renderTargetGenerator.d.ts.map +1 -1
  96. package/dist/runtime/renderTargetGenerator.js +5 -7
  97. package/dist/runtime/renderTargetGenerator.js.map +1 -1
  98. package/dist/runtime/shaderRuntime.d.ts +7 -7
  99. package/dist/runtime/shaderRuntime.d.ts.map +1 -1
  100. package/dist/runtime/shaderRuntime.js +7 -8
  101. package/dist/runtime/shaderRuntime.js.map +1 -1
  102. package/dist/runtime/smartFilterRuntime.d.ts +3 -3
  103. package/dist/runtime/smartFilterRuntime.d.ts.map +1 -1
  104. package/dist/runtime/smartFilterRuntime.js.map +1 -1
  105. package/dist/runtime/strongRef.d.ts +1 -1
  106. package/dist/runtime/strongRef.js +1 -1
  107. package/dist/serialization/importCustomBlockDefinition.d.ts +1 -1
  108. package/dist/serialization/importCustomBlockDefinition.js +8 -9
  109. package/dist/serialization/importCustomBlockDefinition.js.map +1 -1
  110. package/dist/serialization/index.d.ts +1 -0
  111. package/dist/serialization/index.d.ts.map +1 -1
  112. package/dist/serialization/index.js +3 -0
  113. package/dist/serialization/index.js.map +1 -1
  114. package/dist/serialization/serializedBlockDefinition.d.ts +2 -2
  115. package/dist/serialization/serializedBlockDefinition.d.ts.map +1 -1
  116. package/dist/serialization/serializedShaderBlockDefinition.d.ts +1 -1
  117. package/dist/serialization/serializedShaderBlockDefinition.d.ts.map +1 -1
  118. package/dist/serialization/serializedSmartFilter.d.ts +1 -1
  119. package/dist/serialization/serializedSmartFilter.d.ts.map +1 -1
  120. package/dist/serialization/smartFilterDeserializer.d.ts +6 -6
  121. package/dist/serialization/smartFilterDeserializer.d.ts.map +1 -1
  122. package/dist/serialization/smartFilterDeserializer.js +9 -12
  123. package/dist/serialization/smartFilterDeserializer.js.map +1 -1
  124. package/dist/serialization/smartFilterSerializer.d.ts +2 -2
  125. package/dist/serialization/smartFilterSerializer.d.ts.map +1 -1
  126. package/dist/serialization/smartFilterSerializer.js +10 -13
  127. package/dist/serialization/smartFilterSerializer.js.map +1 -1
  128. package/dist/serialization/v1/defaultBlockSerializer.d.ts +2 -2
  129. package/dist/serialization/v1/defaultBlockSerializer.d.ts.map +1 -1
  130. package/dist/serialization/v1/defaultBlockSerializer.js +1 -1
  131. package/dist/serialization/v1/index.d.ts +1 -0
  132. package/dist/serialization/v1/index.d.ts.map +1 -1
  133. package/dist/serialization/v1/index.js +2 -0
  134. package/dist/serialization/v1/index.js.map +1 -1
  135. package/dist/serialization/v1/shaderBlockSerialization.types.d.ts +3 -3
  136. package/dist/serialization/v1/shaderBlockSerialization.types.d.ts.map +1 -1
  137. package/dist/serialization/v1/smartFilterSerialization.types.d.ts +2 -2
  138. package/dist/serialization/v1/smartFilterSerialization.types.d.ts.map +1 -1
  139. package/dist/smartFilter.d.ts +8 -8
  140. package/dist/smartFilter.d.ts.map +1 -1
  141. package/dist/smartFilter.js +6 -7
  142. package/dist/smartFilter.js.map +1 -1
  143. package/dist/utils/buildTools/buildShaders.d.ts +3 -2
  144. package/dist/utils/buildTools/buildShaders.d.ts.map +1 -1
  145. package/dist/utils/buildTools/buildShaders.js +7 -6
  146. package/dist/utils/buildTools/buildShaders.js.map +1 -1
  147. package/dist/utils/buildTools/convertGlslIntoBlock.d.ts +4 -2
  148. package/dist/utils/buildTools/convertGlslIntoBlock.d.ts.map +1 -1
  149. package/dist/utils/buildTools/convertGlslIntoBlock.js +136 -141
  150. package/dist/utils/buildTools/convertGlslIntoBlock.js.map +1 -1
  151. package/dist/utils/buildTools/convertGlslIntoShaderProgram.d.ts +4 -4
  152. package/dist/utils/buildTools/convertGlslIntoShaderProgram.d.ts.map +1 -1
  153. package/dist/utils/buildTools/convertGlslIntoShaderProgram.js +55 -48
  154. package/dist/utils/buildTools/convertGlslIntoShaderProgram.js.map +1 -1
  155. package/dist/utils/buildTools/convertShaders.d.ts +8 -5
  156. package/dist/utils/buildTools/convertShaders.d.ts.map +1 -1
  157. package/dist/utils/buildTools/convertShaders.js +38 -14
  158. package/dist/utils/buildTools/convertShaders.js.map +1 -1
  159. package/dist/utils/buildTools/shaderConverter.d.ts +4 -4
  160. package/dist/utils/buildTools/shaderConverter.d.ts.map +1 -1
  161. package/dist/utils/buildTools/shaderConverter.js +17 -21
  162. package/dist/utils/buildTools/shaderConverter.js.map +1 -1
  163. package/dist/utils/buildTools/watchShaders.d.ts +3 -2
  164. package/dist/utils/buildTools/watchShaders.d.ts.map +1 -1
  165. package/dist/utils/buildTools/watchShaders.js +10 -8
  166. package/dist/utils/buildTools/watchShaders.js.map +1 -1
  167. package/dist/utils/index.d.ts +1 -0
  168. package/dist/utils/index.d.ts.map +1 -1
  169. package/dist/utils/index.js +2 -1
  170. package/dist/utils/index.js.map +1 -1
  171. package/dist/utils/renderTargetUtils.d.ts +3 -3
  172. package/dist/utils/renderTargetUtils.d.ts.map +1 -1
  173. package/dist/utils/renderTargetUtils.js +4 -4
  174. package/dist/utils/renderTargetUtils.js.map +1 -1
  175. package/dist/utils/shaderCodeUtils.d.ts +8 -8
  176. package/dist/utils/shaderCodeUtils.d.ts.map +1 -1
  177. package/dist/utils/shaderCodeUtils.js +16 -28
  178. package/dist/utils/shaderCodeUtils.js.map +1 -1
  179. package/dist/utils/textureLoaders.d.ts +2 -2
  180. package/dist/utils/textureLoaders.d.ts.map +1 -1
  181. package/dist/utils/textureLoaders.js +2 -3
  182. package/dist/utils/textureLoaders.js.map +1 -1
  183. package/dist/utils/textureUtils.d.ts +5 -5
  184. package/dist/utils/textureUtils.d.ts.map +1 -1
  185. package/dist/utils/textureUtils.js +1 -1
  186. package/dist/utils/textureUtils.js.map +1 -1
  187. package/dist/version.d.ts +2 -2
  188. package/dist/version.js +2 -2
  189. package/license.md +21 -21
  190. package/package.json +13 -8
  191. package/src/blockFoundation/aggregateBlock.ts +148 -151
  192. package/src/blockFoundation/baseBlock.ts +339 -364
  193. package/src/blockFoundation/customAggregateBlock.ts +88 -104
  194. package/src/blockFoundation/customShaderBlock.ts +362 -326
  195. package/src/blockFoundation/disableableShaderBlock.ts +91 -100
  196. package/src/blockFoundation/index.ts +9 -9
  197. package/src/blockFoundation/inputBlock.deserializer.ts +72 -97
  198. package/src/blockFoundation/inputBlock.serialization.types.ts +126 -132
  199. package/src/blockFoundation/inputBlock.serializer.ts +150 -150
  200. package/src/blockFoundation/inputBlock.ts +181 -192
  201. package/src/blockFoundation/outputBlock.ts +144 -151
  202. package/src/blockFoundation/shaderBlock.ts +156 -163
  203. package/src/blockFoundation/textureOptions.ts +57 -57
  204. package/src/command/command.ts +59 -59
  205. package/src/command/commandBuffer.ts +71 -71
  206. package/src/command/commandBufferDebugger.ts +14 -14
  207. package/src/command/index.ts +7 -3
  208. package/src/connection/connectionPoint.ts +205 -214
  209. package/src/connection/connectionPointCompatibilityState.ts +31 -31
  210. package/src/connection/connectionPointType.ts +45 -45
  211. package/src/connection/connectionPointWithDefault.ts +27 -35
  212. package/src/connection/index.ts +8 -9
  213. package/src/editorUtils/editableInPropertyPage.ts +106 -106
  214. package/src/editorUtils/index.ts +3 -1
  215. package/src/index.ts +16 -15
  216. package/src/optimization/dependencyGraph.ts +96 -96
  217. package/src/optimization/index.ts +1 -1
  218. package/src/optimization/optimizedShaderBlock.ts +131 -134
  219. package/src/optimization/smartFilterOptimizer.ts +757 -825
  220. package/src/runtime/index.ts +8 -6
  221. package/src/runtime/renderTargetGenerator.ts +222 -248
  222. package/src/runtime/shaderRuntime.ts +174 -173
  223. package/src/runtime/smartFilterRuntime.ts +112 -112
  224. package/src/runtime/strongRef.ts +18 -18
  225. package/src/serialization/importCustomBlockDefinition.ts +86 -86
  226. package/src/serialization/index.ts +10 -7
  227. package/src/serialization/serializedBlockDefinition.ts +12 -12
  228. package/src/serialization/serializedShaderBlockDefinition.ts +7 -7
  229. package/src/serialization/serializedSmartFilter.ts +6 -6
  230. package/src/serialization/smartFilterDeserializer.ts +190 -220
  231. package/src/serialization/smartFilterSerializer.ts +110 -121
  232. package/src/serialization/v1/defaultBlockSerializer.ts +21 -21
  233. package/src/serialization/v1/index.ts +4 -2
  234. package/src/serialization/v1/shaderBlockSerialization.types.ts +85 -85
  235. package/src/serialization/v1/smartFilterSerialization.types.ts +129 -133
  236. package/src/smartFilter.ts +255 -260
  237. package/src/utils/buildTools/buildShaders.ts +14 -13
  238. package/src/utils/buildTools/convertGlslIntoBlock.ts +370 -385
  239. package/src/utils/buildTools/convertGlslIntoShaderProgram.ts +173 -172
  240. package/src/utils/buildTools/convertShaders.ts +65 -43
  241. package/src/utils/buildTools/recordVersionNumber.js +24 -0
  242. package/src/utils/buildTools/shaderConverter.ts +466 -478
  243. package/src/utils/buildTools/watchShaders.ts +44 -42
  244. package/src/utils/index.ts +4 -2
  245. package/src/utils/renderTargetUtils.ts +30 -35
  246. package/src/utils/shaderCodeUtils.ts +192 -207
  247. package/src/utils/textureLoaders.ts +31 -43
  248. package/src/utils/textureUtils.ts +28 -32
  249. package/src/version.ts +2 -2
  250. package/dist/utils/buildTools/determineVersion.d.ts +0 -36
  251. package/dist/utils/buildTools/determineVersion.d.ts.map +0 -1
  252. package/dist/utils/buildTools/determineVersion.js +0 -109
  253. package/dist/utils/buildTools/determineVersion.js.map +0 -1
  254. package/dist/utils/buildTools/versionUp.d.ts +0 -2
  255. package/dist/utils/buildTools/versionUp.d.ts.map +0 -1
  256. package/dist/utils/buildTools/versionUp.js +0 -53
  257. package/dist/utils/buildTools/versionUp.js.map +0 -1
  258. package/src/utils/buildTools/determineVersion.ts +0 -128
  259. package/src/utils/buildTools/versionUp.ts +0 -61
@@ -1,220 +1,190 @@
1
- import type { BaseBlock } from "../blockFoundation/baseBlock";
2
- import type { SerializedSmartFilter } from "./serializedSmartFilter.js";
3
- import { SmartFilter } from "../smartFilter.js";
4
- import { ShaderBlock } from "../blockFoundation/shaderBlock.js";
5
- import { inputBlockDeserializer } from "../blockFoundation/inputBlock.deserializer.js";
6
- import { OutputBlock } from "../blockFoundation/outputBlock.js";
7
- import type { ThinEngine } from "@babylonjs/core/Engines/thinEngine";
8
- import { InputBlock } from "../blockFoundation/inputBlock.js";
9
- import type {
10
- ISerializedBlockV1,
11
- ISerializedConnectionV1,
12
- OptionalBlockDeserializerV1,
13
- SerializedSmartFilterV1,
14
- } from "./v1/smartFilterSerialization.types";
15
- import { UniqueIdGenerator } from "../utils/uniqueIdGenerator.js";
16
- import type { Nullable } from "@babylonjs/core/types";
17
-
18
- /**
19
- * A function that creates a block instance of the given class block type, or return null if it cannot.
20
- */
21
- export type BlockFactory = (
22
- smartFilter: SmartFilter,
23
- engine: ThinEngine,
24
- serializedBlock: ISerializedBlockV1,
25
- smartFilterDeserializer: SmartFilterDeserializer
26
- ) => Promise<Nullable<BaseBlock>>;
27
-
28
- /**
29
- * Deserializes serialized SmartFilters. The caller passes in a map of block deserializers it wants to use,
30
- * which allows the caller to provide custom deserializers for blocks beyond the core blocks.
31
- * The deserializer supports versioned serialized SmartFilters.
32
- */
33
- export class SmartFilterDeserializer {
34
- private readonly _blockFactory: BlockFactory;
35
- private readonly _customInputBlockDeserializer?: OptionalBlockDeserializerV1;
36
-
37
- /**
38
- * Creates a new SmartFilterDeserializer
39
- * @param blockFactory - A function that creates a block of the given class name, or returns null if it cannot
40
- * @param customInputBlockDeserializer - An optional custom deserializer for InputBlocks - if supplied and it returns null, the default deserializer will be used
41
- */
42
- public constructor(blockFactory: BlockFactory, customInputBlockDeserializer?: OptionalBlockDeserializerV1) {
43
- this._blockFactory = blockFactory;
44
- this._customInputBlockDeserializer = customInputBlockDeserializer;
45
- }
46
-
47
- /**
48
- * Deserializes a SmartFilter from a JSON object - can be safely called multiple times and has no side effects within the class.
49
- * @param engine - The ThinEngine to pass to the new SmartFilter
50
- * @param smartFilterJson - The JSON object to deserialize
51
- * @returns A promise that resolves to the deserialized SmartFilter
52
- */
53
- public async deserialize(engine: ThinEngine, smartFilterJson: any): Promise<SmartFilter> {
54
- const serializedSmartFilter: SerializedSmartFilter = smartFilterJson;
55
-
56
- // Back-compat for the rename of version to formatVersion, didn't warrant a new version
57
- if ((serializedSmartFilter as any).version && serializedSmartFilter.formatVersion === undefined) {
58
- serializedSmartFilter.formatVersion = (serializedSmartFilter as any).version;
59
- }
60
-
61
- switch (serializedSmartFilter.formatVersion) {
62
- case 1:
63
- return await this._deserializeV1(engine, serializedSmartFilter);
64
- }
65
- }
66
-
67
- private async _deserializeV1(
68
- engine: ThinEngine,
69
- serializedSmartFilter: SerializedSmartFilterV1
70
- ): Promise<SmartFilter> {
71
- const smartFilter = new SmartFilter(serializedSmartFilter.name, serializedSmartFilter.namespace);
72
- const blockIdMap = new Map<number, BaseBlock>();
73
-
74
- // Only needed for smart filters saved before we started using uniqueIds for the maps, didn't warrant new version
75
- const blockNameMap = new Map<string, BaseBlock>();
76
-
77
- // Deserialize the SmartFilter level data
78
- smartFilter.comments = serializedSmartFilter.comments;
79
- smartFilter.editorData = serializedSmartFilter.editorData;
80
-
81
- // Deserialize the blocks
82
- const blockDeserializationWork: Promise<void>[] = [];
83
- const blockDefinitionsWhichCouldNotBeDeserialized: string[] = [];
84
- serializedSmartFilter.blocks.forEach((serializedBlock: ISerializedBlockV1) => {
85
- blockDeserializationWork.push(
86
- this._deserializeBlockV1(
87
- smartFilter,
88
- serializedBlock,
89
- engine,
90
- blockDefinitionsWhichCouldNotBeDeserialized,
91
- blockIdMap,
92
- blockNameMap
93
- )
94
- );
95
- });
96
- await Promise.all(blockDeserializationWork);
97
-
98
- // If any block definitions could not be deserialized, throw an error
99
- if (blockDefinitionsWhichCouldNotBeDeserialized.length > 0) {
100
- throw new Error(
101
- `Could not deserialize the following block definitions: ${blockDefinitionsWhichCouldNotBeDeserialized.join(", ")}`
102
- );
103
- }
104
-
105
- // Deserialize the connections
106
- serializedSmartFilter.connections.forEach((connection: ISerializedConnectionV1) => {
107
- // Find the source block and its connection point's connectTo function
108
- const sourceBlock =
109
- typeof connection.outputBlock === "string"
110
- ? blockNameMap.get(connection.outputBlock)
111
- : blockIdMap.get(connection.outputBlock);
112
-
113
- if (!sourceBlock) {
114
- throw new Error(`Source block ${connection.outputBlock} not found`);
115
- }
116
- const sourceConnectionPoint = sourceBlock.outputs.find(
117
- (output) => output.name === connection.outputConnectionPoint
118
- );
119
- if (!sourceConnectionPoint || typeof sourceConnectionPoint.connectTo !== "function") {
120
- throw new Error(
121
- `Block ${connection.outputBlock} does not have an connection point named ${connection.outputConnectionPoint}`
122
- );
123
- }
124
- const sourceConnectToFunction = sourceConnectionPoint.connectTo.bind(sourceConnectionPoint);
125
-
126
- // Find the target block and its connection point
127
- const targetBlock =
128
- typeof connection.inputBlock === "string"
129
- ? blockNameMap.get(connection.inputBlock)
130
- : blockIdMap.get(connection.inputBlock);
131
- if (!targetBlock) {
132
- throw new Error(`Target block ${connection.inputBlock} not found`);
133
- }
134
-
135
- const targetConnectionPoint = targetBlock.inputs.find(
136
- (input) => input.name === connection.inputConnectionPoint
137
- );
138
- if (!targetConnectionPoint || typeof targetConnectionPoint !== "object") {
139
- throw new Error(
140
- `Block ${connection.inputBlock} does not have a connection point named ${connection.inputConnectionPoint}`
141
- );
142
- }
143
-
144
- // Create the connection
145
- sourceConnectToFunction.call(sourceBlock, targetConnectionPoint);
146
- });
147
-
148
- return smartFilter;
149
- }
150
-
151
- private async _deserializeBlockV1(
152
- smartFilter: SmartFilter,
153
- serializedBlock: ISerializedBlockV1,
154
- engine: ThinEngine,
155
- blockTypesWhichCouldNotBeDeserialized: string[],
156
- blockIdMap: Map<number, BaseBlock>,
157
- blockNameMap: Map<string, BaseBlock>
158
- ): Promise<void> {
159
- let newBlock: Nullable<BaseBlock> = null;
160
-
161
- // Back compat for early Smart Filter V1 serialization where the blockType was stored in className
162
- // Not worth creating a new version for this, as it's only used in the deserializer
163
- if ((serializedBlock as any).className && !serializedBlock.blockType) {
164
- serializedBlock.blockType = (serializedBlock as any).className;
165
- }
166
-
167
- // Back compat for early Smart Filter V1 serialization where the namespace was not stored
168
- if (serializedBlock.namespace === undefined) {
169
- serializedBlock.namespace = null;
170
- }
171
-
172
- // Get the instance of the block
173
- switch (serializedBlock.blockType) {
174
- case InputBlock.ClassName:
175
- {
176
- if (this._customInputBlockDeserializer) {
177
- newBlock = await this._customInputBlockDeserializer(smartFilter, serializedBlock, engine);
178
- }
179
- if (newBlock === null) {
180
- newBlock = inputBlockDeserializer(smartFilter, serializedBlock);
181
- }
182
- }
183
- break;
184
- case OutputBlock.ClassName:
185
- {
186
- newBlock = smartFilter.output.ownerBlock;
187
- }
188
- break;
189
- default: {
190
- // If it's not an input or output block, use the provided block factory
191
- newBlock = await this._blockFactory(smartFilter, engine, serializedBlock, this);
192
- if (!newBlock) {
193
- if (blockTypesWhichCouldNotBeDeserialized.indexOf(serializedBlock.blockType) === -1) {
194
- blockTypesWhichCouldNotBeDeserialized.push(serializedBlock.blockType);
195
- }
196
- return;
197
- }
198
- }
199
- }
200
-
201
- // Deserializers are not responsible for setting the uniqueId or comments.
202
- // This is so they don't have to be passed into the constructors when programmatically creating
203
- // blocks, and so each deserializer doesn't have to remember to do it.
204
- newBlock.uniqueId = serializedBlock.uniqueId;
205
- newBlock.comments = serializedBlock.comments;
206
-
207
- // Deserializers are also not responsible for deserializing the outputTextureOptions of ShaderBlocks.
208
- if (serializedBlock.outputTextureOptions && newBlock instanceof ShaderBlock) {
209
- newBlock.outputTextureOptions = serializedBlock.outputTextureOptions;
210
- }
211
-
212
- // We need to ensure any uniqueIds generated in the future (e.g. a new block is added to the SmartFilter)
213
- // are higher than this one.
214
- UniqueIdGenerator.EnsureIdsGreaterThan(newBlock.uniqueId);
215
-
216
- // Save in the map
217
- blockIdMap.set(newBlock.uniqueId, newBlock);
218
- blockNameMap.set(newBlock.name, newBlock);
219
- }
220
- }
1
+ import type { BaseBlock } from "../blockFoundation/baseBlock.js";
2
+ import type { SerializedSmartFilter } from "./serializedSmartFilter.js";
3
+ import { SmartFilter } from "../smartFilter.js";
4
+ import { ShaderBlock } from "../blockFoundation/shaderBlock.js";
5
+ import { InputBlockDeserializer } from "../blockFoundation/inputBlock.deserializer.js";
6
+ import { OutputBlock } from "../blockFoundation/outputBlock.js";
7
+ import type { ThinEngine } from "core/Engines/thinEngine.js";
8
+ import { InputBlock } from "../blockFoundation/inputBlock.js";
9
+ import type { ISerializedBlockV1, ISerializedConnectionV1, OptionalBlockDeserializerV1, SerializedSmartFilterV1 } from "./v1/smartFilterSerialization.types.js";
10
+ import { UniqueIdGenerator } from "../utils/uniqueIdGenerator.js";
11
+ import type { Nullable } from "core/types.js";
12
+
13
+ /**
14
+ * A function that creates a block instance of the given class block type, or return null if it cannot.
15
+ */
16
+ export type BlockFactory = (
17
+ smartFilter: SmartFilter,
18
+ engine: ThinEngine,
19
+ serializedBlock: ISerializedBlockV1,
20
+ smartFilterDeserializer: SmartFilterDeserializer
21
+ ) => Promise<Nullable<BaseBlock>>;
22
+
23
+ /**
24
+ * Deserializes serialized SmartFilters. The caller passes in a map of block deserializers it wants to use,
25
+ * which allows the caller to provide custom deserializers for blocks beyond the core blocks.
26
+ * The deserializer supports versioned serialized SmartFilters.
27
+ */
28
+ export class SmartFilterDeserializer {
29
+ private readonly _blockFactory: BlockFactory;
30
+ private readonly _customInputBlockDeserializer?: OptionalBlockDeserializerV1;
31
+
32
+ /**
33
+ * Creates a new SmartFilterDeserializer
34
+ * @param blockFactory - A function that creates a block of the given class name, or returns null if it cannot
35
+ * @param customInputBlockDeserializer - An optional custom deserializer for InputBlocks - if supplied and it returns null, the default deserializer will be used
36
+ */
37
+ public constructor(blockFactory: BlockFactory, customInputBlockDeserializer?: OptionalBlockDeserializerV1) {
38
+ this._blockFactory = blockFactory;
39
+ this._customInputBlockDeserializer = customInputBlockDeserializer;
40
+ }
41
+
42
+ /**
43
+ * Deserializes a SmartFilter from a JSON object - can be safely called multiple times and has no side effects within the class.
44
+ * @param engine - The ThinEngine to pass to the new SmartFilter
45
+ * @param smartFilterJson - The JSON object to deserialize
46
+ * @returns A promise that resolves to the deserialized SmartFilter
47
+ */
48
+ // eslint-disable-next-line @typescript-eslint/naming-convention
49
+ public async deserialize(engine: ThinEngine, smartFilterJson: any): Promise<SmartFilter> {
50
+ const serializedSmartFilter: SerializedSmartFilter = smartFilterJson;
51
+
52
+ // Back-compat for the rename of version to formatVersion, didn't warrant a new version
53
+ if ((serializedSmartFilter as any).version && serializedSmartFilter.formatVersion === undefined) {
54
+ serializedSmartFilter.formatVersion = (serializedSmartFilter as any).version;
55
+ }
56
+
57
+ switch (serializedSmartFilter.formatVersion) {
58
+ case 1:
59
+ return await this._deserializeV1Async(engine, serializedSmartFilter);
60
+ }
61
+ }
62
+
63
+ private async _deserializeV1Async(engine: ThinEngine, serializedSmartFilter: SerializedSmartFilterV1): Promise<SmartFilter> {
64
+ const smartFilter = new SmartFilter(serializedSmartFilter.name, serializedSmartFilter.namespace);
65
+ const blockIdMap = new Map<number, BaseBlock>();
66
+
67
+ // Only needed for smart filters saved before we started using uniqueIds for the maps, didn't warrant new version
68
+ const blockNameMap = new Map<string, BaseBlock>();
69
+
70
+ // Deserialize the SmartFilter level data
71
+ smartFilter.comments = serializedSmartFilter.comments;
72
+ smartFilter.editorData = serializedSmartFilter.editorData;
73
+
74
+ // Deserialize the blocks
75
+ const blockDeserializationWork: Promise<void>[] = [];
76
+ const blockDefinitionsWhichCouldNotBeDeserialized: string[] = [];
77
+ serializedSmartFilter.blocks.forEach((serializedBlock: ISerializedBlockV1) => {
78
+ blockDeserializationWork.push(
79
+ this._deserializeBlockV1Async(smartFilter, serializedBlock, engine, blockDefinitionsWhichCouldNotBeDeserialized, blockIdMap, blockNameMap)
80
+ );
81
+ });
82
+ await Promise.all(blockDeserializationWork);
83
+
84
+ // If any block definitions could not be deserialized, throw an error
85
+ if (blockDefinitionsWhichCouldNotBeDeserialized.length > 0) {
86
+ throw new Error(`Could not deserialize the following block definitions: ${blockDefinitionsWhichCouldNotBeDeserialized.join(", ")}`);
87
+ }
88
+
89
+ // Deserialize the connections
90
+ serializedSmartFilter.connections.forEach((connection: ISerializedConnectionV1) => {
91
+ // Find the source block and its connection point's connectTo function
92
+ const sourceBlock = typeof connection.outputBlock === "string" ? blockNameMap.get(connection.outputBlock) : blockIdMap.get(connection.outputBlock);
93
+
94
+ if (!sourceBlock) {
95
+ throw new Error(`Source block ${connection.outputBlock} not found`);
96
+ }
97
+ const sourceConnectionPoint = sourceBlock.outputs.find((output) => output.name === connection.outputConnectionPoint);
98
+ if (!sourceConnectionPoint || typeof sourceConnectionPoint.connectTo !== "function") {
99
+ throw new Error(`Block ${connection.outputBlock} does not have an connection point named ${connection.outputConnectionPoint}`);
100
+ }
101
+ const sourceConnectToFunction = sourceConnectionPoint.connectTo.bind(sourceConnectionPoint);
102
+
103
+ // Find the target block and its connection point
104
+ const targetBlock = typeof connection.inputBlock === "string" ? blockNameMap.get(connection.inputBlock) : blockIdMap.get(connection.inputBlock);
105
+ if (!targetBlock) {
106
+ throw new Error(`Target block ${connection.inputBlock} not found`);
107
+ }
108
+
109
+ const targetConnectionPoint = targetBlock.inputs.find((input) => input.name === connection.inputConnectionPoint);
110
+ if (!targetConnectionPoint || typeof targetConnectionPoint !== "object") {
111
+ throw new Error(`Block ${connection.inputBlock} does not have a connection point named ${connection.inputConnectionPoint}`);
112
+ }
113
+
114
+ // Create the connection
115
+ sourceConnectToFunction.call(sourceBlock, targetConnectionPoint);
116
+ });
117
+
118
+ return smartFilter;
119
+ }
120
+
121
+ private async _deserializeBlockV1Async(
122
+ smartFilter: SmartFilter,
123
+ serializedBlock: ISerializedBlockV1,
124
+ engine: ThinEngine,
125
+ blockTypesWhichCouldNotBeDeserialized: string[],
126
+ blockIdMap: Map<number, BaseBlock>,
127
+ blockNameMap: Map<string, BaseBlock>
128
+ ): Promise<void> {
129
+ let newBlock: Nullable<BaseBlock> = null;
130
+
131
+ // Back compat for early Smart Filter V1 serialization where the blockType was stored in className
132
+ // Not worth creating a new version for this, as it's only used in the deserializer
133
+ if ((serializedBlock as any).className && !serializedBlock.blockType) {
134
+ serializedBlock.blockType = (serializedBlock as any).className;
135
+ }
136
+
137
+ // Back compat for early Smart Filter V1 serialization where the namespace was not stored
138
+ if (serializedBlock.namespace === undefined) {
139
+ serializedBlock.namespace = null;
140
+ }
141
+
142
+ // Get the instance of the block
143
+ switch (serializedBlock.blockType) {
144
+ case InputBlock.ClassName:
145
+ {
146
+ if (this._customInputBlockDeserializer) {
147
+ newBlock = await this._customInputBlockDeserializer(smartFilter, serializedBlock, engine);
148
+ }
149
+ if (newBlock === null) {
150
+ newBlock = InputBlockDeserializer(smartFilter, serializedBlock);
151
+ }
152
+ }
153
+ break;
154
+ case OutputBlock.ClassName:
155
+ {
156
+ newBlock = smartFilter.output.ownerBlock;
157
+ }
158
+ break;
159
+ default: {
160
+ // If it's not an input or output block, use the provided block factory
161
+ newBlock = await this._blockFactory(smartFilter, engine, serializedBlock, this);
162
+ if (!newBlock) {
163
+ if (blockTypesWhichCouldNotBeDeserialized.indexOf(serializedBlock.blockType) === -1) {
164
+ blockTypesWhichCouldNotBeDeserialized.push(serializedBlock.blockType);
165
+ }
166
+ return;
167
+ }
168
+ }
169
+ }
170
+
171
+ // Deserializers are not responsible for setting the uniqueId or comments.
172
+ // This is so they don't have to be passed into the constructors when programmatically creating
173
+ // blocks, and so each deserializer doesn't have to remember to do it.
174
+ newBlock.uniqueId = serializedBlock.uniqueId;
175
+ newBlock.comments = serializedBlock.comments;
176
+
177
+ // Deserializers are also not responsible for deserializing the outputTextureOptions of ShaderBlocks.
178
+ if (serializedBlock.outputTextureOptions && newBlock instanceof ShaderBlock) {
179
+ newBlock.outputTextureOptions = serializedBlock.outputTextureOptions;
180
+ }
181
+
182
+ // We need to ensure any uniqueIds generated in the future (e.g. a new block is added to the SmartFilter)
183
+ // are higher than this one.
184
+ UniqueIdGenerator.EnsureIdsGreaterThan(newBlock.uniqueId);
185
+
186
+ // Save in the map
187
+ blockIdMap.set(newBlock.uniqueId, newBlock);
188
+ blockNameMap.set(newBlock.name, newBlock);
189
+ }
190
+ }