nodeMaterialBlock.ts 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716
  1. import { NodeMaterialBlockConnectionPointTypes } from './Enums/nodeMaterialBlockConnectionPointTypes';
  2. import { NodeMaterialBuildState } from './nodeMaterialBuildState';
  3. import { Nullable } from '../../types';
  4. import { NodeMaterialConnectionPoint, NodeMaterialConnectionPointDirection } from './nodeMaterialBlockConnectionPoint';
  5. import { NodeMaterialBlockTargets } from './Enums/nodeMaterialBlockTargets';
  6. import { Effect } from '../effect';
  7. import { AbstractMesh } from '../../Meshes/abstractMesh';
  8. import { Mesh } from '../../Meshes/mesh';
  9. import { SubMesh } from '../../Meshes/subMesh';
  10. import { NodeMaterial, NodeMaterialDefines } from './nodeMaterial';
  11. import { InputBlock } from './Blocks/Input/inputBlock';
  12. import { UniqueIdGenerator } from '../../Misc/uniqueIdGenerator';
  13. import { Scene } from '../../scene';
  14. import { _TypeStore } from '../../Misc/typeStore';
  15. import { EffectFallbacks } from '../effectFallbacks';
  16. import { Matrix } from "../../Maths/math.vector";
  17. /**
  18. * Defines a block that can be used inside a node based material
  19. */
  20. export class NodeMaterialBlock {
  21. private _buildId: number;
  22. private _buildTarget: NodeMaterialBlockTargets;
  23. private _target: NodeMaterialBlockTargets;
  24. private _isFinalMerger = false;
  25. private _isInput = false;
  26. protected _isUnique = false;
  27. /** Gets or sets a boolean indicating that only one input can be connected at a time */
  28. public inputsAreExclusive = false;
  29. /** @hidden */
  30. public _codeVariableName = "";
  31. /** @hidden */
  32. public _inputs = new Array<NodeMaterialConnectionPoint>();
  33. /** @hidden */
  34. public _outputs = new Array<NodeMaterialConnectionPoint>();
  35. /** @hidden */
  36. public _preparationId: number;
  37. /**
  38. * Gets or sets the name of the block
  39. */
  40. public name: string;
  41. /**
  42. * Gets or sets the unique id of the node
  43. */
  44. public uniqueId: number;
  45. /**
  46. * Gets or sets the comments associated with this block
  47. */
  48. public comments: string = "";
  49. /**
  50. * Gets a boolean indicating that this block can only be used once per NodeMaterial
  51. */
  52. public get isUnique() {
  53. return this._isUnique;
  54. }
  55. /**
  56. * Gets a boolean indicating that this block is an end block (e.g. it is generating a system value)
  57. */
  58. public get isFinalMerger(): boolean {
  59. return this._isFinalMerger;
  60. }
  61. /**
  62. * Gets a boolean indicating that this block is an input (e.g. it sends data to the shader)
  63. */
  64. public get isInput(): boolean {
  65. return this._isInput;
  66. }
  67. /**
  68. * Gets or sets the build Id
  69. */
  70. public get buildId(): number {
  71. return this._buildId;
  72. }
  73. public set buildId(value: number) {
  74. this._buildId = value;
  75. }
  76. /**
  77. * Gets or sets the target of the block
  78. */
  79. public get target() {
  80. return this._target;
  81. }
  82. public set target(value: NodeMaterialBlockTargets) {
  83. if ((this._target & value) !== 0) {
  84. return;
  85. }
  86. this._target = value;
  87. }
  88. /**
  89. * Gets the list of input points
  90. */
  91. public get inputs(): NodeMaterialConnectionPoint[] {
  92. return this._inputs;
  93. }
  94. /** Gets the list of output points */
  95. public get outputs(): NodeMaterialConnectionPoint[] {
  96. return this._outputs;
  97. }
  98. /**
  99. * Find an input by its name
  100. * @param name defines the name of the input to look for
  101. * @returns the input or null if not found
  102. */
  103. public getInputByName(name: string) {
  104. let filter = this._inputs.filter((e) => e.name === name);
  105. if (filter.length) {
  106. return filter[0];
  107. }
  108. return null;
  109. }
  110. /**
  111. * Find an output by its name
  112. * @param name defines the name of the outputto look for
  113. * @returns the output or null if not found
  114. */
  115. public getOutputByName(name: string) {
  116. let filter = this._outputs.filter((e) => e.name === name);
  117. if (filter.length) {
  118. return filter[0];
  119. }
  120. return null;
  121. }
  122. /**
  123. * Creates a new NodeMaterialBlock
  124. * @param name defines the block name
  125. * @param target defines the target of that block (Vertex by default)
  126. * @param isFinalMerger defines a boolean indicating that this block is an end block (e.g. it is generating a system value). Default is false
  127. * @param isInput defines a boolean indicating that this block is an input (e.g. it sends data to the shader). Default is false
  128. */
  129. public constructor(name: string, target = NodeMaterialBlockTargets.Vertex, isFinalMerger = false, isInput = false) {
  130. this.name = name;
  131. this._target = target;
  132. this._isFinalMerger = isFinalMerger;
  133. this._isInput = isInput;
  134. this.uniqueId = UniqueIdGenerator.UniqueId;
  135. }
  136. /**
  137. * Initialize the block and prepare the context for build
  138. * @param state defines the state that will be used for the build
  139. */
  140. public initialize(state: NodeMaterialBuildState) {
  141. // Do nothing
  142. }
  143. /**
  144. * Bind data to effect. Will only be called for blocks with isBindable === true
  145. * @param effect defines the effect to bind data to
  146. * @param nodeMaterial defines the hosting NodeMaterial
  147. * @param mesh defines the mesh that will be rendered
  148. * @param subMesh defines the submesh that will be rendered
  149. * @param world defines the world transformation matrix
  150. */
  151. public bind(effect: Effect, nodeMaterial: NodeMaterial, mesh?: Mesh, subMesh?: SubMesh, world?: Matrix) {
  152. // Do nothing
  153. }
  154. protected _declareOutput(output: NodeMaterialConnectionPoint, state: NodeMaterialBuildState): string {
  155. return `${state._getGLType(output.type)} ${output.associatedVariableName}`;
  156. }
  157. protected _writeVariable(currentPoint: NodeMaterialConnectionPoint): string {
  158. let connectionPoint = currentPoint.connectedPoint;
  159. if (connectionPoint) {
  160. return `${currentPoint.associatedVariableName}`;
  161. }
  162. return `0.`;
  163. }
  164. protected _writeFloat(value: number) {
  165. let stringVersion = value.toString();
  166. if (stringVersion.indexOf(".") === -1) {
  167. stringVersion += ".0";
  168. }
  169. return `${stringVersion}`;
  170. }
  171. /**
  172. * Gets the current class name e.g. "NodeMaterialBlock"
  173. * @returns the class name
  174. */
  175. public getClassName() {
  176. return "NodeMaterialBlock";
  177. }
  178. /**
  179. * Register a new input. Must be called inside a block constructor
  180. * @param name defines the connection point name
  181. * @param type defines the connection point type
  182. * @param isOptional defines a boolean indicating that this input can be omitted
  183. * @param target defines the target to use to limit the connection point (will be VertexAndFragment by default)
  184. * @param point an already created connection point. If not provided, create a new one
  185. * @returns the current block
  186. */
  187. public registerInput(name: string, type: NodeMaterialBlockConnectionPointTypes, isOptional: boolean = false, target?: NodeMaterialBlockTargets, point?: NodeMaterialConnectionPoint) {
  188. point = point ?? new NodeMaterialConnectionPoint(name, this, NodeMaterialConnectionPointDirection.Input);
  189. point.type = type;
  190. point.isOptional = isOptional;
  191. if (target) {
  192. point.target = target;
  193. }
  194. this._inputs.push(point);
  195. return this;
  196. }
  197. /**
  198. * Register a new output. Must be called inside a block constructor
  199. * @param name defines the connection point name
  200. * @param type defines the connection point type
  201. * @param target defines the target to use to limit the connection point (will be VertexAndFragment by default)
  202. * @param point an already created connection point. If not provided, create a new one
  203. * @returns the current block
  204. */
  205. public registerOutput(name: string, type: NodeMaterialBlockConnectionPointTypes, target?: NodeMaterialBlockTargets, point?: NodeMaterialConnectionPoint) {
  206. point = point ?? new NodeMaterialConnectionPoint(name, this, NodeMaterialConnectionPointDirection.Output);
  207. point.type = type;
  208. if (target) {
  209. point.target = target;
  210. }
  211. this._outputs.push(point);
  212. return this;
  213. }
  214. /**
  215. * Will return the first available input e.g. the first one which is not an uniform or an attribute
  216. * @param forOutput defines an optional connection point to check compatibility with
  217. * @returns the first available input or null
  218. */
  219. public getFirstAvailableInput(forOutput: Nullable<NodeMaterialConnectionPoint> = null) {
  220. for (var input of this._inputs) {
  221. if (!input.connectedPoint) {
  222. if (!forOutput || (forOutput.type === input.type) || (input.type === NodeMaterialBlockConnectionPointTypes.AutoDetect)) {
  223. return input;
  224. }
  225. }
  226. }
  227. return null;
  228. }
  229. /**
  230. * Will return the first available output e.g. the first one which is not yet connected and not a varying
  231. * @param forBlock defines an optional block to check compatibility with
  232. * @returns the first available input or null
  233. */
  234. public getFirstAvailableOutput(forBlock: Nullable<NodeMaterialBlock> = null) {
  235. for (var output of this._outputs) {
  236. if (!forBlock || !forBlock.target || forBlock.target === NodeMaterialBlockTargets.Neutral || (forBlock.target & output.target) !== 0) {
  237. return output;
  238. }
  239. }
  240. return null;
  241. }
  242. /**
  243. * Gets the sibling of the given output
  244. * @param current defines the current output
  245. * @returns the next output in the list or null
  246. */
  247. public getSiblingOutput(current: NodeMaterialConnectionPoint) {
  248. let index = this._outputs.indexOf(current);
  249. if (index === -1 || index >= this._outputs.length) {
  250. return null;
  251. }
  252. return this._outputs[index + 1];
  253. }
  254. /**
  255. * Connect current block with another block
  256. * @param other defines the block to connect with
  257. * @param options define the various options to help pick the right connections
  258. * @returns the current block
  259. */
  260. public connectTo(other: NodeMaterialBlock, options?: {
  261. input?: string,
  262. output?: string,
  263. outputSwizzle?: string
  264. }) {
  265. if (this._outputs.length === 0) {
  266. return;
  267. }
  268. let output = options && options.output ? this.getOutputByName(options.output) : this.getFirstAvailableOutput(other);
  269. let notFound = true;
  270. while (notFound) {
  271. let input = options && options.input ? other.getInputByName(options.input) : other.getFirstAvailableInput(output);
  272. if (output && input && output.canConnectTo(input)) {
  273. output.connectTo(input);
  274. notFound = false;
  275. } else if (!output) {
  276. throw "Unable to find a compatible match";
  277. } else {
  278. output = this.getSiblingOutput(output);
  279. }
  280. }
  281. return this;
  282. }
  283. protected _buildBlock(state: NodeMaterialBuildState) {
  284. // Empty. Must be defined by child nodes
  285. }
  286. /**
  287. * Add uniforms, samplers and uniform buffers at compilation time
  288. * @param state defines the state to update
  289. * @param nodeMaterial defines the node material requesting the update
  290. * @param defines defines the material defines to update
  291. * @param uniformBuffers defines the list of uniform buffer names
  292. */
  293. public updateUniformsAndSamples(state: NodeMaterialBuildState, nodeMaterial: NodeMaterial, defines: NodeMaterialDefines, uniformBuffers: string[]) {
  294. // Do nothing
  295. }
  296. /**
  297. * Add potential fallbacks if shader compilation fails
  298. * @param mesh defines the mesh to be rendered
  299. * @param fallbacks defines the current prioritized list of fallbacks
  300. */
  301. public provideFallbacks(mesh: AbstractMesh, fallbacks: EffectFallbacks) {
  302. // Do nothing
  303. }
  304. /**
  305. * Initialize defines for shader compilation
  306. * @param mesh defines the mesh to be rendered
  307. * @param nodeMaterial defines the node material requesting the update
  308. * @param defines defines the material defines to update
  309. * @param useInstances specifies that instances should be used
  310. */
  311. public initializeDefines(mesh: AbstractMesh, nodeMaterial: NodeMaterial, defines: NodeMaterialDefines, useInstances: boolean = false) {
  312. }
  313. /**
  314. * Update defines for shader compilation
  315. * @param mesh defines the mesh to be rendered
  316. * @param nodeMaterial defines the node material requesting the update
  317. * @param defines defines the material defines to update
  318. * @param useInstances specifies that instances should be used
  319. */
  320. public prepareDefines(mesh: AbstractMesh, nodeMaterial: NodeMaterial, defines: NodeMaterialDefines, useInstances: boolean = false) {
  321. // Do nothing
  322. }
  323. /**
  324. * Lets the block try to connect some inputs automatically
  325. * @param material defines the hosting NodeMaterial
  326. */
  327. public autoConfigure(material: NodeMaterial) {
  328. // Do nothing
  329. }
  330. /**
  331. * Function called when a block is declared as repeatable content generator
  332. * @param vertexShaderState defines the current compilation state for the vertex shader
  333. * @param fragmentShaderState defines the current compilation state for the fragment shader
  334. * @param mesh defines the mesh to be rendered
  335. * @param defines defines the material defines to update
  336. */
  337. public replaceRepeatableContent(vertexShaderState: NodeMaterialBuildState, fragmentShaderState: NodeMaterialBuildState, mesh: AbstractMesh, defines: NodeMaterialDefines) {
  338. // Do nothing
  339. }
  340. /**
  341. * Checks if the block is ready
  342. * @param mesh defines the mesh to be rendered
  343. * @param nodeMaterial defines the node material requesting the update
  344. * @param defines defines the material defines to update
  345. * @param useInstances specifies that instances should be used
  346. * @returns true if the block is ready
  347. */
  348. public isReady(mesh: AbstractMesh, nodeMaterial: NodeMaterial, defines: NodeMaterialDefines, useInstances: boolean = false) {
  349. return true;
  350. }
  351. protected _linkConnectionTypes(inputIndex0: number, inputIndex1: number) {
  352. this._inputs[inputIndex0]._linkedConnectionSource = this._inputs[inputIndex1];
  353. this._inputs[inputIndex1]._linkedConnectionSource = this._inputs[inputIndex0];
  354. }
  355. private _processBuild(block: NodeMaterialBlock, state: NodeMaterialBuildState, input: NodeMaterialConnectionPoint, activeBlocks: NodeMaterialBlock[]) {
  356. block.build(state, activeBlocks);
  357. const localBlockIsFragment = (state._vertexState != null);
  358. const otherBlockWasGeneratedInVertexShader = block._buildTarget === NodeMaterialBlockTargets.Vertex && block.target !== NodeMaterialBlockTargets.VertexAndFragment;
  359. if (localBlockIsFragment && (
  360. ((block.target & block._buildTarget) === 0) ||
  361. ((block.target & input.target) === 0) ||
  362. (this.target !== NodeMaterialBlockTargets.VertexAndFragment && otherBlockWasGeneratedInVertexShader)
  363. )) { // context switch! We need a varying
  364. if ((!block.isInput && state.target !== block._buildTarget) // block was already emitted by vertex shader
  365. || (block.isInput && (block as InputBlock).isAttribute) // block is an attribute
  366. ) {
  367. let connectedPoint = input.connectedPoint!;
  368. if (state._vertexState._emitVaryingFromString("v_" + connectedPoint.associatedVariableName, state._getGLType(connectedPoint.type))) {
  369. state._vertexState.compilationString += `${"v_" + connectedPoint.associatedVariableName} = ${connectedPoint.associatedVariableName};\r\n`;
  370. }
  371. input.associatedVariableName = "v_" + connectedPoint.associatedVariableName;
  372. input._enforceAssociatedVariableName = true;
  373. }
  374. }
  375. }
  376. /**
  377. * Compile the current node and generate the shader code
  378. * @param state defines the current compilation state (uniforms, samplers, current string)
  379. * @param activeBlocks defines the list of active blocks (i.e. blocks to compile)
  380. * @returns true if already built
  381. */
  382. public build(state: NodeMaterialBuildState, activeBlocks: NodeMaterialBlock[]): boolean {
  383. if (this._buildId === state.sharedData.buildId) {
  384. return true;
  385. }
  386. if (!this.isInput) {
  387. /** Prepare outputs */
  388. for (var output of this._outputs) {
  389. if (!output.associatedVariableName) {
  390. output.associatedVariableName = state._getFreeVariableName(output.name);
  391. }
  392. }
  393. }
  394. // Check if "parent" blocks are compiled
  395. for (var input of this._inputs) {
  396. if (!input.connectedPoint) {
  397. if (!input.isOptional) { // Emit a warning
  398. state.sharedData.checks.notConnectedNonOptionalInputs.push(input);
  399. }
  400. continue;
  401. }
  402. if (this.target !== NodeMaterialBlockTargets.Neutral) {
  403. if ((input.target & this.target!) === 0) {
  404. continue;
  405. }
  406. if ((input.target & state.target!) === 0) {
  407. continue;
  408. }
  409. }
  410. let block = input.connectedPoint.ownerBlock;
  411. if (block && block !== this) {
  412. this._processBuild(block, state, input, activeBlocks);
  413. }
  414. }
  415. if (this._buildId === state.sharedData.buildId) {
  416. return true; // Need to check again as inputs can be connected multiple time to this endpoint
  417. }
  418. // Logs
  419. if (state.sharedData.verbose) {
  420. console.log(`${state.target === NodeMaterialBlockTargets.Vertex ? "Vertex shader" : "Fragment shader"}: Building ${this.name} [${this.getClassName()}]`);
  421. }
  422. // Checks final outputs
  423. if (this.isFinalMerger) {
  424. switch (state.target) {
  425. case NodeMaterialBlockTargets.Vertex:
  426. state.sharedData.checks.emitVertex = true;
  427. break;
  428. case NodeMaterialBlockTargets.Fragment:
  429. state.sharedData.checks.emitFragment = true;
  430. break;
  431. }
  432. }
  433. if (!this.isInput && state.sharedData.emitComments) {
  434. state.compilationString += `\r\n//${this.name}\r\n`;
  435. }
  436. this._buildBlock(state);
  437. this._buildId = state.sharedData.buildId;
  438. this._buildTarget = state.target;
  439. // Compile connected blocks
  440. for (var output of this._outputs) {
  441. if ((output.target & state.target) === 0) {
  442. continue;
  443. }
  444. for (var endpoint of output.endpoints) {
  445. let block = endpoint.ownerBlock;
  446. if (block && (block.target & state.target) !== 0 && activeBlocks.indexOf(block) !== -1) {
  447. this._processBuild(block, state, endpoint, activeBlocks);
  448. }
  449. }
  450. }
  451. return false;
  452. }
  453. protected _inputRename(name: string) {
  454. return name;
  455. }
  456. protected _outputRename(name: string) {
  457. return name;
  458. }
  459. protected _dumpPropertiesCode() {
  460. return "";
  461. }
  462. /** @hidden */
  463. public _dumpCode(uniqueNames: string[], alreadyDumped: NodeMaterialBlock[]) {
  464. alreadyDumped.push(this);
  465. let codeString: string;
  466. // Get unique name
  467. let nameAsVariableName = this.name.replace(/[^A-Za-z_]+/g, "");
  468. this._codeVariableName = nameAsVariableName || `${this.getClassName()}_${this.uniqueId}`;
  469. if (uniqueNames.indexOf(this._codeVariableName) !== -1) {
  470. let index = 0;
  471. do {
  472. index++;
  473. this._codeVariableName = nameAsVariableName + index;
  474. }
  475. while (uniqueNames.indexOf(this._codeVariableName) !== -1);
  476. }
  477. uniqueNames.push(this._codeVariableName);
  478. // Declaration
  479. codeString = `\r\n// ${this.getClassName()}\r\n`;
  480. if (this.comments) {
  481. codeString += `// ${this.comments}\r\n`;
  482. }
  483. codeString += `var ${this._codeVariableName} = new BABYLON.${this.getClassName()}("${this.name}");\r\n`;
  484. // Properties
  485. codeString += this._dumpPropertiesCode();
  486. // Inputs
  487. for (var input of this.inputs) {
  488. if (!input.isConnected) {
  489. continue;
  490. }
  491. var connectedOutput = input.connectedPoint!;
  492. var connectedBlock = connectedOutput.ownerBlock;
  493. if (alreadyDumped.indexOf(connectedBlock) === -1) {
  494. codeString += connectedBlock._dumpCode(uniqueNames, alreadyDumped);
  495. }
  496. }
  497. // Outputs
  498. for (var output of this.outputs) {
  499. if (!output.hasEndpoints) {
  500. continue;
  501. }
  502. for (var endpoint of output.endpoints) {
  503. var connectedBlock = endpoint.ownerBlock;
  504. if (connectedBlock && alreadyDumped.indexOf(connectedBlock) === -1) {
  505. codeString += connectedBlock._dumpCode(uniqueNames, alreadyDumped);
  506. }
  507. }
  508. }
  509. return codeString;
  510. }
  511. /** @hidden */
  512. public _dumpCodeForOutputConnections(alreadyDumped: NodeMaterialBlock[]) {
  513. let codeString = "";
  514. if (alreadyDumped.indexOf(this) !== -1) {
  515. return codeString;
  516. }
  517. alreadyDumped.push(this);
  518. for (var input of this.inputs) {
  519. if (!input.isConnected) {
  520. continue;
  521. }
  522. var connectedOutput = input.connectedPoint!;
  523. var connectedBlock = connectedOutput.ownerBlock;
  524. codeString += connectedBlock._dumpCodeForOutputConnections(alreadyDumped);
  525. codeString += `${connectedBlock._codeVariableName}.${connectedBlock._outputRename(connectedOutput.name)}.connectTo(${this._codeVariableName}.${this._inputRename(input.name)});\r\n`;
  526. }
  527. return codeString;
  528. }
  529. /**
  530. * Clone the current block to a new identical block
  531. * @param scene defines the hosting scene
  532. * @param rootUrl defines the root URL to use to load textures and relative dependencies
  533. * @returns a copy of the current block
  534. */
  535. public clone(scene: Scene, rootUrl: string = "") {
  536. let serializationObject = this.serialize();
  537. let blockType = _TypeStore.GetClass(serializationObject.customType);
  538. if (blockType) {
  539. let block: NodeMaterialBlock = new blockType();
  540. block._deserialize(serializationObject, scene, rootUrl);
  541. return block;
  542. }
  543. return null;
  544. }
  545. /**
  546. * Serializes this block in a JSON representation
  547. * @returns the serialized block object
  548. */
  549. public serialize(): any {
  550. let serializationObject: any = {};
  551. serializationObject.customType = "BABYLON." + this.getClassName();
  552. serializationObject.id = this.uniqueId;
  553. serializationObject.name = this.name;
  554. serializationObject.comments = this.comments;
  555. serializationObject.inputs = [];
  556. serializationObject.outputs = [];
  557. for (var input of this.inputs) {
  558. serializationObject.inputs.push(input.serialize());
  559. }
  560. for (var output of this.outputs) {
  561. serializationObject.outputs.push(output.serialize(false));
  562. }
  563. return serializationObject;
  564. }
  565. /** @hidden */
  566. public _deserialize(serializationObject: any, scene: Scene, rootUrl: string) {
  567. this.name = serializationObject.name;
  568. this.comments = serializationObject.comments;
  569. this._deserializePortDisplayNames(serializationObject);
  570. }
  571. private _deserializePortDisplayNames(serializationObject: any) {
  572. const serializedInputs = serializationObject.inputs;
  573. const serializedOutputs = serializationObject.outputs;
  574. if (serializedInputs) {
  575. serializedInputs.forEach((port: any, i: number) => {
  576. if (port.displayName) {
  577. this.inputs[i].displayName = port.displayName;
  578. }
  579. });
  580. }
  581. if (serializedOutputs) {
  582. serializedOutputs.forEach((port: any, i: number) => {
  583. if (port.displayName) {
  584. this.outputs[i].displayName = port.displayName;
  585. }
  586. });
  587. }
  588. }
  589. /**
  590. * Release resources
  591. */
  592. public dispose() {
  593. for (var input of this.inputs) {
  594. input.dispose();
  595. }
  596. for (var output of this.outputs) {
  597. output.dispose();
  598. }
  599. }
  600. }