@babylonjs/smart-filters 0.5.0-alpha → 0.6.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/blocks/baseBlock.d.ts +6 -0
- package/dist/blocks/baseBlock.d.ts.map +1 -1
- package/dist/blocks/baseBlock.js +8 -0
- package/dist/blocks/baseBlock.js.map +1 -1
- package/dist/blocks/customShaderBlock.d.ts +55 -0
- package/dist/blocks/customShaderBlock.d.ts.map +1 -0
- package/dist/blocks/customShaderBlock.js +139 -0
- package/dist/blocks/customShaderBlock.js.map +1 -0
- package/dist/blocks/inputBlock.deserializer.d.ts +1 -1
- package/dist/blocks/inputBlock.deserializer.d.ts.map +1 -1
- package/dist/blocks/inputBlock.serializer.d.ts +1 -1
- package/dist/blocks/inputBlock.serializer.d.ts.map +1 -1
- package/dist/blocks/inputBlock.serializer.js +3 -3
- package/dist/blocks/inputBlock.serializer.js.map +1 -1
- package/dist/blocks/shaderBlock.js +2 -2
- package/dist/blocks/shaderBlock.js.map +1 -1
- package/dist/command/command.d.ts +2 -3
- package/dist/command/command.d.ts.map +1 -1
- package/dist/command/command.js.map +1 -1
- package/dist/command/commandBufferDebugger.js +1 -1
- package/dist/command/commandBufferDebugger.js.map +1 -1
- package/dist/connection/connectionPointType.d.ts +4 -0
- package/dist/connection/connectionPointType.d.ts.map +1 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/dist/optimization/smartFilterOptimizer.js +3 -3
- package/dist/optimization/smartFilterOptimizer.js.map +1 -1
- package/dist/serialization/importCustomShaderBlockDefinition.d.ts +11 -0
- package/dist/serialization/importCustomShaderBlockDefinition.d.ts.map +1 -0
- package/dist/serialization/importCustomShaderBlockDefinition.js +80 -0
- package/dist/serialization/importCustomShaderBlockDefinition.js.map +1 -0
- package/dist/serialization/index.d.ts +1 -0
- package/dist/serialization/index.d.ts.map +1 -1
- package/dist/serialization/index.js +1 -0
- package/dist/serialization/index.js.map +1 -1
- package/dist/serialization/serializedBlockDefinition.d.ts +7 -0
- package/dist/serialization/serializedBlockDefinition.d.ts.map +1 -0
- package/dist/serialization/serializedBlockDefinition.js +2 -0
- package/dist/serialization/serializedBlockDefinition.js.map +1 -0
- package/dist/serialization/serializedSmartFilter.d.ts +1 -1
- package/dist/serialization/serializedSmartFilter.d.ts.map +1 -1
- package/dist/serialization/smartFilterDeserializer.d.ts +12 -4
- package/dist/serialization/smartFilterDeserializer.d.ts.map +1 -1
- package/dist/serialization/smartFilterDeserializer.js +63 -34
- package/dist/serialization/smartFilterDeserializer.js.map +1 -1
- package/dist/serialization/smartFilterSerializer.d.ts +2 -2
- package/dist/serialization/smartFilterSerializer.d.ts.map +1 -1
- package/dist/serialization/smartFilterSerializer.js +9 -6
- package/dist/serialization/smartFilterSerializer.js.map +1 -1
- package/dist/serialization/v1/blockSerialization.types.d.ts +55 -0
- package/dist/serialization/v1/blockSerialization.types.d.ts.map +1 -0
- package/dist/serialization/v1/blockSerialization.types.js +7 -0
- package/dist/serialization/v1/blockSerialization.types.js.map +1 -0
- package/dist/serialization/v1/defaultBlockSerializer.d.ts +1 -1
- package/dist/serialization/v1/defaultBlockSerializer.d.ts.map +1 -1
- package/dist/serialization/v1/defaultBlockSerializer.js +1 -1
- package/dist/serialization/v1/defaultBlockSerializer.js.map +1 -1
- package/dist/serialization/v1/index.d.ts +1 -1
- package/dist/serialization/v1/index.d.ts.map +1 -1
- package/dist/serialization/v1/index.js +1 -1
- package/dist/serialization/v1/index.js.map +1 -1
- package/dist/serialization/v1/{serialization.types.d.ts → smartFilterSerialization.types.d.ts} +12 -11
- package/dist/serialization/v1/smartFilterSerialization.types.d.ts.map +1 -0
- package/dist/serialization/v1/smartFilterSerialization.types.js +2 -0
- package/dist/serialization/v1/smartFilterSerialization.types.js.map +1 -0
- package/dist/utils/buildTools/buildShaders.js +1 -1
- package/dist/utils/buildTools/buildShaders.js.map +1 -1
- package/dist/utils/buildTools/convertShaderForHardcodedBlock.d.ts +13 -0
- package/dist/utils/buildTools/convertShaderForHardcodedBlock.d.ts.map +1 -0
- package/dist/utils/buildTools/convertShaderForHardcodedBlock.js +116 -0
- package/dist/utils/buildTools/convertShaderForHardcodedBlock.js.map +1 -0
- package/dist/utils/buildTools/shaderCode.types.d.ts +43 -0
- package/dist/utils/buildTools/shaderCode.types.d.ts.map +1 -0
- package/dist/utils/buildTools/shaderCode.types.js +2 -0
- package/dist/utils/buildTools/shaderCode.types.js.map +1 -0
- package/dist/utils/buildTools/shaderConverter.d.ts +56 -8
- package/dist/utils/buildTools/shaderConverter.d.ts.map +1 -1
- package/dist/utils/buildTools/shaderConverter.js +87 -137
- package/dist/utils/buildTools/shaderConverter.js.map +1 -1
- package/dist/utils/buildTools/watchShaders.js +2 -2
- package/dist/utils/buildTools/watchShaders.js.map +1 -1
- package/dist/utils/renderTargetUtils.js +3 -3
- package/dist/utils/renderTargetUtils.js.map +1 -1
- package/dist/utils/shaderCodeUtils.d.ts +1 -42
- package/dist/utils/shaderCodeUtils.d.ts.map +1 -1
- package/dist/utils/shaderCodeUtils.js.map +1 -1
- package/package.json +1 -1
- package/readme.md +19 -15
- package/src/blocks/baseBlock.ts +9 -0
- package/src/blocks/customShaderBlock.ts +217 -0
- package/src/blocks/inputBlock.deserializer.ts +1 -1
- package/src/blocks/inputBlock.serializer.ts +4 -4
- package/src/blocks/shaderBlock.ts +2 -2
- package/src/command/command.ts +2 -3
- package/src/command/commandBufferDebugger.ts +1 -1
- package/src/connection/connectionPointType.ts +11 -0
- package/src/index.ts +2 -0
- package/src/optimization/smartFilterOptimizer.ts +3 -3
- package/src/serialization/importCustomShaderBlockDefinition.ts +85 -0
- package/src/serialization/index.ts +1 -0
- package/src/serialization/serializedBlockDefinition.ts +7 -0
- package/src/serialization/serializedSmartFilter.ts +1 -1
- package/src/serialization/smartFilterDeserializer.ts +106 -52
- package/src/serialization/smartFilterSerializer.ts +11 -7
- package/src/serialization/v1/blockSerialization.types.ts +63 -0
- package/src/serialization/v1/defaultBlockSerializer.ts +2 -2
- package/src/serialization/v1/index.ts +1 -1
- package/src/serialization/v1/{serialization.types.ts → smartFilterSerialization.types.ts} +11 -10
- package/src/utils/buildTools/buildShaders.ts +1 -1
- package/src/utils/buildTools/convertShaderForHardcodedBlock.ts +149 -0
- package/src/utils/buildTools/shaderCode.types.ts +49 -0
- package/src/utils/buildTools/shaderConverter.ts +158 -178
- package/src/utils/buildTools/watchShaders.ts +2 -2
- package/src/utils/renderTargetUtils.ts +3 -3
- package/src/utils/shaderCodeUtils.ts +1 -50
- package/dist/serialization/v1/serialization.types.d.ts.map +0 -1
- package/dist/serialization/v1/serialization.types.js +0 -2
- package/dist/serialization/v1/serialization.types.js.map +0 -1
package/dist/serialization/v1/{serialization.types.d.ts → smartFilterSerialization.types.d.ts}
RENAMED
|
@@ -12,8 +12,8 @@ import type { IEditorData } from "@babylonjs/shared-ui-components/nodeGraphSyste
|
|
|
12
12
|
* V1 Serialized Smart Filter
|
|
13
13
|
*/
|
|
14
14
|
export type SerializedSmartFilterV1 = {
|
|
15
|
-
/** The version of the serialized data
|
|
16
|
-
|
|
15
|
+
/** The format version of the serialized data (not the version of the SmartFilter itself).*/
|
|
16
|
+
formatVersion: 1;
|
|
17
17
|
/** The SmartFilter name */
|
|
18
18
|
name: string;
|
|
19
19
|
/** The SmartFilter comments */
|
|
@@ -26,15 +26,16 @@ export type SerializedSmartFilterV1 = {
|
|
|
26
26
|
connections: ISerializedConnectionV1[];
|
|
27
27
|
};
|
|
28
28
|
/**
|
|
29
|
-
* V1
|
|
29
|
+
* V1 format of a block in a serialized Smart Filter.
|
|
30
|
+
* Not to be confused with a SerializedBlockDefinitionV1 which serializes the definition of a CustomShaderBlock.
|
|
30
31
|
*/
|
|
31
32
|
export interface ISerializedBlockV1 {
|
|
32
33
|
/** The name of the block */
|
|
33
34
|
name: string;
|
|
34
35
|
/** The unique ID of the block - correlates with the ID in the editorData for block position, etc. */
|
|
35
36
|
uniqueId: number;
|
|
36
|
-
/** The
|
|
37
|
-
|
|
37
|
+
/** The blockType of the block - used to determine how to instantiate the block during deserialization */
|
|
38
|
+
blockType: string;
|
|
38
39
|
/** The comments for the block */
|
|
39
40
|
comments: Nullable<string>;
|
|
40
41
|
/** Block specific serialized data */
|
|
@@ -63,12 +64,12 @@ export interface ISerializedConnectionV1 {
|
|
|
63
64
|
*/
|
|
64
65
|
export type SerializeBlockV1 = (block: BaseBlock) => ISerializedBlockV1;
|
|
65
66
|
/**
|
|
66
|
-
* A V1
|
|
67
|
+
* A V1 serializer for blocks in a SmartFilter
|
|
67
68
|
*/
|
|
68
69
|
export interface IBlockSerializerV1 {
|
|
69
|
-
/** The
|
|
70
|
-
|
|
71
|
-
/** The function that serializes the block */
|
|
70
|
+
/** The blockType of the block that this serializer can serialize */
|
|
71
|
+
blockType: string;
|
|
72
|
+
/** The function that serializes the block in the Smart Filter */
|
|
72
73
|
serialize: SerializeBlockV1;
|
|
73
74
|
}
|
|
74
75
|
/**
|
|
@@ -77,11 +78,11 @@ export interface IBlockSerializerV1 {
|
|
|
77
78
|
* ----------------------------------------------------------------------------
|
|
78
79
|
*/
|
|
79
80
|
/**
|
|
80
|
-
* A function that deserializes a block
|
|
81
|
+
* A function that deserializes a V1 block in a SmartFilter
|
|
81
82
|
*/
|
|
82
83
|
export type DeserializeBlockV1 = (smartFilter: SmartFilter, serializedBlock: ISerializedBlockV1, engine: ThinEngine) => Promise<BaseBlock>;
|
|
83
84
|
/**
|
|
84
85
|
* A function that optionally deserializes a block from a V1 serialized block object, returning null if it cannot
|
|
85
86
|
*/
|
|
86
87
|
export type OptionalBlockDeserializerV1 = (smartFilter: SmartFilter, serializedBlock: ISerializedBlockV1, engine: ThinEngine) => Promise<Nullable<BaseBlock>>;
|
|
87
|
-
//# sourceMappingURL=
|
|
88
|
+
//# sourceMappingURL=smartFilterSerialization.types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"smartFilterSerialization.types.d.ts","sourceRoot":"","sources":["../../../src/serialization/v1/smartFilterSerialization.types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,0BAA0B,CAAC;AACzD,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,2BAA2B,CAAC;AAC3D,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,sBAAsB,CAAC;AACxD,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,oCAAoC,CAAC;AACrE,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,gFAAgF,CAAC;AAElH;;;;GAIG;AAEH;;GAEG;AACH,MAAM,MAAM,uBAAuB,GAAG;IAClC,4FAA4F;IAC5F,aAAa,EAAE,CAAC,CAAC;IAEjB,2BAA2B;IAC3B,IAAI,EAAE,MAAM,CAAC;IAEb,+BAA+B;IAC/B,QAAQ,EAAE,QAAQ,CAAC,MAAM,CAAC,CAAC;IAE3B,0CAA0C;IAC1C,UAAU,EAAE,QAAQ,CAAC,WAAW,CAAC,CAAC;IAElC,4BAA4B;IAC5B,MAAM,EAAE,kBAAkB,EAAE,CAAC;IAE7B,iCAAiC;IACjC,WAAW,EAAE,uBAAuB,EAAE,CAAC;CAC1C,CAAC;AAEF;;;GAGG;AACH,MAAM,WAAW,kBAAkB;IAC/B,4BAA4B;IAC5B,IAAI,EAAE,MAAM,CAAC;IAEb,qGAAqG;IACrG,QAAQ,EAAE,MAAM,CAAC;IAEjB,yGAAyG;IACzG,SAAS,EAAE,MAAM,CAAC;IAElB,iCAAiC;IACjC,QAAQ,EAAE,QAAQ,CAAC,MAAM,CAAC,CAAC;IAE3B,qCAAqC;IACrC,IAAI,EAAE,GAAG,CAAC;CACb;AAED;;GAEG;AACH,MAAM,WAAW,uBAAuB;IACpC,0DAA0D;IAC1D,WAAW,EAAE,MAAM,CAAC;IAEpB,yDAAyD;IACzD,qBAAqB,EAAE,MAAM,CAAC;IAE9B,4DAA4D;IAC5D,UAAU,EAAE,MAAM,CAAC;IAEnB,wDAAwD;IACxD,oBAAoB,EAAE,MAAM,CAAC;CAChC;AAED;;;;GAIG;AAEH;;GAEG;AACH,MAAM,MAAM,gBAAgB,GAAG,CAAC,KAAK,EAAE,SAAS,KAAK,kBAAkB,CAAC;AAExE;;GAEG;AACH,MAAM,WAAW,kBAAkB;IAC/B,oEAAoE;IACpE,SAAS,EAAE,MAAM,CAAC;IAElB,iEAAiE;IACjE,SAAS,EAAE,gBAAgB,CAAC;CAC/B;AAED;;;;GAIG;AAEH;;GAEG;AACH,MAAM,MAAM,kBAAkB,GAAG,CAC7B,WAAW,EAAE,WAAW,EACxB,eAAe,EAAE,kBAAkB,EACnC,MAAM,EAAE,UAAU,KACjB,OAAO,CAAC,SAAS,CAAC,CAAC;AAExB;;GAEG;AACH,MAAM,MAAM,2BAA2B,GAAG,CACtC,WAAW,EAAE,WAAW,EACxB,eAAe,EAAE,kBAAkB,EACnC,MAAM,EAAE,UAAU,KACjB,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAC,CAAC,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"smartFilterSerialization.types.js","sourceRoot":"","sources":["../../../src/serialization/v1/smartFilterSerialization.types.ts"],"names":[],"mappings":""}
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* @param importPath - The path to import the converted shaders
|
|
5
5
|
* @example node buildShaders.js <shaderPath> <importPath>
|
|
6
6
|
*/
|
|
7
|
-
import { convertShaders } from "./
|
|
7
|
+
import { convertShaders } from "./convertShaderForHardcodedBlock.js";
|
|
8
8
|
const externalArguments = process.argv.slice(2);
|
|
9
9
|
if (externalArguments.length >= 2 && externalArguments[0] && externalArguments[1]) {
|
|
10
10
|
convertShaders(externalArguments[0], externalArguments[1]);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"buildShaders.js","sourceRoot":"","sources":["../../../src/utils/buildTools/buildShaders.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,cAAc,EAAE,MAAM,
|
|
1
|
+
{"version":3,"file":"buildShaders.js","sourceRoot":"","sources":["../../../src/utils/buildTools/buildShaders.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,cAAc,EAAE,MAAM,qCAAqC,CAAC;AAErE,MAAM,iBAAiB,GAAG,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;AAChD,IAAI,iBAAiB,CAAC,MAAM,IAAI,CAAC,IAAI,iBAAiB,CAAC,CAAC,CAAC,IAAI,iBAAiB,CAAC,CAAC,CAAC,EAAE,CAAC;IAChF,cAAc,CAAC,iBAAiB,CAAC,CAAC,CAAC,EAAE,iBAAiB,CAAC,CAAC,CAAC,CAAC,CAAC;AAC/D,CAAC"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Converts a single shader to a .ts file which can be imported by a hardcoded block
|
|
3
|
+
* @param fragmentShaderPath - The path to the fragment file for the shader
|
|
4
|
+
* @param importPath - The path to import the ShaderProgram type from
|
|
5
|
+
*/
|
|
6
|
+
export declare function convertShaderForHardcodedBlock(fragmentShaderPath: string, importPath: string): void;
|
|
7
|
+
/**
|
|
8
|
+
* Converts .fragment.glsl and vertex.glsl file pairs into .shader.ts files which export a ShaderProgram object.
|
|
9
|
+
* @param shaderPath - The path to the .glsl files to convert.
|
|
10
|
+
* @param importPath - The path to import the ShaderProgram type from.
|
|
11
|
+
*/
|
|
12
|
+
export declare function convertShaders(shaderPath: string, importPath: string): void;
|
|
13
|
+
//# sourceMappingURL=convertShaderForHardcodedBlock.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"convertShaderForHardcodedBlock.d.ts","sourceRoot":"","sources":["../../../src/utils/buildTools/convertShaderForHardcodedBlock.ts"],"names":[],"mappings":"AA0DA;;;;GAIG;AACH,wBAAgB,8BAA8B,CAAC,kBAAkB,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,GAAG,IAAI,CAoDnG;AAED;;;;GAIG;AACH,wBAAgB,cAAc,CAAC,UAAU,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,QAapE"}
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import * as fs from "fs";
|
|
2
|
+
import * as path from "path";
|
|
3
|
+
import { parseFragmentShader } from "./shaderConverter.js";
|
|
4
|
+
const TYPE_IMPORT_PATH = "@TYPE_IMPORT_PATH@";
|
|
5
|
+
const VERTEX_SHADER = "@VERTEX_SHADER@";
|
|
6
|
+
const UNIFORMS = "@UNIFORMS@";
|
|
7
|
+
const CONSTS_VALUE = "@CONSTS@";
|
|
8
|
+
const CONSTS_PROPERTY = "@CONSTS_PROPERTY@";
|
|
9
|
+
const MAIN_INPUT_NAME = "@MAIN_INPUT_NAME@";
|
|
10
|
+
const MAIN_FUNCTION_NAME = "@MAIN_FUNCTION_NAME@";
|
|
11
|
+
const FUNCTIONS = "@FUNCTIONS@";
|
|
12
|
+
const FUNCTION_NAME = "@FUNCTION_NAME@";
|
|
13
|
+
const FUNCTION_CODE = "@FUNCTION_CODE@";
|
|
14
|
+
const UNIFORM_NAMES = "@UNIFORM_NAMES@";
|
|
15
|
+
const ConstsTemplate = `
|
|
16
|
+
const: \`${CONSTS_VALUE}\`,`;
|
|
17
|
+
const FunctionTemplate = `
|
|
18
|
+
{
|
|
19
|
+
name: "${FUNCTION_NAME}",
|
|
20
|
+
code: \`
|
|
21
|
+
${FUNCTION_CODE}
|
|
22
|
+
\`,
|
|
23
|
+
},`;
|
|
24
|
+
const CodeLinePrefix = " ";
|
|
25
|
+
const UniformLinePrefix = " ";
|
|
26
|
+
const ConstLinePrefix = " ";
|
|
27
|
+
const ShaderTemplate = `import type { ShaderProgram } from "${TYPE_IMPORT_PATH}";
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* The shader program for the block.
|
|
31
|
+
*/
|
|
32
|
+
export const shaderProgram: ShaderProgram = {
|
|
33
|
+
vertex: ${VERTEX_SHADER},
|
|
34
|
+
fragment: {
|
|
35
|
+
uniform: \`${UNIFORMS}\`,${CONSTS_PROPERTY}
|
|
36
|
+
mainInputTexture: "${MAIN_INPUT_NAME}",
|
|
37
|
+
mainFunctionName: "${MAIN_FUNCTION_NAME}",
|
|
38
|
+
functions: [${FUNCTIONS}
|
|
39
|
+
],
|
|
40
|
+
},
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* The uniform names for this shader, to be used in the shader binding so
|
|
45
|
+
* that the names are always in sync.
|
|
46
|
+
*/
|
|
47
|
+
export const uniforms = {
|
|
48
|
+
${UNIFORM_NAMES}
|
|
49
|
+
};
|
|
50
|
+
`;
|
|
51
|
+
const UniformNameLinePrefix = " ";
|
|
52
|
+
/**
|
|
53
|
+
* Converts a single shader to a .ts file which can be imported by a hardcoded block
|
|
54
|
+
* @param fragmentShaderPath - The path to the fragment file for the shader
|
|
55
|
+
* @param importPath - The path to import the ShaderProgram type from
|
|
56
|
+
*/
|
|
57
|
+
export function convertShaderForHardcodedBlock(fragmentShaderPath, importPath) {
|
|
58
|
+
console.log(`Processing fragment shader: ${fragmentShaderPath}`);
|
|
59
|
+
// See if there is a corresponding vertex shader
|
|
60
|
+
let vertexShader = undefined;
|
|
61
|
+
const vertexShaderPath = fragmentShaderPath.replace(".fragment.glsl", ".vertex.glsl");
|
|
62
|
+
if (fs.existsSync(vertexShaderPath)) {
|
|
63
|
+
vertexShader = fs.readFileSync(vertexShaderPath, "utf8");
|
|
64
|
+
}
|
|
65
|
+
if (vertexShader) {
|
|
66
|
+
console.log("Found vertex shader");
|
|
67
|
+
}
|
|
68
|
+
// Read the fragment shader
|
|
69
|
+
const fragmentShader = fs.readFileSync(fragmentShaderPath, "utf8");
|
|
70
|
+
const fragmentShaderInfo = parseFragmentShader(fragmentShader);
|
|
71
|
+
// Write the shader TS file
|
|
72
|
+
const shaderFile = fragmentShaderPath.replace(".fragment.glsl", ".shader.ts");
|
|
73
|
+
const functionsSection = [];
|
|
74
|
+
for (const shaderFunction of fragmentShaderInfo.shaderCode.functions) {
|
|
75
|
+
functionsSection.push(FunctionTemplate.replace(FUNCTION_NAME, shaderFunction.name).replace(FUNCTION_CODE, addLinePrefixes(shaderFunction.code, CodeLinePrefix)));
|
|
76
|
+
}
|
|
77
|
+
const finalContents = ShaderTemplate.replace(VERTEX_SHADER, vertexShader ? `\`${vertexShader}\`` : "undefined")
|
|
78
|
+
.replace(TYPE_IMPORT_PATH, importPath)
|
|
79
|
+
.replace(UNIFORMS, "\n" + addLinePrefixes(fragmentShaderInfo.shaderCode.uniform || "", UniformLinePrefix))
|
|
80
|
+
.replace(MAIN_FUNCTION_NAME, fragmentShaderInfo.shaderCode.mainFunctionName)
|
|
81
|
+
.replace(MAIN_INPUT_NAME, fragmentShaderInfo.shaderCode.mainInputTexture || '""')
|
|
82
|
+
.replace(CONSTS_PROPERTY, fragmentShaderInfo.shaderCode.const
|
|
83
|
+
? ConstsTemplate.replace(CONSTS_VALUE, addLinePrefixes(fragmentShaderInfo.shaderCode.const, ConstLinePrefix))
|
|
84
|
+
: "")
|
|
85
|
+
.replace(FUNCTIONS, functionsSection.join(""))
|
|
86
|
+
.replace(UNIFORM_NAMES, addLinePrefixes(fragmentShaderInfo.uniforms.map((u) => `${u.name}: "${u.name}",`).join("\n"), UniformNameLinePrefix));
|
|
87
|
+
fs.writeFileSync(shaderFile, finalContents);
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Converts .fragment.glsl and vertex.glsl file pairs into .shader.ts files which export a ShaderProgram object.
|
|
91
|
+
* @param shaderPath - The path to the .glsl files to convert.
|
|
92
|
+
* @param importPath - The path to import the ShaderProgram type from.
|
|
93
|
+
*/
|
|
94
|
+
export function convertShaders(shaderPath, importPath) {
|
|
95
|
+
// Get all files in the path
|
|
96
|
+
const allFiles = fs.readdirSync(shaderPath, { withFileTypes: true, recursive: true });
|
|
97
|
+
// Find all fragment shaders (excluding the template)
|
|
98
|
+
const fragmentShaderFiles = allFiles.filter((file) => file.isFile() && file.name.endsWith(".fragment.glsl") && !file.name.endsWith("template.fragment.glsl"));
|
|
99
|
+
// Convert all shaders
|
|
100
|
+
for (const fragmentShaderFile of fragmentShaderFiles) {
|
|
101
|
+
convertShaderForHardcodedBlock(path.join(fragmentShaderFile.path, fragmentShaderFile.name), importPath);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Prefixes each line in the input
|
|
106
|
+
* @param input - The input string
|
|
107
|
+
* @param prefix - The prefix to add to each line
|
|
108
|
+
* @returns The input with each line prefixed
|
|
109
|
+
*/
|
|
110
|
+
function addLinePrefixes(input, prefix) {
|
|
111
|
+
return input
|
|
112
|
+
.split("\n")
|
|
113
|
+
.map((line) => prefix + line)
|
|
114
|
+
.join("\n");
|
|
115
|
+
}
|
|
116
|
+
//# sourceMappingURL=convertShaderForHardcodedBlock.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"convertShaderForHardcodedBlock.js","sourceRoot":"","sources":["../../../src/utils/buildTools/convertShaderForHardcodedBlock.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,IAAI,CAAC;AACzB,OAAO,KAAK,IAAI,MAAM,MAAM,CAAC;AAC7B,OAAO,EAAE,mBAAmB,EAAE,MAAM,sBAAsB,CAAC;AAE3D,MAAM,gBAAgB,GAAG,oBAAoB,CAAC;AAC9C,MAAM,aAAa,GAAG,iBAAiB,CAAC;AACxC,MAAM,QAAQ,GAAG,YAAY,CAAC;AAC9B,MAAM,YAAY,GAAG,UAAU,CAAC;AAChC,MAAM,eAAe,GAAG,mBAAmB,CAAC;AAC5C,MAAM,eAAe,GAAG,mBAAmB,CAAC;AAC5C,MAAM,kBAAkB,GAAG,sBAAsB,CAAC;AAClD,MAAM,SAAS,GAAG,aAAa,CAAC;AAChC,MAAM,aAAa,GAAG,iBAAiB,CAA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the function.
|
|
7
|
+
*/
|
|
8
|
+
name: string;
|
|
9
|
+
/**
|
|
10
|
+
* The code of the function.
|
|
11
|
+
*/
|
|
12
|
+
code: string;
|
|
13
|
+
};
|
|
14
|
+
/**
|
|
15
|
+
* Describes a shader code.
|
|
16
|
+
*/
|
|
17
|
+
export type ShaderCode = {
|
|
18
|
+
/**
|
|
19
|
+
* The declaration of the const variables.
|
|
20
|
+
*/
|
|
21
|
+
const?: string;
|
|
22
|
+
/**
|
|
23
|
+
* The declaration of the uniform variables.
|
|
24
|
+
*/
|
|
25
|
+
uniform?: string;
|
|
26
|
+
/**
|
|
27
|
+
* The declaration of the uniform variables that should be common for all ShaderBlock instances using this shader code.
|
|
28
|
+
*/
|
|
29
|
+
uniformSingle?: string;
|
|
30
|
+
/**
|
|
31
|
+
* The name of the main function.
|
|
32
|
+
*/
|
|
33
|
+
mainFunctionName: string;
|
|
34
|
+
/**
|
|
35
|
+
* The name of the input texture which is passed through if the block is disabled.
|
|
36
|
+
*/
|
|
37
|
+
mainInputTexture?: string;
|
|
38
|
+
/**
|
|
39
|
+
* The list of functions used in the shader.
|
|
40
|
+
*/
|
|
41
|
+
functions: ShaderFunction[];
|
|
42
|
+
};
|
|
43
|
+
//# sourceMappingURL=shaderCode.types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"shaderCode.types.d.ts","sourceRoot":"","sources":["../../../src/utils/buildTools/shaderCode.types.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,MAAM,cAAc,GAAG;IACzB;;OAEG;IACH,IAAI,EAAE,MAAM,CAAC;IAEb;;OAEG;IACH,IAAI,EAAE,MAAM,CAAC;CAChB,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,UAAU,GAAG;IACrB;;OAEG;IACH,KAAK,CAAC,EAAE,MAAM,CAAC;IAEf;;OAEG;IACH,OAAO,CAAC,EAAE,MAAM,CAAC;IAEjB;;OAEG;IACH,aAAa,CAAC,EAAE,MAAM,CAAC;IAEvB;;OAEG;IACH,gBAAgB,EAAE,MAAM,CAAC;IAEzB;;OAEG;IACH,gBAAgB,CAAC,EAAE,MAAM,CAAC;IAE1B;;OAEG;IACH,SAAS,EAAE,cAAc,EAAE,CAAC;CAC/B,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"shaderCode.types.js","sourceRoot":"","sources":["../../../src/utils/buildTools/shaderCode.types.ts"],"names":[],"mappings":""}
|
|
@@ -1,13 +1,61 @@
|
|
|
1
|
+
import type { ShaderCode } from "./shaderCode.types";
|
|
1
2
|
/**
|
|
2
|
-
*
|
|
3
|
-
* @param fragmentShaderPath - The path to the fragment file for the shader
|
|
4
|
-
* @param importPath - The path to import the ShaderProgram type from
|
|
3
|
+
* Describes the supported metadata properties for a uniform
|
|
5
4
|
*/
|
|
6
|
-
export
|
|
5
|
+
export type UniformMetadataProperties = {
|
|
6
|
+
/**
|
|
7
|
+
* If supplied, the default value to use for the corresponding input connection point
|
|
8
|
+
*/
|
|
9
|
+
default?: any;
|
|
10
|
+
};
|
|
7
11
|
/**
|
|
8
|
-
*
|
|
9
|
-
* @param shaderPath - The path to the .glsl files to convert.
|
|
10
|
-
* @param importPath - The path to import the ShaderProgram type from.
|
|
12
|
+
* Describes a uniform in a shader
|
|
11
13
|
*/
|
|
12
|
-
export
|
|
14
|
+
export type UniformMetadata = {
|
|
15
|
+
/**
|
|
16
|
+
* The original name of the uniform (not renamed)
|
|
17
|
+
*/
|
|
18
|
+
name: string;
|
|
19
|
+
/**
|
|
20
|
+
* The type string of the uniform
|
|
21
|
+
*/
|
|
22
|
+
type: string;
|
|
23
|
+
/**
|
|
24
|
+
* Optional properties of the uniform
|
|
25
|
+
*/
|
|
26
|
+
properties?: UniformMetadataProperties;
|
|
27
|
+
};
|
|
28
|
+
/**
|
|
29
|
+
* Information about a fragment shader
|
|
30
|
+
*/
|
|
31
|
+
export type FragmentShaderInfo = {
|
|
32
|
+
/**
|
|
33
|
+
* If supplied, the blockType to use for the block
|
|
34
|
+
*/
|
|
35
|
+
blockType?: string;
|
|
36
|
+
/**
|
|
37
|
+
* If true, optimization should be disabled for this shader
|
|
38
|
+
*/
|
|
39
|
+
disableOptimization?: boolean;
|
|
40
|
+
/**
|
|
41
|
+
* The shader code
|
|
42
|
+
*/
|
|
43
|
+
shaderCode: ShaderCode;
|
|
44
|
+
/**
|
|
45
|
+
* The set of uniforms
|
|
46
|
+
*/
|
|
47
|
+
uniforms: UniformMetadata[];
|
|
48
|
+
};
|
|
49
|
+
/**
|
|
50
|
+
* Parses a fragment shader
|
|
51
|
+
* @param fragmentShader - The fragment shader to process
|
|
52
|
+
* @returns The processed fragment shader
|
|
53
|
+
*/
|
|
54
|
+
export declare function parseFragmentShader(fragmentShader: string): FragmentShaderInfo;
|
|
55
|
+
/**
|
|
56
|
+
* Determines if a fragment shader has the GLSL header required for parsing
|
|
57
|
+
* @param fragmentShader - The fragment shader to check
|
|
58
|
+
* @returns True if the fragment shader has the GLSL header
|
|
59
|
+
*/
|
|
60
|
+
export declare function hasGlslHeader(fragmentShader: string): boolean;
|
|
13
61
|
//# sourceMappingURL=shaderConverter.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"shaderConverter.d.ts","sourceRoot":"","sources":["../../../src/utils/buildTools/shaderConverter.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"shaderConverter.d.ts","sourceRoot":"","sources":["../../../src/utils/buildTools/shaderConverter.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,UAAU,EAAkB,MAAM,oBAAoB,CAAC;AAIrE;;GAEG;AACH,MAAM,MAAM,yBAAyB,GAAG;IACpC;;OAEG;IACH,OAAO,CAAC,EAAE,GAAG,CAAC;CACjB,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,eAAe,GAAG;IAC1B;;OAEG;IACH,IAAI,EAAE,MAAM,CAAC;IAEb;;OAEG;IACH,IAAI,EAAE,MAAM,CAAC;IAEb;;OAEG;IACH,UAAU,CAAC,EAAE,yBAAyB,CAAC;CAC1C,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,kBAAkB,GAAG;IAC7B;;OAEG;IACH,SAAS,CAAC,EAAE,MAAM,CAAC;IAEnB;;OAEG;IACH,mBAAmB,CAAC,EAAE,OAAO,CAAC;IAE9B;;OAEG;IACH,UAAU,EAAE,UAAU,CAAC;IAEvB;;OAEG;IACH,QAAQ,EAAE,eAAe,EAAE,CAAC;CAC/B,CAAC;AAEF;;;;GAIG;AACH,wBAAgB,mBAAmB,CAAC,cAAc,EAAE,MAAM,GAAG,kBAAkB,CAsG9E;AAiMD;;;;GAIG;AACH,wBAAgB,aAAa,CAAC,cAAc,EAAE,MAAM,GAAG,OAAO,CAE7D"}
|
|
@@ -1,128 +1,62 @@
|
|
|
1
|
-
import * as fs from "fs";
|
|
2
|
-
import * as path from "path";
|
|
3
|
-
const TYPE_IMPORT_PATH = "@TYPE_IMPORT_PATH@";
|
|
4
|
-
const VERTEX_SHADER = "@VERTEX_SHADER@";
|
|
5
|
-
const UNIFORMS = "@UNIFORMS@";
|
|
6
|
-
const CONSTS_VALUE = "@CONSTS@";
|
|
7
|
-
const CONSTS_PROPERTY = "@CONSTS_PROPERTY@";
|
|
8
|
-
const MAIN_INPUT_NAME = "@MAIN_INPUT_NAME@";
|
|
9
|
-
const MAIN_FUNCTION_NAME = "@MAIN_FUNCTION_NAME@";
|
|
10
|
-
const FUNCTIONS = "@FUNCTIONS@";
|
|
11
|
-
const FUNCTION_NAME = "@FUNCTION_NAME@";
|
|
12
|
-
const FUNCTION_CODE = "@FUNCTION_CODE@";
|
|
13
|
-
const UNIFORM_NAMES = "@UNIFORM_NAMES@";
|
|
14
|
-
const ConstsTemplate = `
|
|
15
|
-
const: \`${CONSTS_VALUE}\`,`;
|
|
16
|
-
const FunctionTemplate = `
|
|
17
|
-
{
|
|
18
|
-
name: "${FUNCTION_NAME}",
|
|
19
|
-
code: \`
|
|
20
|
-
${FUNCTION_CODE}
|
|
21
|
-
\`,
|
|
22
|
-
},`;
|
|
23
|
-
const CodeLinePrefix = " ";
|
|
24
|
-
const UniformLinePrefix = " ";
|
|
25
|
-
const ConstLinePrefix = " ";
|
|
26
|
-
const ShaderTemplate = `import type { ShaderProgram } from "${TYPE_IMPORT_PATH}";
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* The shader program for the block.
|
|
30
|
-
*/
|
|
31
|
-
export const shaderProgram: ShaderProgram = {
|
|
32
|
-
vertex: ${VERTEX_SHADER},
|
|
33
|
-
fragment: {
|
|
34
|
-
uniform: \`${UNIFORMS}\`,${CONSTS_PROPERTY}
|
|
35
|
-
mainInputTexture: "${MAIN_INPUT_NAME}",
|
|
36
|
-
mainFunctionName: "${MAIN_FUNCTION_NAME}",
|
|
37
|
-
functions: [${FUNCTIONS}
|
|
38
|
-
],
|
|
39
|
-
},
|
|
40
|
-
};
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* The uniform names for this shader, to be used in the shader binding so
|
|
44
|
-
* that the names are always in sync.
|
|
45
|
-
*/
|
|
46
|
-
export const uniforms = {
|
|
47
|
-
${UNIFORM_NAMES}
|
|
48
|
-
};
|
|
49
|
-
`;
|
|
50
|
-
const UniformNameLinePrefix = " ";
|
|
51
1
|
const GetFunctionNamesRegEx = /\S*\w+\s+(\w+)\s*\(/g;
|
|
52
2
|
/**
|
|
53
|
-
*
|
|
54
|
-
* @param fragmentShaderPath - The path to the fragment file for the shader
|
|
55
|
-
* @param importPath - The path to import the ShaderProgram type from
|
|
56
|
-
*/
|
|
57
|
-
export function convertShader(fragmentShaderPath, importPath) {
|
|
58
|
-
console.log(`Processing fragment shader: ${fragmentShaderPath}`);
|
|
59
|
-
// See if there is a corresponding vertex shader
|
|
60
|
-
let vertexShader = undefined;
|
|
61
|
-
const vertexShaderPath = fragmentShaderPath.replace(".fragment.glsl", ".vertex.glsl");
|
|
62
|
-
if (fs.existsSync(vertexShaderPath)) {
|
|
63
|
-
vertexShader = fs.readFileSync(vertexShaderPath, "utf8");
|
|
64
|
-
}
|
|
65
|
-
if (vertexShader) {
|
|
66
|
-
console.log("Found vertex shader");
|
|
67
|
-
}
|
|
68
|
-
// Read the fragment shader
|
|
69
|
-
const fragmentShader = fs.readFileSync(fragmentShaderPath, "utf8");
|
|
70
|
-
// Version check
|
|
71
|
-
const versionsFound = [...fragmentShader.matchAll(/\/\/\s+\[Smart Filter Shader Version\]\s*=\s*(\d+)/g)].map((match) => match[1]);
|
|
72
|
-
let version = 1;
|
|
73
|
-
if (versionsFound.length === 1 && versionsFound[0]) {
|
|
74
|
-
version = parseInt(versionsFound[0]);
|
|
75
|
-
}
|
|
76
|
-
console.log(`Shader version: ${version}`);
|
|
77
|
-
let fragmentShaderInfo;
|
|
78
|
-
switch (version) {
|
|
79
|
-
case 1:
|
|
80
|
-
{
|
|
81
|
-
fragmentShaderInfo = processFragmentShaderV1(fragmentShader);
|
|
82
|
-
}
|
|
83
|
-
break;
|
|
84
|
-
default: {
|
|
85
|
-
throw new Error(`Unsupported shader version: ${version}`);
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
// Write the shader TS file
|
|
89
|
-
const shaderFile = fragmentShaderPath.replace(".fragment.glsl", ".shader.ts");
|
|
90
|
-
const finalContents = ShaderTemplate.replace(VERTEX_SHADER, vertexShader ? `\`${vertexShader}\`` : "undefined")
|
|
91
|
-
.replace(TYPE_IMPORT_PATH, importPath)
|
|
92
|
-
.replace(UNIFORMS, "\n" + addLinePrefixes(fragmentShaderInfo.finalUniforms.join("\n"), UniformLinePrefix))
|
|
93
|
-
.replace(MAIN_FUNCTION_NAME, fragmentShaderInfo.mainFunctionName)
|
|
94
|
-
.replace(MAIN_INPUT_NAME, fragmentShaderInfo.mainInputName)
|
|
95
|
-
.replace(CONSTS_PROPERTY, fragmentShaderInfo.finalConsts.length > 0
|
|
96
|
-
? ConstsTemplate.replace(CONSTS_VALUE, addLinePrefixes(fragmentShaderInfo.finalConsts.join("\n"), ConstLinePrefix))
|
|
97
|
-
: "")
|
|
98
|
-
.replace(FUNCTIONS, fragmentShaderInfo.extractedFunctions.join(""))
|
|
99
|
-
.replace(UNIFORM_NAMES, addLinePrefixes(fragmentShaderInfo.uniformNames.join("\n"), UniformNameLinePrefix));
|
|
100
|
-
fs.writeFileSync(shaderFile, finalContents);
|
|
101
|
-
}
|
|
102
|
-
/**
|
|
103
|
-
* Processes a fragment shader
|
|
3
|
+
* Parses a fragment shader
|
|
104
4
|
* @param fragmentShader - The fragment shader to process
|
|
105
5
|
* @returns The processed fragment shader
|
|
106
6
|
*/
|
|
107
|
-
function
|
|
7
|
+
export function parseFragmentShader(fragmentShader) {
|
|
8
|
+
const { header, fragmentShaderWithoutHeader } = readHeader(fragmentShader);
|
|
9
|
+
fragmentShader = fragmentShaderWithoutHeader;
|
|
10
|
+
const blockType = (header === null || header === void 0 ? void 0 : header[SmartFilterBlockTypeKey]) || undefined;
|
|
11
|
+
// Read the uniforms
|
|
12
|
+
const uniforms = [];
|
|
13
|
+
const uniformRegExp = new RegExp(/(\/\/\s*\{.*\}\n)?(uniform .*)/gm);
|
|
14
|
+
const uniformGroups = fragmentShader.matchAll(uniformRegExp);
|
|
15
|
+
for (const matches of uniformGroups) {
|
|
16
|
+
const annotationJSON = matches[1];
|
|
17
|
+
const uniformLine = matches[2];
|
|
18
|
+
if (!uniformLine) {
|
|
19
|
+
throw new Error("Uniform line not found");
|
|
20
|
+
}
|
|
21
|
+
const uniformLineMatches = new RegExp(/^uniform\s+(\w+)\s+(\w+)\s*;?/gm).exec(uniformLine);
|
|
22
|
+
if (!uniformLineMatches || uniformLineMatches.length < 3) {
|
|
23
|
+
throw new Error(`Uniforms must have a type and a name: '${uniformLine}'`);
|
|
24
|
+
}
|
|
25
|
+
const uniformTypeString = uniformLineMatches[1];
|
|
26
|
+
const uniformName = uniformLineMatches[2];
|
|
27
|
+
if (!uniformTypeString) {
|
|
28
|
+
throw new Error(`Uniforms must have a type: '${uniformLine}'`);
|
|
29
|
+
}
|
|
30
|
+
if (!uniformName) {
|
|
31
|
+
throw new Error(`Uniforms must have a name: '${uniformLine}'`);
|
|
32
|
+
}
|
|
33
|
+
uniforms.push({
|
|
34
|
+
name: uniformName,
|
|
35
|
+
type: uniformTypeString,
|
|
36
|
+
properties: annotationJSON ? JSON.parse(annotationJSON.replace("//", "").trim()) : undefined,
|
|
37
|
+
});
|
|
38
|
+
if (annotationJSON) {
|
|
39
|
+
// Strip out any annotation so it isn't mistaken for function bodies
|
|
40
|
+
fragmentShader = fragmentShader.replace(annotationJSON, "");
|
|
41
|
+
}
|
|
42
|
+
}
|
|
108
43
|
const fragmentShaderWithNoFunctionBodies = removeFunctionBodies(fragmentShader);
|
|
109
44
|
// Collect uniform, const, and function names which need to be decorated
|
|
110
45
|
// eslint-disable-next-line prettier/prettier
|
|
111
|
-
const
|
|
46
|
+
const uniformNames = uniforms.map((uniform) => uniform.name);
|
|
112
47
|
console.log(`Uniforms found: ${JSON.stringify(uniforms)}`);
|
|
113
48
|
const consts = [...fragmentShader.matchAll(/\S*const\s+\w*\s+(\w*)\s*=.*;/g)].map((match) => match[1]);
|
|
114
49
|
console.log(`Consts found: ${JSON.stringify(consts)}`);
|
|
115
50
|
const functionNames = [...fragmentShaderWithNoFunctionBodies.matchAll(GetFunctionNamesRegEx)].map((match) => match[1]);
|
|
116
51
|
console.log(`Functions found: ${JSON.stringify(functionNames)}`);
|
|
117
52
|
// Decorate the uniforms, consts, and functions
|
|
118
|
-
const symbolsToDecorate = [...
|
|
53
|
+
const symbolsToDecorate = [...uniformNames, ...consts, ...functionNames];
|
|
119
54
|
let fragmentShaderWithRenamedSymbols = fragmentShader;
|
|
120
55
|
for (const symbol of symbolsToDecorate) {
|
|
121
56
|
const regex = new RegExp(`(?<=\\W+)${symbol}(?=\\W+)`, "gs");
|
|
122
57
|
fragmentShaderWithRenamedSymbols = fragmentShaderWithRenamedSymbols.replace(regex, `_${symbol}_`);
|
|
123
58
|
}
|
|
124
59
|
console.log(`${symbolsToDecorate.length} symbol(s) renamed`);
|
|
125
|
-
const uniformNames = uniforms.map((uniform) => `${uniform}: "${uniform}",`);
|
|
126
60
|
// Extract all the uniforms
|
|
127
61
|
const finalUniforms = [...fragmentShaderWithRenamedSymbols.matchAll(/^\s*(uniform\s.*)/gm)].map((match) => match[1]);
|
|
128
62
|
// Extract all the consts
|
|
@@ -132,29 +66,24 @@ function processFragmentShaderV1(fragmentShader) {
|
|
|
132
66
|
if (mainInputs.length !== 1 || !mainInputs[0]) {
|
|
133
67
|
throw new Error("Exactly one main input must be defined in the shader");
|
|
134
68
|
}
|
|
135
|
-
const
|
|
69
|
+
const mainInputTexture = mainInputs[0];
|
|
136
70
|
// Extract all the functions
|
|
137
71
|
const { extractedFunctions, mainFunctionName } = extractFunctions(fragmentShaderWithRenamedSymbols);
|
|
138
|
-
|
|
139
|
-
finalUniforms,
|
|
72
|
+
const shaderCode = {
|
|
73
|
+
uniform: finalUniforms.join("\n"),
|
|
140
74
|
mainFunctionName,
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
75
|
+
mainInputTexture,
|
|
76
|
+
functions: extractedFunctions,
|
|
77
|
+
};
|
|
78
|
+
if (finalConsts.length > 0) {
|
|
79
|
+
shaderCode.const = finalConsts.join("\n");
|
|
80
|
+
}
|
|
81
|
+
return {
|
|
82
|
+
blockType,
|
|
83
|
+
shaderCode,
|
|
84
|
+
uniforms,
|
|
85
|
+
disableOptimization: !!(header === null || header === void 0 ? void 0 : header.disableOptimizer),
|
|
145
86
|
};
|
|
146
|
-
}
|
|
147
|
-
/**
|
|
148
|
-
* Prefixes each line in the input
|
|
149
|
-
* @param input - The input string
|
|
150
|
-
* @param prefix - The prefix to add to each line
|
|
151
|
-
* @returns The input with each line prefixed
|
|
152
|
-
*/
|
|
153
|
-
function addLinePrefixes(input, prefix) {
|
|
154
|
-
return input
|
|
155
|
-
.split("\n")
|
|
156
|
-
.map((line) => prefix + line)
|
|
157
|
-
.join("\n");
|
|
158
87
|
}
|
|
159
88
|
/**
|
|
160
89
|
* Extracts all the functions from the shader
|
|
@@ -186,7 +115,10 @@ function extractFunctions(fragment) {
|
|
|
186
115
|
if (inFunction && depth === 0) {
|
|
187
116
|
inFunction = false;
|
|
188
117
|
const { functionBody, functionName, isMainFunction } = processFunctionBody(currentFunction);
|
|
189
|
-
extractedFunctions.push(
|
|
118
|
+
extractedFunctions.push({
|
|
119
|
+
name: functionName,
|
|
120
|
+
code: functionBody,
|
|
121
|
+
});
|
|
190
122
|
if (isMainFunction) {
|
|
191
123
|
if (mainFunctionName) {
|
|
192
124
|
throw new Error("Multiple main functions found in shader code");
|
|
@@ -251,20 +183,38 @@ function removeFunctionBodies(input) {
|
|
|
251
183
|
}
|
|
252
184
|
return output;
|
|
253
185
|
}
|
|
186
|
+
const SmartFilterBlockTypeKey = "smartFilterBlockType";
|
|
254
187
|
/**
|
|
255
|
-
*
|
|
256
|
-
* @param
|
|
257
|
-
* @
|
|
188
|
+
* Reads the GlslHeader from the shader code
|
|
189
|
+
* @param fragmentShader - The shader code to read
|
|
190
|
+
* @returns - The GlslHeader if found, otherwise null
|
|
258
191
|
*/
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
convertShader(path.join(fragmentShaderFile.path, fragmentShaderFile.name), importPath);
|
|
192
|
+
function readHeader(fragmentShader) {
|
|
193
|
+
const singleLineHeaderMatch = new RegExp(/^\/\/\s*(\{.*\})/gm).exec(fragmentShader);
|
|
194
|
+
if (singleLineHeaderMatch && singleLineHeaderMatch[1]) {
|
|
195
|
+
return {
|
|
196
|
+
header: JSON.parse(singleLineHeaderMatch[1].trim()),
|
|
197
|
+
fragmentShaderWithoutHeader: fragmentShader.replace(singleLineHeaderMatch[0], ""),
|
|
198
|
+
};
|
|
267
199
|
}
|
|
200
|
+
const multiLineHeaderMatch = new RegExp("^\\/\\*(.*)\\*\\/", "gs").exec(fragmentShader);
|
|
201
|
+
if (multiLineHeaderMatch && multiLineHeaderMatch[1]) {
|
|
202
|
+
return {
|
|
203
|
+
header: JSON.parse(multiLineHeaderMatch[1].trim()),
|
|
204
|
+
fragmentShaderWithoutHeader: fragmentShader.replace(multiLineHeaderMatch[0], ""),
|
|
205
|
+
};
|
|
206
|
+
}
|
|
207
|
+
return {
|
|
208
|
+
header: null,
|
|
209
|
+
fragmentShaderWithoutHeader: fragmentShader,
|
|
210
|
+
};
|
|
211
|
+
}
|
|
212
|
+
/**
|
|
213
|
+
* Determines if a fragment shader has the GLSL header required for parsing
|
|
214
|
+
* @param fragmentShader - The fragment shader to check
|
|
215
|
+
* @returns True if the fragment shader has the GLSL header
|
|
216
|
+
*/
|
|
217
|
+
export function hasGlslHeader(fragmentShader) {
|
|
218
|
+
return fragmentShader.indexOf(SmartFilterBlockTypeKey) !== -1;
|
|
268
219
|
}
|
|
269
|
-
// TODO: simple copy from shader file to .ts, get it to build (including import trick)
|
|
270
220
|
//# sourceMappingURL=shaderConverter.js.map
|