babylon.effect.js 15 KB

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