babylon.effect.js 19 KB

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