@babylonjs/smart-filters 0.1.0-alpha
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.
- package/dist/IDisposable.d.ts +10 -0
- package/dist/IDisposable.d.ts.map +1 -0
- package/dist/IDisposable.js +2 -0
- package/dist/IDisposable.js.map +1 -0
- package/dist/blocks/aggregateBlock.d.ts +50 -0
- package/dist/blocks/aggregateBlock.d.ts.map +1 -0
- package/dist/blocks/aggregateBlock.js +103 -0
- package/dist/blocks/aggregateBlock.js.map +1 -0
- package/dist/blocks/baseBlock.d.ts +160 -0
- package/dist/blocks/baseBlock.d.ts.map +1 -0
- package/dist/blocks/baseBlock.js +256 -0
- package/dist/blocks/baseBlock.js.map +1 -0
- package/dist/blocks/copyBlock.d.ts +59 -0
- package/dist/blocks/copyBlock.d.ts.map +1 -0
- package/dist/blocks/copyBlock.js +84 -0
- package/dist/blocks/copyBlock.js.map +1 -0
- package/dist/blocks/disableableBlock.d.ts +30 -0
- package/dist/blocks/disableableBlock.d.ts.map +1 -0
- package/dist/blocks/disableableBlock.js +22 -0
- package/dist/blocks/disableableBlock.js.map +1 -0
- package/dist/blocks/inputBlock.d.ts +64 -0
- package/dist/blocks/inputBlock.d.ts.map +1 -0
- package/dist/blocks/inputBlock.js +74 -0
- package/dist/blocks/inputBlock.js.map +1 -0
- package/dist/blocks/outputBlock.d.ts +42 -0
- package/dist/blocks/outputBlock.d.ts.map +1 -0
- package/dist/blocks/outputBlock.js +74 -0
- package/dist/blocks/outputBlock.js.map +1 -0
- package/dist/blocks/shaderBlock.d.ts +68 -0
- package/dist/blocks/shaderBlock.d.ts.map +1 -0
- package/dist/blocks/shaderBlock.js +101 -0
- package/dist/blocks/shaderBlock.js.map +1 -0
- package/dist/command/command.d.ts +49 -0
- package/dist/command/command.d.ts.map +1 -0
- package/dist/command/command.js +15 -0
- package/dist/command/command.js.map +1 -0
- package/dist/command/commandBuffer.d.ts +40 -0
- package/dist/command/commandBuffer.d.ts.map +1 -0
- package/dist/command/commandBuffer.js +58 -0
- package/dist/command/commandBuffer.js.map +1 -0
- package/dist/command/commandBufferDebugger.d.ts +7 -0
- package/dist/command/commandBufferDebugger.d.ts.map +1 -0
- package/dist/command/commandBufferDebugger.js +12 -0
- package/dist/command/commandBufferDebugger.js.map +1 -0
- package/dist/connection/connectionPoint.d.ts +110 -0
- package/dist/connection/connectionPoint.d.ts.map +1 -0
- package/dist/connection/connectionPoint.js +153 -0
- package/dist/connection/connectionPoint.js.map +1 -0
- package/dist/connection/connectionPointCompatibilityState.d.ts +20 -0
- package/dist/connection/connectionPointCompatibilityState.d.ts.map +1 -0
- package/dist/connection/connectionPointCompatibilityState.js +32 -0
- package/dist/connection/connectionPointCompatibilityState.js.map +1 -0
- package/dist/connection/connectionPointDirection.d.ts +10 -0
- package/dist/connection/connectionPointDirection.d.ts.map +1 -0
- package/dist/connection/connectionPointDirection.js +11 -0
- package/dist/connection/connectionPointDirection.js.map +1 -0
- package/dist/connection/connectionPointType.d.ts +22 -0
- package/dist/connection/connectionPointType.d.ts.map +1 -0
- package/dist/connection/connectionPointType.js +17 -0
- package/dist/connection/connectionPointType.js.map +1 -0
- package/dist/connection/connectionPointWithDefault.d.ts +23 -0
- package/dist/connection/connectionPointWithDefault.d.ts.map +1 -0
- package/dist/connection/connectionPointWithDefault.js +19 -0
- package/dist/connection/connectionPointWithDefault.js.map +1 -0
- package/dist/index.d.ts +28 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +28 -0
- package/dist/index.js.map +1 -0
- package/dist/optimization/dependencyGraph.d.ts +31 -0
- package/dist/optimization/dependencyGraph.d.ts.map +1 -0
- package/dist/optimization/dependencyGraph.js +77 -0
- package/dist/optimization/dependencyGraph.js.map +1 -0
- package/dist/optimization/optimizedShaderBlock.d.ts +75 -0
- package/dist/optimization/optimizedShaderBlock.d.ts.map +1 -0
- package/dist/optimization/optimizedShaderBlock.js +105 -0
- package/dist/optimization/optimizedShaderBlock.js.map +1 -0
- package/dist/optimization/smartFilterOptimizer.d.ts +72 -0
- package/dist/optimization/smartFilterOptimizer.d.ts.map +1 -0
- package/dist/optimization/smartFilterOptimizer.js +482 -0
- package/dist/optimization/smartFilterOptimizer.js.map +1 -0
- package/dist/runtime/renderTargetGenerator.d.ts +35 -0
- package/dist/runtime/renderTargetGenerator.d.ts.map +1 -0
- package/dist/runtime/renderTargetGenerator.js +153 -0
- package/dist/runtime/renderTargetGenerator.js.map +1 -0
- package/dist/runtime/shaderRuntime.d.ts +95 -0
- package/dist/runtime/shaderRuntime.d.ts.map +1 -0
- package/dist/runtime/shaderRuntime.js +120 -0
- package/dist/runtime/shaderRuntime.js.map +1 -0
- package/dist/runtime/smartFilterRuntime.d.ts +69 -0
- package/dist/runtime/smartFilterRuntime.d.ts.map +1 -0
- package/dist/runtime/smartFilterRuntime.js +57 -0
- package/dist/runtime/smartFilterRuntime.js.map +1 -0
- package/dist/runtime/strongRef.d.ts +16 -0
- package/dist/runtime/strongRef.d.ts.map +1 -0
- package/dist/runtime/strongRef.js +9 -0
- package/dist/runtime/strongRef.js.map +1 -0
- package/dist/smartFilter.d.ts +100 -0
- package/dist/smartFilter.d.ts.map +1 -0
- package/dist/smartFilter.js +154 -0
- package/dist/smartFilter.js.map +1 -0
- package/dist/utils/shaderCodeUtils.d.ts +131 -0
- package/dist/utils/shaderCodeUtils.d.ts.map +1 -0
- package/dist/utils/shaderCodeUtils.js +115 -0
- package/dist/utils/shaderCodeUtils.js.map +1 -0
- package/dist/utils/textureLoaders.d.ts +14 -0
- package/dist/utils/textureLoaders.d.ts.map +1 -0
- package/dist/utils/textureLoaders.js +22 -0
- package/dist/utils/textureLoaders.js.map +1 -0
- package/license.md +21 -0
- package/package.json +47 -0
- package/readme.md +165 -0
- package/src/IDisposable.ts +9 -0
- package/src/blocks/aggregateBlock.ts +121 -0
- package/src/blocks/baseBlock.ts +341 -0
- package/src/blocks/copyBlock.ts +103 -0
- package/src/blocks/disableableBlock.ts +40 -0
- package/src/blocks/inputBlock.ts +114 -0
- package/src/blocks/outputBlock.ts +97 -0
- package/src/blocks/shaderBlock.ts +145 -0
- package/src/command/command.ts +60 -0
- package/src/command/commandBuffer.ts +71 -0
- package/src/command/commandBufferDebugger.ts +13 -0
- package/src/connection/connectionPoint.ts +212 -0
- package/src/connection/connectionPointCompatibilityState.ts +31 -0
- package/src/connection/connectionPointDirection.ts +9 -0
- package/src/connection/connectionPointType.ts +30 -0
- package/src/connection/connectionPointWithDefault.ts +35 -0
- package/src/index.ts +36 -0
- package/src/optimization/dependencyGraph.ts +94 -0
- package/src/optimization/optimizedShaderBlock.ts +133 -0
- package/src/optimization/smartFilterOptimizer.ts +706 -0
- package/src/runtime/renderTargetGenerator.ts +204 -0
- package/src/runtime/shaderRuntime.ts +155 -0
- package/src/runtime/smartFilterRuntime.ts +104 -0
- package/src/runtime/strongRef.ts +18 -0
- package/src/smartFilter.ts +227 -0
- package/src/utils/shaderCodeUtils.ts +242 -0
- package/src/utils/textureLoaders.ts +28 -0
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
import type { ThinEngine } from "@babylonjs/core/Engines/thinEngine";
|
|
2
|
+
import type { Nullable } from "@babylonjs/core/types";
|
|
3
|
+
import type { SmartFilterRuntime } from "./runtime/smartFilterRuntime";
|
|
4
|
+
import type { BaseBlock } from "./blocks/baseBlock";
|
|
5
|
+
import type { ConnectionPointType } from "./connection/connectionPointType";
|
|
6
|
+
import type { ConnectionPoint } from "./connection/connectionPoint";
|
|
7
|
+
import { OutputBlock } from "./blocks/outputBlock.js";
|
|
8
|
+
import { InternalSmartFilterRuntime } from "./runtime/smartFilterRuntime.js";
|
|
9
|
+
import { RenderTargetGenerator } from "./runtime/renderTargetGenerator.js";
|
|
10
|
+
import { AggregateBlock } from "./blocks/aggregateBlock.js";
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* How long to wait for shader compilation and texture loading to complete before erroring out.
|
|
14
|
+
*/
|
|
15
|
+
const InitializationTimeout = 10000;
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Data passed to the initialize function of the blocks.
|
|
19
|
+
*/
|
|
20
|
+
export type InitializationData = {
|
|
21
|
+
/**
|
|
22
|
+
* The current smart filter runtime the block is being initialized for.
|
|
23
|
+
*/
|
|
24
|
+
readonly runtime: InternalSmartFilterRuntime;
|
|
25
|
+
/**
|
|
26
|
+
* The output block of the smart filter.
|
|
27
|
+
* This is used to determine if a block is linked to the output block so that we can prevent an
|
|
28
|
+
* extra render pass.
|
|
29
|
+
*/
|
|
30
|
+
readonly outputBlock: OutputBlock;
|
|
31
|
+
/**
|
|
32
|
+
* The list of promises to wait for during the initialization step.
|
|
33
|
+
*/
|
|
34
|
+
readonly initializationPromises: Promise<void>[];
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* The smart filter class is the main class of the smart filter module.
|
|
39
|
+
*
|
|
40
|
+
* It is responsible for managing a graph of smart filter blocks.
|
|
41
|
+
*
|
|
42
|
+
* It is also responsible for creating the runtime associated to the current state of the filter.
|
|
43
|
+
*/
|
|
44
|
+
export class SmartFilter {
|
|
45
|
+
/**
|
|
46
|
+
* The friendly name of the smart filter.
|
|
47
|
+
*/
|
|
48
|
+
public readonly name: string;
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* The smart filter output (input connection point of the output block...).
|
|
52
|
+
*
|
|
53
|
+
* This is where the smart filter final block should be connected to in order to be visible on screen.
|
|
54
|
+
*/
|
|
55
|
+
public readonly output: ConnectionPoint<ConnectionPointType.Texture>;
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* User defined comments to describe the current smart filter.
|
|
59
|
+
*/
|
|
60
|
+
public comments: Nullable<string> = null;
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Data used by the smart filter editor.
|
|
64
|
+
* TODO. strong type and hide this.
|
|
65
|
+
*/
|
|
66
|
+
public editorData: any = null;
|
|
67
|
+
|
|
68
|
+
private readonly _attachedBlocks: Array<BaseBlock>;
|
|
69
|
+
private readonly _outputBlock: OutputBlock;
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Creates a new instance of a @see SmartFilter.
|
|
73
|
+
* @param name - The friendly name of the smart filter
|
|
74
|
+
*/
|
|
75
|
+
constructor(name: string) {
|
|
76
|
+
this.name = name;
|
|
77
|
+
|
|
78
|
+
this._attachedBlocks = new Array<BaseBlock>();
|
|
79
|
+
this._outputBlock = new OutputBlock(this);
|
|
80
|
+
this.output = this._outputBlock.input;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* @returns the list of blocks attached to the smart filter.
|
|
85
|
+
*/
|
|
86
|
+
public get attachedBlocks(): ReadonlyArray<BaseBlock> {
|
|
87
|
+
return this._attachedBlocks;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* @returns The current class name of the smart filter.
|
|
92
|
+
*/
|
|
93
|
+
public getClassName(): string {
|
|
94
|
+
return "SmartFilter";
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Registers a block to be part of this smart filter.
|
|
99
|
+
* @param block - The block to register on the smart filter
|
|
100
|
+
* @throws if the block is already registered on another smart filter
|
|
101
|
+
* @remarks This function will not register the block if it is already registered on the smart filter.
|
|
102
|
+
*/
|
|
103
|
+
public registerBlock(block: BaseBlock): void {
|
|
104
|
+
// It is impossible to attach a block from another filter
|
|
105
|
+
if (block.smartFilter !== this) {
|
|
106
|
+
throw new Error("Block is not part of this smart filter");
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
// No need to attach a block several times
|
|
110
|
+
if (this._attachedBlocks.indexOf(block) !== -1) {
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// Add the block to the list of attached blocks
|
|
115
|
+
this._attachedBlocks.push(block);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Removes the block from the smart filter.
|
|
120
|
+
* @param block - The block to remove from the smart filter
|
|
121
|
+
* @remarks This function will disconnect the block on removal.
|
|
122
|
+
* This Output block cannot be removed.
|
|
123
|
+
*/
|
|
124
|
+
public removeBlock(block: BaseBlock): void {
|
|
125
|
+
const attachedBlockIndex = this._attachedBlocks.indexOf(block);
|
|
126
|
+
|
|
127
|
+
// The block can only be removed if it is not the output block
|
|
128
|
+
// and if it is attached to the smart filter
|
|
129
|
+
if (attachedBlockIndex > -1 && !block.isOutput) {
|
|
130
|
+
// Disconnects all the connections of the block
|
|
131
|
+
block.disconnect();
|
|
132
|
+
|
|
133
|
+
// Removes the block from the list of attached blocks
|
|
134
|
+
this._attachedBlocks.splice(attachedBlockIndex, 1);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
private _generateCommandsAndGatherInitPromises(initializationData: InitializationData): void {
|
|
139
|
+
const outputBlock = this._outputBlock;
|
|
140
|
+
|
|
141
|
+
outputBlock.visit(initializationData, (block: BaseBlock, initializationData: InitializationData) => {
|
|
142
|
+
// If the block is the output block,
|
|
143
|
+
if (block === outputBlock) {
|
|
144
|
+
// We only need to do something if the connected block is an input block.
|
|
145
|
+
// Indeed, any other block linked to the output block would directly render to the canvas
|
|
146
|
+
// as an optimization.
|
|
147
|
+
// In case the output block is not linked to an input block, we do not need extra commands
|
|
148
|
+
// or resources to create a render pass.
|
|
149
|
+
if (outputBlock.input.connectedTo?.ownerBlock.isInput) {
|
|
150
|
+
block.generateCommandsAndGatherInitPromises(initializationData, true);
|
|
151
|
+
}
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
block.generateCommandsAndGatherInitPromises(
|
|
156
|
+
initializationData,
|
|
157
|
+
outputBlock.input.connectedTo?.ownerBlock === block
|
|
158
|
+
);
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* Create a new runtime for the current state of the smart filter.
|
|
164
|
+
* @param engine - The Babylon.js engine to use for the runtime
|
|
165
|
+
* @param renderTargetGenerator - The render target generator to use to generate the RTTs for the shader blocks. If not provided, a default one will be created.
|
|
166
|
+
* @returns the runtime that can be used to render the smart filter
|
|
167
|
+
*/
|
|
168
|
+
public async createRuntimeAsync(
|
|
169
|
+
engine: ThinEngine,
|
|
170
|
+
renderTargetGenerator?: RenderTargetGenerator
|
|
171
|
+
): Promise<SmartFilterRuntime> {
|
|
172
|
+
const runtime = new InternalSmartFilterRuntime(engine);
|
|
173
|
+
|
|
174
|
+
const initializationData: InitializationData = {
|
|
175
|
+
runtime,
|
|
176
|
+
outputBlock: this._outputBlock,
|
|
177
|
+
initializationPromises: [],
|
|
178
|
+
};
|
|
179
|
+
|
|
180
|
+
this._workWithAggregateFreeGraph(() => {
|
|
181
|
+
this._outputBlock.prepareForRuntime();
|
|
182
|
+
|
|
183
|
+
renderTargetGenerator = renderTargetGenerator ?? new RenderTargetGenerator(false);
|
|
184
|
+
renderTargetGenerator.setOutputTextures(this, initializationData);
|
|
185
|
+
|
|
186
|
+
this._outputBlock.propagateRuntimeData();
|
|
187
|
+
|
|
188
|
+
this._generateCommandsAndGatherInitPromises(initializationData);
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
// Wait for all the blocks to be initialized
|
|
192
|
+
if (initializationData.initializationPromises.length > 0) {
|
|
193
|
+
const timeoutPromise = new Promise((resolve) => setTimeout(resolve, InitializationTimeout, true));
|
|
194
|
+
const initializationPromises = Promise.all(initializationData.initializationPromises).then(() => false);
|
|
195
|
+
const timedOut = await Promise.race([initializationPromises, timeoutPromise]);
|
|
196
|
+
if (timedOut) {
|
|
197
|
+
throw new Error("Initialization promises timed out");
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
return runtime;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* @internal
|
|
206
|
+
* Merges all aggregate blocks into the smart filter graph, executes the passed-in work, then restores the aggregate blocks.
|
|
207
|
+
* @param work - The work to execute with the aggregate blocks merged
|
|
208
|
+
*/
|
|
209
|
+
public _workWithAggregateFreeGraph(work: () => void): void {
|
|
210
|
+
const mergedAggregateBlocks: AggregateBlock[] = [];
|
|
211
|
+
|
|
212
|
+
// Merge all aggregate blocks
|
|
213
|
+
this._outputBlock.visit({}, (block: BaseBlock, _extraData: Object) => {
|
|
214
|
+
if (block instanceof AggregateBlock) {
|
|
215
|
+
block._mergeIntoSmartFilter(mergedAggregateBlocks);
|
|
216
|
+
}
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
// Do the passed in work
|
|
220
|
+
work();
|
|
221
|
+
|
|
222
|
+
// Restore all aggregate blocks
|
|
223
|
+
for (const block of mergedAggregateBlocks) {
|
|
224
|
+
block._unmergeFromSmartFilter();
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
}
|
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
import type { Effect } from "@babylonjs/core/Materials/effect";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The shader code decorator.
|
|
5
|
+
* Used to decorate the names of uniform, function and const variables for easier parsing.
|
|
6
|
+
*/
|
|
7
|
+
export const decorateChar = "_";
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Describes a shader function.
|
|
11
|
+
*/
|
|
12
|
+
export type ShaderFunction = {
|
|
13
|
+
/**
|
|
14
|
+
* The name of the function.
|
|
15
|
+
*/
|
|
16
|
+
name: string;
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* The code of the function.
|
|
20
|
+
*/
|
|
21
|
+
code: string;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Describes a shader code.
|
|
26
|
+
*/
|
|
27
|
+
export type ShaderCode = {
|
|
28
|
+
/**
|
|
29
|
+
* The declaration of the const variables.
|
|
30
|
+
*/
|
|
31
|
+
const?: string;
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* The declaration of the uniform variables.
|
|
35
|
+
*/
|
|
36
|
+
uniform?: string;
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* The declaration of the uniform variables that should be common for all ShaderBlock instances using this shader code.
|
|
40
|
+
*/
|
|
41
|
+
uniformSingle?: string;
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* The name of the main function.
|
|
45
|
+
*/
|
|
46
|
+
mainFunctionName: string;
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* The name of the main input texture.
|
|
50
|
+
*/
|
|
51
|
+
mainInputTexture?: string;
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* The list of functions used in the shader.
|
|
55
|
+
*/
|
|
56
|
+
functions: ShaderFunction[];
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Describes a shader program.
|
|
61
|
+
*/
|
|
62
|
+
export type ShaderProgram = {
|
|
63
|
+
/**
|
|
64
|
+
* The vertex shader code.
|
|
65
|
+
*/
|
|
66
|
+
vertex?: string | undefined;
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* The fragment shader code.
|
|
70
|
+
*/
|
|
71
|
+
fragment: ShaderCode;
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* The list of options required to create a shader block.
|
|
76
|
+
* It mainly contains the shader code to execute and its associated parameters.
|
|
77
|
+
*/
|
|
78
|
+
export type ShaderCreationOptions = {
|
|
79
|
+
/**
|
|
80
|
+
* A friendly name for the shader visible in Spector or in logs.
|
|
81
|
+
*/
|
|
82
|
+
name: string;
|
|
83
|
+
/**
|
|
84
|
+
* Fragment shader source code.
|
|
85
|
+
*/
|
|
86
|
+
fragmentShader: string;
|
|
87
|
+
/**
|
|
88
|
+
* Vertex shader source code.
|
|
89
|
+
*/
|
|
90
|
+
vertexShader?: string;
|
|
91
|
+
/**
|
|
92
|
+
* Attributes to use in the shader
|
|
93
|
+
*/
|
|
94
|
+
attributeNames?: Array<string>;
|
|
95
|
+
/**
|
|
96
|
+
* Uniforms to use in the shader
|
|
97
|
+
*/
|
|
98
|
+
uniformNames?: Array<string>;
|
|
99
|
+
/**
|
|
100
|
+
* Texture sampler names to use in the shader
|
|
101
|
+
*/
|
|
102
|
+
samplerNames?: Array<string>;
|
|
103
|
+
/**
|
|
104
|
+
* Defines to use in the shader
|
|
105
|
+
*/
|
|
106
|
+
defines?: Array<string>;
|
|
107
|
+
/**
|
|
108
|
+
* Callback when the effect is compiled
|
|
109
|
+
*/
|
|
110
|
+
onCompiled?: (effect: Effect) => void;
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Injects the disable uniform into a shader program.
|
|
115
|
+
* @param shaderProgram - The shader program to inject the disable uniform into.
|
|
116
|
+
* @returns The modified shader program
|
|
117
|
+
*/
|
|
118
|
+
export function injectDisableUniform(shaderProgram: ShaderProgram) {
|
|
119
|
+
const shaderFragment = shaderProgram.fragment;
|
|
120
|
+
|
|
121
|
+
shaderFragment.uniform += "\nuniform bool _disabled_;";
|
|
122
|
+
|
|
123
|
+
for (let i = 0; i < shaderFragment.functions.length; ++i) {
|
|
124
|
+
const func = shaderFragment.functions[i]!;
|
|
125
|
+
if (func.name === shaderFragment.mainFunctionName) {
|
|
126
|
+
func.code = func.code.replace(
|
|
127
|
+
"{",
|
|
128
|
+
`{\n if (_disabled_) return texture2D(${shaderProgram.fragment.mainInputTexture}, vUV);\n`
|
|
129
|
+
);
|
|
130
|
+
break;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
return shaderProgram;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Gets the shader fragment code.
|
|
139
|
+
* @param shaderProgram - The shader program to extract the code from.
|
|
140
|
+
* @param mainCodeOnly - If true, only the main function code will be returned.
|
|
141
|
+
* @returns The shader fragment code.
|
|
142
|
+
*/
|
|
143
|
+
export function getShaderFragmentCode(shaderProgram: ShaderProgram, mainCodeOnly = false): string {
|
|
144
|
+
const shaderFragment = shaderProgram.fragment;
|
|
145
|
+
|
|
146
|
+
const declarations =
|
|
147
|
+
(shaderFragment.const ?? "") +
|
|
148
|
+
"\n" +
|
|
149
|
+
shaderFragment.uniform +
|
|
150
|
+
"\n" +
|
|
151
|
+
(shaderFragment.uniformSingle ?? "") +
|
|
152
|
+
"\n";
|
|
153
|
+
|
|
154
|
+
let mainFunctionCode = "";
|
|
155
|
+
let otherFunctionsCode = "";
|
|
156
|
+
for (let i = 0; i < shaderFragment.functions.length; ++i) {
|
|
157
|
+
const func = shaderFragment.functions[i]!;
|
|
158
|
+
if (func.name === shaderFragment.mainFunctionName) {
|
|
159
|
+
mainFunctionCode += func.code + "\n";
|
|
160
|
+
if (mainCodeOnly) {
|
|
161
|
+
break;
|
|
162
|
+
}
|
|
163
|
+
} else {
|
|
164
|
+
otherFunctionsCode += func.code + "\n";
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
return mainCodeOnly ? mainFunctionCode : declarations + otherFunctionsCode + mainFunctionCode;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* Gets the shader creation options from a shader program.
|
|
173
|
+
* @param shaderProgram - The shader program to build the create options from.
|
|
174
|
+
* @returns The shader creation options.
|
|
175
|
+
*/
|
|
176
|
+
export function getShaderCreateOptions(shaderProgram: ShaderProgram): ShaderCreationOptions {
|
|
177
|
+
const shaderFragment = shaderProgram.fragment;
|
|
178
|
+
|
|
179
|
+
let code = getShaderFragmentCode(shaderProgram);
|
|
180
|
+
|
|
181
|
+
const uniforms = shaderFragment.uniform + "\n" + (shaderFragment.uniformSingle ?? "");
|
|
182
|
+
const uniformNames = [];
|
|
183
|
+
const samplerNames = [];
|
|
184
|
+
|
|
185
|
+
const rx = new RegExp(`uniform\\s+(\\S+)\\s+(\\w+)\\s*;`, "g");
|
|
186
|
+
|
|
187
|
+
let match = rx.exec(uniforms);
|
|
188
|
+
while (match !== null) {
|
|
189
|
+
const varType = match[1]!;
|
|
190
|
+
const varName = match[2]!;
|
|
191
|
+
|
|
192
|
+
if (varType === "sampler2D" || varType === "sampler3D") {
|
|
193
|
+
samplerNames.push(varName);
|
|
194
|
+
} else {
|
|
195
|
+
uniformNames.push(varName);
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
match = rx.exec(uniforms);
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
code =
|
|
202
|
+
"varying vec2 vUV;\n" +
|
|
203
|
+
code +
|
|
204
|
+
"\nvoid main(void) {\ngl_FragColor = " +
|
|
205
|
+
shaderFragment.mainFunctionName +
|
|
206
|
+
"(vUV);\n}";
|
|
207
|
+
|
|
208
|
+
const options: ShaderCreationOptions = {
|
|
209
|
+
name: shaderFragment.mainFunctionName,
|
|
210
|
+
fragmentShader: code,
|
|
211
|
+
uniformNames: uniformNames,
|
|
212
|
+
samplerNames: samplerNames,
|
|
213
|
+
};
|
|
214
|
+
|
|
215
|
+
if (shaderProgram.vertex) {
|
|
216
|
+
options.vertexShader = shaderProgram.vertex;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
return options;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* Decorates a symbol (uniform, function or const) name.
|
|
224
|
+
* @param symbol - The symbol to decorate.
|
|
225
|
+
* @returns The decorated symbol.
|
|
226
|
+
*/
|
|
227
|
+
export function decorateSymbol(symbol: string): string {
|
|
228
|
+
return decorateChar + symbol + decorateChar;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/**
|
|
232
|
+
* Undecorates a symbol (uniform, function or const) name.
|
|
233
|
+
* @param symbol - The symbol to undecorate.
|
|
234
|
+
* @returns The undecorated symbol. Throws an error if the symbol is not decorated.
|
|
235
|
+
*/
|
|
236
|
+
export function undecorateSymbol(symbol: string): string {
|
|
237
|
+
if (symbol.charAt(0) !== decorateChar || symbol.charAt(symbol.length - 1) !== decorateChar) {
|
|
238
|
+
throw new Error(`undecorateSymbol: Invalid symbol name "${symbol}"`);
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
return symbol.substring(1, symbol.length - 1);
|
|
242
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { ThinTexture } from "@babylonjs/core/Materials/Textures/thinTexture.js";
|
|
2
|
+
import { type ThinEngine } from "@babylonjs/core/Engines/thinEngine.js";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Helper that takes in a URL to an image and returns a ThinTexture
|
|
6
|
+
* @param engine - defines the engine to use to create the texture
|
|
7
|
+
* @param url - defines a value which contains one of the following:
|
|
8
|
+
* * A conventional http URL, e.g. 'http://...' or 'file://...'
|
|
9
|
+
* * A base64 string of in-line texture data, e.g. 'data:image/jpg;base64,/...'
|
|
10
|
+
* @param flipY - Indicates if the Y axis should be flipped
|
|
11
|
+
* @param samplingMode - The sampling mode to use
|
|
12
|
+
* @returns A ThinTexture of the image
|
|
13
|
+
*/
|
|
14
|
+
export function createImageTexture(
|
|
15
|
+
engine: ThinEngine,
|
|
16
|
+
url: string,
|
|
17
|
+
flipY: boolean = true,
|
|
18
|
+
samplingMode: number | undefined = undefined
|
|
19
|
+
): ThinTexture {
|
|
20
|
+
const internalTexture = engine.createTexture(url, true, flipY, null, samplingMode);
|
|
21
|
+
return new ThinTexture(internalTexture);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/*
|
|
25
|
+
Future util ideas:
|
|
26
|
+
HtmlElementTexture
|
|
27
|
+
WebCamTexture
|
|
28
|
+
*/
|