effect.ts 48 KB

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