babylon.effect.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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] === "." || 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] === "." || 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: ");
  198. if (this.name.vertexElement) {
  199. BABYLON.Tools.Error("Vertex shader:" + this.name.vertexElement);
  200. BABYLON.Tools.Error("Fragment shader:" + this.name.fragmentElement);
  201. }
  202. else if (this.name.vertex) {
  203. BABYLON.Tools.Error("Vertex shader:" + this.name.vertex);
  204. BABYLON.Tools.Error("Fragment shader:" + this.name.fragment);
  205. }
  206. else {
  207. BABYLON.Tools.Error("Vertex shader:" + this.name);
  208. BABYLON.Tools.Error("Fragment shader:" + this.name);
  209. }
  210. BABYLON.Tools.Error("Defines: " + defines);
  211. BABYLON.Tools.Error("Error: " + e.message);
  212. this._compilationError = e.message;
  213. if (this.onError) {
  214. this.onError(this, this._compilationError);
  215. }
  216. }
  217. }
  218. };
  219. Effect.prototype._bindTexture = function (channel, texture) {
  220. this._engine._bindTexture(this._samplers.indexOf(channel), texture);
  221. };
  222. Effect.prototype.setTexture = function (channel, texture) {
  223. this._engine.setTexture(this._samplers.indexOf(channel), texture);
  224. };
  225. Effect.prototype.setTextureFromPostProcess = function (channel, postProcess) {
  226. this._engine.setTextureFromPostProcess(this._samplers.indexOf(channel), postProcess);
  227. };
  228. //public _cacheMatrix(uniformName, matrix) {
  229. // if (!this._valueCache[uniformName]) {
  230. // this._valueCache[uniformName] = new BABYLON.Matrix();
  231. // }
  232. // for (var index = 0; index < 16; index++) {
  233. // this._valueCache[uniformName].m[index] = matrix.m[index];
  234. // }
  235. //};
  236. Effect.prototype._cacheFloat2 = function (uniformName, x, y) {
  237. if (!this._valueCache[uniformName]) {
  238. this._valueCache[uniformName] = [x, y];
  239. return;
  240. }
  241. this._valueCache[uniformName][0] = x;
  242. this._valueCache[uniformName][1] = y;
  243. };
  244. Effect.prototype._cacheFloat3 = function (uniformName, x, y, z) {
  245. if (!this._valueCache[uniformName]) {
  246. this._valueCache[uniformName] = [x, y, z];
  247. return;
  248. }
  249. this._valueCache[uniformName][0] = x;
  250. this._valueCache[uniformName][1] = y;
  251. this._valueCache[uniformName][2] = z;
  252. };
  253. Effect.prototype._cacheFloat4 = function (uniformName, x, y, z, w) {
  254. if (!this._valueCache[uniformName]) {
  255. this._valueCache[uniformName] = [x, y, z, w];
  256. return;
  257. }
  258. this._valueCache[uniformName][0] = x;
  259. this._valueCache[uniformName][1] = y;
  260. this._valueCache[uniformName][2] = z;
  261. this._valueCache[uniformName][3] = w;
  262. };
  263. Effect.prototype.setArray = function (uniformName, array) {
  264. this._engine.setArray(this.getUniform(uniformName), array);
  265. return this;
  266. };
  267. Effect.prototype.setArray2 = function (uniformName, array) {
  268. this._engine.setArray2(this.getUniform(uniformName), array);
  269. return this;
  270. };
  271. Effect.prototype.setArray3 = function (uniformName, array) {
  272. this._engine.setArray3(this.getUniform(uniformName), array);
  273. return this;
  274. };
  275. Effect.prototype.setArray4 = function (uniformName, array) {
  276. this._engine.setArray4(this.getUniform(uniformName), array);
  277. return this;
  278. };
  279. Effect.prototype.setMatrices = function (uniformName, matrices) {
  280. this._engine.setMatrices(this.getUniform(uniformName), matrices);
  281. return this;
  282. };
  283. Effect.prototype.setMatrix = function (uniformName, matrix) {
  284. //if (this._valueCache[uniformName] && this._valueCache[uniformName].equals(matrix))
  285. // return;
  286. //this._cacheMatrix(uniformName, matrix);
  287. this._engine.setMatrix(this.getUniform(uniformName), matrix);
  288. return this;
  289. };
  290. Effect.prototype.setFloat = function (uniformName, value) {
  291. if (this._valueCache[uniformName] && this._valueCache[uniformName] === value)
  292. return this;
  293. this._valueCache[uniformName] = value;
  294. this._engine.setFloat(this.getUniform(uniformName), value);
  295. return this;
  296. };
  297. Effect.prototype.setBool = function (uniformName, bool) {
  298. if (this._valueCache[uniformName] && this._valueCache[uniformName] === bool)
  299. return this;
  300. this._valueCache[uniformName] = bool;
  301. this._engine.setBool(this.getUniform(uniformName), bool ? 1 : 0);
  302. return this;
  303. };
  304. Effect.prototype.setVector2 = function (uniformName, vector2) {
  305. if (this._valueCache[uniformName] && this._valueCache[uniformName][0] === vector2.x && this._valueCache[uniformName][1] === vector2.y)
  306. return this;
  307. this._cacheFloat2(uniformName, vector2.x, vector2.y);
  308. this._engine.setFloat2(this.getUniform(uniformName), vector2.x, vector2.y);
  309. return this;
  310. };
  311. Effect.prototype.setFloat2 = function (uniformName, x, y) {
  312. if (this._valueCache[uniformName] && this._valueCache[uniformName][0] === x && this._valueCache[uniformName][1] === y)
  313. return this;
  314. this._cacheFloat2(uniformName, x, y);
  315. this._engine.setFloat2(this.getUniform(uniformName), x, y);
  316. return this;
  317. };
  318. Effect.prototype.setVector3 = function (uniformName, vector3) {
  319. if (this._valueCache[uniformName] && this._valueCache[uniformName][0] === vector3.x && this._valueCache[uniformName][1] === vector3.y && this._valueCache[uniformName][2] === vector3.z)
  320. return this;
  321. this._cacheFloat3(uniformName, vector3.x, vector3.y, vector3.z);
  322. this._engine.setFloat3(this.getUniform(uniformName), vector3.x, vector3.y, vector3.z);
  323. return this;
  324. };
  325. Effect.prototype.setFloat3 = function (uniformName, x, y, z) {
  326. if (this._valueCache[uniformName] && this._valueCache[uniformName][0] === x && this._valueCache[uniformName][1] === y && this._valueCache[uniformName][2] === z)
  327. return this;
  328. this._cacheFloat3(uniformName, x, y, z);
  329. this._engine.setFloat3(this.getUniform(uniformName), x, y, z);
  330. return this;
  331. };
  332. Effect.prototype.setFloat4 = function (uniformName, x, y, z, w) {
  333. if (this._valueCache[uniformName] && this._valueCache[uniformName][0] === x && this._valueCache[uniformName][1] === y && this._valueCache[uniformName][2] === z && this._valueCache[uniformName][3] === w)
  334. return this;
  335. this._cacheFloat4(uniformName, x, y, z, w);
  336. this._engine.setFloat4(this.getUniform(uniformName), x, y, z, w);
  337. return this;
  338. };
  339. Effect.prototype.setColor3 = function (uniformName, color3) {
  340. if (this._valueCache[uniformName] && this._valueCache[uniformName][0] === color3.r && this._valueCache[uniformName][1] === color3.g && this._valueCache[uniformName][2] === color3.b)
  341. return this;
  342. this._cacheFloat3(uniformName, color3.r, color3.g, color3.b);
  343. this._engine.setColor3(this.getUniform(uniformName), color3);
  344. return this;
  345. };
  346. Effect.prototype.setColor4 = function (uniformName, color3, alpha) {
  347. 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)
  348. return this;
  349. this._cacheFloat4(uniformName, color3.r, color3.g, color3.b, alpha);
  350. this._engine.setColor4(this.getUniform(uniformName), color3, alpha);
  351. return this;
  352. };
  353. // Statics
  354. Effect.ShadersStore = {};
  355. return Effect;
  356. })();
  357. BABYLON.Effect = Effect;
  358. })(BABYLON || (BABYLON = {}));
  359. //# sourceMappingURL=babylon.effect.js.map