@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,326 +1,362 @@
1
- import type { Effect } from "@babylonjs/core/Materials/effect";
2
- import { ConnectionPointType } from "../connection/connectionPointType.js";
3
- import { ShaderBinding } from "../runtime/shaderRuntime.js";
4
- import { createStrongRef } from "../runtime/strongRef.js";
5
- import type { SerializedShaderBlockDefinition } from "../serialization/serializedShaderBlockDefinition.js";
6
- import type { SerializedInputConnectionPointV1 } from "../serialization/v1/shaderBlockSerialization.types.js";
7
- import type { SmartFilter } from "../smartFilter.js";
8
- import type { ShaderProgram } from "../utils/shaderCodeUtils.js";
9
- import { ShaderBlock } from "./shaderBlock.js";
10
- import type { RuntimeData } from "../connection/connectionPoint.js";
11
- import type { Nullable } from "@babylonjs/core/types.js";
12
-
13
- /**
14
- * The binding for a CustomShaderBlock
15
- */
16
- class CustomShaderBlockBinding extends ShaderBinding {
17
- private readonly _bindSteps: ((effect: Effect, width: number, height: number) => void)[] = [];
18
-
19
- /**
20
- * Creates a new shader binding instance for the CustomShaderBlock block.
21
- * @param inputsWithRuntimeData - The input connection points of the block
22
- */
23
- public constructor(inputsWithRuntimeData: AnyInputWithRuntimeData[]) {
24
- super();
25
-
26
- for (const input of inputsWithRuntimeData) {
27
- switch (input.type) {
28
- case ConnectionPointType.Float:
29
- this._bindSteps.push((effect) => {
30
- effect.setFloat(this.getRemappedName(input.name), input.runtimeData.value);
31
- });
32
- break;
33
- case ConnectionPointType.Texture:
34
- this._bindSteps.push((effect) => {
35
- effect.setTexture(this.getRemappedName(input.name), input.runtimeData.value);
36
- });
37
- break;
38
- case ConnectionPointType.Color3:
39
- this._bindSteps.push((effect) => {
40
- effect.setColor3(this.getRemappedName(input.name), input.runtimeData.value);
41
- });
42
- break;
43
- case ConnectionPointType.Color4:
44
- this._bindSteps.push((effect) => {
45
- effect.setDirectColor4(this.getRemappedName(input.name), input.runtimeData.value);
46
- });
47
- break;
48
- case ConnectionPointType.Boolean:
49
- this._bindSteps.push((effect) => {
50
- effect.setBool(this.getRemappedName(input.name), input.runtimeData.value);
51
- });
52
- break;
53
- case ConnectionPointType.Vector2:
54
- switch (input.autoBind) {
55
- case "outputResolution":
56
- this._bindSteps.push((effect, width, height) => {
57
- effect.setFloat2(this.getRemappedName(input.name), width, height);
58
- });
59
- break;
60
- case "outputAspectRatio":
61
- this._bindSteps.push((effect, width, height) => {
62
- effect.setFloat2(this.getRemappedName(input.name), width / height, height / width);
63
- });
64
- break;
65
- default:
66
- this._bindSteps.push((effect) => {
67
- effect.setVector2(this.getRemappedName(input.name), input.runtimeData.value);
68
- });
69
- }
70
- break;
71
- }
72
- }
73
- }
74
-
75
- /**
76
- * Binds all the required data to the shader when rendering.
77
- * @param effect - The effect to bind the data to
78
- * @param width - defines the width of the output
79
- * @param height - defines the height of the output
80
- */
81
- public override bind(effect: Effect, width: number, height: number): void {
82
- for (let i = 0; i < this._bindSteps.length; i++) {
83
- this._bindSteps[i]!(effect, width, height);
84
- }
85
- }
86
- }
87
-
88
- /**
89
- * A block which loads a SerializedBlockDefinition for use in a SmartFilter.
90
- */
91
- export class CustomShaderBlock extends ShaderBlock {
92
- /**
93
- * Deserializes a CustomShaderBlock from a serialized block definition.
94
- * @param smartFilter - The smart filter this block belongs to
95
- * @param name - Defines the name of the block
96
- * @param blockDefinition - The serialized block definition
97
- * @returns The deserialized CustomShaderBlock instance
98
- */
99
- public static Create(
100
- smartFilter: SmartFilter,
101
- name: string,
102
- blockDefinition: SerializedShaderBlockDefinition
103
- ): CustomShaderBlock {
104
- // When a new version of SerializedBlockDefinition is created, this function should be updated to handle the new properties.
105
-
106
- return new CustomShaderBlock(
107
- smartFilter,
108
- name,
109
- blockDefinition.disableOptimization,
110
- blockDefinition.blockType,
111
- blockDefinition.namespace,
112
- blockDefinition.inputConnectionPoints,
113
- blockDefinition.shaderProgram
114
- );
115
- }
116
-
117
- /**
118
- * The class name of the block.
119
- */
120
- public static override ClassName = "CustomShaderBlock";
121
-
122
- private readonly _shaderProgram: ShaderProgram;
123
- private readonly _blockType: string;
124
- private readonly _namespace: Nullable<string>;
125
- private _autoBoundInputs: Nullable<SerializedInputConnectionPointV1[]> = null;
126
-
127
- /**
128
- * The type of the block - used when serializing / deserializing the block, and in the editor.
129
- */
130
- public override get blockType(): string {
131
- return this._blockType;
132
- }
133
-
134
- /**
135
- * The namespace of the block, which is used to reduce name collisions between blocks and also to group blocks in the editor UI.
136
- * By convention, sub namespaces are separated by a period (e.g. "Babylon.Demo.Effects").
137
- */
138
- public override get namespace(): Nullable<string> {
139
- return this._namespace;
140
- }
141
-
142
- /**
143
- * Instantiates a new custom shader block.
144
- * @param smartFilter - The smart filter this block belongs to
145
- * @param name - The name of the block
146
- * @param disableOptimization - If true, this optimizer will not attempt to optimize this block
147
- * @param blockType - The type of the block
148
- * @param namespace - The namespace of the block
149
- * @param inputConnectionPoints - The input connection points of the
150
- * @param shaderProgram - The shader program for the block
151
- */
152
- private constructor(
153
- smartFilter: SmartFilter,
154
- name: string,
155
- disableOptimization: boolean,
156
- blockType: string,
157
- namespace: Nullable<string>,
158
- inputConnectionPoints: SerializedInputConnectionPointV1[],
159
- shaderProgram: ShaderProgram
160
- ) {
161
- super(smartFilter, name, disableOptimization);
162
- this._blockType = blockType;
163
- this._namespace = namespace;
164
-
165
- for (const input of inputConnectionPoints) {
166
- this._registerSerializedInputConnectionPointV1(input);
167
- }
168
-
169
- this._shaderProgram = shaderProgram;
170
- }
171
-
172
- /**
173
- * Gets the shader program to use to render the block.
174
- * @returns The shader program to use to render the block
175
- */
176
- public override getShaderProgram() {
177
- return this._shaderProgram;
178
- }
179
-
180
- /**
181
- * Checks a specific input connection point type to see if it has a default value, and registers the input
182
- * connection point accordingly.
183
- * @param connectionPoint - The input connection point to register
184
- */
185
- private _registerSerializedInputConnectionPointV1<U extends ConnectionPointType>(
186
- connectionPoint: SerializedInputConnectionPointV1<U>
187
- ): void {
188
- if (connectionPoint.autoBind) {
189
- // Auto bound inputs are not registered as input connection points
190
- if (this._autoBoundInputs === null) {
191
- this._autoBoundInputs = [];
192
- }
193
- this._autoBoundInputs.push(connectionPoint);
194
- } else {
195
- // If not auto bound, register as an input connection point
196
- if (connectionPoint.defaultValue !== undefined) {
197
- this._registerOptionalInput(
198
- connectionPoint.name,
199
- connectionPoint.type,
200
- createStrongRef(connectionPoint.defaultValue)
201
- );
202
- } else {
203
- this._registerInput(connectionPoint.name, connectionPoint.type);
204
- }
205
- }
206
- }
207
-
208
- /**
209
- * Gets the shader binding for the custom shader block.
210
- * @returns The shader binding for the custom shader block
211
- */
212
- public override getShaderBinding(): ShaderBinding {
213
- const inputs = this.inputs;
214
-
215
- const inputsToBind: AnyInputWithRuntimeData[] = inputs.map((input) => {
216
- return {
217
- name: input.name,
218
- type: input.type,
219
- runtimeData: this._confirmRuntimeDataSupplied(input),
220
- autoBind: undefined,
221
- };
222
- });
223
-
224
- if (this._autoBoundInputs) {
225
- for (const autoBoundInput of this._autoBoundInputs) {
226
- if (
227
- (autoBoundInput.autoBind === "outputResolution" &&
228
- autoBoundInput.type == ConnectionPointType.Vector2) ||
229
- (autoBoundInput.autoBind === "outputAspectRatio" &&
230
- autoBoundInput.type == ConnectionPointType.Vector2)
231
- ) {
232
- inputsToBind.push({
233
- name: autoBoundInput.name,
234
- type: autoBoundInput.type,
235
- autoBind: autoBoundInput.autoBind,
236
- });
237
- } else {
238
- throw new Error(
239
- `Auto bound input ${autoBoundInput.name} has an unsupported type or auto bind value`
240
- );
241
- }
242
- }
243
- }
244
-
245
- return new CustomShaderBlockBinding(inputsToBind);
246
- }
247
- }
248
-
249
- /**
250
- * Represents an input with its runtime data, enforcing type safety.
251
- */
252
- type InputWithRuntimeData<U extends ConnectionPointType> = {
253
- /**
254
- * The name of the input connection point
255
- */
256
- name: string;
257
-
258
- /**
259
- * The type of the input connection point
260
- */
261
- type: U;
262
-
263
- /**
264
- * The runtime data for the input connection point
265
- */
266
- runtimeData: RuntimeData<U>;
267
-
268
- /**
269
- * Since this is an input connection point, it will not be auto bound
270
- */
271
- autoBind: undefined;
272
- };
273
-
274
- /**
275
- * Represents an input which is auto-bound to the output resolution instead of an input connection point
276
- */
277
- type AutoBindOutputResolution = {
278
- /**
279
- * The name of the input
280
- */
281
- name: string;
282
-
283
- /**
284
- * The type of the input
285
- */
286
- type: ConnectionPointType.Vector2;
287
-
288
- /**
289
- * The auto bind value for the input connection point
290
- */
291
- autoBind: "outputResolution";
292
- };
293
-
294
- /**
295
- * Represents an input which is auto-bound to the output's aspect ratio instead of an input connection point.
296
- * The x value is the aspect ratio (width / height) and the y value is the inverse aspect ratio (height / width).
297
- */
298
- type AutoBindOutputAspectRatio = {
299
- /**
300
- * The name of the input
301
- */
302
- name: string;
303
-
304
- /**
305
- * The type of the input
306
- */
307
- type: ConnectionPointType.Vector2;
308
-
309
- /**
310
- * The auto bind value for the input connection point
311
- */
312
- autoBind: "outputAspectRatio";
313
- };
314
-
315
- /**
316
- * All possible input types with runtime data.
317
- */
318
- type AnyInputWithRuntimeData =
319
- | InputWithRuntimeData<ConnectionPointType.Boolean>
320
- | InputWithRuntimeData<ConnectionPointType.Color3>
321
- | InputWithRuntimeData<ConnectionPointType.Color4>
322
- | InputWithRuntimeData<ConnectionPointType.Float>
323
- | InputWithRuntimeData<ConnectionPointType.Texture>
324
- | InputWithRuntimeData<ConnectionPointType.Vector2>
325
- | AutoBindOutputResolution
326
- | AutoBindOutputAspectRatio;
1
+ import { Logger } from "core/Misc/logger.js";
2
+ import type { Effect } from "core/Materials/effect.js";
3
+ import { ConnectionPointType, type ConnectionPointValue } from "../connection/connectionPointType.js";
4
+ import { ShaderBinding } from "../runtime/shaderRuntime.js";
5
+ import { CreateStrongRef } from "../runtime/strongRef.js";
6
+ import type { SerializedShaderBlockDefinition } from "../serialization/serializedShaderBlockDefinition.js";
7
+ import type { SerializedInputConnectionPointV1 } from "../serialization/v1/shaderBlockSerialization.types.js";
8
+ import type { SmartFilter } from "../smartFilter.js";
9
+ import type { ShaderProgram } from "../utils/shaderCodeUtils.js";
10
+ import { ShaderBlock } from "./shaderBlock.js";
11
+ import type { RuntimeData } from "../connection/connectionPoint.js";
12
+ import type { Nullable } from "core/types.js";
13
+
14
+ /**
15
+ * The binding for a CustomShaderBlock
16
+ */
17
+ class CustomShaderBlockBinding extends ShaderBinding {
18
+ private readonly _bindSteps: ((effect: Effect, width: number, height: number) => void)[] = [];
19
+
20
+ /**
21
+ * Creates a new shader binding instance for the CustomShaderBlock block.
22
+ * @param inputsWithRuntimeData - The input connection points of the block
23
+ */
24
+ public constructor(inputsWithRuntimeData: AnyInputWithRuntimeData[]) {
25
+ super();
26
+
27
+ for (const input of inputsWithRuntimeData) {
28
+ switch (input.type) {
29
+ case ConnectionPointType.Float:
30
+ this._bindSteps.push((effect) => {
31
+ effect.setFloat(this.getRemappedName(input.name), input.runtimeData.value);
32
+ });
33
+ break;
34
+ case ConnectionPointType.Texture:
35
+ this._bindSteps.push((effect) => {
36
+ effect.setTexture(this.getRemappedName(input.name), input.runtimeData.value);
37
+ });
38
+ break;
39
+ case ConnectionPointType.Color3:
40
+ this._bindSteps.push((effect) => {
41
+ effect.setColor3(this.getRemappedName(input.name), input.runtimeData.value);
42
+ });
43
+ break;
44
+ case ConnectionPointType.Color4:
45
+ this._bindSteps.push((effect) => {
46
+ effect.setDirectColor4(this.getRemappedName(input.name), input.runtimeData.value);
47
+ });
48
+ break;
49
+ case ConnectionPointType.Boolean:
50
+ this._bindSteps.push((effect) => {
51
+ effect.setBool(this.getRemappedName(input.name), input.runtimeData.value);
52
+ });
53
+ break;
54
+ case ConnectionPointType.Vector2:
55
+ switch (input.autoBind) {
56
+ case "outputResolution":
57
+ this._bindSteps.push((effect, width, height) => {
58
+ effect.setFloat2(this.getRemappedName(input.name), width, height);
59
+ });
60
+ break;
61
+ case "outputAspectRatio":
62
+ this._bindSteps.push((effect, width, height) => {
63
+ effect.setFloat2(this.getRemappedName(input.name), width / height, height / width);
64
+ });
65
+ break;
66
+ default:
67
+ this._bindSteps.push((effect) => {
68
+ effect.setVector2(this.getRemappedName(input.name), input.runtimeData.value);
69
+ });
70
+ }
71
+ break;
72
+ }
73
+ }
74
+ }
75
+
76
+ /**
77
+ * Binds all the required data to the shader when rendering.
78
+ * @param effect - The effect to bind the data to
79
+ * @param width - defines the width of the output
80
+ * @param height - defines the height of the output
81
+ */
82
+ public override bind(effect: Effect, width: number, height: number): void {
83
+ for (let i = 0; i < this._bindSteps.length; i++) {
84
+ this._bindSteps[i]!(effect, width, height);
85
+ }
86
+ }
87
+ }
88
+
89
+ /**
90
+ * A block which loads a SerializedBlockDefinition for use in a SmartFilter.
91
+ */
92
+ export class CustomShaderBlock extends ShaderBlock {
93
+ /**
94
+ * Deserializes a CustomShaderBlock from a serialized block definition.
95
+ * @param smartFilter - The smart filter this block belongs to
96
+ * @param name - Defines the name of the block
97
+ * @param blockDefinition - The serialized block definition
98
+ * @returns The deserialized CustomShaderBlock instance
99
+ */
100
+ public static Create(smartFilter: SmartFilter, name: string, blockDefinition: SerializedShaderBlockDefinition): CustomShaderBlock {
101
+ // When a new version of SerializedBlockDefinition is created, this function should be updated to handle the new properties.
102
+
103
+ return new CustomShaderBlock(
104
+ smartFilter,
105
+ name,
106
+ blockDefinition.disableOptimization,
107
+ blockDefinition.blockType,
108
+ blockDefinition.namespace,
109
+ blockDefinition.inputConnectionPoints,
110
+ blockDefinition.shaderProgram
111
+ );
112
+ }
113
+
114
+ /**
115
+ * The class name of the block.
116
+ */
117
+ public static override ClassName = "CustomShaderBlock";
118
+
119
+ private readonly _shaderProgram: ShaderProgram;
120
+ private readonly _blockType: string;
121
+ private readonly _namespace: Nullable<string>;
122
+ private _autoBoundInputs: Nullable<SerializedInputConnectionPointV1[]> = null;
123
+
124
+ /**
125
+ * The type of the block - used when serializing / deserializing the block, and in the editor.
126
+ */
127
+ public override get blockType(): string {
128
+ return this._blockType;
129
+ }
130
+
131
+ /**
132
+ * The namespace of the block, which is used to reduce name collisions between blocks and also to group blocks in the editor UI.
133
+ * By convention, sub namespaces are separated by a period (e.g. "Babylon.Demo.Effects").
134
+ */
135
+ public override get namespace(): Nullable<string> {
136
+ return this._namespace;
137
+ }
138
+
139
+ /**
140
+ * Instantiates a new custom shader block.
141
+ * @param smartFilter - The smart filter this block belongs to
142
+ * @param name - The name of the block
143
+ * @param disableOptimization - If true, this optimizer will not attempt to optimize this block
144
+ * @param blockType - The type of the block
145
+ * @param namespace - The namespace of the block
146
+ * @param inputConnectionPoints - The input connection points of the
147
+ * @param shaderProgram - The shader program for the block
148
+ */
149
+ private constructor(
150
+ smartFilter: SmartFilter,
151
+ name: string,
152
+ disableOptimization: boolean,
153
+ blockType: string,
154
+ namespace: Nullable<string>,
155
+ inputConnectionPoints: SerializedInputConnectionPointV1[],
156
+ shaderProgram: ShaderProgram
157
+ ) {
158
+ super(smartFilter, name, disableOptimization);
159
+ this._blockType = blockType;
160
+ this._namespace = namespace;
161
+
162
+ for (const input of inputConnectionPoints) {
163
+ this._registerSerializedInputConnectionPointV1(input);
164
+ }
165
+
166
+ this._shaderProgram = shaderProgram;
167
+ }
168
+
169
+ /**
170
+ * Gets the shader program to use to render the block.
171
+ * @returns The shader program to use to render the block
172
+ */
173
+ public override getShaderProgram() {
174
+ return this._shaderProgram;
175
+ }
176
+
177
+ /**
178
+ * Checks a specific input connection point type to see if it has a default value, and registers the input
179
+ * connection point accordingly.
180
+ * @param connectionPoint - The input connection point to register
181
+ */
182
+ private _registerSerializedInputConnectionPointV1<U extends ConnectionPointType>(connectionPoint: SerializedInputConnectionPointV1<U>): void {
183
+ if (connectionPoint.autoBind) {
184
+ // Auto bound inputs are not registered as input connection points
185
+ if (this._autoBoundInputs === null) {
186
+ this._autoBoundInputs = [];
187
+ }
188
+ this._autoBoundInputs.push(connectionPoint);
189
+ } else {
190
+ // If not auto bound, register as an input connection point
191
+ const defaultValue = this._validateDefaultValue(connectionPoint.type, connectionPoint.name, connectionPoint.defaultValue);
192
+ if (defaultValue) {
193
+ this._registerOptionalInput(connectionPoint.name, connectionPoint.type, CreateStrongRef(defaultValue));
194
+ } else {
195
+ this._registerInput(connectionPoint.name, connectionPoint.type);
196
+ }
197
+ }
198
+ }
199
+
200
+ /**
201
+ * Gets the shader binding for the custom shader block.
202
+ * @returns The shader binding for the custom shader block
203
+ */
204
+ public override getShaderBinding(): ShaderBinding {
205
+ const inputs = this.inputs;
206
+
207
+ const inputsToBind: AnyInputWithRuntimeData[] = inputs.map((input) => {
208
+ return {
209
+ name: input.name,
210
+ type: input.type,
211
+ runtimeData: this._confirmRuntimeDataSupplied(input),
212
+ autoBind: undefined,
213
+ };
214
+ });
215
+
216
+ if (this._autoBoundInputs) {
217
+ for (const autoBoundInput of this._autoBoundInputs) {
218
+ if (
219
+ (autoBoundInput.autoBind === "outputResolution" && autoBoundInput.type == ConnectionPointType.Vector2) ||
220
+ (autoBoundInput.autoBind === "outputAspectRatio" && autoBoundInput.type == ConnectionPointType.Vector2)
221
+ ) {
222
+ inputsToBind.push({
223
+ name: autoBoundInput.name,
224
+ type: autoBoundInput.type,
225
+ autoBind: autoBoundInput.autoBind,
226
+ });
227
+ } else {
228
+ throw new Error(`Auto bound input ${autoBoundInput.name} has an unsupported type or auto bind value`);
229
+ }
230
+ }
231
+ }
232
+
233
+ return new CustomShaderBlockBinding(inputsToBind);
234
+ }
235
+
236
+ /**
237
+ * Validates the default value of a connection point and returns it if valid.
238
+ * If the default value is not provided or is invalid, this returns null.
239
+ * @param connectionPointType - The type of the connection point
240
+ * @param connectionPointName - The name of the connection point
241
+ * @param defaultValue - The default value of the connection point
242
+ * @returns The default value, or null if no default value is provided or it was invalid
243
+ */
244
+ private _validateDefaultValue<U extends ConnectionPointType>(
245
+ connectionPointType: U,
246
+ connectionPointName: string,
247
+ defaultValue?: ConnectionPointValue<U>
248
+ ): Nullable<ConnectionPointValue<U>> {
249
+ if (defaultValue === undefined || defaultValue === null) {
250
+ return null;
251
+ }
252
+
253
+ // Validate the default value based on the connection point type
254
+ let returnValue: Nullable<ConnectionPointValue<U>> = null;
255
+ switch (connectionPointType) {
256
+ case ConnectionPointType.Float:
257
+ returnValue = typeof defaultValue === "number" ? defaultValue : null;
258
+ break;
259
+ case ConnectionPointType.Color3:
260
+ returnValue = typeof defaultValue === "object" && "r" in defaultValue && "g" in defaultValue && "b" in defaultValue ? defaultValue : null;
261
+ break;
262
+ case ConnectionPointType.Color4:
263
+ returnValue = typeof defaultValue === "object" && "r" in defaultValue && "g" in defaultValue && "b" in defaultValue && "a" in defaultValue ? defaultValue : null;
264
+ break;
265
+ case ConnectionPointType.Boolean:
266
+ returnValue = typeof defaultValue === "boolean" ? defaultValue : null;
267
+ break;
268
+ case ConnectionPointType.Vector2:
269
+ returnValue = typeof defaultValue === "object" && "x" in defaultValue && "y" in defaultValue ? defaultValue : null;
270
+ break;
271
+ default: {
272
+ Logger.Warn(`Default value supplied when unsupported. Block Type: "${this.blockType}" Connection Point: "${connectionPointName}"`);
273
+ return null;
274
+ }
275
+ }
276
+
277
+ if (returnValue === null) {
278
+ Logger.Warn(`Invalid default value. Block Type: "${this.blockType}" Connection Point: "${connectionPointName}"`);
279
+ }
280
+
281
+ return returnValue;
282
+ }
283
+ }
284
+
285
+ /**
286
+ * Represents an input with its runtime data, enforcing type safety.
287
+ */
288
+ type InputWithRuntimeData<U extends ConnectionPointType> = {
289
+ /**
290
+ * The name of the input connection point
291
+ */
292
+ name: string;
293
+
294
+ /**
295
+ * The type of the input connection point
296
+ */
297
+ type: U;
298
+
299
+ /**
300
+ * The runtime data for the input connection point
301
+ */
302
+ runtimeData: RuntimeData<U>;
303
+
304
+ /**
305
+ * Since this is an input connection point, it will not be auto bound
306
+ */
307
+ autoBind: undefined;
308
+ };
309
+
310
+ /**
311
+ * Represents an input which is auto-bound to the output resolution instead of an input connection point
312
+ */
313
+ type AutoBindOutputResolution = {
314
+ /**
315
+ * The name of the input
316
+ */
317
+ name: string;
318
+
319
+ /**
320
+ * The type of the input
321
+ */
322
+ type: ConnectionPointType.Vector2;
323
+
324
+ /**
325
+ * The auto bind value for the input connection point
326
+ */
327
+ autoBind: "outputResolution";
328
+ };
329
+
330
+ /**
331
+ * Represents an input which is auto-bound to the output's aspect ratio instead of an input connection point.
332
+ * The x value is the aspect ratio (width / height) and the y value is the inverse aspect ratio (height / width).
333
+ */
334
+ type AutoBindOutputAspectRatio = {
335
+ /**
336
+ * The name of the input
337
+ */
338
+ name: string;
339
+
340
+ /**
341
+ * The type of the input
342
+ */
343
+ type: ConnectionPointType.Vector2;
344
+
345
+ /**
346
+ * The auto bind value for the input connection point
347
+ */
348
+ autoBind: "outputAspectRatio";
349
+ };
350
+
351
+ /**
352
+ * All possible input types with runtime data.
353
+ */
354
+ type AnyInputWithRuntimeData =
355
+ | InputWithRuntimeData<ConnectionPointType.Boolean>
356
+ | InputWithRuntimeData<ConnectionPointType.Color3>
357
+ | InputWithRuntimeData<ConnectionPointType.Color4>
358
+ | InputWithRuntimeData<ConnectionPointType.Float>
359
+ | InputWithRuntimeData<ConnectionPointType.Texture>
360
+ | InputWithRuntimeData<ConnectionPointType.Vector2>
361
+ | AutoBindOutputResolution
362
+ | AutoBindOutputAspectRatio;