effect.ts 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524
  1. import { Observable } from "../Misc/observable";
  2. import { Nullable } from "../types";
  3. import { Matrix, Vector3, Vector2, Color3, Color4, Vector4 } from "../Maths/math";
  4. import { Constants } from "../Engines/constants";
  5. import { DomManagement } from "../Misc/domManagement";
  6. import { Logger } from "../Misc/logger";
  7. declare type Engine = import("../Engines/engine").Engine;
  8. declare type InternalTexture = import("../Materials/Textures/internalTexture").InternalTexture;
  9. declare type BaseTexture = import("../Materials/Textures/baseTexture").BaseTexture;
  10. declare type RenderTargetTexture = import("../Materials/Textures/renderTargetTexture").RenderTargetTexture;
  11. declare type PostProcess = import("../PostProcesses/postProcess").PostProcess;
  12. declare type AbstractMesh = import("../Meshes/abstractMesh").AbstractMesh;
  13. /**
  14. * EffectFallbacks can be used to add fallbacks (properties to disable) to certain properties when desired to improve performance.
  15. * (Eg. Start at high quality with reflection and fog, if fps is low, remove reflection, if still low remove fog)
  16. */
  17. export class EffectFallbacks {
  18. private _defines: { [key: string]: Array<String> } = {};
  19. private _currentRank = 32;
  20. private _maxRank = -1;
  21. private _mesh: Nullable<AbstractMesh>;
  22. /**
  23. * Removes the fallback from the bound mesh.
  24. */
  25. public unBindMesh() {
  26. this._mesh = null;
  27. }
  28. /**
  29. * Adds a fallback on the specified property.
  30. * @param rank The rank of the fallback (Lower ranks will be fallbacked to first)
  31. * @param define The name of the define in the shader
  32. */
  33. public addFallback(rank: number, define: string): void {
  34. if (!this._defines[rank]) {
  35. if (rank < this._currentRank) {
  36. this._currentRank = rank;
  37. }
  38. if (rank > this._maxRank) {
  39. this._maxRank = rank;
  40. }
  41. this._defines[rank] = new Array<String>();
  42. }
  43. this._defines[rank].push(define);
  44. }
  45. /**
  46. * Sets the mesh to use CPU skinning when needing to fallback.
  47. * @param rank The rank of the fallback (Lower ranks will be fallbacked to first)
  48. * @param mesh The mesh to use the fallbacks.
  49. */
  50. public addCPUSkinningFallback(rank: number, mesh: AbstractMesh) {
  51. this._mesh = mesh;
  52. if (rank < this._currentRank) {
  53. this._currentRank = rank;
  54. }
  55. if (rank > this._maxRank) {
  56. this._maxRank = rank;
  57. }
  58. }
  59. /**
  60. * Checks to see if more fallbacks are still availible.
  61. */
  62. public get isMoreFallbacks(): boolean {
  63. return this._currentRank <= this._maxRank;
  64. }
  65. /**
  66. * Removes the defines that shoould be removed when falling back.
  67. * @param currentDefines defines the current define statements for the shader.
  68. * @param effect defines the current effect we try to compile
  69. * @returns The resulting defines with defines of the current rank removed.
  70. */
  71. public reduce(currentDefines: string, effect: Effect): string {
  72. // First we try to switch to CPU skinning
  73. if (this._mesh && this._mesh.computeBonesUsingShaders && this._mesh.numBoneInfluencers > 0 && this._mesh.material) {
  74. this._mesh.computeBonesUsingShaders = false;
  75. currentDefines = currentDefines.replace("#define NUM_BONE_INFLUENCERS " + this._mesh.numBoneInfluencers, "#define NUM_BONE_INFLUENCERS 0");
  76. effect._bonesComputationForcedToCPU = true;
  77. var scene = this._mesh.getScene();
  78. for (var index = 0; index < scene.meshes.length; index++) {
  79. var otherMesh = scene.meshes[index];
  80. if (!otherMesh.material) {
  81. continue;
  82. }
  83. if (!otherMesh.computeBonesUsingShaders || otherMesh.numBoneInfluencers === 0) {
  84. continue;
  85. }
  86. if (otherMesh.material.getEffect() === effect) {
  87. otherMesh.computeBonesUsingShaders = false;
  88. } else if (otherMesh.subMeshes) {
  89. for (var subMesh of otherMesh.subMeshes) {
  90. let subMeshEffect = subMesh.effect;
  91. if (subMeshEffect === effect) {
  92. otherMesh.computeBonesUsingShaders = false;
  93. break;
  94. }
  95. }
  96. }
  97. }
  98. }
  99. else {
  100. var currentFallbacks = this._defines[this._currentRank];
  101. if (currentFallbacks) {
  102. for (var index = 0; index < currentFallbacks.length; index++) {
  103. currentDefines = currentDefines.replace("#define " + currentFallbacks[index], "");
  104. }
  105. }
  106. this._currentRank++;
  107. }
  108. return currentDefines;
  109. }
  110. }
  111. /**
  112. * Options to be used when creating an effect.
  113. */
  114. export class EffectCreationOptions {
  115. /**
  116. * Atrributes that will be used in the shader.
  117. */
  118. public attributes: string[];
  119. /**
  120. * Uniform varible names that will be set in the shader.
  121. */
  122. public uniformsNames: string[];
  123. /**
  124. * Uniform buffer varible names that will be set in the shader.
  125. */
  126. public uniformBuffersNames: string[];
  127. /**
  128. * Sampler texture variable names that will be set in the shader.
  129. */
  130. public samplers: string[];
  131. /**
  132. * Define statements that will be set in the shader.
  133. */
  134. public defines: any;
  135. /**
  136. * Possible fallbacks for this effect to improve performance when needed.
  137. */
  138. public fallbacks: Nullable<EffectFallbacks>;
  139. /**
  140. * Callback that will be called when the shader is compiled.
  141. */
  142. public onCompiled: Nullable<(effect: Effect) => void>;
  143. /**
  144. * Callback that will be called if an error occurs during shader compilation.
  145. */
  146. public onError: Nullable<(effect: Effect, errors: string) => void>;
  147. /**
  148. * Parameters to be used with Babylons include syntax to iterate over an array (eg. {lights: 10})
  149. */
  150. public indexParameters: any;
  151. /**
  152. * Max number of lights that can be used in the shader.
  153. */
  154. public maxSimultaneousLights: number;
  155. /**
  156. * See https://developer.mozilla.org/en-US/docs/Web/API/WebGL2RenderingContext/transformFeedbackVaryings
  157. */
  158. public transformFeedbackVaryings: Nullable<string[]>;
  159. }
  160. /**
  161. * Effect containing vertex and fragment shader that can be executed on an object.
  162. */
  163. export class Effect {
  164. /**
  165. * Gets or sets the relative url used to load shaders if using the engine in non-minified mode
  166. */
  167. public static ShadersRepository = "src/Shaders/";
  168. /**
  169. * Name of the effect.
  170. */
  171. public name: any;
  172. /**
  173. * String container all the define statements that should be set on the shader.
  174. */
  175. public defines: string;
  176. /**
  177. * Callback that will be called when the shader is compiled.
  178. */
  179. public onCompiled: Nullable<(effect: Effect) => void>;
  180. /**
  181. * Callback that will be called if an error occurs during shader compilation.
  182. */
  183. public onError: Nullable<(effect: Effect, errors: string) => void>;
  184. /**
  185. * Callback that will be called when effect is bound.
  186. */
  187. public onBind: Nullable<(effect: Effect) => void>;
  188. /**
  189. * Unique ID of the effect.
  190. */
  191. public uniqueId = 0;
  192. /**
  193. * Observable that will be called when the shader is compiled.
  194. * It is recommended to use executeWhenCompile() or to make sure that scene.isReady() is called to get this observable raised.
  195. */
  196. public onCompileObservable = new Observable<Effect>();
  197. /**
  198. * Observable that will be called if an error occurs during shader compilation.
  199. */
  200. public onErrorObservable = new Observable<Effect>();
  201. /** @hidden */
  202. public _onBindObservable: Nullable<Observable<Effect>>;
  203. /**
  204. * Observable that will be called when effect is bound.
  205. */
  206. public get onBindObservable(): Observable<Effect> {
  207. if (!this._onBindObservable) {
  208. this._onBindObservable = new Observable<Effect>();
  209. }
  210. return this._onBindObservable;
  211. }
  212. /** @hidden */
  213. public _bonesComputationForcedToCPU = false;
  214. private static _uniqueIdSeed = 0;
  215. private _engine: Engine;
  216. private _uniformBuffersNames: { [key: string]: number } = {};
  217. private _uniformsNames: string[];
  218. private _samplers: string[];
  219. private _isReady = false;
  220. private _compilationError = "";
  221. private _attributesNames: string[];
  222. private _attributes: number[];
  223. private _uniforms: Nullable<WebGLUniformLocation>[];
  224. /**
  225. * Key for the effect.
  226. * @hidden
  227. */
  228. public _key: string;
  229. private _indexParameters: any;
  230. private _fallbacks: Nullable<EffectFallbacks>;
  231. private _vertexSourceCode: string;
  232. private _fragmentSourceCode: string;
  233. private _vertexSourceCodeOverride: string;
  234. private _fragmentSourceCodeOverride: string;
  235. private _transformFeedbackVaryings: Nullable<string[]>;
  236. /**
  237. * Compiled shader to webGL program.
  238. * @hidden
  239. */
  240. public _program: WebGLProgram;
  241. private _valueCache: { [key: string]: any };
  242. private static _baseCache: { [key: number]: WebGLBuffer } = {};
  243. /**
  244. * Instantiates an effect.
  245. * An effect can be used to create/manage/execute vertex and fragment shaders.
  246. * @param baseName Name of the effect.
  247. * @param attributesNamesOrOptions List of attribute names that will be passed to the shader or set of all options to create the effect.
  248. * @param uniformsNamesOrEngine List of uniform variable names that will be passed to the shader or the engine that will be used to render effect.
  249. * @param samplers List of sampler variables that will be passed to the shader.
  250. * @param engine Engine to be used to render the effect
  251. * @param defines Define statements to be added to the shader.
  252. * @param fallbacks Possible fallbacks for this effect to improve performance when needed.
  253. * @param onCompiled Callback that will be called when the shader is compiled.
  254. * @param onError Callback that will be called if an error occurs during shader compilation.
  255. * @param indexParameters Parameters to be used with Babylons include syntax to iterate over an array (eg. {lights: 10})
  256. */
  257. constructor(baseName: any, attributesNamesOrOptions: string[] | EffectCreationOptions, uniformsNamesOrEngine: string[] | Engine, samplers: Nullable<string[]> = null, engine?: Engine, defines: Nullable<string> = null,
  258. fallbacks: Nullable<EffectFallbacks> = null, onCompiled: Nullable<(effect: Effect) => void> = null, onError: Nullable<(effect: Effect, errors: string) => void> = null, indexParameters?: any) {
  259. this.name = baseName;
  260. if ((<EffectCreationOptions>attributesNamesOrOptions).attributes) {
  261. var options = <EffectCreationOptions>attributesNamesOrOptions;
  262. this._engine = <Engine>uniformsNamesOrEngine;
  263. this._attributesNames = options.attributes;
  264. this._uniformsNames = options.uniformsNames.concat(options.samplers);
  265. this._samplers = options.samplers.slice();
  266. this.defines = options.defines;
  267. this.onError = options.onError;
  268. this.onCompiled = options.onCompiled;
  269. this._fallbacks = options.fallbacks;
  270. this._indexParameters = options.indexParameters;
  271. this._transformFeedbackVaryings = options.transformFeedbackVaryings;
  272. if (options.uniformBuffersNames) {
  273. for (var i = 0; i < options.uniformBuffersNames.length; i++) {
  274. this._uniformBuffersNames[options.uniformBuffersNames[i]] = i;
  275. }
  276. }
  277. } else {
  278. this._engine = <Engine>engine;
  279. this.defines = <string>defines;
  280. this._uniformsNames = (<string[]>uniformsNamesOrEngine).concat(<string[]>samplers);
  281. this._samplers = samplers ? <string[]>samplers.slice() : [];
  282. this._attributesNames = (<string[]>attributesNamesOrOptions);
  283. this.onError = onError;
  284. this.onCompiled = onCompiled;
  285. this._indexParameters = indexParameters;
  286. this._fallbacks = fallbacks;
  287. }
  288. this.uniqueId = Effect._uniqueIdSeed++;
  289. var vertexSource: any;
  290. var fragmentSource: any;
  291. if (baseName.vertexElement) {
  292. vertexSource = document.getElementById(baseName.vertexElement);
  293. if (!vertexSource) {
  294. vertexSource = baseName.vertexElement;
  295. }
  296. } else {
  297. vertexSource = baseName.vertex || baseName;
  298. }
  299. if (baseName.fragmentElement) {
  300. fragmentSource = document.getElementById(baseName.fragmentElement);
  301. if (!fragmentSource) {
  302. fragmentSource = baseName.fragmentElement;
  303. }
  304. } else {
  305. fragmentSource = baseName.fragment || baseName;
  306. }
  307. this._loadVertexShader(vertexSource, (vertexCode) => {
  308. this._processIncludes(vertexCode, (vertexCodeWithIncludes) => {
  309. this._processShaderConversion(vertexCodeWithIncludes, false, (migratedVertexCode) => {
  310. this._loadFragmentShader(fragmentSource, (fragmentCode) => {
  311. this._processIncludes(fragmentCode, (fragmentCodeWithIncludes) => {
  312. this._processShaderConversion(fragmentCodeWithIncludes, true, (migratedFragmentCode) => {
  313. if (baseName) {
  314. var vertex = baseName.vertexElement || baseName.vertex || baseName;
  315. var fragment = baseName.fragmentElement || baseName.fragment || baseName;
  316. this._vertexSourceCode = "#define SHADER_NAME vertex:" + vertex + "\n" + migratedVertexCode;
  317. this._fragmentSourceCode = "#define SHADER_NAME fragment:" + fragment + "\n" + migratedFragmentCode;
  318. } else {
  319. this._vertexSourceCode = migratedVertexCode;
  320. this._fragmentSourceCode = migratedFragmentCode;
  321. }
  322. this._prepareEffect();
  323. });
  324. });
  325. });
  326. });
  327. });
  328. });
  329. }
  330. /**
  331. * Unique key for this effect
  332. */
  333. public get key(): string {
  334. return this._key;
  335. }
  336. /**
  337. * If the effect has been compiled and prepared.
  338. * @returns if the effect is compiled and prepared.
  339. */
  340. public isReady(): boolean {
  341. if (!this._isReady && this._program && this._program.isParallelCompiled) {
  342. return this._engine._isProgramCompiled(this._program);
  343. }
  344. return this._isReady;
  345. }
  346. /**
  347. * The engine the effect was initialized with.
  348. * @returns the engine.
  349. */
  350. public getEngine(): Engine {
  351. return this._engine;
  352. }
  353. /**
  354. * The compiled webGL program for the effect
  355. * @returns the webGL program.
  356. */
  357. public getProgram(): WebGLProgram {
  358. return this._program;
  359. }
  360. /**
  361. * The set of names of attribute variables for the shader.
  362. * @returns An array of attribute names.
  363. */
  364. public getAttributesNames(): string[] {
  365. return this._attributesNames;
  366. }
  367. /**
  368. * Returns the attribute at the given index.
  369. * @param index The index of the attribute.
  370. * @returns The location of the attribute.
  371. */
  372. public getAttributeLocation(index: number): number {
  373. return this._attributes[index];
  374. }
  375. /**
  376. * Returns the attribute based on the name of the variable.
  377. * @param name of the attribute to look up.
  378. * @returns the attribute location.
  379. */
  380. public getAttributeLocationByName(name: string): number {
  381. var index = this._attributesNames.indexOf(name);
  382. return this._attributes[index];
  383. }
  384. /**
  385. * The number of attributes.
  386. * @returns the numnber of attributes.
  387. */
  388. public getAttributesCount(): number {
  389. return this._attributes.length;
  390. }
  391. /**
  392. * Gets the index of a uniform variable.
  393. * @param uniformName of the uniform to look up.
  394. * @returns the index.
  395. */
  396. public getUniformIndex(uniformName: string): number {
  397. return this._uniformsNames.indexOf(uniformName);
  398. }
  399. /**
  400. * Returns the attribute based on the name of the variable.
  401. * @param uniformName of the uniform to look up.
  402. * @returns the location of the uniform.
  403. */
  404. public getUniform(uniformName: string): Nullable<WebGLUniformLocation> {
  405. return this._uniforms[this._uniformsNames.indexOf(uniformName)];
  406. }
  407. /**
  408. * Returns an array of sampler variable names
  409. * @returns The array of sampler variable neames.
  410. */
  411. public getSamplers(): string[] {
  412. return this._samplers;
  413. }
  414. /**
  415. * The error from the last compilation.
  416. * @returns the error string.
  417. */
  418. public getCompilationError(): string {
  419. return this._compilationError;
  420. }
  421. /**
  422. * Adds a callback to the onCompiled observable and call the callback imediatly if already ready.
  423. * @param func The callback to be used.
  424. */
  425. public executeWhenCompiled(func: (effect: Effect) => void): void {
  426. if (this.isReady()) {
  427. func(this);
  428. return;
  429. }
  430. this.onCompileObservable.add((effect) => {
  431. func(effect);
  432. });
  433. if (!this._program || this._program.isParallelCompiled) {
  434. setTimeout(() => {
  435. this._checkIsReady();
  436. }, 16);
  437. }
  438. }
  439. private _checkIsReady() {
  440. if (this.isReady()) {
  441. return;
  442. }
  443. setTimeout(() => {
  444. this._checkIsReady();
  445. }, 16);
  446. }
  447. /** @hidden */
  448. public _loadVertexShader(vertex: any, callback: (data: any) => void): void {
  449. if (DomManagement.IsWindowObjectExist()) {
  450. // DOM element ?
  451. if (vertex instanceof HTMLElement) {
  452. var vertexCode = DomManagement.GetDOMTextContent(vertex);
  453. callback(vertexCode);
  454. return;
  455. }
  456. }
  457. // Base64 encoded ?
  458. if (vertex.substr(0, 7) === "base64:") {
  459. var vertexBinary = window.atob(vertex.substr(7));
  460. callback(vertexBinary);
  461. return;
  462. }
  463. // Is in local store ?
  464. if (Effect.ShadersStore[vertex + "VertexShader"]) {
  465. callback(Effect.ShadersStore[vertex + "VertexShader"]);
  466. return;
  467. }
  468. var vertexShaderUrl;
  469. if (vertex[0] === "." || vertex[0] === "/" || vertex.indexOf("http") > -1) {
  470. vertexShaderUrl = vertex;
  471. } else {
  472. vertexShaderUrl = Effect.ShadersRepository + vertex;
  473. }
  474. // Vertex shader
  475. this._engine._loadFile(vertexShaderUrl + ".vertex.fx", callback);
  476. }
  477. /** @hidden */
  478. public _loadFragmentShader(fragment: any, callback: (data: any) => void): void {
  479. if (DomManagement.IsWindowObjectExist()) {
  480. // DOM element ?
  481. if (fragment instanceof HTMLElement) {
  482. var fragmentCode = DomManagement.GetDOMTextContent(fragment);
  483. callback(fragmentCode);
  484. return;
  485. }
  486. }
  487. // Base64 encoded ?
  488. if (fragment.substr(0, 7) === "base64:") {
  489. var fragmentBinary = window.atob(fragment.substr(7));
  490. callback(fragmentBinary);
  491. return;
  492. }
  493. // Is in local store ?
  494. if (Effect.ShadersStore[fragment + "PixelShader"]) {
  495. callback(Effect.ShadersStore[fragment + "PixelShader"]);
  496. return;
  497. }
  498. if (Effect.ShadersStore[fragment + "FragmentShader"]) {
  499. callback(Effect.ShadersStore[fragment + "FragmentShader"]);
  500. return;
  501. }
  502. var fragmentShaderUrl;
  503. if (fragment[0] === "." || fragment[0] === "/" || fragment.indexOf("http") > -1) {
  504. fragmentShaderUrl = fragment;
  505. } else {
  506. fragmentShaderUrl = Effect.ShadersRepository + fragment;
  507. }
  508. // Fragment shader
  509. this._engine._loadFile(fragmentShaderUrl + ".fragment.fx", callback);
  510. }
  511. /** @hidden */
  512. public _dumpShadersSource(vertexCode: string, fragmentCode: string, defines: string): void {
  513. // Rebuild shaders source code
  514. var shaderVersion = (this._engine.webGLVersion > 1) ? "#version 300 es\n#define WEBGL2 \n" : "";
  515. var prefix = shaderVersion + (defines ? defines + "\n" : "");
  516. vertexCode = prefix + vertexCode;
  517. fragmentCode = prefix + fragmentCode;
  518. // Number lines of shaders source code
  519. var i = 2;
  520. var regex = /\n/gm;
  521. var formattedVertexCode = "\n1\t" + vertexCode.replace(regex, function() { return "\n" + (i++) + "\t"; });
  522. i = 2;
  523. var formattedFragmentCode = "\n1\t" + fragmentCode.replace(regex, function() { return "\n" + (i++) + "\t"; });
  524. // Dump shaders name and formatted source code
  525. if (this.name.vertexElement) {
  526. Logger.Error("Vertex shader: " + this.name.vertexElement + formattedVertexCode);
  527. Logger.Error("Fragment shader: " + this.name.fragmentElement + formattedFragmentCode);
  528. }
  529. else if (this.name.vertex) {
  530. Logger.Error("Vertex shader: " + this.name.vertex + formattedVertexCode);
  531. Logger.Error("Fragment shader: " + this.name.fragment + formattedFragmentCode);
  532. }
  533. else {
  534. Logger.Error("Vertex shader: " + this.name + formattedVertexCode);
  535. Logger.Error("Fragment shader: " + this.name + formattedFragmentCode);
  536. }
  537. }
  538. private _processShaderConversion(sourceCode: string, isFragment: boolean, callback: (data: any) => void): void {
  539. var preparedSourceCode = this._processPrecision(sourceCode);
  540. if (this._engine.webGLVersion == 1) {
  541. callback(preparedSourceCode);
  542. return;
  543. }
  544. // Already converted
  545. if (preparedSourceCode.indexOf("#version 3") !== -1) {
  546. callback(preparedSourceCode.replace("#version 300 es", ""));
  547. return;
  548. }
  549. var hasDrawBuffersExtension = preparedSourceCode.search(/#extension.+GL_EXT_draw_buffers.+require/) !== -1;
  550. // Remove extensions
  551. // #extension GL_OES_standard_derivatives : enable
  552. // #extension GL_EXT_shader_texture_lod : enable
  553. // #extension GL_EXT_frag_depth : enable
  554. // #extension GL_EXT_draw_buffers : require
  555. var regex = /#extension.+(GL_OVR_multiview|GL_OES_standard_derivatives|GL_EXT_shader_texture_lod|GL_EXT_frag_depth|GL_EXT_draw_buffers).+(enable|require)/g;
  556. var result = preparedSourceCode.replace(regex, "");
  557. // Migrate to GLSL v300
  558. result = result.replace(/varying(?![\n\r])\s/g, isFragment ? "in " : "out ");
  559. result = result.replace(/attribute[ \t]/g, "in ");
  560. result = result.replace(/[ \t]attribute/g, " in");
  561. result = result.replace(/texture2D\s*\(/g, "texture(");
  562. if (isFragment) {
  563. result = result.replace(/texture2DLodEXT\s*\(/g, "textureLod(");
  564. result = result.replace(/textureCubeLodEXT\s*\(/g, "textureLod(");
  565. result = result.replace(/textureCube\s*\(/g, "texture(");
  566. result = result.replace(/gl_FragDepthEXT/g, "gl_FragDepth");
  567. result = result.replace(/gl_FragColor/g, "glFragColor");
  568. result = result.replace(/gl_FragData/g, "glFragData");
  569. result = result.replace(/void\s+?main\s*\(/g, (hasDrawBuffersExtension ? "" : "out vec4 glFragColor;\n") + "void main(");
  570. }
  571. // Add multiview setup to top of file when defined
  572. var hasMultiviewExtension = this.defines && this.defines.indexOf("#define MULTIVIEW\n") !== -1;
  573. if (hasMultiviewExtension && !isFragment) {
  574. result = "#extension GL_OVR_multiview : require\nlayout (num_views = 2) in;\n" + result;
  575. }
  576. callback(result);
  577. }
  578. private _processIncludes(sourceCode: string, callback: (data: any) => void): void {
  579. var regex = /#include<(.+)>(\((.*)\))*(\[(.*)\])*/g;
  580. var match = regex.exec(sourceCode);
  581. var returnValue = new String(sourceCode);
  582. while (match != null) {
  583. var includeFile = match[1];
  584. // Uniform declaration
  585. if (includeFile.indexOf("__decl__") !== -1) {
  586. includeFile = includeFile.replace(/__decl__/, "");
  587. if (this._engine.supportsUniformBuffers) {
  588. includeFile = includeFile.replace(/Vertex/, "Ubo");
  589. includeFile = includeFile.replace(/Fragment/, "Ubo");
  590. }
  591. includeFile = includeFile + "Declaration";
  592. }
  593. if (Effect.IncludesShadersStore[includeFile]) {
  594. // Substitution
  595. var includeContent = Effect.IncludesShadersStore[includeFile];
  596. if (match[2]) {
  597. var splits = match[3].split(",");
  598. for (var index = 0; index < splits.length; index += 2) {
  599. var source = new RegExp(splits[index], "g");
  600. var dest = splits[index + 1];
  601. includeContent = includeContent.replace(source, dest);
  602. }
  603. }
  604. if (match[4]) {
  605. var indexString = match[5];
  606. if (indexString.indexOf("..") !== -1) {
  607. var indexSplits = indexString.split("..");
  608. var minIndex = parseInt(indexSplits[0]);
  609. var maxIndex = parseInt(indexSplits[1]);
  610. var sourceIncludeContent = includeContent.slice(0);
  611. includeContent = "";
  612. if (isNaN(maxIndex)) {
  613. maxIndex = this._indexParameters[indexSplits[1]];
  614. }
  615. for (var i = minIndex; i < maxIndex; i++) {
  616. if (!this._engine.supportsUniformBuffers) {
  617. // Ubo replacement
  618. sourceIncludeContent = sourceIncludeContent.replace(/light\{X\}.(\w*)/g, (str: string, p1: string) => {
  619. return p1 + "{X}";
  620. });
  621. }
  622. includeContent += sourceIncludeContent.replace(/\{X\}/g, i.toString()) + "\n";
  623. }
  624. } else {
  625. if (!this._engine.supportsUniformBuffers) {
  626. // Ubo replacement
  627. includeContent = includeContent.replace(/light\{X\}.(\w*)/g, (str: string, p1: string) => {
  628. return p1 + "{X}";
  629. });
  630. }
  631. includeContent = includeContent.replace(/\{X\}/g, indexString);
  632. }
  633. }
  634. // Replace
  635. returnValue = returnValue.replace(match[0], includeContent);
  636. } else {
  637. var includeShaderUrl = Effect.ShadersRepository + "ShadersInclude/" + includeFile + ".fx";
  638. this._engine._loadFile(includeShaderUrl, (fileContent) => {
  639. Effect.IncludesShadersStore[includeFile] = fileContent as string;
  640. this._processIncludes(<string>returnValue, callback);
  641. });
  642. return;
  643. }
  644. match = regex.exec(sourceCode);
  645. }
  646. callback(returnValue);
  647. }
  648. private _processPrecision(source: string): string {
  649. const shouldUseHighPrecisionShader = this._engine._shouldUseHighPrecisionShader;
  650. if (source.indexOf("precision highp float") === -1) {
  651. if (!shouldUseHighPrecisionShader) {
  652. source = "precision mediump float;\n" + source;
  653. } else {
  654. source = "precision highp float;\n" + source;
  655. }
  656. } else {
  657. if (!shouldUseHighPrecisionShader) { // Moving highp to mediump
  658. source = source.replace("precision highp float", "precision mediump float");
  659. }
  660. }
  661. return source;
  662. }
  663. /**
  664. * Recompiles the webGL program
  665. * @param vertexSourceCode The source code for the vertex shader.
  666. * @param fragmentSourceCode The source code for the fragment shader.
  667. * @param onCompiled Callback called when completed.
  668. * @param onError Callback called on error.
  669. * @hidden
  670. */
  671. public _rebuildProgram(vertexSourceCode: string, fragmentSourceCode: string, onCompiled: (program: WebGLProgram) => void, onError: (message: string) => void) {
  672. this._isReady = false;
  673. this._vertexSourceCodeOverride = vertexSourceCode;
  674. this._fragmentSourceCodeOverride = fragmentSourceCode;
  675. this.onError = (effect, error) => {
  676. if (onError) {
  677. onError(error);
  678. }
  679. };
  680. this.onCompiled = () => {
  681. var scenes = this.getEngine().scenes;
  682. for (var i = 0; i < scenes.length; i++) {
  683. scenes[i].markAllMaterialsAsDirty(Constants.MATERIAL_AllDirtyFlag);
  684. }
  685. if (onCompiled) {
  686. onCompiled(this._program);
  687. }
  688. };
  689. this._fallbacks = null;
  690. this._prepareEffect();
  691. }
  692. /**
  693. * Gets the uniform locations of the the specified variable names
  694. * @param names THe names of the variables to lookup.
  695. * @returns Array of locations in the same order as variable names.
  696. */
  697. public getSpecificUniformLocations(names: string[]): Nullable<WebGLUniformLocation>[] {
  698. let engine = this._engine;
  699. return engine.getUniforms(this._program, names);
  700. }
  701. /**
  702. * Prepares the effect
  703. * @hidden
  704. */
  705. public _prepareEffect() {
  706. let attributesNames = this._attributesNames;
  707. let defines = this.defines;
  708. let fallbacks = this._fallbacks;
  709. this._valueCache = {};
  710. var previousProgram = this._program;
  711. try {
  712. let engine = this._engine;
  713. if (this._vertexSourceCodeOverride && this._fragmentSourceCodeOverride) {
  714. this._program = engine.createRawShaderProgram(this._vertexSourceCodeOverride, this._fragmentSourceCodeOverride, undefined, this._transformFeedbackVaryings);
  715. }
  716. else {
  717. this._program = engine.createShaderProgram(this._vertexSourceCode, this._fragmentSourceCode, defines, undefined, this._transformFeedbackVaryings);
  718. }
  719. this._program.__SPECTOR_rebuildProgram = this._rebuildProgram.bind(this);
  720. engine._executeWhenProgramIsCompiled(this._program, () => {
  721. if (engine.supportsUniformBuffers) {
  722. for (var name in this._uniformBuffersNames) {
  723. this.bindUniformBlock(name, this._uniformBuffersNames[name]);
  724. }
  725. }
  726. this._uniforms = engine.getUniforms(this._program, this._uniformsNames);
  727. this._attributes = engine.getAttributes(this._program, attributesNames);
  728. var index: number;
  729. for (index = 0; index < this._samplers.length; index++) {
  730. var sampler = this.getUniform(this._samplers[index]);
  731. if (sampler == null) {
  732. this._samplers.splice(index, 1);
  733. index--;
  734. }
  735. }
  736. engine.bindSamplers(this);
  737. this._compilationError = "";
  738. this._isReady = true;
  739. if (this.onCompiled) {
  740. this.onCompiled(this);
  741. }
  742. this.onCompileObservable.notifyObservers(this);
  743. this.onCompileObservable.clear();
  744. // Unbind mesh reference in fallbacks
  745. if (this._fallbacks) {
  746. this._fallbacks.unBindMesh();
  747. }
  748. if (previousProgram) {
  749. this.getEngine()._deleteProgram(previousProgram);
  750. }
  751. });
  752. if (this._program.isParallelCompiled) {
  753. this._checkIsReady();
  754. }
  755. } catch (e) {
  756. this._compilationError = e.message;
  757. // Let's go through fallbacks then
  758. Logger.Error("Unable to compile effect:");
  759. Logger.Error("Uniforms: " + this._uniformsNames.map(function(uniform) {
  760. return " " + uniform;
  761. }));
  762. Logger.Error("Attributes: " + attributesNames.map(function(attribute) {
  763. return " " + attribute;
  764. }));
  765. Logger.Error("Error: " + this._compilationError);
  766. if (previousProgram) {
  767. this._program = previousProgram;
  768. this._isReady = true;
  769. if (this.onError) {
  770. this.onError(this, this._compilationError);
  771. }
  772. this.onErrorObservable.notifyObservers(this);
  773. }
  774. if (fallbacks && fallbacks.isMoreFallbacks) {
  775. Logger.Error("Trying next fallback.");
  776. this.defines = fallbacks.reduce(this.defines, this);
  777. this._prepareEffect();
  778. } else { // Sorry we did everything we can
  779. if (this.onError) {
  780. this.onError(this, this._compilationError);
  781. }
  782. this.onErrorObservable.notifyObservers(this);
  783. this.onErrorObservable.clear();
  784. // Unbind mesh reference in fallbacks
  785. if (this._fallbacks) {
  786. this._fallbacks.unBindMesh();
  787. }
  788. }
  789. }
  790. }
  791. /**
  792. * Checks if the effect is supported. (Must be called after compilation)
  793. */
  794. public get isSupported(): boolean {
  795. return this._compilationError === "";
  796. }
  797. /**
  798. * Binds a texture to the engine to be used as output of the shader.
  799. * @param channel Name of the output variable.
  800. * @param texture Texture to bind.
  801. * @hidden
  802. */
  803. public _bindTexture(channel: string, texture: InternalTexture): void {
  804. this._engine._bindTexture(this._samplers.indexOf(channel), texture);
  805. }
  806. /**
  807. * Sets a texture on the engine to be used in the shader.
  808. * @param channel Name of the sampler variable.
  809. * @param texture Texture to set.
  810. */
  811. public setTexture(channel: string, texture: Nullable<BaseTexture>): void {
  812. this._engine.setTexture(this._samplers.indexOf(channel), this.getUniform(channel), texture);
  813. }
  814. /**
  815. * Sets a depth stencil texture from a render target on the engine to be used in the shader.
  816. * @param channel Name of the sampler variable.
  817. * @param texture Texture to set.
  818. */
  819. public setDepthStencilTexture(channel: string, texture: Nullable<RenderTargetTexture>): void {
  820. this._engine.setDepthStencilTexture(this._samplers.indexOf(channel), this.getUniform(channel), texture);
  821. }
  822. /**
  823. * Sets an array of textures on the engine to be used in the shader.
  824. * @param channel Name of the variable.
  825. * @param textures Textures to set.
  826. */
  827. public setTextureArray(channel: string, textures: BaseTexture[]): void {
  828. if (this._samplers.indexOf(channel + "Ex") === -1) {
  829. var initialPos = this._samplers.indexOf(channel);
  830. for (var index = 1; index < textures.length; index++) {
  831. this._samplers.splice(initialPos + index, 0, channel + "Ex");
  832. }
  833. }
  834. this._engine.setTextureArray(this._samplers.indexOf(channel), this.getUniform(channel), textures);
  835. }
  836. /**
  837. * 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)
  838. * @param channel Name of the sampler variable.
  839. * @param postProcess Post process to get the input texture from.
  840. */
  841. public setTextureFromPostProcess(channel: string, postProcess: Nullable<PostProcess>): void {
  842. this._engine.setTextureFromPostProcess(this._samplers.indexOf(channel), postProcess);
  843. }
  844. /**
  845. * (Warning! setTextureFromPostProcessOutput may be desired instead)
  846. * 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)
  847. * @param channel Name of the sampler variable.
  848. * @param postProcess Post process to get the output texture from.
  849. */
  850. public setTextureFromPostProcessOutput(channel: string, postProcess: Nullable<PostProcess>): void {
  851. this._engine.setTextureFromPostProcessOutput(this._samplers.indexOf(channel), postProcess);
  852. }
  853. /** @hidden */
  854. public _cacheMatrix(uniformName: string, matrix: Matrix): boolean {
  855. var cache = this._valueCache[uniformName];
  856. var flag = matrix.updateFlag;
  857. if (cache !== undefined && cache === flag) {
  858. return false;
  859. }
  860. this._valueCache[uniformName] = flag;
  861. return true;
  862. }
  863. /** @hidden */
  864. public _cacheFloat2(uniformName: string, x: number, y: number): boolean {
  865. var cache = this._valueCache[uniformName];
  866. if (!cache) {
  867. cache = [x, y];
  868. this._valueCache[uniformName] = cache;
  869. return true;
  870. }
  871. var changed = false;
  872. if (cache[0] !== x) {
  873. cache[0] = x;
  874. changed = true;
  875. }
  876. if (cache[1] !== y) {
  877. cache[1] = y;
  878. changed = true;
  879. }
  880. return changed;
  881. }
  882. /** @hidden */
  883. public _cacheFloat3(uniformName: string, x: number, y: number, z: number): boolean {
  884. var cache = this._valueCache[uniformName];
  885. if (!cache) {
  886. cache = [x, y, z];
  887. this._valueCache[uniformName] = cache;
  888. return true;
  889. }
  890. var changed = false;
  891. if (cache[0] !== x) {
  892. cache[0] = x;
  893. changed = true;
  894. }
  895. if (cache[1] !== y) {
  896. cache[1] = y;
  897. changed = true;
  898. }
  899. if (cache[2] !== z) {
  900. cache[2] = z;
  901. changed = true;
  902. }
  903. return changed;
  904. }
  905. /** @hidden */
  906. public _cacheFloat4(uniformName: string, x: number, y: number, z: number, w: number): boolean {
  907. var cache = this._valueCache[uniformName];
  908. if (!cache) {
  909. cache = [x, y, z, w];
  910. this._valueCache[uniformName] = cache;
  911. return true;
  912. }
  913. var changed = false;
  914. if (cache[0] !== x) {
  915. cache[0] = x;
  916. changed = true;
  917. }
  918. if (cache[1] !== y) {
  919. cache[1] = y;
  920. changed = true;
  921. }
  922. if (cache[2] !== z) {
  923. cache[2] = z;
  924. changed = true;
  925. }
  926. if (cache[3] !== w) {
  927. cache[3] = w;
  928. changed = true;
  929. }
  930. return changed;
  931. }
  932. /**
  933. * Binds a buffer to a uniform.
  934. * @param buffer Buffer to bind.
  935. * @param name Name of the uniform variable to bind to.
  936. */
  937. public bindUniformBuffer(buffer: WebGLBuffer, name: string): void {
  938. let bufferName = this._uniformBuffersNames[name];
  939. if (bufferName === undefined || Effect._baseCache[bufferName] === buffer) {
  940. return;
  941. }
  942. Effect._baseCache[bufferName] = buffer;
  943. this._engine.bindUniformBufferBase(buffer, bufferName);
  944. }
  945. /**
  946. * Binds block to a uniform.
  947. * @param blockName Name of the block to bind.
  948. * @param index Index to bind.
  949. */
  950. public bindUniformBlock(blockName: string, index: number): void {
  951. this._engine.bindUniformBlock(this._program, blockName, index);
  952. }
  953. /**
  954. * Sets an interger value on a uniform variable.
  955. * @param uniformName Name of the variable.
  956. * @param value Value to be set.
  957. * @returns this effect.
  958. */
  959. public setInt(uniformName: string, value: number): Effect {
  960. var cache = this._valueCache[uniformName];
  961. if (cache !== undefined && cache === value) {
  962. return this;
  963. }
  964. this._valueCache[uniformName] = value;
  965. this._engine.setInt(this.getUniform(uniformName), value);
  966. return this;
  967. }
  968. /**
  969. * Sets an int array on a uniform variable.
  970. * @param uniformName Name of the variable.
  971. * @param array array to be set.
  972. * @returns this effect.
  973. */
  974. public setIntArray(uniformName: string, array: Int32Array): Effect {
  975. this._valueCache[uniformName] = null;
  976. this._engine.setIntArray(this.getUniform(uniformName), array);
  977. return this;
  978. }
  979. /**
  980. * 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)
  981. * @param uniformName Name of the variable.
  982. * @param array array to be set.
  983. * @returns this effect.
  984. */
  985. public setIntArray2(uniformName: string, array: Int32Array): Effect {
  986. this._valueCache[uniformName] = null;
  987. this._engine.setIntArray2(this.getUniform(uniformName), array);
  988. return this;
  989. }
  990. /**
  991. * 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)
  992. * @param uniformName Name of the variable.
  993. * @param array array to be set.
  994. * @returns this effect.
  995. */
  996. public setIntArray3(uniformName: string, array: Int32Array): Effect {
  997. this._valueCache[uniformName] = null;
  998. this._engine.setIntArray3(this.getUniform(uniformName), array);
  999. return this;
  1000. }
  1001. /**
  1002. * 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)
  1003. * @param uniformName Name of the variable.
  1004. * @param array array to be set.
  1005. * @returns this effect.
  1006. */
  1007. public setIntArray4(uniformName: string, array: Int32Array): Effect {
  1008. this._valueCache[uniformName] = null;
  1009. this._engine.setIntArray4(this.getUniform(uniformName), array);
  1010. return this;
  1011. }
  1012. /**
  1013. * Sets an float array on a uniform variable.
  1014. * @param uniformName Name of the variable.
  1015. * @param array array to be set.
  1016. * @returns this effect.
  1017. */
  1018. public setFloatArray(uniformName: string, array: Float32Array): Effect {
  1019. this._valueCache[uniformName] = null;
  1020. this._engine.setFloatArray(this.getUniform(uniformName), array);
  1021. return this;
  1022. }
  1023. /**
  1024. * 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)
  1025. * @param uniformName Name of the variable.
  1026. * @param array array to be set.
  1027. * @returns this effect.
  1028. */
  1029. public setFloatArray2(uniformName: string, array: Float32Array): Effect {
  1030. this._valueCache[uniformName] = null;
  1031. this._engine.setFloatArray2(this.getUniform(uniformName), array);
  1032. return this;
  1033. }
  1034. /**
  1035. * 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)
  1036. * @param uniformName Name of the variable.
  1037. * @param array array to be set.
  1038. * @returns this effect.
  1039. */
  1040. public setFloatArray3(uniformName: string, array: Float32Array): Effect {
  1041. this._valueCache[uniformName] = null;
  1042. this._engine.setFloatArray3(this.getUniform(uniformName), array);
  1043. return this;
  1044. }
  1045. /**
  1046. * 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)
  1047. * @param uniformName Name of the variable.
  1048. * @param array array to be set.
  1049. * @returns this effect.
  1050. */
  1051. public setFloatArray4(uniformName: string, array: Float32Array): Effect {
  1052. this._valueCache[uniformName] = null;
  1053. this._engine.setFloatArray4(this.getUniform(uniformName), array);
  1054. return this;
  1055. }
  1056. /**
  1057. * Sets an array on a uniform variable.
  1058. * @param uniformName Name of the variable.
  1059. * @param array array to be set.
  1060. * @returns this effect.
  1061. */
  1062. public setArray(uniformName: string, array: number[]): Effect {
  1063. this._valueCache[uniformName] = null;
  1064. this._engine.setArray(this.getUniform(uniformName), array);
  1065. return this;
  1066. }
  1067. /**
  1068. * 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)
  1069. * @param uniformName Name of the variable.
  1070. * @param array array to be set.
  1071. * @returns this effect.
  1072. */
  1073. public setArray2(uniformName: string, array: number[]): Effect {
  1074. this._valueCache[uniformName] = null;
  1075. this._engine.setArray2(this.getUniform(uniformName), array);
  1076. return this;
  1077. }
  1078. /**
  1079. * 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)
  1080. * @param uniformName Name of the variable.
  1081. * @param array array to be set.
  1082. * @returns this effect.
  1083. */
  1084. public setArray3(uniformName: string, array: number[]): Effect {
  1085. this._valueCache[uniformName] = null;
  1086. this._engine.setArray3(this.getUniform(uniformName), array);
  1087. return this;
  1088. }
  1089. /**
  1090. * 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)
  1091. * @param uniformName Name of the variable.
  1092. * @param array array to be set.
  1093. * @returns this effect.
  1094. */
  1095. public setArray4(uniformName: string, array: number[]): Effect {
  1096. this._valueCache[uniformName] = null;
  1097. this._engine.setArray4(this.getUniform(uniformName), array);
  1098. return this;
  1099. }
  1100. /**
  1101. * Sets matrices on a uniform variable.
  1102. * @param uniformName Name of the variable.
  1103. * @param matrices matrices to be set.
  1104. * @returns this effect.
  1105. */
  1106. public setMatrices(uniformName: string, matrices: Float32Array): Effect {
  1107. if (!matrices) {
  1108. return this;
  1109. }
  1110. this._valueCache[uniformName] = null;
  1111. this._engine.setMatrices(this.getUniform(uniformName), matrices);
  1112. return this;
  1113. }
  1114. /**
  1115. * Sets matrix on a uniform variable.
  1116. * @param uniformName Name of the variable.
  1117. * @param matrix matrix to be set.
  1118. * @returns this effect.
  1119. */
  1120. public setMatrix(uniformName: string, matrix: Matrix): Effect {
  1121. if (this._cacheMatrix(uniformName, matrix)) {
  1122. this._engine.setMatrix(this.getUniform(uniformName), matrix);
  1123. }
  1124. return this;
  1125. }
  1126. /**
  1127. * 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)
  1128. * @param uniformName Name of the variable.
  1129. * @param matrix matrix to be set.
  1130. * @returns this effect.
  1131. */
  1132. public setMatrix3x3(uniformName: string, matrix: Float32Array): Effect {
  1133. this._valueCache[uniformName] = null;
  1134. this._engine.setMatrix3x3(this.getUniform(uniformName), matrix);
  1135. return this;
  1136. }
  1137. /**
  1138. * Sets a 2x2 matrix on a uniform variable. (Speicified as [1,2,3,4] will result in [1,2][3,4] matrix)
  1139. * @param uniformName Name of the variable.
  1140. * @param matrix matrix to be set.
  1141. * @returns this effect.
  1142. */
  1143. public setMatrix2x2(uniformName: string, matrix: Float32Array): Effect {
  1144. this._valueCache[uniformName] = null;
  1145. this._engine.setMatrix2x2(this.getUniform(uniformName), matrix);
  1146. return this;
  1147. }
  1148. /**
  1149. * Sets a float on a uniform variable.
  1150. * @param uniformName Name of the variable.
  1151. * @param value value to be set.
  1152. * @returns this effect.
  1153. */
  1154. public setFloat(uniformName: string, value: number): Effect {
  1155. var cache = this._valueCache[uniformName];
  1156. if (cache !== undefined && cache === value) {
  1157. return this;
  1158. }
  1159. this._valueCache[uniformName] = value;
  1160. this._engine.setFloat(this.getUniform(uniformName), value);
  1161. return this;
  1162. }
  1163. /**
  1164. * Sets a boolean on a uniform variable.
  1165. * @param uniformName Name of the variable.
  1166. * @param bool value to be set.
  1167. * @returns this effect.
  1168. */
  1169. public setBool(uniformName: string, bool: boolean): Effect {
  1170. var cache = this._valueCache[uniformName];
  1171. if (cache !== undefined && cache === bool) {
  1172. return this;
  1173. }
  1174. this._valueCache[uniformName] = bool;
  1175. this._engine.setBool(this.getUniform(uniformName), bool ? 1 : 0);
  1176. return this;
  1177. }
  1178. /**
  1179. * Sets a Vector2 on a uniform variable.
  1180. * @param uniformName Name of the variable.
  1181. * @param vector2 vector2 to be set.
  1182. * @returns this effect.
  1183. */
  1184. public setVector2(uniformName: string, vector2: Vector2): Effect {
  1185. if (this._cacheFloat2(uniformName, vector2.x, vector2.y)) {
  1186. this._engine.setFloat2(this.getUniform(uniformName), vector2.x, vector2.y);
  1187. }
  1188. return this;
  1189. }
  1190. /**
  1191. * Sets a float2 on a uniform variable.
  1192. * @param uniformName Name of the variable.
  1193. * @param x First float in float2.
  1194. * @param y Second float in float2.
  1195. * @returns this effect.
  1196. */
  1197. public setFloat2(uniformName: string, x: number, y: number): Effect {
  1198. if (this._cacheFloat2(uniformName, x, y)) {
  1199. this._engine.setFloat2(this.getUniform(uniformName), x, y);
  1200. }
  1201. return this;
  1202. }
  1203. /**
  1204. * Sets a Vector3 on a uniform variable.
  1205. * @param uniformName Name of the variable.
  1206. * @param vector3 Value to be set.
  1207. * @returns this effect.
  1208. */
  1209. public setVector3(uniformName: string, vector3: Vector3): Effect {
  1210. if (this._cacheFloat3(uniformName, vector3.x, vector3.y, vector3.z)) {
  1211. this._engine.setFloat3(this.getUniform(uniformName), vector3.x, vector3.y, vector3.z);
  1212. }
  1213. return this;
  1214. }
  1215. /**
  1216. * Sets a float3 on a uniform variable.
  1217. * @param uniformName Name of the variable.
  1218. * @param x First float in float3.
  1219. * @param y Second float in float3.
  1220. * @param z Third float in float3.
  1221. * @returns this effect.
  1222. */
  1223. public setFloat3(uniformName: string, x: number, y: number, z: number): Effect {
  1224. if (this._cacheFloat3(uniformName, x, y, z)) {
  1225. this._engine.setFloat3(this.getUniform(uniformName), x, y, z);
  1226. }
  1227. return this;
  1228. }
  1229. /**
  1230. * Sets a Vector4 on a uniform variable.
  1231. * @param uniformName Name of the variable.
  1232. * @param vector4 Value to be set.
  1233. * @returns this effect.
  1234. */
  1235. public setVector4(uniformName: string, vector4: Vector4): Effect {
  1236. if (this._cacheFloat4(uniformName, vector4.x, vector4.y, vector4.z, vector4.w)) {
  1237. this._engine.setFloat4(this.getUniform(uniformName), vector4.x, vector4.y, vector4.z, vector4.w);
  1238. }
  1239. return this;
  1240. }
  1241. /**
  1242. * Sets a float4 on a uniform variable.
  1243. * @param uniformName Name of the variable.
  1244. * @param x First float in float4.
  1245. * @param y Second float in float4.
  1246. * @param z Third float in float4.
  1247. * @param w Fourth float in float4.
  1248. * @returns this effect.
  1249. */
  1250. public setFloat4(uniformName: string, x: number, y: number, z: number, w: number): Effect {
  1251. if (this._cacheFloat4(uniformName, x, y, z, w)) {
  1252. this._engine.setFloat4(this.getUniform(uniformName), x, y, z, w);
  1253. }
  1254. return this;
  1255. }
  1256. /**
  1257. * Sets a Color3 on a uniform variable.
  1258. * @param uniformName Name of the variable.
  1259. * @param color3 Value to be set.
  1260. * @returns this effect.
  1261. */
  1262. public setColor3(uniformName: string, color3: Color3): Effect {
  1263. if (this._cacheFloat3(uniformName, color3.r, color3.g, color3.b)) {
  1264. this._engine.setColor3(this.getUniform(uniformName), color3);
  1265. }
  1266. return this;
  1267. }
  1268. /**
  1269. * Sets a Color4 on a uniform variable.
  1270. * @param uniformName Name of the variable.
  1271. * @param color3 Value to be set.
  1272. * @param alpha Alpha value to be set.
  1273. * @returns this effect.
  1274. */
  1275. public setColor4(uniformName: string, color3: Color3, alpha: number): Effect {
  1276. if (this._cacheFloat4(uniformName, color3.r, color3.g, color3.b, alpha)) {
  1277. this._engine.setColor4(this.getUniform(uniformName), color3, alpha);
  1278. }
  1279. return this;
  1280. }
  1281. /**
  1282. * Sets a Color4 on a uniform variable
  1283. * @param uniformName defines the name of the variable
  1284. * @param color4 defines the value to be set
  1285. * @returns this effect.
  1286. */
  1287. public setDirectColor4(uniformName: string, color4: Color4): Effect {
  1288. if (this._cacheFloat4(uniformName, color4.r, color4.g, color4.b, color4.a)) {
  1289. this._engine.setDirectColor4(this.getUniform(uniformName), color4);
  1290. }
  1291. return this;
  1292. }
  1293. /**
  1294. * This function will add a new shader to the shader store
  1295. * @param name the name of the shader
  1296. * @param pixelShader optional pixel shader content
  1297. * @param vertexShader optional vertex shader content
  1298. */
  1299. public static RegisterShader(name: string, pixelShader?: string, vertexShader?: string) {
  1300. if (pixelShader) {
  1301. Effect.ShadersStore[`${name}PixelShader`] = pixelShader;
  1302. }
  1303. if (vertexShader) {
  1304. Effect.ShadersStore[`${name}VertexShader`] = vertexShader;
  1305. }
  1306. }
  1307. /**
  1308. * Store of each shader (The can be looked up using effect.key)
  1309. */
  1310. public static ShadersStore: { [key: string]: string } = {};
  1311. /**
  1312. * Store of each included file for a shader (The can be looked up using effect.key)
  1313. */
  1314. public static IncludesShadersStore: { [key: string]: string } = {};
  1315. /**
  1316. * Resets the cache of effects.
  1317. */
  1318. public static ResetCache() {
  1319. Effect._baseCache = {};
  1320. }
  1321. }