nodeMaterialBlock.d.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. import { NodeMaterialBlockConnectionPointTypes } from "./Enums/nodeMaterialBlockConnectionPointTypes";
  2. import type { NodeMaterialBuildState } from "./nodeMaterialBuildState";
  3. import type { Nullable } from "../../types";
  4. import { NodeMaterialConnectionPoint } from "./nodeMaterialBlockConnectionPoint";
  5. import { NodeMaterialBlockTargets } from "./Enums/nodeMaterialBlockTargets";
  6. import type { Effect } from "../effect";
  7. import type { AbstractMesh } from "../../Meshes/abstractMesh";
  8. import type { Mesh } from "../../Meshes/mesh";
  9. import type { SubMesh } from "../../Meshes/subMesh";
  10. import type { NodeMaterial, NodeMaterialDefines } from "./nodeMaterial";
  11. import type { Scene } from "../../scene";
  12. import type { EffectFallbacks } from "../effectFallbacks";
  13. /**
  14. * Defines a block that can be used inside a node based material
  15. */
  16. export declare class NodeMaterialBlock {
  17. private _buildId;
  18. private _buildTarget;
  19. protected _target: NodeMaterialBlockTargets;
  20. private _isFinalMerger;
  21. private _isInput;
  22. private _isTeleportOut;
  23. private _isTeleportIn;
  24. private _name;
  25. protected _isUnique: boolean;
  26. /** Gets or sets a boolean indicating that only one input can be connected at a time */
  27. inputsAreExclusive: boolean;
  28. /** @internal */
  29. _codeVariableName: string;
  30. /** @internal */
  31. _inputs: NodeMaterialConnectionPoint[];
  32. /** @internal */
  33. _outputs: NodeMaterialConnectionPoint[];
  34. /** @internal */
  35. _preparationId: number;
  36. /** @internal */
  37. readonly _originalTargetIsNeutral: boolean;
  38. /**
  39. * Gets the name of the block
  40. */
  41. get name(): string;
  42. /**
  43. * Sets the name of the block. Will check if the name is valid.
  44. */
  45. set name(newName: string);
  46. /**
  47. * Gets or sets the unique id of the node
  48. */
  49. uniqueId: number;
  50. /**
  51. * Gets or sets the comments associated with this block
  52. */
  53. comments: string;
  54. /**
  55. * Gets a boolean indicating that this block can only be used once per NodeMaterial
  56. */
  57. get isUnique(): boolean;
  58. /**
  59. * Gets a boolean indicating that this block is an end block (e.g. it is generating a system value)
  60. */
  61. get isFinalMerger(): boolean;
  62. /**
  63. * Gets a boolean indicating that this block is an input (e.g. it sends data to the shader)
  64. */
  65. get isInput(): boolean;
  66. /**
  67. * Gets a boolean indicating if this block is a teleport out
  68. */
  69. get isTeleportOut(): boolean;
  70. /**
  71. * Gets a boolean indicating if this block is a teleport in
  72. */
  73. get isTeleportIn(): boolean;
  74. /**
  75. * Gets or sets the build Id
  76. */
  77. get buildId(): number;
  78. set buildId(value: number);
  79. /**
  80. * Gets or sets the target of the block
  81. */
  82. get target(): NodeMaterialBlockTargets;
  83. set target(value: NodeMaterialBlockTargets);
  84. /**
  85. * Gets the list of input points
  86. */
  87. get inputs(): NodeMaterialConnectionPoint[];
  88. /** Gets the list of output points */
  89. get outputs(): NodeMaterialConnectionPoint[];
  90. /**
  91. * Find an input by its name
  92. * @param name defines the name of the input to look for
  93. * @returns the input or null if not found
  94. */
  95. getInputByName(name: string): NodeMaterialConnectionPoint | null;
  96. /**
  97. * Find an output by its name
  98. * @param name defines the name of the output to look for
  99. * @returns the output or null if not found
  100. */
  101. getOutputByName(name: string): NodeMaterialConnectionPoint | null;
  102. /** Gets or sets a boolean indicating that this input can be edited in the Inspector (false by default) */
  103. visibleInInspector: boolean;
  104. /** Gets or sets a boolean indicating that this input can be edited from a collapsed frame */
  105. visibleOnFrame: boolean;
  106. /**
  107. * Creates a new NodeMaterialBlock
  108. * @param name defines the block name
  109. * @param target defines the target of that block (Vertex by default)
  110. * @param isFinalMerger defines a boolean indicating that this block is an end block (e.g. it is generating a system value). Default is false
  111. */
  112. constructor(name: string, target?: NodeMaterialBlockTargets, isFinalMerger?: boolean);
  113. /** @internal */
  114. _setInitialTarget(target: NodeMaterialBlockTargets): void;
  115. /**
  116. * Initialize the block and prepare the context for build
  117. * @param state defines the state that will be used for the build
  118. */
  119. initialize(state: NodeMaterialBuildState): void;
  120. /**
  121. * Bind data to effect. Will only be called for blocks with isBindable === true
  122. * @param effect defines the effect to bind data to
  123. * @param nodeMaterial defines the hosting NodeMaterial
  124. * @param mesh defines the mesh that will be rendered
  125. * @param subMesh defines the submesh that will be rendered
  126. */
  127. bind(effect: Effect, nodeMaterial: NodeMaterial, mesh?: Mesh, subMesh?: SubMesh): void;
  128. protected _declareOutput(output: NodeMaterialConnectionPoint, state: NodeMaterialBuildState): string;
  129. protected _writeVariable(currentPoint: NodeMaterialConnectionPoint): string;
  130. protected _writeFloat(value: number): string;
  131. /**
  132. * Gets the current class name e.g. "NodeMaterialBlock"
  133. * @returns the class name
  134. */
  135. getClassName(): string;
  136. /** Gets a boolean indicating that this connection will be used in the fragment shader
  137. * @returns true if connected in fragment shader
  138. */
  139. isConnectedInFragmentShader(): boolean;
  140. /**
  141. * Register a new input. Must be called inside a block constructor
  142. * @param name defines the connection point name
  143. * @param type defines the connection point type
  144. * @param isOptional defines a boolean indicating that this input can be omitted
  145. * @param target defines the target to use to limit the connection point (will be VertexAndFragment by default)
  146. * @param point an already created connection point. If not provided, create a new one
  147. * @returns the current block
  148. */
  149. registerInput(name: string, type: NodeMaterialBlockConnectionPointTypes, isOptional?: boolean, target?: NodeMaterialBlockTargets, point?: NodeMaterialConnectionPoint): this;
  150. /**
  151. * Register a new output. Must be called inside a block constructor
  152. * @param name defines the connection point name
  153. * @param type defines the connection point type
  154. * @param target defines the target to use to limit the connection point (will be VertexAndFragment by default)
  155. * @param point an already created connection point. If not provided, create a new one
  156. * @returns the current block
  157. */
  158. registerOutput(name: string, type: NodeMaterialBlockConnectionPointTypes, target?: NodeMaterialBlockTargets, point?: NodeMaterialConnectionPoint): this;
  159. /**
  160. * Will return the first available input e.g. the first one which is not an uniform or an attribute
  161. * @param forOutput defines an optional connection point to check compatibility with
  162. * @returns the first available input or null
  163. */
  164. getFirstAvailableInput(forOutput?: Nullable<NodeMaterialConnectionPoint>): NodeMaterialConnectionPoint | null;
  165. /**
  166. * Will return the first available output e.g. the first one which is not yet connected and not a varying
  167. * @param forBlock defines an optional block to check compatibility with
  168. * @returns the first available input or null
  169. */
  170. getFirstAvailableOutput(forBlock?: Nullable<NodeMaterialBlock>): NodeMaterialConnectionPoint | null;
  171. /**
  172. * Gets the sibling of the given output
  173. * @param current defines the current output
  174. * @returns the next output in the list or null
  175. */
  176. getSiblingOutput(current: NodeMaterialConnectionPoint): NodeMaterialConnectionPoint | null;
  177. /**
  178. * Checks if the current block is an ancestor of a given block
  179. * @param block defines the potential descendant block to check
  180. * @returns true if block is a descendant
  181. */
  182. isAnAncestorOf(block: NodeMaterialBlock): boolean;
  183. /**
  184. * Connect current block with another block
  185. * @param other defines the block to connect with
  186. * @param options define the various options to help pick the right connections
  187. * @param options.input
  188. * @param options.output
  189. * @param options.outputSwizzle
  190. * @returns the current block
  191. */
  192. connectTo(other: NodeMaterialBlock, options?: {
  193. input?: string;
  194. output?: string;
  195. outputSwizzle?: string;
  196. }): this | undefined;
  197. protected _buildBlock(state: NodeMaterialBuildState): void;
  198. /**
  199. * Add uniforms, samplers and uniform buffers at compilation time
  200. * @param state defines the state to update
  201. * @param nodeMaterial defines the node material requesting the update
  202. * @param defines defines the material defines to update
  203. * @param uniformBuffers defines the list of uniform buffer names
  204. */
  205. updateUniformsAndSamples(state: NodeMaterialBuildState, nodeMaterial: NodeMaterial, defines: NodeMaterialDefines, uniformBuffers: string[]): void;
  206. /**
  207. * Add potential fallbacks if shader compilation fails
  208. * @param mesh defines the mesh to be rendered
  209. * @param fallbacks defines the current prioritized list of fallbacks
  210. */
  211. provideFallbacks(mesh: AbstractMesh, fallbacks: EffectFallbacks): void;
  212. /**
  213. * Initialize defines for shader compilation
  214. * @param mesh defines the mesh to be rendered
  215. * @param nodeMaterial defines the node material requesting the update
  216. * @param defines defines the material defines to update
  217. * @param useInstances specifies that instances should be used
  218. */
  219. initializeDefines(mesh: AbstractMesh, nodeMaterial: NodeMaterial, defines: NodeMaterialDefines, useInstances?: boolean): void;
  220. /**
  221. * Update defines for shader compilation
  222. * @param mesh defines the mesh to be rendered
  223. * @param nodeMaterial defines the node material requesting the update
  224. * @param defines defines the material defines to update
  225. * @param useInstances specifies that instances should be used
  226. * @param subMesh defines which submesh to render
  227. */
  228. prepareDefines(mesh: AbstractMesh, nodeMaterial: NodeMaterial, defines: NodeMaterialDefines, useInstances?: boolean, subMesh?: SubMesh): void;
  229. /**
  230. * Lets the block try to connect some inputs automatically
  231. * @param material defines the hosting NodeMaterial
  232. * @param additionalFilteringInfo optional additional filtering condition when looking for compatible blocks
  233. */
  234. autoConfigure(material: NodeMaterial, additionalFilteringInfo?: (node: NodeMaterialBlock) => boolean): void;
  235. /**
  236. * Function called when a block is declared as repeatable content generator
  237. * @param vertexShaderState defines the current compilation state for the vertex shader
  238. * @param fragmentShaderState defines the current compilation state for the fragment shader
  239. * @param mesh defines the mesh to be rendered
  240. * @param defines defines the material defines to update
  241. */
  242. replaceRepeatableContent(vertexShaderState: NodeMaterialBuildState, fragmentShaderState: NodeMaterialBuildState, mesh: AbstractMesh, defines: NodeMaterialDefines): void;
  243. /** Gets a boolean indicating that the code of this block will be promoted to vertex shader even if connected to fragment output */
  244. get willBeGeneratedIntoVertexShaderFromFragmentShader(): boolean;
  245. /**
  246. * Checks if the block is ready
  247. * @param mesh defines the mesh to be rendered
  248. * @param nodeMaterial defines the node material requesting the update
  249. * @param defines defines the material defines to update
  250. * @param useInstances specifies that instances should be used
  251. * @returns true if the block is ready
  252. */
  253. isReady(mesh: AbstractMesh, nodeMaterial: NodeMaterial, defines: NodeMaterialDefines, useInstances?: boolean): boolean;
  254. protected _linkConnectionTypes(inputIndex0: number, inputIndex1: number, looseCoupling?: boolean): void;
  255. private _processBuild;
  256. /**
  257. * Validates the new name for the block node.
  258. * @param newName the new name to be given to the node.
  259. * @returns false if the name is a reserve word, else true.
  260. */
  261. validateBlockName(newName: string): boolean;
  262. protected _customBuildStep(state: NodeMaterialBuildState, activeBlocks: NodeMaterialBlock[]): void;
  263. /**
  264. * Compile the current node and generate the shader code
  265. * @param state defines the current compilation state (uniforms, samplers, current string)
  266. * @param activeBlocks defines the list of active blocks (i.e. blocks to compile)
  267. * @returns true if already built
  268. */
  269. build(state: NodeMaterialBuildState, activeBlocks: NodeMaterialBlock[]): boolean;
  270. protected _inputRename(name: string): string;
  271. protected _outputRename(name: string): string;
  272. protected _dumpPropertiesCode(): string;
  273. /**
  274. * @internal
  275. */
  276. _dumpCode(uniqueNames: string[], alreadyDumped: NodeMaterialBlock[]): string;
  277. /**
  278. * @internal
  279. */
  280. _dumpCodeForOutputConnections(alreadyDumped: NodeMaterialBlock[]): string;
  281. /**
  282. * Clone the current block to a new identical block
  283. * @param scene defines the hosting scene
  284. * @param rootUrl defines the root URL to use to load textures and relative dependencies
  285. * @returns a copy of the current block
  286. */
  287. clone(scene: Scene, rootUrl?: string): NodeMaterialBlock | null;
  288. /**
  289. * Serializes this block in a JSON representation
  290. * @returns the serialized block object
  291. */
  292. serialize(): any;
  293. /**
  294. * @internal
  295. */
  296. _deserialize(serializationObject: any, scene: Scene, rootUrl: string): void;
  297. private _deserializePortDisplayNamesAndExposedOnFrame;
  298. /**
  299. * Release resources
  300. */
  301. dispose(): void;
  302. }