babylon.effect.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. "use strict";
  2. var BABYLON = BABYLON || {};
  3. (function () {
  4. BABYLON.Effect = function (baseName, attributesNames, uniformsNames, samplers, engine, defines, optionalDefines) {
  5. this._engine = engine;
  6. this.name = baseName;
  7. this.defines = defines;
  8. this._uniformsNames = uniformsNames.concat(samplers);
  9. this._samplers = samplers;
  10. this._isReady = false;
  11. this._compilationError = "";
  12. this._attributesNames = attributesNames;
  13. var vertex = baseName.vertex || baseName;
  14. var fragment = baseName.fragment || baseName;
  15. var that = this;
  16. this._loadVertexShader(vertex, function (vertexCode) {
  17. that._loadFragmentShader(fragment, function (fragmentCode) {
  18. that._prepareEffect(vertexCode, fragmentCode, attributesNames, defines, optionalDefines);
  19. });
  20. });
  21. // Cache
  22. this._valueCache = [];
  23. };
  24. // Properties
  25. BABYLON.Effect.prototype.isReady = function () {
  26. return this._isReady;
  27. };
  28. BABYLON.Effect.prototype.getProgram = function () {
  29. return this._program;
  30. };
  31. BABYLON.Effect.prototype.getAttributesNames = function () {
  32. return this._attributesNames;
  33. };
  34. BABYLON.Effect.prototype.getAttribute = function (index) {
  35. return this._attributes[index];
  36. };
  37. BABYLON.Effect.prototype.getAttributesCount = function () {
  38. return this._attributes.length;
  39. };
  40. BABYLON.Effect.prototype.getUniformIndex = function (uniformName) {
  41. return this._uniformsNames.indexOf(uniformName);
  42. };
  43. BABYLON.Effect.prototype.getUniform = function (uniformName) {
  44. return this._uniforms[this._uniformsNames.indexOf(uniformName)];
  45. };
  46. BABYLON.Effect.prototype.getSamplers = function () {
  47. return this._samplers;
  48. };
  49. BABYLON.Effect.prototype.getCompilationError = function () {
  50. return this._compilationError;
  51. };
  52. // Methods
  53. BABYLON.Effect.prototype._loadVertexShader = function (vertex, callback) {
  54. // Is in local store ?
  55. if (BABYLON.Effect.ShadersStore[vertex + "VertexShader"]) {
  56. callback(BABYLON.Effect.ShadersStore[vertex + "VertexShader"]);
  57. return;
  58. }
  59. var vertexShaderUrl;
  60. if (vertex[0] === ".") {
  61. vertexShaderUrl = vertex;
  62. } else {
  63. vertexShaderUrl = BABYLON.Engine.ShadersRepository + vertex;
  64. }
  65. // Vertex shader
  66. BABYLON.Tools.LoadFile(vertexShaderUrl + ".vertex.fx", callback);
  67. };
  68. BABYLON.Effect.prototype._loadFragmentShader = function (fragment, callback) {
  69. // Is in local store ?
  70. if (BABYLON.Effect.ShadersStore[fragment + "PixelShader"]) {
  71. callback(BABYLON.Effect.ShadersStore[fragment + "PixelShader"]);
  72. return;
  73. }
  74. var fragmentShaderUrl;
  75. if (fragment[0] === ".") {
  76. fragmentShaderUrl = fragment;
  77. } else {
  78. fragmentShaderUrl = BABYLON.Engine.ShadersRepository + fragment;
  79. }
  80. // Fragment shader
  81. BABYLON.Tools.LoadFile(fragmentShaderUrl + ".fragment.fx", callback);
  82. };
  83. BABYLON.Effect.prototype._prepareEffect = function (vertexSourceCode, fragmentSourceCode, attributesNames, defines, optionalDefines, useFallback) {
  84. try {
  85. var engine = this._engine;
  86. this._program = engine.createShaderProgram(vertexSourceCode, fragmentSourceCode, defines);
  87. this._uniforms = engine.getUniforms(this._program, this._uniformsNames);
  88. this._attributes = engine.getAttributes(this._program, attributesNames);
  89. for (var index = 0; index < this._samplers.length; index++) {
  90. var sampler = this.getUniform(this._samplers[index]);
  91. if (sampler == null) {
  92. this._samplers.splice(index, 1);
  93. index--;
  94. }
  95. }
  96. engine.bindSamplers(this);
  97. this._isReady = true;
  98. } catch (e) {
  99. if (!useFallback && optionalDefines) {
  100. for (var index = 0; index < optionalDefines.length; index++) {
  101. defines = defines.replace(optionalDefines[index], "");
  102. }
  103. this._prepareEffect(vertexSourceCode, fragmentSourceCode, attributesNames, defines, optionalDefines, true);
  104. } else {
  105. console.error("Unable to compile effect: " + this.name);
  106. console.error("Defines: " + defines);
  107. console.error("Optional defines: " + optionalDefines);
  108. this._compilationError = e.message;
  109. }
  110. }
  111. };
  112. BABYLON.Effect.prototype._bindTexture = function (channel, texture) {
  113. this._engine._bindTexture(this._samplers.indexOf(channel), texture);
  114. };
  115. BABYLON.Effect.prototype.setTexture = function (channel, texture) {
  116. this._engine.setTexture(this._samplers.indexOf(channel), texture);
  117. };
  118. BABYLON.Effect.prototype.setTextureFromPostProcess = function (channel, postProcess) {
  119. this._engine.setTextureFromPostProcess(this._samplers.indexOf(channel), postProcess);
  120. };
  121. //BABYLON.Effect.prototype._cacheMatrix = function (uniformName, matrix) {
  122. // if (!this._valueCache[uniformName]) {
  123. // this._valueCache[uniformName] = new BABYLON.Matrix();
  124. // }
  125. // for (var index = 0; index < 16; index++) {
  126. // this._valueCache[uniformName].m[index] = matrix.m[index];
  127. // }
  128. //};
  129. BABYLON.Effect.prototype._cacheFloat2 = function (uniformName, x, y) {
  130. if (!this._valueCache[uniformName]) {
  131. this._valueCache[uniformName] = [x, y];
  132. return;
  133. }
  134. this._valueCache[uniformName][0] = x;
  135. this._valueCache[uniformName][1] = y;
  136. };
  137. BABYLON.Effect.prototype._cacheFloat3 = function (uniformName, x, y, z) {
  138. if (!this._valueCache[uniformName]) {
  139. this._valueCache[uniformName] = [x, y, z];
  140. return;
  141. }
  142. this._valueCache[uniformName][0] = x;
  143. this._valueCache[uniformName][1] = y;
  144. this._valueCache[uniformName][2] = z;
  145. };
  146. BABYLON.Effect.prototype._cacheFloat4 = function (uniformName, x, y, z, w) {
  147. if (!this._valueCache[uniformName]) {
  148. this._valueCache[uniformName] = [x, y, z, w];
  149. return;
  150. }
  151. this._valueCache[uniformName][0] = x;
  152. this._valueCache[uniformName][1] = y;
  153. this._valueCache[uniformName][2] = z;
  154. this._valueCache[uniformName][3] = w;
  155. };
  156. BABYLON.Effect.prototype.setArray = function (uniformName, array) {
  157. this._engine.setArray(this.getUniform(uniformName), array);
  158. };
  159. BABYLON.Effect.prototype.setMatrices = function (uniformName, matrices) {
  160. this._engine.setMatrices(this.getUniform(uniformName), matrices);
  161. };
  162. BABYLON.Effect.prototype.setMatrix = function (uniformName, matrix) {
  163. //if (this._valueCache[uniformName] && this._valueCache[uniformName].equals(matrix))
  164. // return;
  165. //this._cacheMatrix(uniformName, matrix);
  166. this._engine.setMatrix(this.getUniform(uniformName), matrix);
  167. };
  168. BABYLON.Effect.prototype.setFloat = function (uniformName, value) {
  169. if (this._valueCache[uniformName] && this._valueCache[uniformName] === value)
  170. return;
  171. this._valueCache[uniformName] = value;
  172. this._engine.setFloat(this.getUniform(uniformName), value);
  173. };
  174. BABYLON.Effect.prototype.setBool = function (uniformName, bool) {
  175. if (this._valueCache[uniformName] && this._valueCache[uniformName] === bool)
  176. return;
  177. this._valueCache[uniformName] = bool;
  178. this._engine.setBool(this.getUniform(uniformName), bool);
  179. };
  180. BABYLON.Effect.prototype.setVector2 = function (uniformName, vector2) {
  181. if (this._valueCache[uniformName] && this._valueCache[uniformName][0] == vector2.x && this._valueCache[uniformName][1] == vector2.y)
  182. return;
  183. this._cacheFloat2(uniformName, vector2.x, vector2.y);
  184. this._engine.setFloat2(this.getUniform(uniformName), vector2.x, vector2.y);
  185. };
  186. BABYLON.Effect.prototype.setFloat2 = function (uniformName, x, y) {
  187. if (this._valueCache[uniformName] && this._valueCache[uniformName][0] == x && this._valueCache[uniformName][1] == y)
  188. return;
  189. this._cacheFloat2(uniformName, x, y);
  190. this._engine.setFloat2(this.getUniform(uniformName), x, y);
  191. };
  192. BABYLON.Effect.prototype.setVector3 = function (uniformName, vector3) {
  193. if (this._valueCache[uniformName] && this._valueCache[uniformName][0] == vector3.x && this._valueCache[uniformName][1] == vector3.y && this._valueCache[uniformName][2] == vector3.z)
  194. return;
  195. this._cacheFloat3(uniformName, vector3.x, vector3.y, vector3.z);
  196. this._engine.setFloat3(this.getUniform(uniformName), vector3.x, vector3.y, vector3.z);
  197. };
  198. BABYLON.Effect.prototype.setFloat3 = function (uniformName, x, y, z) {
  199. if (this._valueCache[uniformName] && this._valueCache[uniformName][0] == x && this._valueCache[uniformName][1] == y && this._valueCache[uniformName][2] == z)
  200. return;
  201. this._cacheFloat3(uniformName, x, y, z);
  202. this._engine.setFloat3(this.getUniform(uniformName), x, y, z);
  203. };
  204. BABYLON.Effect.prototype.setFloat4 = function (uniformName, x, y, z, w) {
  205. if (this._valueCache[uniformName] && this._valueCache[uniformName][0] == x && this._valueCache[uniformName][1] == y && this._valueCache[uniformName][2] == z && this._valueCache[uniformName][3] == w)
  206. return;
  207. this._cacheFloat4(uniformName, x, y, z, w);
  208. this._engine.setFloat4(this.getUniform(uniformName), x, y, z, w);
  209. };
  210. BABYLON.Effect.prototype.setColor3 = function (uniformName, color3) {
  211. if (this._valueCache[uniformName] && this._valueCache[uniformName][0] == color3.r && this._valueCache[uniformName][1] == color3.g && this._valueCache[uniformName][2] == color3.b)
  212. return;
  213. this._cacheFloat3(uniformName, color3.r, color3.g, color3.b);
  214. this._engine.setColor3(this.getUniform(uniformName), color3);
  215. };
  216. BABYLON.Effect.prototype.setColor4 = function (uniformName, color3, alpha) {
  217. 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)
  218. return;
  219. this._cacheFloat4(uniformName, color3.r, color3.g, color3.b, alpha);
  220. this._engine.setColor4(this.getUniform(uniformName), color3, alpha);
  221. };
  222. // Statics
  223. BABYLON.Effect.ShadersStore = {};
  224. })();