babylon.effect.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. module BABYLON {
  2. export class EffectFallbacks {
  3. private _defines = {};
  4. private _currentRank = 32;
  5. private _maxRank = -1;
  6. public addFallback(rank: number, define: string): void {
  7. if (!this._defines[rank]) {
  8. if (rank < this._currentRank) {
  9. this._currentRank = rank;
  10. }
  11. if (rank > this._maxRank) {
  12. this._maxRank = rank;
  13. }
  14. this._defines[rank] = new Array<String>();
  15. }
  16. this._defines[rank].push(define);
  17. }
  18. public get isMoreFallbacks(): boolean {
  19. return this._currentRank <= this._maxRank;
  20. }
  21. public reduce(currentDefines: string): string {
  22. var currentFallbacks = this._defines[this._currentRank];
  23. for (var index = 0; index < currentFallbacks.length; index++) {
  24. currentDefines = currentDefines.replace("#define " + currentFallbacks[index], "");
  25. }
  26. this._currentRank++;
  27. return currentDefines;
  28. }
  29. }
  30. export class Effect {
  31. public name: any;
  32. public defines: string;
  33. public onCompiled: (effect: Effect) => void;
  34. public onError: (effect: Effect, errors: string) => void;
  35. private _engine: Engine;
  36. private _uniformsNames: string[];
  37. private _samplers: string[];
  38. private _isReady = false;
  39. private _compilationError = "";
  40. private _attributesNames: string[];
  41. private _attributes: number[];
  42. private _uniforms: WebGLUniformLocation[];
  43. public _key: string;
  44. private _program: WebGLProgram;
  45. private _valueCache = [];
  46. constructor(baseName: any, attributesNames: string[], uniformsNames: string[], samplers: string[], engine, defines?: string, fallbacks?: EffectFallbacks, onCompiled?: (effect: Effect) => void, onError?: (effect: Effect, errors: string) => void) {
  47. this._engine = engine;
  48. this.name = baseName;
  49. this.defines = defines;
  50. this._uniformsNames = uniformsNames.concat(samplers);
  51. this._samplers = samplers;
  52. this._attributesNames = attributesNames;
  53. this.onError = onError;
  54. this.onCompiled = onCompiled;
  55. var vertexSource;
  56. var fragmentSource;
  57. if (baseName.vertexElement) {
  58. vertexSource = document.getElementById(baseName.vertexElement);
  59. if (!vertexSource) {
  60. vertexSource = baseName.vertexElement;
  61. }
  62. } else {
  63. vertexSource = baseName.vertex || baseName;
  64. }
  65. if (baseName.fragmentElement) {
  66. fragmentSource = document.getElementById(baseName.fragmentElement);
  67. if (!fragmentSource) {
  68. fragmentSource = baseName.fragmentElement;
  69. }
  70. } else {
  71. fragmentSource = baseName.fragment || baseName;
  72. }
  73. this._loadVertexShader(vertexSource, vertexCode => {
  74. this._loadFragmentShader(fragmentSource, (fragmentCode) => {
  75. this._prepareEffect(vertexCode, fragmentCode, attributesNames, defines, fallbacks);
  76. });
  77. });
  78. }
  79. // Properties
  80. public isReady(): boolean {
  81. return this._isReady;
  82. }
  83. public getProgram(): WebGLProgram {
  84. return this._program;
  85. }
  86. public getAttributesNames(): string[] {
  87. return this._attributesNames;
  88. }
  89. public getAttributeLocation(index: number): number {
  90. return this._attributes[index];
  91. }
  92. public getAttributeLocationByName(name: string): number {
  93. var index = this._attributesNames.indexOf(name);
  94. return this._attributes[index];
  95. }
  96. public getAttributesCount(): number {
  97. return this._attributes.length;
  98. }
  99. public getUniformIndex(uniformName: string): number {
  100. return this._uniformsNames.indexOf(uniformName);
  101. }
  102. public getUniform(uniformName: string): WebGLUniformLocation {
  103. return this._uniforms[this._uniformsNames.indexOf(uniformName)];
  104. }
  105. public getSamplers(): string[] {
  106. return this._samplers;
  107. }
  108. public getCompilationError(): string {
  109. return this._compilationError;
  110. }
  111. // Methods
  112. public _loadVertexShader(vertex: any, callback: (data: any) => void): void {
  113. // DOM element ?
  114. if (vertex instanceof HTMLElement) {
  115. var vertexCode = BABYLON.Tools.GetDOMTextContent(vertex);
  116. callback(vertexCode);
  117. return;
  118. }
  119. // Is in local store ?
  120. if (BABYLON.Effect.ShadersStore[vertex + "VertexShader"]) {
  121. callback(BABYLON.Effect.ShadersStore[vertex + "VertexShader"]);
  122. return;
  123. }
  124. var vertexShaderUrl;
  125. if (vertex[0] === ".") {
  126. vertexShaderUrl = vertex;
  127. } else {
  128. vertexShaderUrl = BABYLON.Engine.ShadersRepository + vertex;
  129. }
  130. // Vertex shader
  131. BABYLON.Tools.LoadFile(vertexShaderUrl + ".vertex.fx", callback);
  132. }
  133. public _loadFragmentShader(fragment: any, callback: (data: any) => void): void {
  134. // DOM element ?
  135. if (fragment instanceof HTMLElement) {
  136. var fragmentCode = BABYLON.Tools.GetDOMTextContent(fragment);
  137. callback(fragmentCode);
  138. return;
  139. }
  140. // Is in local store ?
  141. if (BABYLON.Effect.ShadersStore[fragment + "PixelShader"]) {
  142. callback(BABYLON.Effect.ShadersStore[fragment + "PixelShader"]);
  143. return;
  144. }
  145. if (BABYLON.Effect.ShadersStore[fragment + "FragmentShader"]) {
  146. callback(BABYLON.Effect.ShadersStore[fragment + "FragmentShader"]);
  147. return;
  148. }
  149. var fragmentShaderUrl;
  150. if (fragment[0] === ".") {
  151. fragmentShaderUrl = fragment;
  152. } else {
  153. fragmentShaderUrl = BABYLON.Engine.ShadersRepository + fragment;
  154. }
  155. // Fragment shader
  156. BABYLON.Tools.LoadFile(fragmentShaderUrl + ".fragment.fx", callback);
  157. }
  158. private _prepareEffect(vertexSourceCode: string, fragmentSourceCode: string, attributesNames: string[], defines: string, fallbacks?: EffectFallbacks): void {
  159. try {
  160. var engine = this._engine;
  161. this._program = engine.createShaderProgram(vertexSourceCode, fragmentSourceCode, defines);
  162. this._uniforms = engine.getUniforms(this._program, this._uniformsNames);
  163. this._attributes = engine.getAttributes(this._program, attributesNames);
  164. for (var index = 0; index < this._samplers.length; index++) {
  165. var sampler = this.getUniform(this._samplers[index]);
  166. if (sampler == null) {
  167. this._samplers.splice(index, 1);
  168. index--;
  169. }
  170. }
  171. engine.bindSamplers(this);
  172. this._isReady = true;
  173. if (this.onCompiled) {
  174. this.onCompiled(this);
  175. }
  176. } catch (e) {
  177. if (fallbacks && fallbacks.isMoreFallbacks) {
  178. defines = fallbacks.reduce(defines);
  179. this._prepareEffect(vertexSourceCode, fragmentSourceCode, attributesNames, defines, fallbacks);
  180. } else {
  181. Tools.Error("Unable to compile effect: " + this.name);
  182. Tools.Error("Defines: " + defines);
  183. Tools.Error("Error: " + e.message);
  184. this._compilationError = e.message;
  185. if (this.onError) {
  186. this.onError(this, this._compilationError);
  187. }
  188. }
  189. }
  190. }
  191. public _bindTexture(channel: string, texture: WebGLTexture): void {
  192. this._engine._bindTexture(this._samplers.indexOf(channel), texture);
  193. }
  194. public setTexture(channel: string, texture: BaseTexture): void {
  195. this._engine.setTexture(this._samplers.indexOf(channel), texture);
  196. }
  197. public setTextureFromPostProcess(channel: string, postProcess: PostProcess): void {
  198. this._engine.setTextureFromPostProcess(this._samplers.indexOf(channel), postProcess);
  199. }
  200. //public _cacheMatrix(uniformName, matrix) {
  201. // if (!this._valueCache[uniformName]) {
  202. // this._valueCache[uniformName] = new BABYLON.Matrix();
  203. // }
  204. // for (var index = 0; index < 16; index++) {
  205. // this._valueCache[uniformName].m[index] = matrix.m[index];
  206. // }
  207. //};
  208. public _cacheFloat2(uniformName: string, x: number, y: number): void {
  209. if (!this._valueCache[uniformName]) {
  210. this._valueCache[uniformName] = [x, y];
  211. return;
  212. }
  213. this._valueCache[uniformName][0] = x;
  214. this._valueCache[uniformName][1] = y;
  215. }
  216. public _cacheFloat3(uniformName: string, x: number, y: number, z: number): void {
  217. if (!this._valueCache[uniformName]) {
  218. this._valueCache[uniformName] = [x, y, z];
  219. return;
  220. }
  221. this._valueCache[uniformName][0] = x;
  222. this._valueCache[uniformName][1] = y;
  223. this._valueCache[uniformName][2] = z;
  224. }
  225. public _cacheFloat4(uniformName: string, x: number, y: number, z: number, w: number): void {
  226. if (!this._valueCache[uniformName]) {
  227. this._valueCache[uniformName] = [x, y, z, w];
  228. return;
  229. }
  230. this._valueCache[uniformName][0] = x;
  231. this._valueCache[uniformName][1] = y;
  232. this._valueCache[uniformName][2] = z;
  233. this._valueCache[uniformName][3] = w;
  234. }
  235. public setArray(uniformName: string, array: number[]): Effect {
  236. this._engine.setArray(this.getUniform(uniformName), array);
  237. return this;
  238. }
  239. public setMatrices(uniformName: string, matrices: Float32Array): Effect {
  240. this._engine.setMatrices(this.getUniform(uniformName), matrices);
  241. return this;
  242. }
  243. public setMatrix(uniformName: string, matrix: Matrix): Effect {
  244. //if (this._valueCache[uniformName] && this._valueCache[uniformName].equals(matrix))
  245. // return;
  246. //this._cacheMatrix(uniformName, matrix);
  247. this._engine.setMatrix(this.getUniform(uniformName), matrix);
  248. return this;
  249. }
  250. public setFloat(uniformName: string, value: number): Effect {
  251. if (this._valueCache[uniformName] && this._valueCache[uniformName] === value)
  252. return this;
  253. this._valueCache[uniformName] = value;
  254. this._engine.setFloat(this.getUniform(uniformName), value);
  255. return this;
  256. }
  257. public setBool(uniformName: string, bool: boolean): Effect {
  258. if (this._valueCache[uniformName] && this._valueCache[uniformName] === bool)
  259. return this;
  260. this._valueCache[uniformName] = bool;
  261. this._engine.setBool(this.getUniform(uniformName), bool ? 1 : 0);
  262. return this;
  263. }
  264. public setVector2(uniformName: string, vector2: Vector2): Effect {
  265. if (this._valueCache[uniformName] && this._valueCache[uniformName][0] == vector2.x && this._valueCache[uniformName][1] == vector2.y)
  266. return this;
  267. this._cacheFloat2(uniformName, vector2.x, vector2.y);
  268. this._engine.setFloat2(this.getUniform(uniformName), vector2.x, vector2.y);
  269. return this;
  270. }
  271. public setFloat2(uniformName: string, x: number, y: number): Effect {
  272. if (this._valueCache[uniformName] && this._valueCache[uniformName][0] == x && this._valueCache[uniformName][1] == y)
  273. return this;
  274. this._cacheFloat2(uniformName, x, y);
  275. this._engine.setFloat2(this.getUniform(uniformName), x, y);
  276. return this;
  277. }
  278. public setVector3(uniformName: string, vector3: Vector3): Effect {
  279. if (this._valueCache[uniformName] && this._valueCache[uniformName][0] == vector3.x && this._valueCache[uniformName][1] == vector3.y && this._valueCache[uniformName][2] == vector3.z)
  280. return this;
  281. this._cacheFloat3(uniformName, vector3.x, vector3.y, vector3.z);
  282. this._engine.setFloat3(this.getUniform(uniformName), vector3.x, vector3.y, vector3.z);
  283. return this;
  284. }
  285. public setFloat3(uniformName: string, x: number, y: number, z: number): Effect {
  286. if (this._valueCache[uniformName] && this._valueCache[uniformName][0] == x && this._valueCache[uniformName][1] == y && this._valueCache[uniformName][2] == z)
  287. return this;
  288. this._cacheFloat3(uniformName, x, y, z);
  289. this._engine.setFloat3(this.getUniform(uniformName), x, y, z);
  290. return this;
  291. }
  292. public setFloat4(uniformName: string, x: number, y: number, z: number, w: number): Effect {
  293. if (this._valueCache[uniformName] && this._valueCache[uniformName][0] == x && this._valueCache[uniformName][1] == y && this._valueCache[uniformName][2] == z && this._valueCache[uniformName][3] == w)
  294. return this;
  295. this._cacheFloat4(uniformName, x, y, z, w);
  296. this._engine.setFloat4(this.getUniform(uniformName), x, y, z, w);
  297. return this;
  298. }
  299. public setColor3(uniformName: string, color3: Color3): Effect {
  300. if (this._valueCache[uniformName] && this._valueCache[uniformName][0] == color3.r && this._valueCache[uniformName][1] == color3.g && this._valueCache[uniformName][2] == color3.b)
  301. return this;
  302. this._cacheFloat3(uniformName, color3.r, color3.g, color3.b);
  303. this._engine.setColor3(this.getUniform(uniformName), color3);
  304. return this;
  305. }
  306. public setColor4(uniformName: string, color3: Color3, alpha: number): Effect {
  307. if (this._valueCache[uniformName] && this._valueCache[uniformName][0] == color3.r && this._valueCache[uniformName][1] == color3.g && this._valueCache[uniformName][2] == color3.b && this._valueCache[uniformName][3] == alpha)
  308. return this;
  309. this._cacheFloat4(uniformName, color3.r, color3.g, color3.b, alpha);
  310. this._engine.setColor4(this.getUniform(uniformName), color3, alpha);
  311. return this;
  312. }
  313. // Statics
  314. public static ShadersStore = {};
  315. }
  316. }