@onerjs/smart-filters 8.25.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/license.md +21 -0
- package/package.json +52 -0
- package/readme.md +9 -0
- package/src/IDisposable.ts +9 -0
- package/src/blockFoundation/aggregateBlock.ts +148 -0
- package/src/blockFoundation/baseBlock.ts +339 -0
- package/src/blockFoundation/customAggregateBlock.ts +88 -0
- package/src/blockFoundation/customShaderBlock.ts +362 -0
- package/src/blockFoundation/disableableShaderBlock.ts +91 -0
- package/src/blockFoundation/index.ts +9 -0
- package/src/blockFoundation/inputBlock.deserializer.ts +72 -0
- package/src/blockFoundation/inputBlock.serialization.types.ts +126 -0
- package/src/blockFoundation/inputBlock.serializer.ts +150 -0
- package/src/blockFoundation/inputBlock.ts +181 -0
- package/src/blockFoundation/outputBlock.ts +144 -0
- package/src/blockFoundation/shaderBlock.ts +156 -0
- package/src/blockFoundation/textureOptions.ts +57 -0
- package/src/command/command.ts +59 -0
- package/src/command/commandBuffer.ts +71 -0
- package/src/command/commandBufferDebugger.ts +14 -0
- package/src/command/index.ts +7 -0
- package/src/connection/connectionPoint.ts +205 -0
- package/src/connection/connectionPointCompatibilityState.ts +31 -0
- package/src/connection/connectionPointDirection.ts +9 -0
- package/src/connection/connectionPointType.ts +45 -0
- package/src/connection/connectionPointWithDefault.ts +27 -0
- package/src/connection/index.ts +8 -0
- package/src/editorUtils/editableInPropertyPage.ts +106 -0
- package/src/editorUtils/index.ts +3 -0
- package/src/index.ts +16 -0
- package/src/optimization/dependencyGraph.ts +96 -0
- package/src/optimization/index.ts +1 -0
- package/src/optimization/optimizedShaderBlock.ts +131 -0
- package/src/optimization/smartFilterOptimizer.ts +757 -0
- package/src/runtime/index.ts +8 -0
- package/src/runtime/renderTargetGenerator.ts +222 -0
- package/src/runtime/shaderRuntime.ts +174 -0
- package/src/runtime/smartFilterRuntime.ts +112 -0
- package/src/runtime/strongRef.ts +18 -0
- package/src/serialization/importCustomBlockDefinition.ts +86 -0
- package/src/serialization/index.ts +10 -0
- package/src/serialization/serializedBlockDefinition.ts +12 -0
- package/src/serialization/serializedShaderBlockDefinition.ts +7 -0
- package/src/serialization/serializedSmartFilter.ts +6 -0
- package/src/serialization/smartFilterDeserializer.ts +190 -0
- package/src/serialization/smartFilterSerializer.ts +110 -0
- package/src/serialization/v1/defaultBlockSerializer.ts +21 -0
- package/src/serialization/v1/index.ts +4 -0
- package/src/serialization/v1/shaderBlockSerialization.types.ts +85 -0
- package/src/serialization/v1/smartFilterSerialization.types.ts +129 -0
- package/src/smartFilter.ts +255 -0
- package/src/utils/buildTools/buildShaders.ts +14 -0
- package/src/utils/buildTools/convertGlslIntoBlock.ts +370 -0
- package/src/utils/buildTools/convertGlslIntoShaderProgram.ts +173 -0
- package/src/utils/buildTools/convertShaders.ts +65 -0
- package/src/utils/buildTools/recordVersionNumber.js +24 -0
- package/src/utils/buildTools/shaderCode.types.ts +59 -0
- package/src/utils/buildTools/shaderConverter.ts +466 -0
- package/src/utils/buildTools/watchShaders.ts +44 -0
- package/src/utils/index.ts +4 -0
- package/src/utils/renderTargetUtils.ts +30 -0
- package/src/utils/shaderCodeUtils.ts +192 -0
- package/src/utils/textureLoaders.ts +31 -0
- package/src/utils/textureUtils.ts +28 -0
- package/src/utils/uniqueIdGenerator.ts +28 -0
- package/src/version.ts +4 -0
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
import type { BaseBlock } from "../blockFoundation/baseBlock.js";
|
|
2
|
+
import type { SerializedSmartFilter } from "./serializedSmartFilter.js";
|
|
3
|
+
import { SmartFilter } from "../smartFilter.js";
|
|
4
|
+
import { ShaderBlock } from "../blockFoundation/shaderBlock.js";
|
|
5
|
+
import { InputBlockDeserializer } from "../blockFoundation/inputBlock.deserializer.js";
|
|
6
|
+
import { OutputBlock } from "../blockFoundation/outputBlock.js";
|
|
7
|
+
import type { ThinEngine } from "core/Engines/thinEngine.js";
|
|
8
|
+
import { InputBlock } from "../blockFoundation/inputBlock.js";
|
|
9
|
+
import type { ISerializedBlockV1, ISerializedConnectionV1, OptionalBlockDeserializerV1, SerializedSmartFilterV1 } from "./v1/smartFilterSerialization.types.js";
|
|
10
|
+
import { UniqueIdGenerator } from "../utils/uniqueIdGenerator.js";
|
|
11
|
+
import type { Nullable } from "core/types.js";
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* A function that creates a block instance of the given class block type, or return null if it cannot.
|
|
15
|
+
*/
|
|
16
|
+
export type BlockFactory = (
|
|
17
|
+
smartFilter: SmartFilter,
|
|
18
|
+
engine: ThinEngine,
|
|
19
|
+
serializedBlock: ISerializedBlockV1,
|
|
20
|
+
smartFilterDeserializer: SmartFilterDeserializer
|
|
21
|
+
) => Promise<Nullable<BaseBlock>>;
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Deserializes serialized SmartFilters. The caller passes in a map of block deserializers it wants to use,
|
|
25
|
+
* which allows the caller to provide custom deserializers for blocks beyond the core blocks.
|
|
26
|
+
* The deserializer supports versioned serialized SmartFilters.
|
|
27
|
+
*/
|
|
28
|
+
export class SmartFilterDeserializer {
|
|
29
|
+
private readonly _blockFactory: BlockFactory;
|
|
30
|
+
private readonly _customInputBlockDeserializer?: OptionalBlockDeserializerV1;
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Creates a new SmartFilterDeserializer
|
|
34
|
+
* @param blockFactory - A function that creates a block of the given class name, or returns null if it cannot
|
|
35
|
+
* @param customInputBlockDeserializer - An optional custom deserializer for InputBlocks - if supplied and it returns null, the default deserializer will be used
|
|
36
|
+
*/
|
|
37
|
+
public constructor(blockFactory: BlockFactory, customInputBlockDeserializer?: OptionalBlockDeserializerV1) {
|
|
38
|
+
this._blockFactory = blockFactory;
|
|
39
|
+
this._customInputBlockDeserializer = customInputBlockDeserializer;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Deserializes a SmartFilter from a JSON object - can be safely called multiple times and has no side effects within the class.
|
|
44
|
+
* @param engine - The ThinEngine to pass to the new SmartFilter
|
|
45
|
+
* @param smartFilterJson - The JSON object to deserialize
|
|
46
|
+
* @returns A promise that resolves to the deserialized SmartFilter
|
|
47
|
+
*/
|
|
48
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
49
|
+
public async deserialize(engine: ThinEngine, smartFilterJson: any): Promise<SmartFilter> {
|
|
50
|
+
const serializedSmartFilter: SerializedSmartFilter = smartFilterJson;
|
|
51
|
+
|
|
52
|
+
// Back-compat for the rename of version to formatVersion, didn't warrant a new version
|
|
53
|
+
if ((serializedSmartFilter as any).version && serializedSmartFilter.formatVersion === undefined) {
|
|
54
|
+
serializedSmartFilter.formatVersion = (serializedSmartFilter as any).version;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
switch (serializedSmartFilter.formatVersion) {
|
|
58
|
+
case 1:
|
|
59
|
+
return await this._deserializeV1Async(engine, serializedSmartFilter);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
private async _deserializeV1Async(engine: ThinEngine, serializedSmartFilter: SerializedSmartFilterV1): Promise<SmartFilter> {
|
|
64
|
+
const smartFilter = new SmartFilter(serializedSmartFilter.name, serializedSmartFilter.namespace);
|
|
65
|
+
const blockIdMap = new Map<number, BaseBlock>();
|
|
66
|
+
|
|
67
|
+
// Only needed for smart filters saved before we started using uniqueIds for the maps, didn't warrant new version
|
|
68
|
+
const blockNameMap = new Map<string, BaseBlock>();
|
|
69
|
+
|
|
70
|
+
// Deserialize the SmartFilter level data
|
|
71
|
+
smartFilter.comments = serializedSmartFilter.comments;
|
|
72
|
+
smartFilter.editorData = serializedSmartFilter.editorData;
|
|
73
|
+
|
|
74
|
+
// Deserialize the blocks
|
|
75
|
+
const blockDeserializationWork: Promise<void>[] = [];
|
|
76
|
+
const blockDefinitionsWhichCouldNotBeDeserialized: string[] = [];
|
|
77
|
+
serializedSmartFilter.blocks.forEach((serializedBlock: ISerializedBlockV1) => {
|
|
78
|
+
blockDeserializationWork.push(
|
|
79
|
+
this._deserializeBlockV1Async(smartFilter, serializedBlock, engine, blockDefinitionsWhichCouldNotBeDeserialized, blockIdMap, blockNameMap)
|
|
80
|
+
);
|
|
81
|
+
});
|
|
82
|
+
await Promise.all(blockDeserializationWork);
|
|
83
|
+
|
|
84
|
+
// If any block definitions could not be deserialized, throw an error
|
|
85
|
+
if (blockDefinitionsWhichCouldNotBeDeserialized.length > 0) {
|
|
86
|
+
throw new Error(`Could not deserialize the following block definitions: ${blockDefinitionsWhichCouldNotBeDeserialized.join(", ")}`);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// Deserialize the connections
|
|
90
|
+
serializedSmartFilter.connections.forEach((connection: ISerializedConnectionV1) => {
|
|
91
|
+
// Find the source block and its connection point's connectTo function
|
|
92
|
+
const sourceBlock = typeof connection.outputBlock === "string" ? blockNameMap.get(connection.outputBlock) : blockIdMap.get(connection.outputBlock);
|
|
93
|
+
|
|
94
|
+
if (!sourceBlock) {
|
|
95
|
+
throw new Error(`Source block ${connection.outputBlock} not found`);
|
|
96
|
+
}
|
|
97
|
+
const sourceConnectionPoint = sourceBlock.outputs.find((output) => output.name === connection.outputConnectionPoint);
|
|
98
|
+
if (!sourceConnectionPoint || typeof sourceConnectionPoint.connectTo !== "function") {
|
|
99
|
+
throw new Error(`Block ${connection.outputBlock} does not have an connection point named ${connection.outputConnectionPoint}`);
|
|
100
|
+
}
|
|
101
|
+
const sourceConnectToFunction = sourceConnectionPoint.connectTo.bind(sourceConnectionPoint);
|
|
102
|
+
|
|
103
|
+
// Find the target block and its connection point
|
|
104
|
+
const targetBlock = typeof connection.inputBlock === "string" ? blockNameMap.get(connection.inputBlock) : blockIdMap.get(connection.inputBlock);
|
|
105
|
+
if (!targetBlock) {
|
|
106
|
+
throw new Error(`Target block ${connection.inputBlock} not found`);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
const targetConnectionPoint = targetBlock.inputs.find((input) => input.name === connection.inputConnectionPoint);
|
|
110
|
+
if (!targetConnectionPoint || typeof targetConnectionPoint !== "object") {
|
|
111
|
+
throw new Error(`Block ${connection.inputBlock} does not have a connection point named ${connection.inputConnectionPoint}`);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// Create the connection
|
|
115
|
+
sourceConnectToFunction.call(sourceBlock, targetConnectionPoint);
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
return smartFilter;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
private async _deserializeBlockV1Async(
|
|
122
|
+
smartFilter: SmartFilter,
|
|
123
|
+
serializedBlock: ISerializedBlockV1,
|
|
124
|
+
engine: ThinEngine,
|
|
125
|
+
blockTypesWhichCouldNotBeDeserialized: string[],
|
|
126
|
+
blockIdMap: Map<number, BaseBlock>,
|
|
127
|
+
blockNameMap: Map<string, BaseBlock>
|
|
128
|
+
): Promise<void> {
|
|
129
|
+
let newBlock: Nullable<BaseBlock> = null;
|
|
130
|
+
|
|
131
|
+
// Back compat for early Smart Filter V1 serialization where the blockType was stored in className
|
|
132
|
+
// Not worth creating a new version for this, as it's only used in the deserializer
|
|
133
|
+
if ((serializedBlock as any).className && !serializedBlock.blockType) {
|
|
134
|
+
serializedBlock.blockType = (serializedBlock as any).className;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// Back compat for early Smart Filter V1 serialization where the namespace was not stored
|
|
138
|
+
if (serializedBlock.namespace === undefined) {
|
|
139
|
+
serializedBlock.namespace = null;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
// Get the instance of the block
|
|
143
|
+
switch (serializedBlock.blockType) {
|
|
144
|
+
case InputBlock.ClassName:
|
|
145
|
+
{
|
|
146
|
+
if (this._customInputBlockDeserializer) {
|
|
147
|
+
newBlock = await this._customInputBlockDeserializer(smartFilter, serializedBlock, engine);
|
|
148
|
+
}
|
|
149
|
+
if (newBlock === null) {
|
|
150
|
+
newBlock = InputBlockDeserializer(smartFilter, serializedBlock);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
break;
|
|
154
|
+
case OutputBlock.ClassName:
|
|
155
|
+
{
|
|
156
|
+
newBlock = smartFilter.output.ownerBlock;
|
|
157
|
+
}
|
|
158
|
+
break;
|
|
159
|
+
default: {
|
|
160
|
+
// If it's not an input or output block, use the provided block factory
|
|
161
|
+
newBlock = await this._blockFactory(smartFilter, engine, serializedBlock, this);
|
|
162
|
+
if (!newBlock) {
|
|
163
|
+
if (blockTypesWhichCouldNotBeDeserialized.indexOf(serializedBlock.blockType) === -1) {
|
|
164
|
+
blockTypesWhichCouldNotBeDeserialized.push(serializedBlock.blockType);
|
|
165
|
+
}
|
|
166
|
+
return;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
// Deserializers are not responsible for setting the uniqueId or comments.
|
|
172
|
+
// This is so they don't have to be passed into the constructors when programmatically creating
|
|
173
|
+
// blocks, and so each deserializer doesn't have to remember to do it.
|
|
174
|
+
newBlock.uniqueId = serializedBlock.uniqueId;
|
|
175
|
+
newBlock.comments = serializedBlock.comments;
|
|
176
|
+
|
|
177
|
+
// Deserializers are also not responsible for deserializing the outputTextureOptions of ShaderBlocks.
|
|
178
|
+
if (serializedBlock.outputTextureOptions && newBlock instanceof ShaderBlock) {
|
|
179
|
+
newBlock.outputTextureOptions = serializedBlock.outputTextureOptions;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
// We need to ensure any uniqueIds generated in the future (e.g. a new block is added to the SmartFilter)
|
|
183
|
+
// are higher than this one.
|
|
184
|
+
UniqueIdGenerator.EnsureIdsGreaterThan(newBlock.uniqueId);
|
|
185
|
+
|
|
186
|
+
// Save in the map
|
|
187
|
+
blockIdMap.set(newBlock.uniqueId, newBlock);
|
|
188
|
+
blockNameMap.set(newBlock.name, newBlock);
|
|
189
|
+
}
|
|
190
|
+
}
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import type { SmartFilter } from "../smartFilter.js";
|
|
2
|
+
import type { BaseBlock } from "../blockFoundation/baseBlock.js";
|
|
3
|
+
import { InputBlockSerializer } from "../blockFoundation/inputBlock.serializer.js";
|
|
4
|
+
import type { ConnectionPoint } from "../connection/connectionPoint.js";
|
|
5
|
+
import { DefaultBlockSerializer } from "./v1/defaultBlockSerializer.js";
|
|
6
|
+
import { OutputBlock } from "../blockFoundation/outputBlock.js";
|
|
7
|
+
import type { IBlockSerializerV1, ISerializedBlockV1, ISerializedConnectionV1, SerializeBlockV1, SerializedSmartFilterV1 } from "./v1/smartFilterSerialization.types.js";
|
|
8
|
+
import { CustomShaderBlock } from "../blockFoundation/customShaderBlock.js";
|
|
9
|
+
import { CustomAggregateBlock } from "../blockFoundation/customAggregateBlock.js";
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Determines if two serialized connection points are equivalent to each other
|
|
13
|
+
* @param a - The first connection point to compare
|
|
14
|
+
* @param b - The second connection point to compare
|
|
15
|
+
* @returns True if the connection points are equivalent, false otherwise
|
|
16
|
+
*/
|
|
17
|
+
function SerializedConnectionPointsEqual(a: ISerializedConnectionV1, b: ISerializedConnectionV1): boolean {
|
|
18
|
+
return (
|
|
19
|
+
a.inputBlock === b.inputBlock && a.inputConnectionPoint === b.inputConnectionPoint && a.outputBlock === b.outputBlock && a.outputConnectionPoint === b.outputConnectionPoint
|
|
20
|
+
);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Serializes SmartFilters using the latest SmartFilter serialization version.
|
|
25
|
+
* The caller passes in information necessary to serialize the blocks in the SmartFilter.
|
|
26
|
+
* This allows the caller to provide custom serializers for blocks beyond the core blocks.
|
|
27
|
+
*/
|
|
28
|
+
export class SmartFilterSerializer {
|
|
29
|
+
private readonly _blockSerializers: Map<string, SerializeBlockV1> = new Map();
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Creates a new SmartFilterSerializer
|
|
33
|
+
* @param blocksUsingDefaultSerialization - A list of the blockType of blocks which can use default serialization (they only have ConnectionPoint properties and no constructor parameters)
|
|
34
|
+
* @param additionalBlockSerializers - An array of block serializers to use, beyond those for the core blocks
|
|
35
|
+
*/
|
|
36
|
+
public constructor(blocksUsingDefaultSerialization: string[], additionalBlockSerializers: IBlockSerializerV1[]) {
|
|
37
|
+
this._blockSerializers.set(InputBlockSerializer.blockType, InputBlockSerializer.serialize);
|
|
38
|
+
this._blockSerializers.set(OutputBlock.ClassName, DefaultBlockSerializer);
|
|
39
|
+
blocksUsingDefaultSerialization.forEach((block) => {
|
|
40
|
+
this._blockSerializers.set(block, DefaultBlockSerializer);
|
|
41
|
+
});
|
|
42
|
+
additionalBlockSerializers.forEach((serializer) => this._blockSerializers.set(serializer.blockType, serializer.serialize));
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Serializes a SmartFilter to a JSON object of the latest version
|
|
47
|
+
* @param smartFilter - The SmartFilter to serialize
|
|
48
|
+
* @returns The serialized SmartFilter
|
|
49
|
+
*/
|
|
50
|
+
public serialize(smartFilter: SmartFilter): SerializedSmartFilterV1 {
|
|
51
|
+
const connections: ISerializedConnectionV1[] = [];
|
|
52
|
+
|
|
53
|
+
const blocks = smartFilter.attachedBlocks.map((block: BaseBlock) => {
|
|
54
|
+
// Serialize the block itself
|
|
55
|
+
const blockClassName = block.getClassName();
|
|
56
|
+
const serializeFn =
|
|
57
|
+
blockClassName === CustomShaderBlock.ClassName || blockClassName === CustomAggregateBlock.ClassName
|
|
58
|
+
? DefaultBlockSerializer
|
|
59
|
+
: this._blockSerializers.get(block.blockType);
|
|
60
|
+
if (!serializeFn) {
|
|
61
|
+
throw new Error(`No serializer was provided for a block of type ${block.blockType}`);
|
|
62
|
+
}
|
|
63
|
+
const serializedBlock: ISerializedBlockV1 = serializeFn(block);
|
|
64
|
+
|
|
65
|
+
// Serialize the connections to the inputs
|
|
66
|
+
block.inputs.forEach((input: ConnectionPoint) => {
|
|
67
|
+
const connectedTo = input.connectedTo;
|
|
68
|
+
if (connectedTo) {
|
|
69
|
+
const newConnection: ISerializedConnectionV1 = {
|
|
70
|
+
inputBlock: block.uniqueId,
|
|
71
|
+
inputConnectionPoint: input.name,
|
|
72
|
+
outputBlock: connectedTo.ownerBlock.uniqueId,
|
|
73
|
+
outputConnectionPoint: connectedTo.name,
|
|
74
|
+
};
|
|
75
|
+
if (!connections.find((other) => SerializedConnectionPointsEqual(newConnection, other))) {
|
|
76
|
+
connections.push(newConnection);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
// Serialize the connections to the outputs
|
|
82
|
+
block.outputs.forEach((output: ConnectionPoint) => {
|
|
83
|
+
output.endpoints.forEach((input: ConnectionPoint) => {
|
|
84
|
+
const newConnection: ISerializedConnectionV1 = {
|
|
85
|
+
inputBlock: input.ownerBlock.uniqueId,
|
|
86
|
+
inputConnectionPoint: input.name,
|
|
87
|
+
outputBlock: block.uniqueId,
|
|
88
|
+
outputConnectionPoint: output.name,
|
|
89
|
+
};
|
|
90
|
+
if (!connections.find((other) => SerializedConnectionPointsEqual(newConnection, other))) {
|
|
91
|
+
connections.push(newConnection);
|
|
92
|
+
}
|
|
93
|
+
});
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
return serializedBlock;
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
return {
|
|
100
|
+
format: "smartFilter",
|
|
101
|
+
formatVersion: 1,
|
|
102
|
+
name: smartFilter.name,
|
|
103
|
+
namespace: smartFilter.namespace,
|
|
104
|
+
comments: smartFilter.comments,
|
|
105
|
+
editorData: smartFilter.editorData,
|
|
106
|
+
blocks,
|
|
107
|
+
connections,
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { ShaderBlock } from "../../blockFoundation/shaderBlock.js";
|
|
2
|
+
import type { BaseBlock } from "../../blockFoundation/baseBlock.js";
|
|
3
|
+
import type { ISerializedBlockV1, SerializeBlockV1 } from "./smartFilterSerialization.types.js";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* The default V1 block serializer which can be used for any block that relies only on ConnectionPoints
|
|
7
|
+
* and does not have any constructor parameters or class properties that need to be serialized.
|
|
8
|
+
* @param block - The block to serialize
|
|
9
|
+
* @returns The serialized block
|
|
10
|
+
*/
|
|
11
|
+
export const DefaultBlockSerializer: SerializeBlockV1 = (block: BaseBlock): ISerializedBlockV1 => {
|
|
12
|
+
return {
|
|
13
|
+
name: block.name,
|
|
14
|
+
uniqueId: block.uniqueId,
|
|
15
|
+
blockType: block.blockType,
|
|
16
|
+
namespace: block.namespace,
|
|
17
|
+
comments: block.comments,
|
|
18
|
+
data: undefined,
|
|
19
|
+
outputTextureOptions: block instanceof ShaderBlock ? block.outputTextureOptions : undefined,
|
|
20
|
+
};
|
|
21
|
+
};
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ----------------------------------------------------------------------------
|
|
3
|
+
* Data Types Used For Block Serialization
|
|
4
|
+
* ----------------------------------------------------------------------------
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import type { Nullable } from "core/types.js";
|
|
8
|
+
import type { AllConnectionPointTypes, ConnectionPointValue } from "../../connection/connectionPointType.js";
|
|
9
|
+
import type { ShaderProgram } from "../../utils/shaderCodeUtils.js";
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* The V1 definition of a serialized shader block. This block definition is loaded by a CustomShaderBlock and defines how a
|
|
13
|
+
* blockType works. This should not be confused with an ISerializedBockV1, which is a serialized instance of a block in a
|
|
14
|
+
* serialized SmartFilter graph. It is referenced by blockType in a serialized SmartFilter.
|
|
15
|
+
*/
|
|
16
|
+
export type SerializedShaderBlockDefinitionV1 = {
|
|
17
|
+
/**
|
|
18
|
+
* Which type of serialized data this is.
|
|
19
|
+
*/
|
|
20
|
+
format: "shaderBlockDefinition";
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* The version of the block definition format (format of the serialized data, not the version of the block definition itself).
|
|
24
|
+
*/
|
|
25
|
+
formatVersion: 1;
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* The type used to refer to the block in serialized SmartFilters and in the editor UI.
|
|
29
|
+
* The application doing the deserialization will use this to instantiate the correct block definition.
|
|
30
|
+
* Block types are expected to be unique and their behavior should be semantically equivalent across implementations
|
|
31
|
+
* (their results must be similar enough that the differences are not perceivable).
|
|
32
|
+
*/
|
|
33
|
+
blockType: string;
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* The namespace of the block, which is used to reduce name collisions between blocks and also to group blocks in the editor UI.
|
|
37
|
+
* By convention, sub namespaces are separated by a period (e.g. "Babylon.Demo.Effects").
|
|
38
|
+
*/
|
|
39
|
+
namespace: Nullable<string>;
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* The shader program for the block.
|
|
43
|
+
*/
|
|
44
|
+
shaderProgram: ShaderProgram;
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* The input connection points of the block.
|
|
48
|
+
*/
|
|
49
|
+
inputConnectionPoints: SerializedInputConnectionPointV1[];
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* If true, the optimizer will not attempt to optimize this block.
|
|
53
|
+
*/
|
|
54
|
+
disableOptimization: boolean;
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Possible V1 auto bind values for input connection points.
|
|
59
|
+
*/
|
|
60
|
+
export type InputAutoBindV1 = "outputResolution" | "outputAspectRatio";
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* A V1 input connection point of a serialized block definition.
|
|
64
|
+
*/
|
|
65
|
+
export type SerializedInputConnectionPointV1<U extends AllConnectionPointTypes = AllConnectionPointTypes> = {
|
|
66
|
+
/**
|
|
67
|
+
* The name of the connection point.
|
|
68
|
+
*/
|
|
69
|
+
name: string;
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* The type of the connection point.
|
|
73
|
+
*/
|
|
74
|
+
type: U;
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* The optional default value of the connection point.
|
|
78
|
+
*/
|
|
79
|
+
defaultValue?: ConnectionPointValue<U>;
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* If supplied, the input will be automatically bound to this value, instead of creating an input connection point.
|
|
83
|
+
*/
|
|
84
|
+
autoBind?: InputAutoBindV1;
|
|
85
|
+
};
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import type { Nullable } from "core/types.js";
|
|
2
|
+
import type { BaseBlock } from "../../blockFoundation/baseBlock.js";
|
|
3
|
+
import type { SmartFilter } from "../../smartFilter.js";
|
|
4
|
+
import type { ThinEngine } from "core/Engines/thinEngine.js";
|
|
5
|
+
import type { IEditorData } from "shared-ui-components/nodeGraphSystem/interfaces/nodeLocationInfo.js";
|
|
6
|
+
import type { SmartFilterDeserializer } from "../smartFilterDeserializer.js";
|
|
7
|
+
import type { OutputTextureOptions } from "../../blockFoundation/textureOptions.js";
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* ----------------------------------------------------------------------------
|
|
11
|
+
* Serialized Data Types
|
|
12
|
+
* ----------------------------------------------------------------------------
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* V1 Serialized Smart Filter
|
|
17
|
+
*/
|
|
18
|
+
export type SerializedSmartFilterV1 = {
|
|
19
|
+
/** Which type of serialized data this is. */
|
|
20
|
+
format: "smartFilter";
|
|
21
|
+
|
|
22
|
+
/** The format version of the serialized data (not the version of the SmartFilter itself).*/
|
|
23
|
+
formatVersion: 1;
|
|
24
|
+
|
|
25
|
+
/** The SmartFilter name */
|
|
26
|
+
name: string;
|
|
27
|
+
|
|
28
|
+
/** The namespace of the SmartFilter */
|
|
29
|
+
namespace: Nullable<string>;
|
|
30
|
+
|
|
31
|
+
/** The SmartFilter comments */
|
|
32
|
+
comments: Nullable<string>;
|
|
33
|
+
|
|
34
|
+
/** The editor data for the SmartFilter */
|
|
35
|
+
editorData: Nullable<IEditorData>;
|
|
36
|
+
|
|
37
|
+
/** The serialized blocks */
|
|
38
|
+
blocks: ISerializedBlockV1[];
|
|
39
|
+
|
|
40
|
+
/** The serialized connections */
|
|
41
|
+
connections: ISerializedConnectionV1[];
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* V1 format of a block in a serialized Smart Filter.
|
|
46
|
+
* Not to be confused with a SerializedBlockDefinitionV1 which serializes the definition of a CustomShaderBlock.
|
|
47
|
+
*/
|
|
48
|
+
export interface ISerializedBlockV1 {
|
|
49
|
+
/** The name of the block */
|
|
50
|
+
name: string;
|
|
51
|
+
|
|
52
|
+
/** The namespace of the block */
|
|
53
|
+
namespace: Nullable<string>;
|
|
54
|
+
|
|
55
|
+
/** The unique ID of the block - correlates with the ID in the editorData for block position, etc. */
|
|
56
|
+
uniqueId: number;
|
|
57
|
+
|
|
58
|
+
/** The blockType of the block - used to determine how to instantiate the block during deserialization */
|
|
59
|
+
blockType: string;
|
|
60
|
+
|
|
61
|
+
/** The comments for the block */
|
|
62
|
+
comments: Nullable<string>;
|
|
63
|
+
|
|
64
|
+
/** The OutputTextureOptions for serialized ShaderBlocks */
|
|
65
|
+
outputTextureOptions?: OutputTextureOptions;
|
|
66
|
+
|
|
67
|
+
/** Block specific serialized data */
|
|
68
|
+
data: any;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* V1 Serialized Connection
|
|
73
|
+
*/
|
|
74
|
+
export interface ISerializedConnectionV1 {
|
|
75
|
+
/** The uniqueId of the block that the connection is to */
|
|
76
|
+
outputBlock: number;
|
|
77
|
+
|
|
78
|
+
/** The name of the connectionPoint on the outputBlock */
|
|
79
|
+
outputConnectionPoint: string;
|
|
80
|
+
|
|
81
|
+
/** The uniqueId of the block that the connection is from */
|
|
82
|
+
inputBlock: number;
|
|
83
|
+
|
|
84
|
+
/** The name of the connectionPoint on the inputBlock */
|
|
85
|
+
inputConnectionPoint: string;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* ----------------------------------------------------------------------------
|
|
90
|
+
* Serializer Types
|
|
91
|
+
* ----------------------------------------------------------------------------
|
|
92
|
+
*/
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* A function that serializes a block to a V1 serialized block object
|
|
96
|
+
*/
|
|
97
|
+
export type SerializeBlockV1 = (block: BaseBlock) => ISerializedBlockV1;
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* A V1 serializer for blocks in a SmartFilter
|
|
101
|
+
*/
|
|
102
|
+
export interface IBlockSerializerV1 {
|
|
103
|
+
/** The blockType of the block that this serializer can serialize */
|
|
104
|
+
blockType: string;
|
|
105
|
+
|
|
106
|
+
/** The function that serializes the block in the Smart Filter */
|
|
107
|
+
serialize: SerializeBlockV1;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* ----------------------------------------------------------------------------
|
|
112
|
+
* Deserializer Types
|
|
113
|
+
* ----------------------------------------------------------------------------
|
|
114
|
+
*/
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* A function that deserializes a V1 block in a SmartFilter
|
|
118
|
+
*/
|
|
119
|
+
export type DeserializeBlockV1 = (
|
|
120
|
+
smartFilter: SmartFilter,
|
|
121
|
+
serializedBlock: ISerializedBlockV1,
|
|
122
|
+
engine: ThinEngine,
|
|
123
|
+
smartFilterDeserializer: SmartFilterDeserializer
|
|
124
|
+
) => Promise<BaseBlock>;
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* A function that optionally deserializes a block from a V1 serialized block object, returning null if it cannot
|
|
128
|
+
*/
|
|
129
|
+
export type OptionalBlockDeserializerV1 = (smartFilter: SmartFilter, serializedBlock: ISerializedBlockV1, engine: ThinEngine) => Promise<Nullable<BaseBlock>>;
|