effect.ts 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276
  1. import { Observable } from "../Misc/observable";
  2. import { FloatArray, Nullable } from "../types";
  3. import { Constants } from "../Engines/constants";
  4. import { DomManagement } from "../Misc/domManagement";
  5. import { Logger } from "../Misc/logger";
  6. import { IDisposable } from '../scene';
  7. import { IPipelineContext } from '../Engines/IPipelineContext';
  8. import { DataBuffer } from '../Meshes/dataBuffer';
  9. import { ShaderProcessor } from '../Engines/Processors/shaderProcessor';
  10. import { ProcessingOptions, ShaderProcessingContext } from '../Engines/Processors/shaderProcessingOptions';
  11. import { IMatrixLike, IVector2Like, IVector3Like, IVector4Like, IColor3Like, IColor4Like } from '../Maths/math.like';
  12. import { ThinEngine } from '../Engines/thinEngine';
  13. import { IEffectFallbacks } from './iEffectFallbacks';
  14. declare type Engine = import("../Engines/engine").Engine;
  15. declare type InternalTexture = import("../Materials/Textures/internalTexture").InternalTexture;
  16. declare type BaseTexture = import("../Materials/Textures/baseTexture").BaseTexture;
  17. declare type RenderTargetTexture = import("../Materials/Textures/renderTargetTexture").RenderTargetTexture;
  18. declare type PostProcess = import("../PostProcesses/postProcess").PostProcess;
  19. /**
  20. * Options to be used when creating an effect.
  21. */
  22. export interface IEffectCreationOptions {
  23. /**
  24. * Atrributes that will be used in the shader.
  25. */
  26. attributes: string[];
  27. /**
  28. * Uniform varible names that will be set in the shader.
  29. */
  30. uniformsNames: string[];
  31. /**
  32. * Uniform buffer variable names that will be set in the shader.
  33. */
  34. uniformBuffersNames: string[];
  35. /**
  36. * Sampler texture variable names that will be set in the shader.
  37. */
  38. samplers: string[];
  39. /**
  40. * Define statements that will be set in the shader.
  41. */
  42. defines: any;
  43. /**
  44. * Possible fallbacks for this effect to improve performance when needed.
  45. */
  46. fallbacks: Nullable<IEffectFallbacks>;
  47. /**
  48. * Callback that will be called when the shader is compiled.
  49. */
  50. onCompiled: Nullable<(effect: Effect) => void>;
  51. /**
  52. * Callback that will be called if an error occurs during shader compilation.
  53. */
  54. onError: Nullable<(effect: Effect, errors: string) => void>;
  55. /**
  56. * Parameters to be used with Babylons include syntax to iterate over an array (eg. {lights: 10})
  57. */
  58. indexParameters?: any;
  59. /**
  60. * Max number of lights that can be used in the shader.
  61. */
  62. maxSimultaneousLights?: number;
  63. /**
  64. * See https://developer.mozilla.org/en-US/docs/Web/API/WebGL2RenderingContext/transformFeedbackVaryings
  65. */
  66. transformFeedbackVaryings?: Nullable<string[]>;
  67. /**
  68. * If provided, will be called two times with the vertex and fragment code so that this code can be updated before it is compiled by the GPU
  69. */
  70. processFinalCode?: Nullable<(shaderType: string, code: string) => string>;
  71. /**
  72. * Is this effect rendering to several color attachments ?
  73. */
  74. multiTarget?: boolean;
  75. }
  76. /**
  77. * Effect containing vertex and fragment shader that can be executed on an object.
  78. */
  79. export class Effect implements IDisposable {
  80. /**
  81. * Gets or sets the relative url used to load shaders if using the engine in non-minified mode
  82. */
  83. public static ShadersRepository = "src/Shaders/";
  84. /**
  85. * Enable logging of the shader code when a compilation error occurs
  86. */
  87. public static LogShaderCodeOnCompilationError = true;
  88. /**
  89. * Name of the effect.
  90. */
  91. public name: any = null;
  92. /**
  93. * String container all the define statements that should be set on the shader.
  94. */
  95. public defines: string = "";
  96. /**
  97. * Callback that will be called when the shader is compiled.
  98. */
  99. public onCompiled: Nullable<(effect: Effect) => void> = null;
  100. /**
  101. * Callback that will be called if an error occurs during shader compilation.
  102. */
  103. public onError: Nullable<(effect: Effect, errors: string) => void> = null;
  104. /**
  105. * Callback that will be called when effect is bound.
  106. */
  107. public onBind: Nullable<(effect: Effect) => void> = null;
  108. /**
  109. * Unique ID of the effect.
  110. */
  111. public uniqueId = 0;
  112. /**
  113. * Observable that will be called when the shader is compiled.
  114. * It is recommended to use executeWhenCompile() or to make sure that scene.isReady() is called to get this observable raised.
  115. */
  116. public onCompileObservable = new Observable<Effect>();
  117. /**
  118. * Observable that will be called if an error occurs during shader compilation.
  119. */
  120. public onErrorObservable = new Observable<Effect>();
  121. /** @hidden */
  122. public _onBindObservable: Nullable<Observable<Effect>> = null;
  123. /**
  124. * @hidden
  125. * Specifies if the effect was previously ready
  126. */
  127. public _wasPreviouslyReady = false;
  128. /**
  129. * Observable that will be called when effect is bound.
  130. */
  131. public get onBindObservable(): Observable<Effect> {
  132. if (!this._onBindObservable) {
  133. this._onBindObservable = new Observable<Effect>();
  134. }
  135. return this._onBindObservable;
  136. }
  137. /** @hidden */
  138. public _bonesComputationForcedToCPU = false;
  139. /** @hidden */
  140. public _uniformBuffersNames: { [key: string]: number } = {};
  141. /** @hidden */
  142. public _samplerList: string[];
  143. public _multiTarget: boolean = false;
  144. private static _uniqueIdSeed = 0;
  145. private _engine: Engine;
  146. private _uniformBuffersNamesList: string[];
  147. private _uniformsNames: string[];
  148. private _samplers: { [key: string]: number } = {};
  149. private _isReady = false;
  150. private _compilationError = "";
  151. private _allFallbacksProcessed = false;
  152. private _attributesNames: string[];
  153. private _attributes: number[];
  154. private _attributeLocationByName: { [name: string] : number };
  155. private _uniforms: { [key: string]: Nullable<WebGLUniformLocation> } = {};
  156. /**
  157. * Key for the effect.
  158. * @hidden
  159. */
  160. public _key: string = "";
  161. private _indexParameters: any;
  162. private _fallbacks: Nullable<IEffectFallbacks> = null;
  163. private _vertexSourceCodeOverride: string = "";
  164. private _fragmentSourceCodeOverride: string = "";
  165. private _transformFeedbackVaryings: Nullable<string[]> = null;
  166. /**
  167. * Compiled shader to webGL program.
  168. * @hidden
  169. */
  170. public _pipelineContext: Nullable<IPipelineContext> = null;
  171. /** @hidden */
  172. public _vertexSourceCode: string = "";
  173. /** @hidden */
  174. public _fragmentSourceCode: string = "";
  175. /** @hidden */
  176. private _rawVertexSourceCode: string = "";
  177. /** @hidden */
  178. private _rawFragmentSourceCode: string = "";
  179. private static _baseCache: { [key: number]: DataBuffer } = {};
  180. private _processingContext: Nullable<ShaderProcessingContext>;
  181. /**
  182. * Instantiates an effect.
  183. * An effect can be used to create/manage/execute vertex and fragment shaders.
  184. * @param baseName Name of the effect.
  185. * @param attributesNamesOrOptions List of attribute names that will be passed to the shader or set of all options to create the effect.
  186. * @param uniformsNamesOrEngine List of uniform variable names that will be passed to the shader or the engine that will be used to render effect.
  187. * @param samplers List of sampler variables that will be passed to the shader.
  188. * @param engine Engine to be used to render the effect
  189. * @param defines Define statements to be added to the shader.
  190. * @param fallbacks Possible fallbacks for this effect to improve performance when needed.
  191. * @param onCompiled Callback that will be called when the shader is compiled.
  192. * @param onError Callback that will be called if an error occurs during shader compilation.
  193. * @param indexParameters Parameters to be used with Babylons include syntax to iterate over an array (eg. {lights: 10})
  194. * @param key Effect Key identifying uniquely compiled shader variants
  195. * @param sources Already processed sources for the current key.
  196. */
  197. constructor(baseName: any, attributesNamesOrOptions: string[] | IEffectCreationOptions, uniformsNamesOrEngine: string[] | ThinEngine, samplers: Nullable<string[]> = null,
  198. engine?: ThinEngine, defines: Nullable<string> = null,
  199. fallbacks: Nullable<IEffectFallbacks> = null, onCompiled: Nullable<(effect: Effect) => void> = null, onError: Nullable<(effect: Effect, errors: string) => void> = null, indexParameters?: any, key: string = "",
  200. sources?: {
  201. vertex: string,
  202. fragment: string,
  203. rawVertex: string,
  204. rawFragment: string,
  205. }) {
  206. this.name = baseName;
  207. this._key = key;
  208. let processFinalCode: Nullable<(shaderType: string, code: string) => string> = null;
  209. if ((<IEffectCreationOptions>attributesNamesOrOptions).attributes) {
  210. var options = <IEffectCreationOptions>attributesNamesOrOptions;
  211. this._engine = <Engine>uniformsNamesOrEngine;
  212. this._attributesNames = options.attributes;
  213. this._uniformsNames = options.uniformsNames.concat(options.samplers);
  214. this._samplerList = options.samplers.slice();
  215. this.defines = options.defines;
  216. this.onError = options.onError;
  217. this.onCompiled = options.onCompiled;
  218. this._fallbacks = options.fallbacks;
  219. this._indexParameters = options.indexParameters;
  220. this._transformFeedbackVaryings = options.transformFeedbackVaryings || null;
  221. this._multiTarget = !!options.multiTarget;
  222. if (options.uniformBuffersNames) {
  223. this._uniformBuffersNamesList = options.uniformBuffersNames.slice();
  224. for (var i = 0; i < options.uniformBuffersNames.length; i++) {
  225. this._uniformBuffersNames[options.uniformBuffersNames[i]] = i;
  226. }
  227. }
  228. processFinalCode = options.processFinalCode ?? null;
  229. } else {
  230. this._engine = <Engine>engine;
  231. this.defines = (defines == null ? "" : defines);
  232. this._uniformsNames = (<string[]>uniformsNamesOrEngine).concat(<string[]>samplers);
  233. this._samplerList = samplers ? <string[]>samplers.slice() : [];
  234. this._attributesNames = (<string[]>attributesNamesOrOptions);
  235. this._uniformBuffersNamesList = [];
  236. this.onError = onError;
  237. this.onCompiled = onCompiled;
  238. this._indexParameters = indexParameters;
  239. this._fallbacks = fallbacks;
  240. }
  241. this._attributeLocationByName = { };
  242. this.uniqueId = Effect._uniqueIdSeed++;
  243. if (sources) {
  244. this._fragmentSourceCode = sources.fragment;
  245. this._vertexSourceCode = sources.vertex;
  246. this._rawFragmentSourceCode = sources.rawFragment;
  247. this._rawVertexSourceCode = sources.rawVertex;
  248. this._prepareEffect();
  249. return;
  250. }
  251. var vertexSource: any;
  252. var fragmentSource: any;
  253. let hostDocument = DomManagement.IsWindowObjectExist() ? this._engine.getHostDocument() : null;
  254. if (baseName.vertexSource) {
  255. vertexSource = "source:" + baseName.vertexSource;
  256. } else if (baseName.vertexElement) {
  257. vertexSource = hostDocument ? hostDocument.getElementById(baseName.vertexElement) : null;
  258. if (!vertexSource) {
  259. vertexSource = baseName.vertexElement;
  260. }
  261. } else {
  262. vertexSource = baseName.vertex || baseName;
  263. }
  264. if (baseName.fragmentSource) {
  265. fragmentSource = "source:" + baseName.fragmentSource;
  266. } else if (baseName.fragmentElement) {
  267. fragmentSource = hostDocument ? hostDocument.getElementById(baseName.fragmentElement) : null;
  268. if (!fragmentSource) {
  269. fragmentSource = baseName.fragmentElement;
  270. }
  271. } else {
  272. fragmentSource = baseName.fragment || baseName;
  273. }
  274. this._processingContext = engine!._getShaderProcessingContext();
  275. const processorOptions: ProcessingOptions = {
  276. defines: this.defines.split("\n"),
  277. indexParameters: this._indexParameters,
  278. isFragment: false,
  279. shouldUseHighPrecisionShader: this._engine._shouldUseHighPrecisionShader,
  280. processor: this._engine._shaderProcessor,
  281. supportsUniformBuffers: this._engine.supportsUniformBuffers,
  282. shadersRepository: Effect.ShadersRepository,
  283. includesShadersStore: Effect.IncludesShadersStore,
  284. version: (this._engine.webGLVersion * 100).toString(),
  285. platformName: this._engine.shaderPlatformName,
  286. processingContext: this._processingContext
  287. };
  288. let shaderCodes : [string | undefined, string | undefined] = [undefined, undefined];
  289. let shadersLoaded = () => {
  290. if (shaderCodes[0] && shaderCodes[1]) {
  291. processorOptions.isFragment = true;
  292. let [migratedVertexCode, fragmentCode] = shaderCodes;
  293. ShaderProcessor.Process(fragmentCode, processorOptions, (migratedFragmentCode) => {
  294. if (processFinalCode) {
  295. migratedFragmentCode = processFinalCode("fragment", migratedFragmentCode);
  296. }
  297. const finalShaders = ShaderProcessor.Finalize(migratedVertexCode, migratedFragmentCode, processorOptions);
  298. this._useFinalCode(finalShaders.vertexCode, finalShaders.fragmentCode, baseName);
  299. });
  300. }
  301. };
  302. this._loadShader(vertexSource, "Vertex", "", (vertexCode) => {
  303. ShaderProcessor.Initialize(processorOptions);
  304. ShaderProcessor.Process(vertexCode, processorOptions, (migratedVertexCode) => {
  305. this._rawVertexSourceCode = vertexCode;
  306. if (processFinalCode) {
  307. migratedVertexCode = processFinalCode("vertex", migratedVertexCode);
  308. }
  309. shaderCodes[0] = migratedVertexCode;
  310. shadersLoaded();
  311. });
  312. });
  313. this._loadShader(fragmentSource, "Fragment", "Pixel", (fragmentCode) => {
  314. this._rawFragmentSourceCode = fragmentCode;
  315. shaderCodes[1] = fragmentCode;
  316. shadersLoaded();
  317. });
  318. }
  319. private _useFinalCode(migratedVertexCode: string, migratedFragmentCode: string, baseName: any) {
  320. if (baseName) {
  321. var vertex = baseName.vertexElement || baseName.vertex || baseName.spectorName || baseName;
  322. var fragment = baseName.fragmentElement || baseName.fragment || baseName.spectorName || baseName;
  323. this._vertexSourceCode = "#define SHADER_NAME vertex:" + vertex + "\n" + migratedVertexCode;
  324. this._fragmentSourceCode = "#define SHADER_NAME fragment:" + fragment + "\n" + migratedFragmentCode;
  325. } else {
  326. this._vertexSourceCode = migratedVertexCode;
  327. this._fragmentSourceCode = migratedFragmentCode;
  328. }
  329. this._prepareEffect();
  330. }
  331. /**
  332. * Unique key for this effect
  333. */
  334. public get key(): string {
  335. return this._key;
  336. }
  337. /**
  338. * If the effect has been compiled and prepared.
  339. * @returns if the effect is compiled and prepared.
  340. */
  341. public isReady(): boolean {
  342. try {
  343. return this._isReadyInternal();
  344. }
  345. catch {
  346. return false;
  347. }
  348. }
  349. private _isReadyInternal(): boolean {
  350. if (this._isReady) {
  351. return true;
  352. }
  353. if (this._pipelineContext) {
  354. return this._pipelineContext.isReady;
  355. }
  356. return false;
  357. }
  358. /**
  359. * The engine the effect was initialized with.
  360. * @returns the engine.
  361. */
  362. public getEngine(): Engine {
  363. return this._engine;
  364. }
  365. /**
  366. * The pipeline context for this effect
  367. * @returns the associated pipeline context
  368. */
  369. public getPipelineContext(): Nullable<IPipelineContext> {
  370. return this._pipelineContext;
  371. }
  372. /**
  373. * The set of names of attribute variables for the shader.
  374. * @returns An array of attribute names.
  375. */
  376. public getAttributesNames(): string[] {
  377. return this._attributesNames;
  378. }
  379. /**
  380. * Returns the attribute at the given index.
  381. * @param index The index of the attribute.
  382. * @returns The location of the attribute.
  383. */
  384. public getAttributeLocation(index: number): number {
  385. return this._attributes[index];
  386. }
  387. /**
  388. * Returns the attribute based on the name of the variable.
  389. * @param name of the attribute to look up.
  390. * @returns the attribute location.
  391. */
  392. public getAttributeLocationByName(name: string): number {
  393. return this._attributeLocationByName[name];
  394. }
  395. /**
  396. * The number of attributes.
  397. * @returns the numnber of attributes.
  398. */
  399. public getAttributesCount(): number {
  400. return this._attributes.length;
  401. }
  402. /**
  403. * Gets the index of a uniform variable.
  404. * @param uniformName of the uniform to look up.
  405. * @returns the index.
  406. */
  407. public getUniformIndex(uniformName: string): number {
  408. return this._uniformsNames.indexOf(uniformName);
  409. }
  410. /**
  411. * Returns the attribute based on the name of the variable.
  412. * @param uniformName of the uniform to look up.
  413. * @returns the location of the uniform.
  414. */
  415. public getUniform(uniformName: string): Nullable<WebGLUniformLocation> {
  416. return this._uniforms[uniformName];
  417. }
  418. /**
  419. * Returns an array of sampler variable names
  420. * @returns The array of sampler variable names.
  421. */
  422. public getSamplers(): string[] {
  423. return this._samplerList;
  424. }
  425. /**
  426. * Returns an array of uniform variable names
  427. * @returns The array of uniform variable names.
  428. */
  429. public getUniformNames(): string[] {
  430. return this._uniformsNames;
  431. }
  432. /**
  433. * Returns an array of uniform buffer variable names
  434. * @returns The array of uniform buffer variable names.
  435. */
  436. public getUniformBuffersNames(): string[] {
  437. return this._uniformBuffersNamesList;
  438. }
  439. /**
  440. * Returns the index parameters used to create the effect
  441. * @returns The index parameters object
  442. */
  443. public getIndexParameters(): any {
  444. return this._indexParameters;
  445. }
  446. /**
  447. * The error from the last compilation.
  448. * @returns the error string.
  449. */
  450. public getCompilationError(): string {
  451. return this._compilationError;
  452. }
  453. /**
  454. * Gets a boolean indicating that all fallbacks were used during compilation
  455. * @returns true if all fallbacks were used
  456. */
  457. public allFallbacksProcessed(): boolean {
  458. return this._allFallbacksProcessed;
  459. }
  460. /**
  461. * Adds a callback to the onCompiled observable and call the callback imediatly if already ready.
  462. * @param func The callback to be used.
  463. */
  464. public executeWhenCompiled(func: (effect: Effect) => void): void {
  465. if (this.isReady()) {
  466. func(this);
  467. return;
  468. }
  469. this.onCompileObservable.add((effect) => {
  470. func(effect);
  471. });
  472. if (!this._pipelineContext || this._pipelineContext.isAsync) {
  473. setTimeout(() => {
  474. this._checkIsReady(null);
  475. }, 16);
  476. }
  477. }
  478. private _checkIsReady(previousPipelineContext: Nullable<IPipelineContext>) {
  479. try {
  480. if (this._isReadyInternal()) {
  481. return;
  482. }
  483. } catch (e) {
  484. this._processCompilationErrors(e, previousPipelineContext);
  485. return;
  486. }
  487. setTimeout(() => {
  488. this._checkIsReady(previousPipelineContext);
  489. }, 16);
  490. }
  491. private _loadShader(shader: any, key: string, optionalKey: string, callback: (data: any) => void): void {
  492. if (typeof(HTMLElement) !== "undefined") {
  493. // DOM element ?
  494. if (shader instanceof HTMLElement) {
  495. var shaderCode = DomManagement.GetDOMTextContent(shader);
  496. callback(shaderCode);
  497. return;
  498. }
  499. }
  500. // Direct source ?
  501. if (shader.substr(0, 7) === "source:") {
  502. callback(shader.substr(7));
  503. return;
  504. }
  505. // Base64 encoded ?
  506. if (shader.substr(0, 7) === "base64:") {
  507. var shaderBinary = window.atob(shader.substr(7));
  508. callback(shaderBinary);
  509. return;
  510. }
  511. // Is in local store ?
  512. if (Effect.ShadersStore[shader + key + "Shader"]) {
  513. callback(Effect.ShadersStore[shader + key + "Shader"]);
  514. return;
  515. }
  516. if (optionalKey && Effect.ShadersStore[shader + optionalKey + "Shader"]) {
  517. callback(Effect.ShadersStore[shader + optionalKey + "Shader"]);
  518. return;
  519. }
  520. var shaderUrl;
  521. if (shader[0] === "." || shader[0] === "/" || shader.indexOf("http") > -1) {
  522. shaderUrl = shader;
  523. } else {
  524. shaderUrl = Effect.ShadersRepository + shader;
  525. }
  526. // Vertex shader
  527. this._engine._loadFile(shaderUrl + "." + key.toLowerCase() + ".fx", callback);
  528. }
  529. /**
  530. * Gets the vertex shader source code of this effect
  531. */
  532. public get vertexSourceCode(): string {
  533. return this._vertexSourceCodeOverride && this._fragmentSourceCodeOverride ? this._vertexSourceCodeOverride : (this._pipelineContext?._getVertexShaderCode() ?? this._vertexSourceCode);
  534. }
  535. /**
  536. * Gets the fragment shader source code of this effect
  537. */
  538. public get fragmentSourceCode(): string {
  539. return this._vertexSourceCodeOverride && this._fragmentSourceCodeOverride ? this._fragmentSourceCodeOverride : (this._pipelineContext?._getFragmentShaderCode() ?? this._fragmentSourceCode);
  540. }
  541. /**
  542. * Gets the vertex shader source code before it has been processed by the preprocessor
  543. */
  544. public get rawVertexSourceCode(): string {
  545. return this._rawVertexSourceCode;
  546. }
  547. /**
  548. * Gets the fragment shader source code before it has been processed by the preprocessor
  549. */
  550. public get rawFragmentSourceCode(): string {
  551. return this._rawFragmentSourceCode;
  552. }
  553. /**
  554. * Recompiles the webGL program
  555. * @param vertexSourceCode The source code for the vertex shader.
  556. * @param fragmentSourceCode The source code for the fragment shader.
  557. * @param onCompiled Callback called when completed.
  558. * @param onError Callback called on error.
  559. * @hidden
  560. */
  561. public _rebuildProgram(vertexSourceCode: string, fragmentSourceCode: string, onCompiled: (pipelineContext: IPipelineContext) => void, onError: (message: string) => void) {
  562. this._isReady = false;
  563. this._vertexSourceCodeOverride = vertexSourceCode;
  564. this._fragmentSourceCodeOverride = fragmentSourceCode;
  565. this.onError = (effect, error) => {
  566. if (onError) {
  567. onError(error);
  568. }
  569. };
  570. this.onCompiled = () => {
  571. var scenes = this.getEngine().scenes;
  572. if (scenes) {
  573. for (var i = 0; i < scenes.length; i++) {
  574. scenes[i].markAllMaterialsAsDirty(Constants.MATERIAL_AllDirtyFlag);
  575. }
  576. }
  577. this._pipelineContext!._handlesSpectorRebuildCallback(onCompiled);
  578. };
  579. this._fallbacks = null;
  580. this._prepareEffect();
  581. }
  582. /**
  583. * Prepares the effect
  584. * @hidden
  585. */
  586. public _prepareEffect() {
  587. let attributesNames = this._attributesNames;
  588. let defines = this.defines;
  589. var previousPipelineContext = this._pipelineContext;
  590. try {
  591. let engine = this._engine;
  592. this._pipelineContext = engine.createPipelineContext(this._processingContext);
  593. let rebuildRebind = this._rebuildProgram.bind(this);
  594. if (this._vertexSourceCodeOverride && this._fragmentSourceCodeOverride) {
  595. engine._preparePipelineContext(this._pipelineContext, this._vertexSourceCodeOverride, this._fragmentSourceCodeOverride, true, this._rawVertexSourceCode, this._rawFragmentSourceCode, rebuildRebind, null, this._transformFeedbackVaryings, this._key);
  596. }
  597. else {
  598. engine._preparePipelineContext(this._pipelineContext, this._vertexSourceCode, this._fragmentSourceCode, false, this._rawVertexSourceCode, this._rawFragmentSourceCode, rebuildRebind, defines, this._transformFeedbackVaryings, this._key);
  599. }
  600. engine._executeWhenRenderingStateIsCompiled(this._pipelineContext, () => {
  601. this._attributes = [];
  602. this._pipelineContext!._fillEffectInformation(this,
  603. this._uniformBuffersNames,
  604. this._uniformsNames,
  605. this._uniforms,
  606. this._samplerList,
  607. this._samplers,
  608. attributesNames,
  609. this._attributes);
  610. // Caches attribute locations.
  611. if (attributesNames) {
  612. for (let i = 0; i < attributesNames.length; i++) {
  613. const name = attributesNames[i];
  614. this._attributeLocationByName[name] = this._attributes[i];
  615. }
  616. }
  617. engine.bindSamplers(this);
  618. this._compilationError = "";
  619. this._isReady = true;
  620. if (this.onCompiled) {
  621. this.onCompiled(this);
  622. }
  623. this.onCompileObservable.notifyObservers(this);
  624. this.onCompileObservable.clear();
  625. // Unbind mesh reference in fallbacks
  626. if (this._fallbacks) {
  627. this._fallbacks.unBindMesh();
  628. }
  629. if (previousPipelineContext) {
  630. this.getEngine()._deletePipelineContext(previousPipelineContext);
  631. }
  632. });
  633. if (this._pipelineContext.isAsync) {
  634. this._checkIsReady(previousPipelineContext);
  635. }
  636. } catch (e) {
  637. this._processCompilationErrors(e, previousPipelineContext);
  638. }
  639. }
  640. private _getShaderCodeAndErrorLine(code: Nullable<string>, error: Nullable<string>, isFragment: boolean): [Nullable<string>, Nullable<string>] {
  641. const regexp = isFragment ? /FRAGMENT SHADER ERROR: 0:(\d+?):/ : /VERTEX SHADER ERROR: 0:(\d+?):/;
  642. let errorLine = null;
  643. if (error && code) {
  644. const res = error.match(regexp);
  645. if (res && res.length === 2) {
  646. const lineNumber = parseInt(res[1]);
  647. const lines = code.split("\n", -1);
  648. if (lines.length >= lineNumber) {
  649. errorLine = `Offending line [${lineNumber}] in ${isFragment ? "fragment" : "vertex"} code: ${lines[lineNumber - 1]}`;
  650. }
  651. }
  652. }
  653. return [code, errorLine];
  654. }
  655. private _processCompilationErrors(e: any, previousPipelineContext: Nullable<IPipelineContext> = null) {
  656. this._compilationError = e.message;
  657. let attributesNames = this._attributesNames;
  658. let fallbacks = this._fallbacks;
  659. // Let's go through fallbacks then
  660. Logger.Error("Unable to compile effect:");
  661. Logger.Error("Uniforms: " + this._uniformsNames.map(function(uniform) {
  662. return " " + uniform;
  663. }));
  664. Logger.Error("Attributes: " + attributesNames.map(function(attribute) {
  665. return " " + attribute;
  666. }));
  667. Logger.Error("Defines:\r\n" + this.defines);
  668. if (Effect.LogShaderCodeOnCompilationError) {
  669. let lineErrorVertex = null, lineErrorFragment = null, code = null;
  670. if (this._pipelineContext?._getVertexShaderCode()) {
  671. [code, lineErrorVertex] = this._getShaderCodeAndErrorLine(this._pipelineContext._getVertexShaderCode(), this._compilationError, false);
  672. if (code) {
  673. Logger.Error("Vertex code:");
  674. Logger.Error(code);
  675. }
  676. }
  677. if (this._pipelineContext?._getFragmentShaderCode()) {
  678. [code, lineErrorFragment] = this._getShaderCodeAndErrorLine(this._pipelineContext?._getFragmentShaderCode(), this._compilationError, true);
  679. if (code) {
  680. Logger.Error("Fragment code:");
  681. Logger.Error(code);
  682. }
  683. }
  684. if (lineErrorVertex) {
  685. Logger.Error(lineErrorVertex);
  686. }
  687. if (lineErrorFragment) {
  688. Logger.Error(lineErrorFragment);
  689. }
  690. }
  691. Logger.Error("Error: " + this._compilationError);
  692. if (previousPipelineContext) {
  693. this._pipelineContext = previousPipelineContext;
  694. this._isReady = true;
  695. if (this.onError) {
  696. this.onError(this, this._compilationError);
  697. }
  698. this.onErrorObservable.notifyObservers(this);
  699. }
  700. if (fallbacks) {
  701. this._pipelineContext = null;
  702. if (fallbacks.hasMoreFallbacks) {
  703. this._allFallbacksProcessed = false;
  704. Logger.Error("Trying next fallback.");
  705. this.defines = fallbacks.reduce(this.defines, this);
  706. this._prepareEffect();
  707. } else { // Sorry we did everything we can
  708. this._allFallbacksProcessed = true;
  709. if (this.onError) {
  710. this.onError(this, this._compilationError);
  711. }
  712. this.onErrorObservable.notifyObservers(this);
  713. this.onErrorObservable.clear();
  714. // Unbind mesh reference in fallbacks
  715. if (this._fallbacks) {
  716. this._fallbacks.unBindMesh();
  717. }
  718. }
  719. } else {
  720. this._allFallbacksProcessed = true;
  721. }
  722. }
  723. /**
  724. * Checks if the effect is supported. (Must be called after compilation)
  725. */
  726. public get isSupported(): boolean {
  727. return this._compilationError === "";
  728. }
  729. /**
  730. * Binds a texture to the engine to be used as output of the shader.
  731. * @param channel Name of the output variable.
  732. * @param texture Texture to bind.
  733. * @hidden
  734. */
  735. public _bindTexture(channel: string, texture: Nullable<InternalTexture>): void {
  736. this._engine._bindTexture(this._samplers[channel], texture, channel);
  737. }
  738. /**
  739. * Sets a texture on the engine to be used in the shader.
  740. * @param channel Name of the sampler variable.
  741. * @param texture Texture to set.
  742. */
  743. public setTexture(channel: string, texture: Nullable<BaseTexture>): void {
  744. this._engine.setTexture(this._samplers[channel], this._uniforms[channel], texture, channel);
  745. }
  746. /**
  747. * Sets a depth stencil texture from a render target on the engine to be used in the shader.
  748. * @param channel Name of the sampler variable.
  749. * @param texture Texture to set.
  750. */
  751. public setDepthStencilTexture(channel: string, texture: Nullable<RenderTargetTexture>): void {
  752. this._engine.setDepthStencilTexture(this._samplers[channel], this._uniforms[channel], texture, channel);
  753. }
  754. /**
  755. * Sets an array of textures on the engine to be used in the shader.
  756. * @param channel Name of the variable.
  757. * @param textures Textures to set.
  758. */
  759. public setTextureArray(channel: string, textures: BaseTexture[]): void {
  760. let exName = channel + "Ex";
  761. if (this._samplerList.indexOf(exName + "0") === -1) {
  762. const initialPos = this._samplerList.indexOf(channel);
  763. for (var index = 1; index < textures.length; index++) {
  764. const currentExName = exName + (index - 1).toString();
  765. this._samplerList.splice(initialPos + index, 0, currentExName);
  766. }
  767. // Reset every channels
  768. let channelIndex = 0;
  769. for (var key of this._samplerList) {
  770. this._samplers[key] = channelIndex;
  771. channelIndex += 1;
  772. }
  773. }
  774. this._engine.setTextureArray(this._samplers[channel], this._uniforms[channel], textures);
  775. }
  776. /**
  777. * Sets a texture to be the input of the specified post process. (To use the output, pass in the next post process in the pipeline)
  778. * @param channel Name of the sampler variable.
  779. * @param postProcess Post process to get the input texture from.
  780. */
  781. public setTextureFromPostProcess(channel: string, postProcess: Nullable<PostProcess>): void {
  782. this._engine.setTextureFromPostProcess(this._samplers[channel], postProcess, channel);
  783. }
  784. /**
  785. * (Warning! setTextureFromPostProcessOutput may be desired instead)
  786. * Sets the input texture of the passed in post process to be input of this effect. (To use the output of the passed in post process use setTextureFromPostProcessOutput)
  787. * @param channel Name of the sampler variable.
  788. * @param postProcess Post process to get the output texture from.
  789. */
  790. public setTextureFromPostProcessOutput(channel: string, postProcess: Nullable<PostProcess>): void {
  791. this._engine.setTextureFromPostProcessOutput(this._samplers[channel], postProcess, channel);
  792. }
  793. /**
  794. * Binds a buffer to a uniform.
  795. * @param buffer Buffer to bind.
  796. * @param name Name of the uniform variable to bind to.
  797. */
  798. public bindUniformBuffer(buffer: DataBuffer, name: string): void {
  799. let bufferName = this._uniformBuffersNames[name];
  800. if (bufferName === undefined || Effect._baseCache[bufferName] === buffer) {
  801. return;
  802. }
  803. Effect._baseCache[bufferName] = buffer;
  804. this._engine.bindUniformBufferBase(buffer, bufferName, name);
  805. }
  806. /**
  807. * Binds block to a uniform.
  808. * @param blockName Name of the block to bind.
  809. * @param index Index to bind.
  810. */
  811. public bindUniformBlock(blockName: string, index: number): void {
  812. this._engine.bindUniformBlock(this._pipelineContext!, blockName, index);
  813. }
  814. /**
  815. * Sets an interger value on a uniform variable.
  816. * @param uniformName Name of the variable.
  817. * @param value Value to be set.
  818. * @returns this effect.
  819. */
  820. public setInt(uniformName: string, value: number): Effect {
  821. this._pipelineContext!.setInt(uniformName, value);
  822. return this;
  823. }
  824. /**
  825. * Sets an int array on a uniform variable.
  826. * @param uniformName Name of the variable.
  827. * @param array array to be set.
  828. * @returns this effect.
  829. */
  830. public setIntArray(uniformName: string, array: Int32Array): Effect {
  831. this._pipelineContext!.setIntArray(uniformName, array);
  832. return this;
  833. }
  834. /**
  835. * Sets an int array 2 on a uniform variable. (Array is specified as single array eg. [1,2,3,4] will result in [[1,2],[3,4]] in the shader)
  836. * @param uniformName Name of the variable.
  837. * @param array array to be set.
  838. * @returns this effect.
  839. */
  840. public setIntArray2(uniformName: string, array: Int32Array): Effect {
  841. this._pipelineContext!.setIntArray2(uniformName, array);
  842. return this;
  843. }
  844. /**
  845. * Sets an int array 3 on a uniform variable. (Array is specified as single array eg. [1,2,3,4,5,6] will result in [[1,2,3],[4,5,6]] in the shader)
  846. * @param uniformName Name of the variable.
  847. * @param array array to be set.
  848. * @returns this effect.
  849. */
  850. public setIntArray3(uniformName: string, array: Int32Array): Effect {
  851. this._pipelineContext!.setIntArray3(uniformName, array);
  852. return this;
  853. }
  854. /**
  855. * Sets an int array 4 on a uniform variable. (Array is specified as single array eg. [1,2,3,4,5,6,7,8] will result in [[1,2,3,4],[5,6,7,8]] in the shader)
  856. * @param uniformName Name of the variable.
  857. * @param array array to be set.
  858. * @returns this effect.
  859. */
  860. public setIntArray4(uniformName: string, array: Int32Array): Effect {
  861. this._pipelineContext!.setIntArray4(uniformName, array);
  862. return this;
  863. }
  864. /**
  865. * Sets an float array on a uniform variable.
  866. * @param uniformName Name of the variable.
  867. * @param array array to be set.
  868. * @returns this effect.
  869. */
  870. public setFloatArray(uniformName: string, array: FloatArray): Effect {
  871. this._pipelineContext!.setArray(uniformName, array);
  872. return this;
  873. }
  874. /**
  875. * Sets an float array 2 on a uniform variable. (Array is specified as single array eg. [1,2,3,4] will result in [[1,2],[3,4]] in the shader)
  876. * @param uniformName Name of the variable.
  877. * @param array array to be set.
  878. * @returns this effect.
  879. */
  880. public setFloatArray2(uniformName: string, array: FloatArray): Effect {
  881. this._pipelineContext!.setArray2(uniformName, array);
  882. return this;
  883. }
  884. /**
  885. * Sets an float array 3 on a uniform variable. (Array is specified as single array eg. [1,2,3,4,5,6] will result in [[1,2,3],[4,5,6]] in the shader)
  886. * @param uniformName Name of the variable.
  887. * @param array array to be set.
  888. * @returns this effect.
  889. */
  890. public setFloatArray3(uniformName: string, array: FloatArray): Effect {
  891. this._pipelineContext!.setArray3(uniformName, array);
  892. return this;
  893. }
  894. /**
  895. * Sets an float array 4 on a uniform variable. (Array is specified as single array eg. [1,2,3,4,5,6,7,8] will result in [[1,2,3,4],[5,6,7,8]] in the shader)
  896. * @param uniformName Name of the variable.
  897. * @param array array to be set.
  898. * @returns this effect.
  899. */
  900. public setFloatArray4(uniformName: string, array: FloatArray): Effect {
  901. this._pipelineContext!.setArray4(uniformName, array);
  902. return this;
  903. }
  904. /**
  905. * Sets an array on a uniform variable.
  906. * @param uniformName Name of the variable.
  907. * @param array array to be set.
  908. * @returns this effect.
  909. */
  910. public setArray(uniformName: string, array: number[]): Effect {
  911. this._pipelineContext!.setArray(uniformName, array);
  912. return this;
  913. }
  914. /**
  915. * Sets an array 2 on a uniform variable. (Array is specified as single array eg. [1,2,3,4] will result in [[1,2],[3,4]] in the shader)
  916. * @param uniformName Name of the variable.
  917. * @param array array to be set.
  918. * @returns this effect.
  919. */
  920. public setArray2(uniformName: string, array: number[]): Effect {
  921. this._pipelineContext!.setArray2(uniformName, array);
  922. return this;
  923. }
  924. /**
  925. * Sets an array 3 on a uniform variable. (Array is specified as single array eg. [1,2,3,4,5,6] will result in [[1,2,3],[4,5,6]] in the shader)
  926. * @param uniformName Name of the variable.
  927. * @param array array to be set.
  928. * @returns this effect.
  929. */
  930. public setArray3(uniformName: string, array: number[]): Effect {
  931. this._pipelineContext!.setArray3(uniformName, array);
  932. return this;
  933. }
  934. /**
  935. * Sets an array 4 on a uniform variable. (Array is specified as single array eg. [1,2,3,4,5,6,7,8] will result in [[1,2,3,4],[5,6,7,8]] in the shader)
  936. * @param uniformName Name of the variable.
  937. * @param array array to be set.
  938. * @returns this effect.
  939. */
  940. public setArray4(uniformName: string, array: number[]): Effect {
  941. this._pipelineContext!.setArray4(uniformName, array);
  942. return this;
  943. }
  944. /**
  945. * Sets matrices on a uniform variable.
  946. * @param uniformName Name of the variable.
  947. * @param matrices matrices to be set.
  948. * @returns this effect.
  949. */
  950. public setMatrices(uniformName: string, matrices: Float32Array | Array<number>): Effect {
  951. this._pipelineContext!.setMatrices(uniformName, matrices as Float32Array);
  952. return this;
  953. }
  954. /**
  955. * Sets matrix on a uniform variable.
  956. * @param uniformName Name of the variable.
  957. * @param matrix matrix to be set.
  958. * @returns this effect.
  959. */
  960. public setMatrix(uniformName: string, matrix: IMatrixLike): Effect {
  961. this._pipelineContext!.setMatrix(uniformName, matrix);
  962. return this;
  963. }
  964. /**
  965. * Sets a 3x3 matrix on a uniform variable. (Speicified as [1,2,3,4,5,6,7,8,9] will result in [1,2,3][4,5,6][7,8,9] matrix)
  966. * @param uniformName Name of the variable.
  967. * @param matrix matrix to be set.
  968. * @returns this effect.
  969. */
  970. public setMatrix3x3(uniformName: string, matrix: Float32Array | Array<number>): Effect {
  971. // the cast is ok because it is gl.uniformMatrix3fv() which is called at the end, and this function accepts Float32Array and Array<number>
  972. this._pipelineContext!.setMatrix3x3(uniformName, matrix as Float32Array);
  973. return this;
  974. }
  975. /**
  976. * Sets a 2x2 matrix on a uniform variable. (Speicified as [1,2,3,4] will result in [1,2][3,4] matrix)
  977. * @param uniformName Name of the variable.
  978. * @param matrix matrix to be set.
  979. * @returns this effect.
  980. */
  981. public setMatrix2x2(uniformName: string, matrix: Float32Array | Array<number>): Effect {
  982. // the cast is ok because it is gl.uniformMatrix3fv() which is called at the end, and this function accepts Float32Array and Array<number>
  983. this._pipelineContext!.setMatrix2x2(uniformName, matrix as Float32Array);
  984. return this;
  985. }
  986. /**
  987. * Sets a float on a uniform variable.
  988. * @param uniformName Name of the variable.
  989. * @param value value to be set.
  990. * @returns this effect.
  991. */
  992. public setFloat(uniformName: string, value: number): Effect {
  993. this._pipelineContext!.setFloat(uniformName, value);
  994. return this;
  995. }
  996. /**
  997. * Sets a boolean on a uniform variable.
  998. * @param uniformName Name of the variable.
  999. * @param bool value to be set.
  1000. * @returns this effect.
  1001. */
  1002. public setBool(uniformName: string, bool: boolean): Effect {
  1003. this._pipelineContext!.setInt(uniformName, bool ? 1 : 0);
  1004. return this;
  1005. }
  1006. /**
  1007. * Sets a Vector2 on a uniform variable.
  1008. * @param uniformName Name of the variable.
  1009. * @param vector2 vector2 to be set.
  1010. * @returns this effect.
  1011. */
  1012. public setVector2(uniformName: string, vector2: IVector2Like): Effect {
  1013. this._pipelineContext!.setVector2(uniformName, vector2);
  1014. return this;
  1015. }
  1016. /**
  1017. * Sets a float2 on a uniform variable.
  1018. * @param uniformName Name of the variable.
  1019. * @param x First float in float2.
  1020. * @param y Second float in float2.
  1021. * @returns this effect.
  1022. */
  1023. public setFloat2(uniformName: string, x: number, y: number): Effect {
  1024. this._pipelineContext!.setFloat2(uniformName, x, y);
  1025. return this;
  1026. }
  1027. /**
  1028. * Sets a Vector3 on a uniform variable.
  1029. * @param uniformName Name of the variable.
  1030. * @param vector3 Value to be set.
  1031. * @returns this effect.
  1032. */
  1033. public setVector3(uniformName: string, vector3: IVector3Like): Effect {
  1034. this._pipelineContext!.setVector3(uniformName, vector3);
  1035. return this;
  1036. }
  1037. /**
  1038. * Sets a float3 on a uniform variable.
  1039. * @param uniformName Name of the variable.
  1040. * @param x First float in float3.
  1041. * @param y Second float in float3.
  1042. * @param z Third float in float3.
  1043. * @returns this effect.
  1044. */
  1045. public setFloat3(uniformName: string, x: number, y: number, z: number): Effect {
  1046. this._pipelineContext!.setFloat3(uniformName, x, y, z);
  1047. return this;
  1048. }
  1049. /**
  1050. * Sets a Vector4 on a uniform variable.
  1051. * @param uniformName Name of the variable.
  1052. * @param vector4 Value to be set.
  1053. * @returns this effect.
  1054. */
  1055. public setVector4(uniformName: string, vector4: IVector4Like): Effect {
  1056. this._pipelineContext!.setVector4(uniformName, vector4);
  1057. return this;
  1058. }
  1059. /**
  1060. * Sets a float4 on a uniform variable.
  1061. * @param uniformName Name of the variable.
  1062. * @param x First float in float4.
  1063. * @param y Second float in float4.
  1064. * @param z Third float in float4.
  1065. * @param w Fourth float in float4.
  1066. * @returns this effect.
  1067. */
  1068. public setFloat4(uniformName: string, x: number, y: number, z: number, w: number): Effect {
  1069. this._pipelineContext!.setFloat4(uniformName, x, y, z, w);
  1070. return this;
  1071. }
  1072. /**
  1073. * Sets a Color3 on a uniform variable.
  1074. * @param uniformName Name of the variable.
  1075. * @param color3 Value to be set.
  1076. * @returns this effect.
  1077. */
  1078. public setColor3(uniformName: string, color3: IColor3Like): Effect {
  1079. this._pipelineContext!.setColor3(uniformName, color3);
  1080. return this;
  1081. }
  1082. /**
  1083. * Sets a Color4 on a uniform variable.
  1084. * @param uniformName Name of the variable.
  1085. * @param color3 Value to be set.
  1086. * @param alpha Alpha value to be set.
  1087. * @returns this effect.
  1088. */
  1089. public setColor4(uniformName: string, color3: IColor3Like, alpha: number): Effect {
  1090. this._pipelineContext!.setColor4(uniformName, color3, alpha);
  1091. return this;
  1092. }
  1093. /**
  1094. * Sets a Color4 on a uniform variable
  1095. * @param uniformName defines the name of the variable
  1096. * @param color4 defines the value to be set
  1097. * @returns this effect.
  1098. */
  1099. public setDirectColor4(uniformName: string, color4: IColor4Like): Effect {
  1100. this._pipelineContext!.setDirectColor4(uniformName, color4);
  1101. return this;
  1102. }
  1103. /**
  1104. * Release all associated resources.
  1105. **/
  1106. public dispose() {
  1107. if (this._pipelineContext) {
  1108. this._pipelineContext.dispose();
  1109. }
  1110. this._engine._releaseEffect(this);
  1111. }
  1112. /**
  1113. * This function will add a new shader to the shader store
  1114. * @param name the name of the shader
  1115. * @param pixelShader optional pixel shader content
  1116. * @param vertexShader optional vertex shader content
  1117. */
  1118. public static RegisterShader(name: string, pixelShader?: string, vertexShader?: string) {
  1119. if (pixelShader) {
  1120. Effect.ShadersStore[`${name}PixelShader`] = pixelShader;
  1121. }
  1122. if (vertexShader) {
  1123. Effect.ShadersStore[`${name}VertexShader`] = vertexShader;
  1124. }
  1125. }
  1126. /**
  1127. * Store of each shader (The can be looked up using effect.key)
  1128. */
  1129. public static ShadersStore: { [key: string]: string } = {};
  1130. /**
  1131. * Store of each included file for a shader (The can be looked up using effect.key)
  1132. */
  1133. public static IncludesShadersStore: { [key: string]: string } = {};
  1134. /**
  1135. * Resets the cache of effects.
  1136. */
  1137. public static ResetCache() {
  1138. Effect._baseCache = {};
  1139. }
  1140. }