shaderMaterial.d.ts 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. import type { Nullable } from "../types";
  2. import { Scene } from "../scene";
  3. import { Matrix, Vector3, Vector2, Vector4, Quaternion } from "../Maths/math.vector";
  4. import type { AbstractMesh } from "../Meshes/abstractMesh";
  5. import type { Mesh } from "../Meshes/mesh";
  6. import type { SubMesh } from "../Meshes/subMesh";
  7. import type { BaseTexture } from "../Materials/Textures/baseTexture";
  8. import type { Effect, IShaderPath } from "./effect";
  9. import { Color3, Color4 } from "../Maths/math.color";
  10. import type { ShaderLanguage } from "./shaderLanguage";
  11. import type { UniformBuffer } from "./uniformBuffer";
  12. import type { TextureSampler } from "./Textures/textureSampler";
  13. import type { StorageBuffer } from "../Buffers/storageBuffer";
  14. import { PushMaterial } from "./pushMaterial";
  15. import type { ExternalTexture } from "./Textures/externalTexture";
  16. /**
  17. * Defines the options associated with the creation of a shader material.
  18. */
  19. export interface IShaderMaterialOptions {
  20. /**
  21. * Does the material work in alpha blend mode
  22. */
  23. needAlphaBlending: boolean;
  24. /**
  25. * Does the material work in alpha test mode
  26. */
  27. needAlphaTesting: boolean;
  28. /**
  29. * The list of attribute names used in the shader
  30. */
  31. attributes: string[];
  32. /**
  33. * The list of uniform names used in the shader
  34. */
  35. uniforms: string[];
  36. /**
  37. * The list of UBO names used in the shader
  38. */
  39. uniformBuffers: string[];
  40. /**
  41. * The list of sampler (texture) names used in the shader
  42. */
  43. samplers: string[];
  44. /**
  45. * The list of external texture names used in the shader
  46. */
  47. externalTextures: string[];
  48. /**
  49. * The list of sampler object names used in the shader
  50. */
  51. samplerObjects: string[];
  52. /**
  53. * The list of storage buffer names used in the shader
  54. */
  55. storageBuffers: string[];
  56. /**
  57. * The list of defines used in the shader
  58. */
  59. defines: string[];
  60. /**
  61. * Defines if clip planes have to be turned on: true to turn them on, false to turn them off and null to turn them on/off depending on the scene configuration (scene.clipPlaneX)
  62. */
  63. useClipPlane: Nullable<boolean>;
  64. /**
  65. * The language the shader is written in (default: GLSL)
  66. */
  67. shaderLanguage?: ShaderLanguage;
  68. }
  69. /**
  70. * The ShaderMaterial object has the necessary methods to pass data from your scene to the Vertex and Fragment Shaders and returns a material that can be applied to any mesh.
  71. *
  72. * This returned material effects how the mesh will look based on the code in the shaders.
  73. *
  74. * @see https://doc.babylonjs.com/features/featuresDeepDive/materials/shaders/shaderMaterial
  75. */
  76. export declare class ShaderMaterial extends PushMaterial {
  77. private _shaderPath;
  78. private _options;
  79. private _textures;
  80. private _textureArrays;
  81. private _externalTextures;
  82. private _floats;
  83. private _ints;
  84. private _uints;
  85. private _floatsArrays;
  86. private _colors3;
  87. private _colors3Arrays;
  88. private _colors4;
  89. private _colors4Arrays;
  90. private _vectors2;
  91. private _vectors3;
  92. private _vectors4;
  93. private _quaternions;
  94. private _quaternionsArrays;
  95. private _matrices;
  96. private _matrixArrays;
  97. private _matrices3x3;
  98. private _matrices2x2;
  99. private _vectors2Arrays;
  100. private _vectors3Arrays;
  101. private _vectors4Arrays;
  102. private _uniformBuffers;
  103. private _textureSamplers;
  104. private _storageBuffers;
  105. private _cachedWorldViewMatrix;
  106. private _cachedWorldViewProjectionMatrix;
  107. private _multiview;
  108. /**
  109. * @internal
  110. */
  111. _materialHelperNeedsPreviousMatrices: boolean;
  112. /** Define the Url to load snippets */
  113. static SnippetUrl: string;
  114. /** Snippet ID if the material was created from the snippet server */
  115. snippetId: string;
  116. /**
  117. * Instantiate a new shader material.
  118. * The ShaderMaterial object has the necessary methods to pass data from your scene to the Vertex and Fragment Shaders and returns a material that can be applied to any mesh.
  119. * This returned material effects how the mesh will look based on the code in the shaders.
  120. * @see https://doc.babylonjs.com/features/featuresDeepDive/materials/shaders/shaderMaterial
  121. * @param name Define the name of the material in the scene
  122. * @param scene Define the scene the material belongs to
  123. * @param shaderPath Defines the route to the shader code.
  124. * @param options Define the options used to create the shader
  125. * @param storeEffectOnSubMeshes true to store effect on submeshes, false to store the effect directly in the material class.
  126. */
  127. constructor(name: string, scene: Scene, shaderPath: IShaderPath | string, options?: Partial<IShaderMaterialOptions>, storeEffectOnSubMeshes?: boolean);
  128. /**
  129. * Gets the shader path used to define the shader code
  130. * It can be modified to trigger a new compilation
  131. */
  132. get shaderPath(): IShaderPath | string;
  133. /**
  134. * Sets the shader path used to define the shader code
  135. * It can be modified to trigger a new compilation
  136. */
  137. set shaderPath(shaderPath: IShaderPath | string);
  138. /**
  139. * Gets the options used to compile the shader.
  140. * They can be modified to trigger a new compilation
  141. */
  142. get options(): IShaderMaterialOptions;
  143. /**
  144. * is multiview set to true?
  145. */
  146. get isMultiview(): boolean;
  147. /**
  148. * Gets the current class name of the material e.g. "ShaderMaterial"
  149. * Mainly use in serialization.
  150. * @returns the class name
  151. */
  152. getClassName(): string;
  153. /**
  154. * Specifies if the material will require alpha blending
  155. * @returns a boolean specifying if alpha blending is needed
  156. */
  157. needAlphaBlending(): boolean;
  158. /**
  159. * Specifies if this material should be rendered in alpha test mode
  160. * @returns a boolean specifying if an alpha test is needed.
  161. */
  162. needAlphaTesting(): boolean;
  163. private _checkUniform;
  164. /**
  165. * Set a texture in the shader.
  166. * @param name Define the name of the uniform samplers as defined in the shader
  167. * @param texture Define the texture to bind to this sampler
  168. * @returns the material itself allowing "fluent" like uniform updates
  169. */
  170. setTexture(name: string, texture: BaseTexture): ShaderMaterial;
  171. /**
  172. * Set a texture array in the shader.
  173. * @param name Define the name of the uniform sampler array as defined in the shader
  174. * @param textures Define the list of textures to bind to this sampler
  175. * @returns the material itself allowing "fluent" like uniform updates
  176. */
  177. setTextureArray(name: string, textures: BaseTexture[]): ShaderMaterial;
  178. /**
  179. * Set an internal texture in the shader.
  180. * @param name Define the name of the uniform samplers as defined in the shader
  181. * @param texture Define the texture to bind to this sampler
  182. * @returns the material itself allowing "fluent" like uniform updates
  183. */
  184. setExternalTexture(name: string, texture: ExternalTexture): ShaderMaterial;
  185. /**
  186. * Set a float in the shader.
  187. * @param name Define the name of the uniform as defined in the shader
  188. * @param value Define the value to give to the uniform
  189. * @returns the material itself allowing "fluent" like uniform updates
  190. */
  191. setFloat(name: string, value: number): ShaderMaterial;
  192. /**
  193. * Set a int in the shader.
  194. * @param name Define the name of the uniform as defined in the shader
  195. * @param value Define the value to give to the uniform
  196. * @returns the material itself allowing "fluent" like uniform updates
  197. */
  198. setInt(name: string, value: number): ShaderMaterial;
  199. /**
  200. * Set a unsigned int in the shader.
  201. * @param name Define the name of the uniform as defined in the shader
  202. * @param value Define the value to give to the uniform
  203. * @returns the material itself allowing "fluent" like uniform updates
  204. */
  205. setUInt(name: string, value: number): ShaderMaterial;
  206. /**
  207. * Set an array of floats in the shader.
  208. * @param name Define the name of the uniform as defined in the shader
  209. * @param value Define the value to give to the uniform
  210. * @returns the material itself allowing "fluent" like uniform updates
  211. */
  212. setFloats(name: string, value: number[]): ShaderMaterial;
  213. /**
  214. * Set a vec3 in the shader from a Color3.
  215. * @param name Define the name of the uniform as defined in the shader
  216. * @param value Define the value to give to the uniform
  217. * @returns the material itself allowing "fluent" like uniform updates
  218. */
  219. setColor3(name: string, value: Color3): ShaderMaterial;
  220. /**
  221. * Set a vec3 array in the shader from a Color3 array.
  222. * @param name Define the name of the uniform as defined in the shader
  223. * @param value Define the value to give to the uniform
  224. * @returns the material itself allowing "fluent" like uniform updates
  225. */
  226. setColor3Array(name: string, value: Color3[]): ShaderMaterial;
  227. /**
  228. * Set a vec4 in the shader from a Color4.
  229. * @param name Define the name of the uniform as defined in the shader
  230. * @param value Define the value to give to the uniform
  231. * @returns the material itself allowing "fluent" like uniform updates
  232. */
  233. setColor4(name: string, value: Color4): ShaderMaterial;
  234. /**
  235. * Set a vec4 array in the shader from a Color4 array.
  236. * @param name Define the name of the uniform as defined in the shader
  237. * @param value Define the value to give to the uniform
  238. * @returns the material itself allowing "fluent" like uniform updates
  239. */
  240. setColor4Array(name: string, value: Color4[]): ShaderMaterial;
  241. /**
  242. * Set a vec2 in the shader from a Vector2.
  243. * @param name Define the name of the uniform as defined in the shader
  244. * @param value Define the value to give to the uniform
  245. * @returns the material itself allowing "fluent" like uniform updates
  246. */
  247. setVector2(name: string, value: Vector2): ShaderMaterial;
  248. /**
  249. * Set a vec3 in the shader from a Vector3.
  250. * @param name Define the name of the uniform as defined in the shader
  251. * @param value Define the value to give to the uniform
  252. * @returns the material itself allowing "fluent" like uniform updates
  253. */
  254. setVector3(name: string, value: Vector3): ShaderMaterial;
  255. /**
  256. * Set a vec4 in the shader from a Vector4.
  257. * @param name Define the name of the uniform as defined in the shader
  258. * @param value Define the value to give to the uniform
  259. * @returns the material itself allowing "fluent" like uniform updates
  260. */
  261. setVector4(name: string, value: Vector4): ShaderMaterial;
  262. /**
  263. * Set a vec4 in the shader from a Quaternion.
  264. * @param name Define the name of the uniform as defined in the shader
  265. * @param value Define the value to give to the uniform
  266. * @returns the material itself allowing "fluent" like uniform updates
  267. */
  268. setQuaternion(name: string, value: Quaternion): ShaderMaterial;
  269. /**
  270. * Set a vec4 array in the shader from a Quaternion array.
  271. * @param name Define the name of the uniform as defined in the shader
  272. * @param value Define the value to give to the uniform
  273. * @returns the material itself allowing "fluent" like uniform updates
  274. */
  275. setQuaternionArray(name: string, value: Quaternion[]): ShaderMaterial;
  276. /**
  277. * Set a mat4 in the shader from a Matrix.
  278. * @param name Define the name of the uniform as defined in the shader
  279. * @param value Define the value to give to the uniform
  280. * @returns the material itself allowing "fluent" like uniform updates
  281. */
  282. setMatrix(name: string, value: Matrix): ShaderMaterial;
  283. /**
  284. * Set a float32Array in the shader from a matrix array.
  285. * @param name Define the name of the uniform as defined in the shader
  286. * @param value Define the value to give to the uniform
  287. * @returns the material itself allowing "fluent" like uniform updates
  288. */
  289. setMatrices(name: string, value: Matrix[]): ShaderMaterial;
  290. /**
  291. * Set a mat3 in the shader from a Float32Array.
  292. * @param name Define the name of the uniform as defined in the shader
  293. * @param value Define the value to give to the uniform
  294. * @returns the material itself allowing "fluent" like uniform updates
  295. */
  296. setMatrix3x3(name: string, value: Float32Array | Array<number>): ShaderMaterial;
  297. /**
  298. * Set a mat2 in the shader from a Float32Array.
  299. * @param name Define the name of the uniform as defined in the shader
  300. * @param value Define the value to give to the uniform
  301. * @returns the material itself allowing "fluent" like uniform updates
  302. */
  303. setMatrix2x2(name: string, value: Float32Array | Array<number>): ShaderMaterial;
  304. /**
  305. * Set a vec2 array in the shader from a number array.
  306. * @param name Define the name of the uniform as defined in the shader
  307. * @param value Define the value to give to the uniform
  308. * @returns the material itself allowing "fluent" like uniform updates
  309. */
  310. setArray2(name: string, value: number[]): ShaderMaterial;
  311. /**
  312. * Set a vec3 array in the shader from a number array.
  313. * @param name Define the name of the uniform as defined in the shader
  314. * @param value Define the value to give to the uniform
  315. * @returns the material itself allowing "fluent" like uniform updates
  316. */
  317. setArray3(name: string, value: number[]): ShaderMaterial;
  318. /**
  319. * Set a vec4 array in the shader from a number array.
  320. * @param name Define the name of the uniform as defined in the shader
  321. * @param value Define the value to give to the uniform
  322. * @returns the material itself allowing "fluent" like uniform updates
  323. */
  324. setArray4(name: string, value: number[]): ShaderMaterial;
  325. /**
  326. * Set a uniform buffer in the shader
  327. * @param name Define the name of the uniform as defined in the shader
  328. * @param buffer Define the value to give to the uniform
  329. * @returns the material itself allowing "fluent" like uniform updates
  330. */
  331. setUniformBuffer(name: string, buffer: UniformBuffer): ShaderMaterial;
  332. /**
  333. * Set a texture sampler in the shader
  334. * @param name Define the name of the uniform as defined in the shader
  335. * @param sampler Define the value to give to the uniform
  336. * @returns the material itself allowing "fluent" like uniform updates
  337. */
  338. setTextureSampler(name: string, sampler: TextureSampler): ShaderMaterial;
  339. /**
  340. * Set a storage buffer in the shader
  341. * @param name Define the name of the storage buffer as defined in the shader
  342. * @param buffer Define the value to give to the uniform
  343. * @returns the material itself allowing "fluent" like uniform updates
  344. */
  345. setStorageBuffer(name: string, buffer: StorageBuffer): ShaderMaterial;
  346. /**
  347. * Adds, removes, or replaces the specified shader define and value.
  348. * * setDefine("MY_DEFINE", true); // enables a boolean define
  349. * * setDefine("MY_DEFINE", "0.5"); // adds "#define MY_DEFINE 0.5" to the shader (or sets and replaces the value of any existing define with that name)
  350. * * setDefine("MY_DEFINE", false); // disables and removes the define
  351. * Note if the active defines do change, the shader will be recompiled and this can be expensive.
  352. * @param define the define name e.g., "OUTPUT_TO_SRGB" or "#define OUTPUT_TO_SRGB". If the define was passed into the constructor already, the version used should match that, and in either case, it should not include any appended value.
  353. * @param value either the value of the define (e.g. a numerical value) or for booleans, true if the define should be enabled or false if it should be disabled
  354. * @returns the material itself allowing "fluent" like uniform updates
  355. */
  356. setDefine(define: string, value: boolean | string): ShaderMaterial;
  357. /**
  358. * Specifies that the submesh is ready to be used
  359. * @param mesh defines the mesh to check
  360. * @param subMesh defines which submesh to check
  361. * @param useInstances specifies that instances should be used
  362. * @returns a boolean indicating that the submesh is ready or not
  363. */
  364. isReadyForSubMesh(mesh: AbstractMesh, subMesh: SubMesh, useInstances?: boolean): boolean;
  365. /**
  366. * Checks if the material is ready to render the requested mesh
  367. * @param mesh Define the mesh to render
  368. * @param useInstances Define whether or not the material is used with instances
  369. * @param subMesh defines which submesh to render
  370. * @returns true if ready, otherwise false
  371. */
  372. isReady(mesh?: AbstractMesh, useInstances?: boolean, subMesh?: SubMesh): boolean;
  373. /**
  374. * Binds the world matrix to the material
  375. * @param world defines the world transformation matrix
  376. * @param effectOverride - If provided, use this effect instead of internal effect
  377. */
  378. bindOnlyWorldMatrix(world: Matrix, effectOverride?: Nullable<Effect>): void;
  379. /**
  380. * Binds the submesh to this material by preparing the effect and shader to draw
  381. * @param world defines the world transformation matrix
  382. * @param mesh defines the mesh containing the submesh
  383. * @param subMesh defines the submesh to bind the material to
  384. */
  385. bindForSubMesh(world: Matrix, mesh: Mesh, subMesh: SubMesh): void;
  386. /**
  387. * Binds the material to the mesh
  388. * @param world defines the world transformation matrix
  389. * @param mesh defines the mesh to bind the material to
  390. * @param effectOverride - If provided, use this effect instead of internal effect
  391. * @param subMesh defines the submesh to bind the material to
  392. */
  393. bind(world: Matrix, mesh?: Mesh, effectOverride?: Nullable<Effect>, subMesh?: SubMesh): void;
  394. /**
  395. * Gets the active textures from the material
  396. * @returns an array of textures
  397. */
  398. getActiveTextures(): BaseTexture[];
  399. /**
  400. * Specifies if the material uses a texture
  401. * @param texture defines the texture to check against the material
  402. * @returns a boolean specifying if the material uses the texture
  403. */
  404. hasTexture(texture: BaseTexture): boolean;
  405. /**
  406. * Makes a duplicate of the material, and gives it a new name
  407. * @param name defines the new name for the duplicated material
  408. * @returns the cloned material
  409. */
  410. clone(name: string): ShaderMaterial;
  411. /**
  412. * Disposes the material
  413. * @param forceDisposeEffect specifies if effects should be forcefully disposed
  414. * @param forceDisposeTextures specifies if textures should be forcefully disposed
  415. * @param notBoundToMesh specifies if the material that is being disposed is known to be not bound to any mesh
  416. */
  417. dispose(forceDisposeEffect?: boolean, forceDisposeTextures?: boolean, notBoundToMesh?: boolean): void;
  418. /**
  419. * Serializes this material in a JSON representation
  420. * @returns the serialized material object
  421. */
  422. serialize(): any;
  423. /**
  424. * Creates a shader material from parsed shader material data
  425. * @param source defines the JSON representation of the material
  426. * @param scene defines the hosting scene
  427. * @param rootUrl defines the root URL to use to load textures and relative dependencies
  428. * @returns a new material
  429. */
  430. static Parse(source: any, scene: Scene, rootUrl: string): ShaderMaterial;
  431. /**
  432. * Creates a new ShaderMaterial from a snippet saved in a remote file
  433. * @param name defines the name of the ShaderMaterial to create (can be null or empty to use the one from the json data)
  434. * @param url defines the url to load from
  435. * @param scene defines the hosting scene
  436. * @param rootUrl defines the root URL to use to load textures and relative dependencies
  437. * @returns a promise that will resolve to the new ShaderMaterial
  438. */
  439. static ParseFromFileAsync(name: Nullable<string>, url: string, scene: Scene, rootUrl?: string): Promise<ShaderMaterial>;
  440. /**
  441. * Creates a ShaderMaterial from a snippet saved by the Inspector
  442. * @param snippetId defines the snippet to load
  443. * @param scene defines the hosting scene
  444. * @param rootUrl defines the root URL to use to load textures and relative dependencies
  445. * @returns a promise that will resolve to the new ShaderMaterial
  446. */
  447. static ParseFromSnippetAsync(snippetId: string, scene: Scene, rootUrl?: string): Promise<ShaderMaterial>;
  448. /**
  449. * Creates a ShaderMaterial from a snippet saved by the Inspector
  450. * @deprecated Please use ParseFromSnippetAsync instead
  451. * @param snippetId defines the snippet to load
  452. * @param scene defines the hosting scene
  453. * @param rootUrl defines the root URL to use to load textures and relative dependencies
  454. * @returns a promise that will resolve to the new ShaderMaterial
  455. */
  456. static CreateFromSnippetAsync: typeof ShaderMaterial.ParseFromSnippetAsync;
  457. }