@babylonjs/smart-filters 0.4.3-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.d.ts +3 -7
- package/dist/blocks/shaderBlock.d.ts.map +1 -1
- package/dist/blocks/shaderBlock.js +45 -15
- package/dist/blocks/shaderBlock.js.map +1 -1
- package/dist/blocks/textureOptions.d.ts +47 -0
- package/dist/blocks/textureOptions.d.ts.map +1 -0
- package/dist/blocks/textureOptions.js +37 -0
- package/dist/blocks/textureOptions.js.map +1 -0
- 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/editorUtils/editableInPropertyPage.d.ts +4 -0
- package/dist/editorUtils/editableInPropertyPage.d.ts.map +1 -1
- package/dist/editorUtils/editableInPropertyPage.js +1 -0
- package/dist/editorUtils/editableInPropertyPage.js.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.d.ts +1 -1
- package/dist/optimization/smartFilterOptimizer.d.ts.map +1 -1
- package/dist/optimization/smartFilterOptimizer.js +10 -9
- package/dist/optimization/smartFilterOptimizer.js.map +1 -1
- package/dist/runtime/renderTargetGenerator.d.ts +8 -1
- package/dist/runtime/renderTargetGenerator.d.ts.map +1 -1
- package/dist/runtime/renderTargetGenerator.js +37 -18
- package/dist/runtime/renderTargetGenerator.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 +36 -15
- package/src/blocks/textureOptions.ts +57 -0
- package/src/command/command.ts +2 -3
- package/src/command/commandBufferDebugger.ts +1 -1
- package/src/connection/connectionPointType.ts +11 -0
- package/src/editorUtils/editableInPropertyPage.ts +5 -0
- package/src/index.ts +2 -0
- package/src/optimization/smartFilterOptimizer.ts +11 -10
- package/src/runtime/renderTargetGenerator.ts +55 -20
- 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
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ----------------------------------------------------------------------------
|
|
3
|
+
* Data Types Used For Block Serialization
|
|
4
|
+
* ----------------------------------------------------------------------------
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import type { AllConnectionPointTypes, ConnectionPointValue } from "../../connection/connectionPointType";
|
|
8
|
+
import type { ShaderProgram } from "../../utils/shaderCodeUtils";
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* The V1 definition of a serialized block. A block definition is loaded by a CustomShaderBlock and defines how a
|
|
12
|
+
* blockType works. This should not be confused with an ISerializedBockV1, which is a serialized instance of a block in a
|
|
13
|
+
* serialized SmartFilter graph. It is referenced by blockType in a serialized SmartFilter.
|
|
14
|
+
*/
|
|
15
|
+
export type SerializedBlockDefinitionV1 = {
|
|
16
|
+
/**
|
|
17
|
+
* The version of the block definition format (format of the serialized data, not the version of the block definition itself).
|
|
18
|
+
*/
|
|
19
|
+
formatVersion: 1;
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* The type used to refer to the block in serialized SmartFilters and in the editor UI.
|
|
23
|
+
* The application doing the deserialization will use this to instantiate the correct block definition.
|
|
24
|
+
* Block types are expected to be unique and their behavior should be semantically equivalent across implementations
|
|
25
|
+
* (their results must be similar enough that the differences are not perceivable).
|
|
26
|
+
*/
|
|
27
|
+
blockType: string;
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* The shader program for the block.
|
|
31
|
+
*/
|
|
32
|
+
shaderProgram: ShaderProgram;
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* The input connection points of the block.
|
|
36
|
+
*/
|
|
37
|
+
inputConnectionPoints: SerializedInputConnectionPointV1[];
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* If true, the optimizer will not attempt to optimize this block.
|
|
41
|
+
*/
|
|
42
|
+
disableOptimization: boolean;
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* A V1 input connection point of a serialized block definition.
|
|
47
|
+
*/
|
|
48
|
+
export type SerializedInputConnectionPointV1<U extends AllConnectionPointTypes = AllConnectionPointTypes> = {
|
|
49
|
+
/**
|
|
50
|
+
* The name of the connection point.
|
|
51
|
+
*/
|
|
52
|
+
name: string;
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* The type of the connection point.
|
|
56
|
+
*/
|
|
57
|
+
type: U;
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* The optional default value of the connection point.
|
|
61
|
+
*/
|
|
62
|
+
defaultValue?: ConnectionPointValue<U>;
|
|
63
|
+
};
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { BaseBlock } from "../../blocks/baseBlock";
|
|
2
|
-
import type { ISerializedBlockV1, SerializeBlockV1 } from "./
|
|
2
|
+
import type { ISerializedBlockV1, SerializeBlockV1 } from "./smartFilterSerialization.types";
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* The default V1 block serializer which can be used for any block that relies only on ConnectionPoints
|
|
@@ -11,7 +11,7 @@ export const defaultBlockSerializer: SerializeBlockV1 = (block: BaseBlock): ISer
|
|
|
11
11
|
return {
|
|
12
12
|
name: block.name,
|
|
13
13
|
uniqueId: block.uniqueId,
|
|
14
|
-
|
|
14
|
+
blockType: block.blockType,
|
|
15
15
|
comments: block.comments,
|
|
16
16
|
data: undefined,
|
|
17
17
|
};
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
export * from "./defaultBlockSerializer.js";
|
|
2
|
-
export * from "./
|
|
2
|
+
export * from "./smartFilterSerialization.types.js";
|
|
@@ -14,8 +14,8 @@ import type { IEditorData } from "@babylonjs/shared-ui-components/nodeGraphSyste
|
|
|
14
14
|
* V1 Serialized Smart Filter
|
|
15
15
|
*/
|
|
16
16
|
export type SerializedSmartFilterV1 = {
|
|
17
|
-
/** The version of the serialized data
|
|
18
|
-
|
|
17
|
+
/** The format version of the serialized data (not the version of the SmartFilter itself).*/
|
|
18
|
+
formatVersion: 1;
|
|
19
19
|
|
|
20
20
|
/** The SmartFilter name */
|
|
21
21
|
name: string;
|
|
@@ -34,7 +34,8 @@ export type SerializedSmartFilterV1 = {
|
|
|
34
34
|
};
|
|
35
35
|
|
|
36
36
|
/**
|
|
37
|
-
* V1
|
|
37
|
+
* V1 format of a block in a serialized Smart Filter.
|
|
38
|
+
* Not to be confused with a SerializedBlockDefinitionV1 which serializes the definition of a CustomShaderBlock.
|
|
38
39
|
*/
|
|
39
40
|
export interface ISerializedBlockV1 {
|
|
40
41
|
/** The name of the block */
|
|
@@ -43,8 +44,8 @@ export interface ISerializedBlockV1 {
|
|
|
43
44
|
/** The unique ID of the block - correlates with the ID in the editorData for block position, etc. */
|
|
44
45
|
uniqueId: number;
|
|
45
46
|
|
|
46
|
-
/** The
|
|
47
|
-
|
|
47
|
+
/** The blockType of the block - used to determine how to instantiate the block during deserialization */
|
|
48
|
+
blockType: string;
|
|
48
49
|
|
|
49
50
|
/** The comments for the block */
|
|
50
51
|
comments: Nullable<string>;
|
|
@@ -82,13 +83,13 @@ export interface ISerializedConnectionV1 {
|
|
|
82
83
|
export type SerializeBlockV1 = (block: BaseBlock) => ISerializedBlockV1;
|
|
83
84
|
|
|
84
85
|
/**
|
|
85
|
-
* A V1
|
|
86
|
+
* A V1 serializer for blocks in a SmartFilter
|
|
86
87
|
*/
|
|
87
88
|
export interface IBlockSerializerV1 {
|
|
88
|
-
/** The
|
|
89
|
-
|
|
89
|
+
/** The blockType of the block that this serializer can serialize */
|
|
90
|
+
blockType: string;
|
|
90
91
|
|
|
91
|
-
/** The function that serializes the block */
|
|
92
|
+
/** The function that serializes the block in the Smart Filter */
|
|
92
93
|
serialize: SerializeBlockV1;
|
|
93
94
|
}
|
|
94
95
|
|
|
@@ -99,7 +100,7 @@ export interface IBlockSerializerV1 {
|
|
|
99
100
|
*/
|
|
100
101
|
|
|
101
102
|
/**
|
|
102
|
-
* A function that deserializes a block
|
|
103
|
+
* A function that deserializes a V1 block in a SmartFilter
|
|
103
104
|
*/
|
|
104
105
|
export type DeserializeBlockV1 = (
|
|
105
106
|
smartFilter: SmartFilter,
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* @example node buildShaders.js <shaderPath> <importPath>
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
|
-
import { convertShaders } from "./
|
|
8
|
+
import { convertShaders } from "./convertShaderForHardcodedBlock.js";
|
|
9
9
|
|
|
10
10
|
const externalArguments = process.argv.slice(2);
|
|
11
11
|
if (externalArguments.length >= 2 && externalArguments[0] && externalArguments[1]) {
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
import * as fs from "fs";
|
|
2
|
+
import * as path from "path";
|
|
3
|
+
import { parseFragmentShader } from "./shaderConverter.js";
|
|
4
|
+
|
|
5
|
+
const TYPE_IMPORT_PATH = "@TYPE_IMPORT_PATH@";
|
|
6
|
+
const VERTEX_SHADER = "@VERTEX_SHADER@";
|
|
7
|
+
const UNIFORMS = "@UNIFORMS@";
|
|
8
|
+
const CONSTS_VALUE = "@CONSTS@";
|
|
9
|
+
const CONSTS_PROPERTY = "@CONSTS_PROPERTY@";
|
|
10
|
+
const MAIN_INPUT_NAME = "@MAIN_INPUT_NAME@";
|
|
11
|
+
const MAIN_FUNCTION_NAME = "@MAIN_FUNCTION_NAME@";
|
|
12
|
+
const FUNCTIONS = "@FUNCTIONS@";
|
|
13
|
+
const FUNCTION_NAME = "@FUNCTION_NAME@";
|
|
14
|
+
const FUNCTION_CODE = "@FUNCTION_CODE@";
|
|
15
|
+
const UNIFORM_NAMES = "@UNIFORM_NAMES@";
|
|
16
|
+
|
|
17
|
+
const ConstsTemplate = `
|
|
18
|
+
const: \`${CONSTS_VALUE}\`,`;
|
|
19
|
+
|
|
20
|
+
const FunctionTemplate = `
|
|
21
|
+
{
|
|
22
|
+
name: "${FUNCTION_NAME}",
|
|
23
|
+
code: \`
|
|
24
|
+
${FUNCTION_CODE}
|
|
25
|
+
\`,
|
|
26
|
+
},`;
|
|
27
|
+
|
|
28
|
+
const CodeLinePrefix = " ";
|
|
29
|
+
const UniformLinePrefix = " ";
|
|
30
|
+
const ConstLinePrefix = " ";
|
|
31
|
+
|
|
32
|
+
const ShaderTemplate = `import type { ShaderProgram } from "${TYPE_IMPORT_PATH}";
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* The shader program for the block.
|
|
36
|
+
*/
|
|
37
|
+
export const shaderProgram: ShaderProgram = {
|
|
38
|
+
vertex: ${VERTEX_SHADER},
|
|
39
|
+
fragment: {
|
|
40
|
+
uniform: \`${UNIFORMS}\`,${CONSTS_PROPERTY}
|
|
41
|
+
mainInputTexture: "${MAIN_INPUT_NAME}",
|
|
42
|
+
mainFunctionName: "${MAIN_FUNCTION_NAME}",
|
|
43
|
+
functions: [${FUNCTIONS}
|
|
44
|
+
],
|
|
45
|
+
},
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* The uniform names for this shader, to be used in the shader binding so
|
|
50
|
+
* that the names are always in sync.
|
|
51
|
+
*/
|
|
52
|
+
export const uniforms = {
|
|
53
|
+
${UNIFORM_NAMES}
|
|
54
|
+
};
|
|
55
|
+
`;
|
|
56
|
+
|
|
57
|
+
const UniformNameLinePrefix = " ";
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Converts a single shader to a .ts file which can be imported by a hardcoded block
|
|
61
|
+
* @param fragmentShaderPath - The path to the fragment file for the shader
|
|
62
|
+
* @param importPath - The path to import the ShaderProgram type from
|
|
63
|
+
*/
|
|
64
|
+
export function convertShaderForHardcodedBlock(fragmentShaderPath: string, importPath: string): void {
|
|
65
|
+
console.log(`Processing fragment shader: ${fragmentShaderPath}`);
|
|
66
|
+
|
|
67
|
+
// See if there is a corresponding vertex shader
|
|
68
|
+
let vertexShader: string | undefined = undefined;
|
|
69
|
+
const vertexShaderPath = fragmentShaderPath.replace(".fragment.glsl", ".vertex.glsl");
|
|
70
|
+
if (fs.existsSync(vertexShaderPath)) {
|
|
71
|
+
vertexShader = fs.readFileSync(vertexShaderPath, "utf8");
|
|
72
|
+
}
|
|
73
|
+
if (vertexShader) {
|
|
74
|
+
console.log("Found vertex shader");
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// Read the fragment shader
|
|
78
|
+
const fragmentShader = fs.readFileSync(fragmentShaderPath, "utf8");
|
|
79
|
+
const fragmentShaderInfo = parseFragmentShader(fragmentShader);
|
|
80
|
+
|
|
81
|
+
// Write the shader TS file
|
|
82
|
+
const shaderFile = fragmentShaderPath.replace(".fragment.glsl", ".shader.ts");
|
|
83
|
+
const functionsSection: string[] = [];
|
|
84
|
+
for (const shaderFunction of fragmentShaderInfo.shaderCode.functions) {
|
|
85
|
+
functionsSection.push(
|
|
86
|
+
FunctionTemplate.replace(FUNCTION_NAME, shaderFunction.name).replace(
|
|
87
|
+
FUNCTION_CODE,
|
|
88
|
+
addLinePrefixes(shaderFunction.code, CodeLinePrefix)
|
|
89
|
+
)
|
|
90
|
+
);
|
|
91
|
+
}
|
|
92
|
+
const finalContents = ShaderTemplate.replace(VERTEX_SHADER, vertexShader ? `\`${vertexShader}\`` : "undefined")
|
|
93
|
+
.replace(TYPE_IMPORT_PATH, importPath)
|
|
94
|
+
.replace(UNIFORMS, "\n" + addLinePrefixes(fragmentShaderInfo.shaderCode.uniform || "", UniformLinePrefix))
|
|
95
|
+
.replace(MAIN_FUNCTION_NAME, fragmentShaderInfo.shaderCode.mainFunctionName)
|
|
96
|
+
.replace(MAIN_INPUT_NAME, fragmentShaderInfo.shaderCode.mainInputTexture || '""')
|
|
97
|
+
.replace(
|
|
98
|
+
CONSTS_PROPERTY,
|
|
99
|
+
fragmentShaderInfo.shaderCode.const
|
|
100
|
+
? ConstsTemplate.replace(
|
|
101
|
+
CONSTS_VALUE,
|
|
102
|
+
addLinePrefixes(fragmentShaderInfo.shaderCode.const, ConstLinePrefix)
|
|
103
|
+
)
|
|
104
|
+
: ""
|
|
105
|
+
)
|
|
106
|
+
.replace(FUNCTIONS, functionsSection.join(""))
|
|
107
|
+
.replace(
|
|
108
|
+
UNIFORM_NAMES,
|
|
109
|
+
addLinePrefixes(
|
|
110
|
+
fragmentShaderInfo.uniforms.map((u) => `${u.name}: "${u.name}",`).join("\n"),
|
|
111
|
+
UniformNameLinePrefix
|
|
112
|
+
)
|
|
113
|
+
);
|
|
114
|
+
|
|
115
|
+
fs.writeFileSync(shaderFile, finalContents);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Converts .fragment.glsl and vertex.glsl file pairs into .shader.ts files which export a ShaderProgram object.
|
|
120
|
+
* @param shaderPath - The path to the .glsl files to convert.
|
|
121
|
+
* @param importPath - The path to import the ShaderProgram type from.
|
|
122
|
+
*/
|
|
123
|
+
export function convertShaders(shaderPath: string, importPath: string) {
|
|
124
|
+
// Get all files in the path
|
|
125
|
+
const allFiles = fs.readdirSync(shaderPath, { withFileTypes: true, recursive: true });
|
|
126
|
+
|
|
127
|
+
// Find all fragment shaders (excluding the template)
|
|
128
|
+
const fragmentShaderFiles = allFiles.filter(
|
|
129
|
+
(file) => file.isFile() && file.name.endsWith(".fragment.glsl") && !file.name.endsWith("template.fragment.glsl")
|
|
130
|
+
);
|
|
131
|
+
|
|
132
|
+
// Convert all shaders
|
|
133
|
+
for (const fragmentShaderFile of fragmentShaderFiles) {
|
|
134
|
+
convertShaderForHardcodedBlock(path.join(fragmentShaderFile.path, fragmentShaderFile.name), importPath);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Prefixes each line in the input
|
|
140
|
+
* @param input - The input string
|
|
141
|
+
* @param prefix - The prefix to add to each line
|
|
142
|
+
* @returns The input with each line prefixed
|
|
143
|
+
*/
|
|
144
|
+
function addLinePrefixes(input: string, prefix: string): string {
|
|
145
|
+
return input
|
|
146
|
+
.split("\n")
|
|
147
|
+
.map((line) => prefix + line)
|
|
148
|
+
.join("\n");
|
|
149
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Describes a shader function.
|
|
3
|
+
*/
|
|
4
|
+
export type ShaderFunction = {
|
|
5
|
+
/**
|
|
6
|
+
* The name of the function.
|
|
7
|
+
*/
|
|
8
|
+
name: string;
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* The code of the function.
|
|
12
|
+
*/
|
|
13
|
+
code: string;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Describes a shader code.
|
|
18
|
+
*/
|
|
19
|
+
export type ShaderCode = {
|
|
20
|
+
/**
|
|
21
|
+
* The declaration of the const variables.
|
|
22
|
+
*/
|
|
23
|
+
const?: string;
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* The declaration of the uniform variables.
|
|
27
|
+
*/
|
|
28
|
+
uniform?: string;
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* The declaration of the uniform variables that should be common for all ShaderBlock instances using this shader code.
|
|
32
|
+
*/
|
|
33
|
+
uniformSingle?: string;
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* The name of the main function.
|
|
37
|
+
*/
|
|
38
|
+
mainFunctionName: string;
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* The name of the input texture which is passed through if the block is disabled.
|
|
42
|
+
*/
|
|
43
|
+
mainInputTexture?: string;
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* The list of functions used in the shader.
|
|
47
|
+
*/
|
|
48
|
+
functions: ShaderFunction[];
|
|
49
|
+
};
|