babylon.effect.js 12 KB

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