babylon.effect.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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. } else {
  61. vertexSource = baseName.vertex || baseName;
  62. }
  63. if (baseName.fragmentElement) {
  64. fragmentSource = document.getElementById(baseName.fragmentElement);
  65. if (!fragmentSource) {
  66. fragmentSource = baseName.fragmentElement;
  67. }
  68. } else {
  69. fragmentSource = baseName.fragment || baseName;
  70. }
  71. this._loadVertexShader(vertexSource, function (vertexCode) {
  72. _this._loadFragmentShader(fragmentSource, function (fragmentCode) {
  73. _this._prepareEffect(vertexCode, fragmentCode, attributesNames, defines, fallbacks);
  74. });
  75. });
  76. }
  77. // Properties
  78. Effect.prototype.isReady = function () {
  79. return this._isReady;
  80. };
  81. Effect.prototype.getProgram = function () {
  82. return this._program;
  83. };
  84. Effect.prototype.getAttributesNames = function () {
  85. return this._attributesNames;
  86. };
  87. Effect.prototype.getAttributeLocation = function (index) {
  88. return this._attributes[index];
  89. };
  90. Effect.prototype.getAttributeLocationByName = function (name) {
  91. var index = this._attributesNames.indexOf(name);
  92. return this._attributes[index];
  93. };
  94. Effect.prototype.getAttributesCount = function () {
  95. return this._attributes.length;
  96. };
  97. Effect.prototype.getUniformIndex = function (uniformName) {
  98. return this._uniformsNames.indexOf(uniformName);
  99. };
  100. Effect.prototype.getUniform = function (uniformName) {
  101. return this._uniforms[this._uniformsNames.indexOf(uniformName)];
  102. };
  103. Effect.prototype.getSamplers = function () {
  104. return this._samplers;
  105. };
  106. Effect.prototype.getCompilationError = function () {
  107. return this._compilationError;
  108. };
  109. // Methods
  110. Effect.prototype._loadVertexShader = function (vertex, callback) {
  111. // DOM element ?
  112. if (vertex instanceof HTMLElement) {
  113. var vertexCode = BABYLON.Tools.GetDOMTextContent(vertex);
  114. callback(vertexCode);
  115. return;
  116. }
  117. // Is in local store ?
  118. if (BABYLON.Effect.ShadersStore[vertex + "VertexShader"]) {
  119. callback(BABYLON.Effect.ShadersStore[vertex + "VertexShader"]);
  120. return;
  121. }
  122. var vertexShaderUrl;
  123. if (vertex[0] === ".") {
  124. vertexShaderUrl = vertex;
  125. } else {
  126. vertexShaderUrl = BABYLON.Engine.ShadersRepository + vertex;
  127. }
  128. // Vertex shader
  129. BABYLON.Tools.LoadFile(vertexShaderUrl + ".vertex.fx", callback);
  130. };
  131. Effect.prototype._loadFragmentShader = function (fragment, callback) {
  132. // DOM element ?
  133. if (fragment instanceof HTMLElement) {
  134. var fragmentCode = BABYLON.Tools.GetDOMTextContent(fragment);
  135. callback(fragmentCode);
  136. return;
  137. }
  138. // Is in local store ?
  139. if (BABYLON.Effect.ShadersStore[fragment + "PixelShader"]) {
  140. callback(BABYLON.Effect.ShadersStore[fragment + "PixelShader"]);
  141. return;
  142. }
  143. if (BABYLON.Effect.ShadersStore[fragment + "FragmentShader"]) {
  144. callback(BABYLON.Effect.ShadersStore[fragment + "FragmentShader"]);
  145. return;
  146. }
  147. var fragmentShaderUrl;
  148. if (fragment[0] === ".") {
  149. fragmentShaderUrl = fragment;
  150. } else {
  151. fragmentShaderUrl = BABYLON.Engine.ShadersRepository + fragment;
  152. }
  153. // Fragment shader
  154. BABYLON.Tools.LoadFile(fragmentShaderUrl + ".fragment.fx", callback);
  155. };
  156. Effect.prototype._prepareEffect = function (vertexSourceCode, fragmentSourceCode, attributesNames, defines, fallbacks) {
  157. try {
  158. var engine = this._engine;
  159. this._program = engine.createShaderProgram(vertexSourceCode, fragmentSourceCode, defines);
  160. this._uniforms = engine.getUniforms(this._program, this._uniformsNames);
  161. this._attributes = engine.getAttributes(this._program, attributesNames);
  162. for (var index = 0; index < this._samplers.length; index++) {
  163. var sampler = this.getUniform(this._samplers[index]);
  164. if (sampler == null) {
  165. this._samplers.splice(index, 1);
  166. index--;
  167. }
  168. }
  169. engine.bindSamplers(this);
  170. this._isReady = true;
  171. if (this.onCompiled) {
  172. this.onCompiled(this);
  173. }
  174. } catch (e) {
  175. // Is it a problem with precision?
  176. if (e.message.indexOf("highp") !== -1) {
  177. vertexSourceCode = vertexSourceCode.replace("precision highp float", "precision mediump float");
  178. fragmentSourceCode = fragmentSourceCode.replace("precision highp float", "precision mediump float");
  179. this._prepareEffect(vertexSourceCode, fragmentSourceCode, attributesNames, defines, fallbacks);
  180. return;
  181. }
  182. // Let's go through fallbacks then
  183. if (fallbacks && fallbacks.isMoreFallbacks) {
  184. defines = fallbacks.reduce(defines);
  185. this._prepareEffect(vertexSourceCode, fragmentSourceCode, attributesNames, defines, fallbacks);
  186. } else {
  187. BABYLON.Tools.Error("Unable to compile effect: " + this.name);
  188. BABYLON.Tools.Error("Defines: " + defines);
  189. BABYLON.Tools.Error("Error: " + e.message);
  190. this._compilationError = e.message;
  191. if (this.onError) {
  192. this.onError(this, this._compilationError);
  193. }
  194. }
  195. }
  196. };
  197. Effect.prototype._bindTexture = function (channel, texture) {
  198. this._engine._bindTexture(this._samplers.indexOf(channel), texture);
  199. };
  200. Effect.prototype.setTexture = function (channel, texture) {
  201. this._engine.setTexture(this._samplers.indexOf(channel), texture);
  202. };
  203. Effect.prototype.setTextureFromPostProcess = function (channel, postProcess) {
  204. this._engine.setTextureFromPostProcess(this._samplers.indexOf(channel), postProcess);
  205. };
  206. //public _cacheMatrix(uniformName, matrix) {
  207. // if (!this._valueCache[uniformName]) {
  208. // this._valueCache[uniformName] = new BABYLON.Matrix();
  209. // }
  210. // for (var index = 0; index < 16; index++) {
  211. // this._valueCache[uniformName].m[index] = matrix.m[index];
  212. // }
  213. //};
  214. Effect.prototype._cacheFloat2 = function (uniformName, x, y) {
  215. if (!this._valueCache[uniformName]) {
  216. this._valueCache[uniformName] = [x, y];
  217. return;
  218. }
  219. this._valueCache[uniformName][0] = x;
  220. this._valueCache[uniformName][1] = y;
  221. };
  222. Effect.prototype._cacheFloat3 = function (uniformName, x, y, z) {
  223. if (!this._valueCache[uniformName]) {
  224. this._valueCache[uniformName] = [x, y, z];
  225. return;
  226. }
  227. this._valueCache[uniformName][0] = x;
  228. this._valueCache[uniformName][1] = y;
  229. this._valueCache[uniformName][2] = z;
  230. };
  231. Effect.prototype._cacheFloat4 = function (uniformName, x, y, z, w) {
  232. if (!this._valueCache[uniformName]) {
  233. this._valueCache[uniformName] = [x, y, z, w];
  234. return;
  235. }
  236. this._valueCache[uniformName][0] = x;
  237. this._valueCache[uniformName][1] = y;
  238. this._valueCache[uniformName][2] = z;
  239. this._valueCache[uniformName][3] = w;
  240. };
  241. Effect.prototype.setArray = function (uniformName, array) {
  242. this._engine.setArray(this.getUniform(uniformName), array);
  243. return this;
  244. };
  245. Effect.prototype.setMatrices = function (uniformName, matrices) {
  246. this._engine.setMatrices(this.getUniform(uniformName), matrices);
  247. return this;
  248. };
  249. Effect.prototype.setMatrix = function (uniformName, matrix) {
  250. //if (this._valueCache[uniformName] && this._valueCache[uniformName].equals(matrix))
  251. // return;
  252. //this._cacheMatrix(uniformName, matrix);
  253. this._engine.setMatrix(this.getUniform(uniformName), matrix);
  254. return this;
  255. };
  256. Effect.prototype.setFloat = function (uniformName, value) {
  257. if (this._valueCache[uniformName] && this._valueCache[uniformName] === value)
  258. return this;
  259. this._valueCache[uniformName] = value;
  260. this._engine.setFloat(this.getUniform(uniformName), value);
  261. return this;
  262. };
  263. Effect.prototype.setBool = function (uniformName, bool) {
  264. if (this._valueCache[uniformName] && this._valueCache[uniformName] === bool)
  265. return this;
  266. this._valueCache[uniformName] = bool;
  267. this._engine.setBool(this.getUniform(uniformName), bool ? 1 : 0);
  268. return this;
  269. };
  270. Effect.prototype.setVector2 = function (uniformName, vector2) {
  271. if (this._valueCache[uniformName] && this._valueCache[uniformName][0] == vector2.x && this._valueCache[uniformName][1] == vector2.y)
  272. return this;
  273. this._cacheFloat2(uniformName, vector2.x, vector2.y);
  274. this._engine.setFloat2(this.getUniform(uniformName), vector2.x, vector2.y);
  275. return this;
  276. };
  277. Effect.prototype.setFloat2 = function (uniformName, x, y) {
  278. if (this._valueCache[uniformName] && this._valueCache[uniformName][0] == x && this._valueCache[uniformName][1] == y)
  279. return this;
  280. this._cacheFloat2(uniformName, x, y);
  281. this._engine.setFloat2(this.getUniform(uniformName), x, y);
  282. return this;
  283. };
  284. Effect.prototype.setVector3 = function (uniformName, vector3) {
  285. if (this._valueCache[uniformName] && this._valueCache[uniformName][0] == vector3.x && this._valueCache[uniformName][1] == vector3.y && this._valueCache[uniformName][2] == vector3.z)
  286. return this;
  287. this._cacheFloat3(uniformName, vector3.x, vector3.y, vector3.z);
  288. this._engine.setFloat3(this.getUniform(uniformName), vector3.x, vector3.y, vector3.z);
  289. return this;
  290. };
  291. Effect.prototype.setFloat3 = function (uniformName, x, y, z) {
  292. if (this._valueCache[uniformName] && this._valueCache[uniformName][0] == x && this._valueCache[uniformName][1] == y && this._valueCache[uniformName][2] == z)
  293. return this;
  294. this._cacheFloat3(uniformName, x, y, z);
  295. this._engine.setFloat3(this.getUniform(uniformName), x, y, z);
  296. return this;
  297. };
  298. Effect.prototype.setFloat4 = function (uniformName, x, y, z, w) {
  299. if (this._valueCache[uniformName] && this._valueCache[uniformName][0] == x && this._valueCache[uniformName][1] == y && this._valueCache[uniformName][2] == z && this._valueCache[uniformName][3] == w)
  300. return this;
  301. this._cacheFloat4(uniformName, x, y, z, w);
  302. this._engine.setFloat4(this.getUniform(uniformName), x, y, z, w);
  303. return this;
  304. };
  305. Effect.prototype.setColor3 = function (uniformName, color3) {
  306. if (this._valueCache[uniformName] && this._valueCache[uniformName][0] == color3.r && this._valueCache[uniformName][1] == color3.g && this._valueCache[uniformName][2] == color3.b)
  307. return this;
  308. this._cacheFloat3(uniformName, color3.r, color3.g, color3.b);
  309. this._engine.setColor3(this.getUniform(uniformName), color3);
  310. return this;
  311. };
  312. Effect.prototype.setColor4 = function (uniformName, color3, alpha) {
  313. 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)
  314. return this;
  315. this._cacheFloat4(uniformName, color3.r, color3.g, color3.b, alpha);
  316. this._engine.setColor4(this.getUniform(uniformName), color3, alpha);
  317. return this;
  318. };
  319. Effect.ShadersStore = {};
  320. return Effect;
  321. })();
  322. BABYLON.Effect = Effect;
  323. })(BABYLON || (BABYLON = {}));
  324. //# sourceMappingURL=babylon.effect.js.map