babylon.effect.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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. this._program = engine.createShaderProgram(vertexSourceCode, fragmentSourceCode, defines);
  164. this._uniforms = engine.getUniforms(this._program, this._uniformsNames);
  165. this._attributes = engine.getAttributes(this._program, attributesNames);
  166. for (var index = 0; index < this._samplers.length; index++) {
  167. var sampler = this.getUniform(this._samplers[index]);
  168. if (sampler == null) {
  169. this._samplers.splice(index, 1);
  170. index--;
  171. }
  172. }
  173. engine.bindSamplers(this);
  174. this._isReady = true;
  175. if (this.onCompiled) {
  176. this.onCompiled(this);
  177. }
  178. }
  179. catch (e) {
  180. // Is it a problem with precision?
  181. if (e.message.indexOf("highp") !== -1) {
  182. vertexSourceCode = vertexSourceCode.replace("precision highp float", "precision mediump float");
  183. fragmentSourceCode = fragmentSourceCode.replace("precision highp float", "precision mediump float");
  184. this._prepareEffect(vertexSourceCode, fragmentSourceCode, attributesNames, defines, fallbacks);
  185. return;
  186. }
  187. // Let's go through fallbacks then
  188. if (fallbacks && fallbacks.isMoreFallbacks) {
  189. defines = fallbacks.reduce(defines);
  190. this._prepareEffect(vertexSourceCode, fragmentSourceCode, attributesNames, defines, fallbacks);
  191. }
  192. else {
  193. BABYLON.Tools.Error("Unable to compile effect: " + this.name);
  194. BABYLON.Tools.Error("Defines: " + defines);
  195. BABYLON.Tools.Error("Error: " + e.message);
  196. this._compilationError = e.message;
  197. if (this.onError) {
  198. this.onError(this, this._compilationError);
  199. }
  200. }
  201. }
  202. };
  203. Effect.prototype._bindTexture = function (channel, texture) {
  204. this._engine._bindTexture(this._samplers.indexOf(channel), texture);
  205. };
  206. Effect.prototype.setTexture = function (channel, texture) {
  207. this._engine.setTexture(this._samplers.indexOf(channel), texture);
  208. };
  209. Effect.prototype.setTextureFromPostProcess = function (channel, postProcess) {
  210. this._engine.setTextureFromPostProcess(this._samplers.indexOf(channel), postProcess);
  211. };
  212. //public _cacheMatrix(uniformName, matrix) {
  213. // if (!this._valueCache[uniformName]) {
  214. // this._valueCache[uniformName] = new BABYLON.Matrix();
  215. // }
  216. // for (var index = 0; index < 16; index++) {
  217. // this._valueCache[uniformName].m[index] = matrix.m[index];
  218. // }
  219. //};
  220. Effect.prototype._cacheFloat2 = function (uniformName, x, y) {
  221. if (!this._valueCache[uniformName]) {
  222. this._valueCache[uniformName] = [x, y];
  223. return;
  224. }
  225. this._valueCache[uniformName][0] = x;
  226. this._valueCache[uniformName][1] = y;
  227. };
  228. Effect.prototype._cacheFloat3 = function (uniformName, x, y, z) {
  229. if (!this._valueCache[uniformName]) {
  230. this._valueCache[uniformName] = [x, y, z];
  231. return;
  232. }
  233. this._valueCache[uniformName][0] = x;
  234. this._valueCache[uniformName][1] = y;
  235. this._valueCache[uniformName][2] = z;
  236. };
  237. Effect.prototype._cacheFloat4 = function (uniformName, x, y, z, w) {
  238. if (!this._valueCache[uniformName]) {
  239. this._valueCache[uniformName] = [x, y, z, w];
  240. return;
  241. }
  242. this._valueCache[uniformName][0] = x;
  243. this._valueCache[uniformName][1] = y;
  244. this._valueCache[uniformName][2] = z;
  245. this._valueCache[uniformName][3] = w;
  246. };
  247. Effect.prototype.setArray = function (uniformName, array) {
  248. this._engine.setArray(this.getUniform(uniformName), array);
  249. return this;
  250. };
  251. Effect.prototype.setMatrices = function (uniformName, matrices) {
  252. this._engine.setMatrices(this.getUniform(uniformName), matrices);
  253. return this;
  254. };
  255. Effect.prototype.setMatrix = function (uniformName, matrix) {
  256. //if (this._valueCache[uniformName] && this._valueCache[uniformName].equals(matrix))
  257. // return;
  258. //this._cacheMatrix(uniformName, matrix);
  259. this._engine.setMatrix(this.getUniform(uniformName), matrix);
  260. return this;
  261. };
  262. Effect.prototype.setFloat = function (uniformName, value) {
  263. if (this._valueCache[uniformName] && this._valueCache[uniformName] === value)
  264. return this;
  265. this._valueCache[uniformName] = value;
  266. this._engine.setFloat(this.getUniform(uniformName), value);
  267. return this;
  268. };
  269. Effect.prototype.setBool = function (uniformName, bool) {
  270. if (this._valueCache[uniformName] && this._valueCache[uniformName] === bool)
  271. return this;
  272. this._valueCache[uniformName] = bool;
  273. this._engine.setBool(this.getUniform(uniformName), bool ? 1 : 0);
  274. return this;
  275. };
  276. Effect.prototype.setVector2 = function (uniformName, vector2) {
  277. if (this._valueCache[uniformName] && this._valueCache[uniformName][0] === vector2.x && this._valueCache[uniformName][1] === vector2.y)
  278. return this;
  279. this._cacheFloat2(uniformName, vector2.x, vector2.y);
  280. this._engine.setFloat2(this.getUniform(uniformName), vector2.x, vector2.y);
  281. return this;
  282. };
  283. Effect.prototype.setFloat2 = function (uniformName, x, y) {
  284. if (this._valueCache[uniformName] && this._valueCache[uniformName][0] === x && this._valueCache[uniformName][1] === y)
  285. return this;
  286. this._cacheFloat2(uniformName, x, y);
  287. this._engine.setFloat2(this.getUniform(uniformName), x, y);
  288. return this;
  289. };
  290. Effect.prototype.setVector3 = function (uniformName, vector3) {
  291. if (this._valueCache[uniformName] && this._valueCache[uniformName][0] === vector3.x && this._valueCache[uniformName][1] === vector3.y && this._valueCache[uniformName][2] === vector3.z)
  292. return this;
  293. this._cacheFloat3(uniformName, vector3.x, vector3.y, vector3.z);
  294. this._engine.setFloat3(this.getUniform(uniformName), vector3.x, vector3.y, vector3.z);
  295. return this;
  296. };
  297. Effect.prototype.setFloat3 = function (uniformName, x, y, z) {
  298. if (this._valueCache[uniformName] && this._valueCache[uniformName][0] === x && this._valueCache[uniformName][1] === y && this._valueCache[uniformName][2] === z)
  299. return this;
  300. this._cacheFloat3(uniformName, x, y, z);
  301. this._engine.setFloat3(this.getUniform(uniformName), x, y, z);
  302. return this;
  303. };
  304. Effect.prototype.setFloat4 = function (uniformName, x, y, z, w) {
  305. if (this._valueCache[uniformName] && this._valueCache[uniformName][0] === x && this._valueCache[uniformName][1] === y && this._valueCache[uniformName][2] === z && this._valueCache[uniformName][3] === w)
  306. return this;
  307. this._cacheFloat4(uniformName, x, y, z, w);
  308. this._engine.setFloat4(this.getUniform(uniformName), x, y, z, w);
  309. return this;
  310. };
  311. Effect.prototype.setColor3 = function (uniformName, color3) {
  312. if (this._valueCache[uniformName] && this._valueCache[uniformName][0] === color3.r && this._valueCache[uniformName][1] === color3.g && this._valueCache[uniformName][2] === color3.b)
  313. return this;
  314. this._cacheFloat3(uniformName, color3.r, color3.g, color3.b);
  315. this._engine.setColor3(this.getUniform(uniformName), color3);
  316. return this;
  317. };
  318. Effect.prototype.setColor4 = function (uniformName, color3, alpha) {
  319. 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)
  320. return this;
  321. this._cacheFloat4(uniformName, color3.r, color3.g, color3.b, alpha);
  322. this._engine.setColor4(this.getUniform(uniformName), color3, alpha);
  323. return this;
  324. };
  325. // Statics
  326. Effect.ShadersStore = {};
  327. return Effect;
  328. })();
  329. BABYLON.Effect = Effect;
  330. })(BABYLON || (BABYLON = {}));
  331. //# sourceMappingURL=babylon.effect.js.map