babylon.effect.ts 13 KB

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