babylon.effect.js 12 KB

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