@babylonjs/smart-filters-blocks 0.8.1

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 (190) hide show
  1. package/dist/blocks/babylon/demo/effects/blackAndWhiteBlock.block.d.ts +34 -0
  2. package/dist/blocks/babylon/demo/effects/blackAndWhiteBlock.block.d.ts.map +1 -0
  3. package/dist/blocks/babylon/demo/effects/blackAndWhiteBlock.block.js +101 -0
  4. package/dist/blocks/babylon/demo/effects/blackAndWhiteBlock.block.js.map +1 -0
  5. package/dist/blocks/babylon/demo/effects/blurBlock.d.ts +53 -0
  6. package/dist/blocks/babylon/demo/effects/blurBlock.d.ts.map +1 -0
  7. package/dist/blocks/babylon/demo/effects/blurBlock.deserializer.d.ts +28 -0
  8. package/dist/blocks/babylon/demo/effects/blurBlock.deserializer.d.ts.map +1 -0
  9. package/dist/blocks/babylon/demo/effects/blurBlock.deserializer.js +17 -0
  10. package/dist/blocks/babylon/demo/effects/blurBlock.deserializer.js.map +1 -0
  11. package/dist/blocks/babylon/demo/effects/blurBlock.js +98 -0
  12. package/dist/blocks/babylon/demo/effects/blurBlock.js.map +1 -0
  13. package/dist/blocks/babylon/demo/effects/blurBlock.serializer.d.ts +8 -0
  14. package/dist/blocks/babylon/demo/effects/blurBlock.serializer.d.ts.map +1 -0
  15. package/dist/blocks/babylon/demo/effects/blurBlock.serializer.js +28 -0
  16. package/dist/blocks/babylon/demo/effects/blurBlock.serializer.js.map +1 -0
  17. package/dist/blocks/babylon/demo/effects/compositionBlock.d.ts +116 -0
  18. package/dist/blocks/babylon/demo/effects/compositionBlock.d.ts.map +1 -0
  19. package/dist/blocks/babylon/demo/effects/compositionBlock.deserializer.d.ts +24 -0
  20. package/dist/blocks/babylon/demo/effects/compositionBlock.deserializer.d.ts.map +1 -0
  21. package/dist/blocks/babylon/demo/effects/compositionBlock.deserializer.js +15 -0
  22. package/dist/blocks/babylon/demo/effects/compositionBlock.deserializer.js.map +1 -0
  23. package/dist/blocks/babylon/demo/effects/compositionBlock.fragment.d.ts +18 -0
  24. package/dist/blocks/babylon/demo/effects/compositionBlock.fragment.d.ts.map +1 -0
  25. package/dist/blocks/babylon/demo/effects/compositionBlock.fragment.js +69 -0
  26. package/dist/blocks/babylon/demo/effects/compositionBlock.fragment.js.map +1 -0
  27. package/dist/blocks/babylon/demo/effects/compositionBlock.js +167 -0
  28. package/dist/blocks/babylon/demo/effects/compositionBlock.js.map +1 -0
  29. package/dist/blocks/babylon/demo/effects/compositionBlock.serializer.d.ts +6 -0
  30. package/dist/blocks/babylon/demo/effects/compositionBlock.serializer.d.ts.map +1 -0
  31. package/dist/blocks/babylon/demo/effects/compositionBlock.serializer.js +25 -0
  32. package/dist/blocks/babylon/demo/effects/compositionBlock.serializer.js.map +1 -0
  33. package/dist/blocks/babylon/demo/effects/contrastBlock.block.d.ts +39 -0
  34. package/dist/blocks/babylon/demo/effects/contrastBlock.block.d.ts.map +1 -0
  35. package/dist/blocks/babylon/demo/effects/contrastBlock.block.js +140 -0
  36. package/dist/blocks/babylon/demo/effects/contrastBlock.block.js.map +1 -0
  37. package/dist/blocks/babylon/demo/effects/desaturateBlock.block.d.ts +39 -0
  38. package/dist/blocks/babylon/demo/effects/desaturateBlock.block.d.ts.map +1 -0
  39. package/dist/blocks/babylon/demo/effects/desaturateBlock.block.js +115 -0
  40. package/dist/blocks/babylon/demo/effects/desaturateBlock.block.js.map +1 -0
  41. package/dist/blocks/babylon/demo/effects/directionalBlurBlock.d.ts +74 -0
  42. package/dist/blocks/babylon/demo/effects/directionalBlurBlock.d.ts.map +1 -0
  43. package/dist/blocks/babylon/demo/effects/directionalBlurBlock.deserializer.d.ts +32 -0
  44. package/dist/blocks/babylon/demo/effects/directionalBlurBlock.deserializer.d.ts.map +1 -0
  45. package/dist/blocks/babylon/demo/effects/directionalBlurBlock.deserializer.js +17 -0
  46. package/dist/blocks/babylon/demo/effects/directionalBlurBlock.deserializer.js.map +1 -0
  47. package/dist/blocks/babylon/demo/effects/directionalBlurBlock.js +167 -0
  48. package/dist/blocks/babylon/demo/effects/directionalBlurBlock.js.map +1 -0
  49. package/dist/blocks/babylon/demo/effects/directionalBlurBlock.serializer.d.ts +6 -0
  50. package/dist/blocks/babylon/demo/effects/directionalBlurBlock.serializer.d.ts.map +1 -0
  51. package/dist/blocks/babylon/demo/effects/directionalBlurBlock.serializer.js +27 -0
  52. package/dist/blocks/babylon/demo/effects/directionalBlurBlock.serializer.js.map +1 -0
  53. package/dist/blocks/babylon/demo/effects/exposureBlock.block.d.ts +38 -0
  54. package/dist/blocks/babylon/demo/effects/exposureBlock.block.d.ts.map +1 -0
  55. package/dist/blocks/babylon/demo/effects/exposureBlock.block.js +107 -0
  56. package/dist/blocks/babylon/demo/effects/exposureBlock.block.js.map +1 -0
  57. package/dist/blocks/babylon/demo/effects/greenScreenBlock.block.d.ts +46 -0
  58. package/dist/blocks/babylon/demo/effects/greenScreenBlock.block.d.ts.map +1 -0
  59. package/dist/blocks/babylon/demo/effects/greenScreenBlock.block.js +133 -0
  60. package/dist/blocks/babylon/demo/effects/greenScreenBlock.block.js.map +1 -0
  61. package/dist/blocks/babylon/demo/effects/index.d.ts +15 -0
  62. package/dist/blocks/babylon/demo/effects/index.d.ts.map +1 -0
  63. package/dist/blocks/babylon/demo/effects/index.js +15 -0
  64. package/dist/blocks/babylon/demo/effects/index.js.map +1 -0
  65. package/dist/blocks/babylon/demo/effects/kaleidoscopeBlock.d.ts +58 -0
  66. package/dist/blocks/babylon/demo/effects/kaleidoscopeBlock.d.ts.map +1 -0
  67. package/dist/blocks/babylon/demo/effects/kaleidoscopeBlock.js +158 -0
  68. package/dist/blocks/babylon/demo/effects/kaleidoscopeBlock.js.map +1 -0
  69. package/dist/blocks/babylon/demo/effects/maskBlock.block.d.ts +38 -0
  70. package/dist/blocks/babylon/demo/effects/maskBlock.block.d.ts.map +1 -0
  71. package/dist/blocks/babylon/demo/effects/maskBlock.block.js +110 -0
  72. package/dist/blocks/babylon/demo/effects/maskBlock.block.js.map +1 -0
  73. package/dist/blocks/babylon/demo/effects/pixelateBlock.block.d.ts +44 -0
  74. package/dist/blocks/babylon/demo/effects/pixelateBlock.block.d.ts.map +1 -0
  75. package/dist/blocks/babylon/demo/effects/pixelateBlock.block.js +125 -0
  76. package/dist/blocks/babylon/demo/effects/pixelateBlock.block.js.map +1 -0
  77. package/dist/blocks/babylon/demo/effects/posterizeBlock.block.d.ts +39 -0
  78. package/dist/blocks/babylon/demo/effects/posterizeBlock.block.d.ts.map +1 -0
  79. package/dist/blocks/babylon/demo/effects/posterizeBlock.block.js +116 -0
  80. package/dist/blocks/babylon/demo/effects/posterizeBlock.block.js.map +1 -0
  81. package/dist/blocks/babylon/demo/effects/spritesheetBlock.d.ts +77 -0
  82. package/dist/blocks/babylon/demo/effects/spritesheetBlock.d.ts.map +1 -0
  83. package/dist/blocks/babylon/demo/effects/spritesheetBlock.fragment.d.ts +18 -0
  84. package/dist/blocks/babylon/demo/effects/spritesheetBlock.fragment.d.ts.map +1 -0
  85. package/dist/blocks/babylon/demo/effects/spritesheetBlock.fragment.js +58 -0
  86. package/dist/blocks/babylon/demo/effects/spritesheetBlock.fragment.js.map +1 -0
  87. package/dist/blocks/babylon/demo/effects/spritesheetBlock.js +98 -0
  88. package/dist/blocks/babylon/demo/effects/spritesheetBlock.js.map +1 -0
  89. package/dist/blocks/babylon/demo/effects/tintBlock.d.ts +7 -0
  90. package/dist/blocks/babylon/demo/effects/tintBlock.d.ts.map +1 -0
  91. package/dist/blocks/babylon/demo/effects/tintBlock.js +51 -0
  92. package/dist/blocks/babylon/demo/effects/tintBlock.js.map +1 -0
  93. package/dist/blocks/babylon/demo/transitions/index.d.ts +2 -0
  94. package/dist/blocks/babylon/demo/transitions/index.d.ts.map +1 -0
  95. package/dist/blocks/babylon/demo/transitions/index.js +2 -0
  96. package/dist/blocks/babylon/demo/transitions/index.js.map +1 -0
  97. package/dist/blocks/babylon/demo/transitions/wipeBlock.block.d.ts +42 -0
  98. package/dist/blocks/babylon/demo/transitions/wipeBlock.block.d.ts.map +1 -0
  99. package/dist/blocks/babylon/demo/transitions/wipeBlock.block.js +116 -0
  100. package/dist/blocks/babylon/demo/transitions/wipeBlock.block.js.map +1 -0
  101. package/dist/blocks/babylon/demo/utilities/index.d.ts +2 -0
  102. package/dist/blocks/babylon/demo/utilities/index.d.ts.map +1 -0
  103. package/dist/blocks/babylon/demo/utilities/index.js +2 -0
  104. package/dist/blocks/babylon/demo/utilities/index.js.map +1 -0
  105. package/dist/blocks/babylon/demo/utilities/premultiplyAlphaBlock.block.d.ts +34 -0
  106. package/dist/blocks/babylon/demo/utilities/premultiplyAlphaBlock.block.d.ts.map +1 -0
  107. package/dist/blocks/babylon/demo/utilities/premultiplyAlphaBlock.block.js +97 -0
  108. package/dist/blocks/babylon/demo/utilities/premultiplyAlphaBlock.block.js.map +1 -0
  109. package/dist/blocks/blockNamespaces.d.ts +5 -0
  110. package/dist/blocks/blockNamespaces.d.ts.map +1 -0
  111. package/dist/blocks/blockNamespaces.js +5 -0
  112. package/dist/blocks/blockNamespaces.js.map +1 -0
  113. package/dist/blocks/blockTypes.d.ts +22 -0
  114. package/dist/blocks/blockTypes.d.ts.map +1 -0
  115. package/dist/blocks/blockTypes.js +22 -0
  116. package/dist/blocks/blockTypes.js.map +1 -0
  117. package/dist/blocks/index.d.ts +6 -0
  118. package/dist/blocks/index.d.ts.map +1 -0
  119. package/dist/blocks/index.js +6 -0
  120. package/dist/blocks/index.js.map +1 -0
  121. package/dist/index.d.ts +3 -0
  122. package/dist/index.d.ts.map +1 -0
  123. package/dist/index.js +3 -0
  124. package/dist/index.js.map +1 -0
  125. package/dist/registration/IBlockRegistration.d.ts +34 -0
  126. package/dist/registration/IBlockRegistration.d.ts.map +1 -0
  127. package/dist/registration/IBlockRegistration.js +2 -0
  128. package/dist/registration/IBlockRegistration.js.map +1 -0
  129. package/dist/registration/blockSerializers.d.ts +13 -0
  130. package/dist/registration/blockSerializers.d.ts.map +1 -0
  131. package/dist/registration/blockSerializers.js +35 -0
  132. package/dist/registration/blockSerializers.js.map +1 -0
  133. package/dist/registration/builtInBlockRegistrations.d.ts +13 -0
  134. package/dist/registration/builtInBlockRegistrations.d.ts.map +1 -0
  135. package/dist/registration/builtInBlockRegistrations.js +269 -0
  136. package/dist/registration/builtInBlockRegistrations.js.map +1 -0
  137. package/dist/registration/index.d.ts +3 -0
  138. package/dist/registration/index.d.ts.map +1 -0
  139. package/dist/registration/index.js +3 -0
  140. package/dist/registration/index.js.map +1 -0
  141. package/license.md +21 -0
  142. package/package.json +52 -0
  143. package/readme.md +7 -0
  144. package/src/blocks/babylon/demo/effects/blackAndWhiteBlock.block.glsl +18 -0
  145. package/src/blocks/babylon/demo/effects/blackAndWhiteBlock.block.ts +133 -0
  146. package/src/blocks/babylon/demo/effects/blurBlock.deserializer.ts +37 -0
  147. package/src/blocks/babylon/demo/effects/blurBlock.serializer.ts +31 -0
  148. package/src/blocks/babylon/demo/effects/blurBlock.ts +122 -0
  149. package/src/blocks/babylon/demo/effects/compositionBlock.deserializer.ts +31 -0
  150. package/src/blocks/babylon/demo/effects/compositionBlock.fragment.glsl +38 -0
  151. package/src/blocks/babylon/demo/effects/compositionBlock.fragment.ts +71 -0
  152. package/src/blocks/babylon/demo/effects/compositionBlock.serializer.ts +28 -0
  153. package/src/blocks/babylon/demo/effects/compositionBlock.ts +243 -0
  154. package/src/blocks/babylon/demo/effects/contrastBlock.block.glsl +36 -0
  155. package/src/blocks/babylon/demo/effects/contrastBlock.block.ts +180 -0
  156. package/src/blocks/babylon/demo/effects/desaturateBlock.block.glsl +24 -0
  157. package/src/blocks/babylon/demo/effects/desaturateBlock.block.ts +155 -0
  158. package/src/blocks/babylon/demo/effects/directionalBlurBlock.deserializer.ts +46 -0
  159. package/src/blocks/babylon/demo/effects/directionalBlurBlock.serializer.ts +30 -0
  160. package/src/blocks/babylon/demo/effects/directionalBlurBlock.ts +196 -0
  161. package/src/blocks/babylon/demo/effects/exposureBlock.block.glsl +15 -0
  162. package/src/blocks/babylon/demo/effects/exposureBlock.block.ts +142 -0
  163. package/src/blocks/babylon/demo/effects/greenScreenBlock.block.glsl +23 -0
  164. package/src/blocks/babylon/demo/effects/greenScreenBlock.block.ts +174 -0
  165. package/src/blocks/babylon/demo/effects/index.ts +14 -0
  166. package/src/blocks/babylon/demo/effects/kaleidoscopeBlock.ts +192 -0
  167. package/src/blocks/babylon/demo/effects/maskBlock.block.glsl +18 -0
  168. package/src/blocks/babylon/demo/effects/maskBlock.block.ts +145 -0
  169. package/src/blocks/babylon/demo/effects/pixelateBlock.block.glsl +28 -0
  170. package/src/blocks/babylon/demo/effects/pixelateBlock.block.ts +169 -0
  171. package/src/blocks/babylon/demo/effects/posterizeBlock.block.glsl +25 -0
  172. package/src/blocks/babylon/demo/effects/posterizeBlock.block.ts +156 -0
  173. package/src/blocks/babylon/demo/effects/spritesheetBlock.fragment.glsl +26 -0
  174. package/src/blocks/babylon/demo/effects/spritesheetBlock.fragment.ts +60 -0
  175. package/src/blocks/babylon/demo/effects/spritesheetBlock.ts +138 -0
  176. package/src/blocks/babylon/demo/effects/tintBlock.ts +51 -0
  177. package/src/blocks/babylon/demo/transitions/index.ts +1 -0
  178. package/src/blocks/babylon/demo/transitions/wipeBlock.block.glsl +11 -0
  179. package/src/blocks/babylon/demo/transitions/wipeBlock.block.ts +152 -0
  180. package/src/blocks/babylon/demo/utilities/index.ts +1 -0
  181. package/src/blocks/babylon/demo/utilities/premultiplyAlphaBlock.block.glsl +14 -0
  182. package/src/blocks/babylon/demo/utilities/premultiplyAlphaBlock.block.ts +129 -0
  183. package/src/blocks/blockNamespaces.ts +4 -0
  184. package/src/blocks/blockTypes.ts +21 -0
  185. package/src/blocks/index.ts +5 -0
  186. package/src/index.ts +2 -0
  187. package/src/registration/IBlockRegistration.ts +43 -0
  188. package/src/registration/blockSerializers.ts +52 -0
  189. package/src/registration/builtInBlockRegistrations.ts +397 -0
  190. package/src/registration/index.ts +2 -0
@@ -0,0 +1,122 @@
1
+ import {
2
+ type ConnectionPointType,
3
+ AggregateBlock,
4
+ type ConnectionPoint,
5
+ editableInPropertyPage,
6
+ PropertyTypeForEdition,
7
+ SmartFilter,
8
+ } from "@babylonjs/smart-filters";
9
+ import { DirectionalBlurBlock } from "./directionalBlurBlock.js";
10
+ import { blurBlockType } from "../../../blockTypes.js";
11
+ import { babylonDemoEffectsNamespace } from "../../../blockNamespaces.js";
12
+
13
+ const defaultBlurTextureRatioPerPass = 0.5;
14
+ const defaultBlurSize = 2;
15
+
16
+ /**
17
+ * A block performing a blur on the input texture.
18
+ *
19
+ * It performs the blur in 4 consecutive passes, 2 verticals and 2 horizontals downsizing the texture as we go.
20
+ */
21
+ export class BlurBlock extends AggregateBlock {
22
+ /**
23
+ * The class name of the block.
24
+ */
25
+ public static override ClassName = blurBlockType;
26
+
27
+ /**
28
+ * The namespace of the block.
29
+ */
30
+ public static override Namespace = babylonDemoEffectsNamespace;
31
+
32
+ /**
33
+ * The input texture connection point.
34
+ */
35
+ public readonly input: ConnectionPoint<ConnectionPointType.Texture>;
36
+
37
+ /**
38
+ * The output blurred texture connection point.
39
+ */
40
+ public readonly output: ConnectionPoint<ConnectionPointType.Texture>;
41
+
42
+ private _blurTextureRatioPerPass = defaultBlurTextureRatioPerPass;
43
+ private _blurSize = defaultBlurSize;
44
+
45
+ private readonly _intermediateBlurV: DirectionalBlurBlock;
46
+ private readonly _intermediateBlurH: DirectionalBlurBlock;
47
+ private readonly _finalBlurV: DirectionalBlurBlock;
48
+ private readonly _finalBlurH: DirectionalBlurBlock;
49
+
50
+ /**
51
+ * Gets how much smaller we should make the texture between the 2 consecutive bi lateral passes.
52
+ */
53
+ public get blurTextureRatioPerPass(): number {
54
+ return this._blurTextureRatioPerPass;
55
+ }
56
+
57
+ /**
58
+ * Sets how much smaller we should make the texture between the 2 consecutive bi lateral passes.
59
+ */
60
+ @editableInPropertyPage("Pass Texture Ratio", PropertyTypeForEdition.Float, "PROPERTIES", {
61
+ min: 0,
62
+ max: 1,
63
+ notifiers: { rebuild: true },
64
+ })
65
+ public set blurTextureRatioPerPass(value: number) {
66
+ this._blurTextureRatioPerPass = value;
67
+ this._intermediateBlurV.blurTextureRatio = value;
68
+ this._intermediateBlurH.blurTextureRatio = value;
69
+ this._finalBlurV.blurTextureRatio = value * value;
70
+ this._finalBlurH.blurTextureRatio = value * value;
71
+ }
72
+
73
+ /**
74
+ * Gets how far the kernel might fetch the data from.
75
+ */
76
+ public get blurSize(): number {
77
+ return this._blurSize;
78
+ }
79
+
80
+ /**
81
+ * Sets how far the kernel might fetch the data from.
82
+ */
83
+ @editableInPropertyPage("Size", PropertyTypeForEdition.Float, "PROPERTIES", {
84
+ notifiers: { rebuild: true },
85
+ })
86
+ public set blurSize(value: number) {
87
+ this._blurSize = value;
88
+ this._intermediateBlurV.blurHorizontalWidth = value;
89
+ this._intermediateBlurH.blurVerticalWidth = value;
90
+ this._finalBlurV.blurHorizontalWidth = value;
91
+ this._finalBlurH.blurVerticalWidth = value;
92
+ }
93
+
94
+ /**
95
+ * Instantiates a new Block.
96
+ * @param smartFilter - The smart filter this block belongs to
97
+ * @param name - The friendly name of the block
98
+ */
99
+ constructor(smartFilter: SmartFilter, name: string) {
100
+ super(smartFilter, name);
101
+
102
+ const internalFilter = new SmartFilter(name + "_BlurBlock_Aggregated");
103
+ this._intermediateBlurV = new DirectionalBlurBlock(internalFilter, name + "IV");
104
+ this._intermediateBlurH = new DirectionalBlurBlock(internalFilter, name + "IH");
105
+ this._finalBlurV = new DirectionalBlurBlock(internalFilter, name + "V");
106
+ this._finalBlurH = new DirectionalBlurBlock(internalFilter, name + "H");
107
+
108
+ this._intermediateBlurV.output.connectTo(this._intermediateBlurH.input);
109
+ this._intermediateBlurH.output.connectTo(this._finalBlurV.input);
110
+ this._finalBlurV.output.connectTo(this._finalBlurH.input);
111
+
112
+ this.input = this._registerSubfilterInput("input", [this._intermediateBlurV.input]);
113
+ this.output = this._registerSubfilterOutput("output", this._finalBlurH.output);
114
+
115
+ this.blurSize = defaultBlurSize;
116
+ this.blurTextureRatioPerPass = defaultBlurTextureRatioPerPass;
117
+ this._intermediateBlurV.blurVerticalWidth = 0;
118
+ this._intermediateBlurH.blurHorizontalWidth = 0;
119
+ this._finalBlurV.blurVerticalWidth = 0;
120
+ this._finalBlurH.blurHorizontalWidth = 0;
121
+ }
122
+ }
@@ -0,0 +1,31 @@
1
+ import type { ISerializedBlockV1, SmartFilter } from "@babylonjs/smart-filters";
2
+ import { CompositionBlock } from "./compositionBlock.js";
3
+
4
+ /**
5
+ * The definition of the extra data serialized for composition blocks.
6
+ */
7
+ export interface ISerializedCompositionBlockV1 extends ISerializedBlockV1 {
8
+ /**
9
+ * The extra data of the block.
10
+ */
11
+ data: {
12
+ /**
13
+ * The alpha mode of the composition.
14
+ */
15
+ alphaMode: number;
16
+ };
17
+ }
18
+
19
+ /**
20
+ * V1 Composition Deserializer.
21
+ * @param smartFilter - The SmartFilter to deserialize the block into
22
+ * @param serializedBlock - The serialized block data
23
+ * @returns A deserialized CompositionBlock
24
+ */
25
+ export function compositionDeserializer(smartFilter: SmartFilter, serializedBlock: ISerializedCompositionBlockV1) {
26
+ const block = new CompositionBlock(smartFilter, serializedBlock.name);
27
+
28
+ // If data is missing, use the default value set by the block
29
+ block.alphaMode = serializedBlock.data.alphaMode ?? block.alphaMode;
30
+ return block;
31
+ }
@@ -0,0 +1,38 @@
1
+ uniform sampler2D background; // main
2
+ uniform sampler2D foreground;
3
+
4
+ uniform vec2 scaleUV;
5
+ uniform vec2 translateUV;
6
+ uniform float alphaMode;
7
+ uniform float foregroundAlphaScale;
8
+
9
+ vec4 composition(vec2 vUV) { // main
10
+ vec4 background = texture2D(background, vUV);
11
+
12
+ vec2 transformedUV = vUV * (1.0 / scaleUV) + translateUV;
13
+ if (transformedUV.x < 0.0 || transformedUV.x > 1.0 || transformedUV.y < 0.0 || transformedUV.y > 1.0) {
14
+ return background;
15
+ }
16
+
17
+ vec4 foreground = texture2D(foreground, transformedUV);
18
+ foreground.a *= foregroundAlphaScale;
19
+
20
+ // SRC is foreground, DEST is background
21
+ if (alphaMode == 0.) {
22
+ return foreground;
23
+ }
24
+ else if (alphaMode == 1.) {
25
+ return foreground.a * foreground + background;
26
+ }
27
+ else if (alphaMode == 2.) {
28
+ return mix(background, foreground, foreground.a);
29
+ }
30
+ else if (alphaMode == 3.) {
31
+ return background - foreground * background;
32
+ }
33
+ else if (alphaMode == 4.) {
34
+ return foreground * background;
35
+ }
36
+
37
+ return background;
38
+ }
@@ -0,0 +1,71 @@
1
+ import type { ShaderProgram } from "@babylonjs/smart-filters";
2
+
3
+ /**
4
+ * The shader program for the block.
5
+ */
6
+ export const shaderProgram: ShaderProgram = {
7
+ vertex: undefined,
8
+ fragment: {
9
+ uniform: `
10
+ uniform sampler2D _background_; // main
11
+ uniform sampler2D _foreground_;
12
+ uniform vec2 _scaleUV_;
13
+ uniform vec2 _translateUV_;
14
+ uniform float _alphaMode_;
15
+ uniform float _foregroundAlphaScale_;`,
16
+ mainInputTexture: "_background_",
17
+ mainFunctionName: "_composition_",
18
+ functions: [
19
+ {
20
+ name: "_composition_",
21
+ code: `
22
+ vec4 _composition_(vec2 vUV) {
23
+ vec4 _background_ = texture2D(_background_, vUV);
24
+
25
+ vec2 transformedUV = vUV * (1.0 / _scaleUV_) + _translateUV_;
26
+ if (transformedUV.x < 0.0 || transformedUV.x > 1.0 || transformedUV.y < 0.0 || transformedUV.y > 1.0) {
27
+ return _background_;
28
+ }
29
+
30
+ vec4 _foreground_ = texture2D(_foreground_, transformedUV);
31
+ _foreground_.a *= _foregroundAlphaScale_;
32
+
33
+ // SRC is _foreground_, DEST is _background_
34
+ if (_alphaMode_ == 0.) {
35
+ return _foreground_;
36
+ }
37
+ else if (_alphaMode_ == 1.) {
38
+ return _foreground_.a * _foreground_ + _background_;
39
+ }
40
+ else if (_alphaMode_ == 2.) {
41
+ return mix(_background_, _foreground_, _foreground_.a);
42
+ }
43
+ else if (_alphaMode_ == 3.) {
44
+ return _background_ - _foreground_ * _background_;
45
+ }
46
+ else if (_alphaMode_ == 4.) {
47
+ return _foreground_ * _background_;
48
+ }
49
+
50
+ return _background_;
51
+ }
52
+
53
+ `,
54
+ params: "vec2 vUV",
55
+ },
56
+ ],
57
+ },
58
+ };
59
+
60
+ /**
61
+ * The uniform names for this shader, to be used in the shader binding so
62
+ * that the names are always in sync.
63
+ */
64
+ export const uniforms = {
65
+ background: "background",
66
+ foreground: "foreground",
67
+ scaleUV: "scaleUV",
68
+ translateUV: "translateUV",
69
+ alphaMode: "alphaMode",
70
+ foregroundAlphaScale: "foregroundAlphaScale",
71
+ };
@@ -0,0 +1,28 @@
1
+ import type { CompositionBlock } from "./compositionBlock";
2
+ import { compositionBlockType } from "../../../blockTypes.js";
3
+ import { babylonDemoEffectsNamespace } from "../../../blockNamespaces.js";
4
+ import type { IBlockSerializerV1, BaseBlock } from "@babylonjs/smart-filters";
5
+
6
+ /**
7
+ * The V1 serializer for a Composition Block
8
+ */
9
+ export const compositionBlockSerializer: IBlockSerializerV1 = {
10
+ blockType: compositionBlockType,
11
+ serialize: (block: BaseBlock) => {
12
+ if (block.getClassName() !== compositionBlockType) {
13
+ throw new Error("Was asked to serialize an unrecognized block type");
14
+ }
15
+
16
+ const compositionBlock = block as unknown as CompositionBlock;
17
+ return {
18
+ name: block.name,
19
+ uniqueId: block.uniqueId,
20
+ blockType: compositionBlockType,
21
+ namespace: babylonDemoEffectsNamespace,
22
+ comments: block.comments,
23
+ data: {
24
+ alphaMode: compositionBlock.alphaMode,
25
+ },
26
+ };
27
+ },
28
+ };
@@ -0,0 +1,243 @@
1
+ import type { Effect } from "@babylonjs/core/Materials/effect";
2
+ import {
3
+ DisableableShaderBinding,
4
+ type RuntimeData,
5
+ ConnectionPointType,
6
+ type IDisableableBlock,
7
+ DisableableShaderBlock,
8
+ type SmartFilter,
9
+ createStrongRef,
10
+ PropertyTypeForEdition,
11
+ editableInPropertyPage,
12
+ } from "@babylonjs/smart-filters";
13
+ import { compositionBlockType } from "../../../blockTypes.js";
14
+ import { babylonDemoEffectsNamespace } from "../../../blockNamespaces.js";
15
+ import { uniforms, shaderProgram } from "./compositionBlock.fragment.js";
16
+
17
+ /** Defines that alpha blending is disabled */
18
+ export const ALPHA_DISABLE = 0;
19
+ /** Defines that alpha blending is SRC ALPHA * SRC + DEST */
20
+ export const ALPHA_ADD = 1;
21
+ /** Defines that alpha blending is SRC ALPHA * SRC + (1 - SRC ALPHA) * DEST */
22
+ export const ALPHA_COMBINE = 2;
23
+ /** Defines that alpha blending is DEST - SRC * DEST */
24
+ export const ALPHA_SUBTRACT = 3;
25
+ /** Defines that alpha blending is SRC * DEST */
26
+ export const ALPHA_MULTIPLY = 4;
27
+
28
+ /**
29
+ * The shader bindings for the Composition block.
30
+ * This demonstrates how multiple input connection point values can be packed into a single
31
+ * uniform.
32
+ */
33
+ export class CompositionShaderBinding extends DisableableShaderBinding {
34
+ private readonly _backgroundTexture: RuntimeData<ConnectionPointType.Texture>;
35
+ private readonly _foregroundTexture: RuntimeData<ConnectionPointType.Texture>;
36
+ private readonly _foregroundTop: RuntimeData<ConnectionPointType.Float>;
37
+ private readonly _foregroundLeft: RuntimeData<ConnectionPointType.Float>;
38
+ private readonly _foregroundWidth: RuntimeData<ConnectionPointType.Float>;
39
+ private readonly _foregroundHeight: RuntimeData<ConnectionPointType.Float>;
40
+ private readonly _foregroundAlphaScale: RuntimeData<ConnectionPointType.Float>;
41
+ private readonly _alphaMode: number;
42
+
43
+ /**
44
+ * Creates a new shader binding instance for the Composition block.
45
+ * @param parentBlock - The parent block
46
+ * @param backgroundTexture - the background texture
47
+ * @param foregroundTexture - the foreground texture
48
+ * @param foregroundTop - the top position of the foreground texture
49
+ * @param foregroundLeft - the left position of the foreground texture
50
+ * @param foregroundWidth - the width of the foreground texture
51
+ * @param foregroundHeight - the height of the foreground texture
52
+ * @param foregroundAlphaScale - the alpha scale of the foreground texture
53
+ * @param alphaMode - the alpha mode to use
54
+ */
55
+ constructor(
56
+ parentBlock: IDisableableBlock,
57
+ backgroundTexture: RuntimeData<ConnectionPointType.Texture>,
58
+ foregroundTexture: RuntimeData<ConnectionPointType.Texture>,
59
+ foregroundTop: RuntimeData<ConnectionPointType.Float>,
60
+ foregroundLeft: RuntimeData<ConnectionPointType.Float>,
61
+ foregroundWidth: RuntimeData<ConnectionPointType.Float>,
62
+ foregroundHeight: RuntimeData<ConnectionPointType.Float>,
63
+ foregroundAlphaScale: RuntimeData<ConnectionPointType.Float>,
64
+ alphaMode: number
65
+ ) {
66
+ super(parentBlock);
67
+ this._backgroundTexture = backgroundTexture;
68
+ this._foregroundTexture = foregroundTexture;
69
+ this._foregroundTop = foregroundTop;
70
+ this._foregroundLeft = foregroundLeft;
71
+ this._foregroundWidth = foregroundWidth;
72
+ this._foregroundHeight = foregroundHeight;
73
+ this._foregroundAlphaScale = foregroundAlphaScale;
74
+ this._alphaMode = alphaMode;
75
+ }
76
+
77
+ /**
78
+ * Binds all the required data to the shader when rendering.
79
+ * @param effect - defines the effect to bind the data to
80
+ * @param width - defines the width of the output
81
+ * @param height - defines the height of the output
82
+ */
83
+ public override bind(effect: Effect, width: number, height: number): void {
84
+ super.bind(effect, width, height);
85
+
86
+ const background = this._backgroundTexture.value;
87
+ const foreground = this._foregroundTexture.value;
88
+ const foregroundTop = this._foregroundTop.value;
89
+ const foregroundLeft = this._foregroundLeft.value;
90
+ const foregroundWidth = this._foregroundWidth.value;
91
+ const foregroundHeight = this._foregroundHeight.value;
92
+ const foregroundAlphaScale = this._foregroundAlphaScale.value;
93
+ const alphaMode = this._alphaMode;
94
+
95
+ effect.setFloat(this.getRemappedName(uniforms.alphaMode), alphaMode);
96
+ effect.setTexture(this.getRemappedName(uniforms.background), background);
97
+ effect.setTexture(this.getRemappedName(uniforms.foreground), foreground);
98
+
99
+ // NOTE: textures may always be undefined if connected to another shader block when the graph is optimized
100
+
101
+ effect.setFloat2(this.getRemappedName(uniforms.scaleUV), foregroundWidth, foregroundHeight);
102
+ effect.setFloat2(this.getRemappedName(uniforms.translateUV), -1 * foregroundLeft, foregroundTop);
103
+ effect.setFloat(this.getRemappedName(uniforms.foregroundAlphaScale), foregroundAlphaScale);
104
+ }
105
+ }
106
+
107
+ /**
108
+ * A simple compositing Block letting the filter "blend" 2 different layers.
109
+ * It demonstrates how a block can use properties for values which will not change at runtime (alphaMode)
110
+ *
111
+ * The alpha mode of the block can be set to one of the following:
112
+ * - ALPHA_DISABLE: alpha blending is disabled
113
+ * - ALPHA_ADD: alpha blending is SRC ALPHA * SRC + DEST
114
+ * - ALPHA_COMBINE: alpha blending is SRC ALPHA * SRC + (1 - SRC ALPHA) * DEST
115
+ * - ALPHA_SUBTRACT: alpha blending is DEST - SRC * DEST
116
+ * - ALPHA_MULTIPLY: alpha blending is SRC * DEST
117
+ */
118
+ export class CompositionBlock extends DisableableShaderBlock {
119
+ /**
120
+ * The class name of the block.
121
+ */
122
+ public static override ClassName = compositionBlockType;
123
+
124
+ /**
125
+ * The namespace of the block.
126
+ */
127
+ public static override Namespace = babylonDemoEffectsNamespace;
128
+
129
+ /**
130
+ * The background texture to composite on to.
131
+ */
132
+ public readonly background = this._registerInput(uniforms.background, ConnectionPointType.Texture);
133
+
134
+ /**
135
+ * The foreground texture to composite in.
136
+ */
137
+ public readonly foreground = this._registerOptionalInput(
138
+ uniforms.foreground,
139
+ ConnectionPointType.Texture,
140
+ createStrongRef(null)
141
+ );
142
+
143
+ /**
144
+ * Defines where the top of the texture to composite in should be displayed. (between 0 and 1).
145
+ */
146
+ public readonly foregroundTop = this._registerOptionalInput(
147
+ "foregroundTop",
148
+ ConnectionPointType.Float,
149
+ createStrongRef(0.0)
150
+ );
151
+
152
+ /**
153
+ * Defines where the left of the texture to composite in should be displayed. (between 0 and 1).
154
+ */
155
+ public readonly foregroundLeft = this._registerOptionalInput(
156
+ "foregroundLeft",
157
+ ConnectionPointType.Float,
158
+ createStrongRef(0.0)
159
+ );
160
+
161
+ /**
162
+ * Defines the width of the texture in the composition.
163
+ */
164
+ public readonly foregroundWidth = this._registerOptionalInput(
165
+ "foregroundWidth",
166
+ ConnectionPointType.Float,
167
+ createStrongRef(1.0)
168
+ );
169
+
170
+ /**
171
+ * Defines the height of the texture in the composition.
172
+ */
173
+ public readonly foregroundHeight = this._registerOptionalInput(
174
+ "foregroundHeight",
175
+ ConnectionPointType.Float,
176
+ createStrongRef(1.0)
177
+ );
178
+
179
+ /**
180
+ * Defines a multiplier applied to the foreground's alpha channel.
181
+ */
182
+ public readonly foregroundAlphaScale = this._registerOptionalInput(
183
+ uniforms.foregroundAlphaScale,
184
+ ConnectionPointType.Float,
185
+ createStrongRef(1.0)
186
+ );
187
+
188
+ /**
189
+ * Defines blend mode of the composition.
190
+ */
191
+ @editableInPropertyPage("Alpha Mode", PropertyTypeForEdition.List, "PROPERTIES", {
192
+ notifiers: { rebuild: true },
193
+ options: [
194
+ { label: "Disable", value: ALPHA_DISABLE },
195
+ { label: "Add", value: ALPHA_ADD },
196
+ { label: "Combine", value: ALPHA_COMBINE },
197
+ { label: "Subtract", value: ALPHA_SUBTRACT },
198
+ { label: "Multiply", value: ALPHA_MULTIPLY },
199
+ ],
200
+ })
201
+ public alphaMode: number = ALPHA_COMBINE;
202
+
203
+ /**
204
+ * The shader program (vertex and fragment code) to use to render the block
205
+ */
206
+ public static override ShaderCode = shaderProgram;
207
+
208
+ /**
209
+ * Instantiates a new Block.
210
+ * @param smartFilter - The smart filter this block belongs to
211
+ * @param name - The friendly name of the block
212
+ */
213
+ constructor(smartFilter: SmartFilter, name: string) {
214
+ super(smartFilter, name, true);
215
+ }
216
+
217
+ /**
218
+ * Get the class instance that binds all the required data to the shader (effect) when rendering.
219
+ * @returns The class instance that binds the data to the effect
220
+ */
221
+ public getShaderBinding(): DisableableShaderBinding {
222
+ const background = this._confirmRuntimeDataSupplied(this.background);
223
+ const foreground = this._confirmRuntimeDataSupplied(this.foreground);
224
+ const foregroundWidth = this.foregroundWidth.runtimeData;
225
+ const foregroundLeft = this.foregroundLeft.runtimeData;
226
+ const foregroundHeight = this.foregroundHeight.runtimeData;
227
+ const foregroundTop = this.foregroundTop.runtimeData;
228
+ const foregroundAlphaScale = this.foregroundAlphaScale.runtimeData;
229
+ const alphaMode = this.alphaMode;
230
+
231
+ return new CompositionShaderBinding(
232
+ this,
233
+ background,
234
+ foreground,
235
+ foregroundTop,
236
+ foregroundLeft,
237
+ foregroundWidth,
238
+ foregroundHeight,
239
+ foregroundAlphaScale,
240
+ alphaMode
241
+ );
242
+ }
243
+ }
@@ -0,0 +1,36 @@
1
+ /*
2
+ {
3
+ "smartFilterBlockType": "ContrastBlock",
4
+ "namespace": "Babylon.Demo.Effects",
5
+ "blockDisableStrategy": "AutoSample"
6
+ }
7
+ */
8
+
9
+ uniform sampler2D input; // main
10
+ // { "default": 0.5 }
11
+ uniform float intensity;
12
+
13
+ vec4 contrast(vec2 vUV) { // main
14
+ vec4 color = texture2D(input, vUV);
15
+
16
+ float contrastLMin = mix(-2., 0., intensity * 2.0);
17
+ float contrastLMax = mix(3., 1., intensity * 2.0);
18
+
19
+ vec3 contrastMin = remap(color.rgb, contrastLMin, contrastLMax, 0., 1.);
20
+
21
+ float intensityMapped = remap(intensity, 0.5, 1., 0., 1.0);
22
+ float contrastHMin = mix(0., 0.45, intensityMapped);
23
+ float contrastHMax = mix(1., 0.5, intensityMapped);
24
+
25
+ vec3 contrastMax = remap(color.rgb, contrastHMin, contrastHMax, 0., 1.);
26
+
27
+ return vec4(mix(contrastMin, contrastMax, step(intensity, 0.5)), color.a);
28
+ }
29
+
30
+ float remap(float i, float smin, float smax, float dmin, float dmax) {
31
+ return dmin + (i - smin) * (dmax - dmin) / (smax - smin);
32
+ }
33
+
34
+ vec3 remap(vec3 i, float smin, float smax, float dmin, float dmax) {
35
+ return dmin + (i - smin) * (dmax - dmin) / (smax - smin);
36
+ }