babylon.effect.js 16 KB

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