babylon.effect.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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._processIncludes(vertexCode, function (vertexCodeWithIncludes) {
  87. _this._loadFragmentShader(fragmentSource, function (fragmentCode) {
  88. _this._processIncludes(fragmentCode, function (fragmentCodeWithIncludes) {
  89. _this._prepareEffect(vertexCodeWithIncludes, fragmentCodeWithIncludes, attributesNames, defines, fallbacks);
  90. });
  91. });
  92. });
  93. });
  94. }
  95. // Properties
  96. Effect.prototype.isReady = function () {
  97. return this._isReady;
  98. };
  99. Effect.prototype.getProgram = function () {
  100. return this._program;
  101. };
  102. Effect.prototype.getAttributesNames = function () {
  103. return this._attributesNames;
  104. };
  105. Effect.prototype.getAttributeLocation = function (index) {
  106. return this._attributes[index];
  107. };
  108. Effect.prototype.getAttributeLocationByName = function (name) {
  109. var index = this._attributesNames.indexOf(name);
  110. return this._attributes[index];
  111. };
  112. Effect.prototype.getAttributesCount = function () {
  113. return this._attributes.length;
  114. };
  115. Effect.prototype.getUniformIndex = function (uniformName) {
  116. return this._uniformsNames.indexOf(uniformName);
  117. };
  118. Effect.prototype.getUniform = function (uniformName) {
  119. return this._uniforms[this._uniformsNames.indexOf(uniformName)];
  120. };
  121. Effect.prototype.getSamplers = function () {
  122. return this._samplers;
  123. };
  124. Effect.prototype.getCompilationError = function () {
  125. return this._compilationError;
  126. };
  127. // Methods
  128. Effect.prototype._loadVertexShader = function (vertex, callback) {
  129. // DOM element ?
  130. if (vertex instanceof HTMLElement) {
  131. var vertexCode = BABYLON.Tools.GetDOMTextContent(vertex);
  132. callback(vertexCode);
  133. return;
  134. }
  135. // Is in local store ?
  136. if (Effect.ShadersStore[vertex + "VertexShader"]) {
  137. callback(Effect.ShadersStore[vertex + "VertexShader"]);
  138. return;
  139. }
  140. var vertexShaderUrl;
  141. if (vertex[0] === "." || vertex[0] === "/" || vertex.indexOf("http") > -1) {
  142. vertexShaderUrl = vertex;
  143. }
  144. else {
  145. vertexShaderUrl = BABYLON.Engine.ShadersRepository + vertex;
  146. }
  147. // Vertex shader
  148. BABYLON.Tools.LoadFile(vertexShaderUrl + ".vertex.fx", callback);
  149. };
  150. Effect.prototype._loadFragmentShader = function (fragment, callback) {
  151. // DOM element ?
  152. if (fragment instanceof HTMLElement) {
  153. var fragmentCode = BABYLON.Tools.GetDOMTextContent(fragment);
  154. callback(fragmentCode);
  155. return;
  156. }
  157. // Is in local store ?
  158. if (Effect.ShadersStore[fragment + "PixelShader"]) {
  159. callback(Effect.ShadersStore[fragment + "PixelShader"]);
  160. return;
  161. }
  162. if (Effect.ShadersStore[fragment + "FragmentShader"]) {
  163. callback(Effect.ShadersStore[fragment + "FragmentShader"]);
  164. return;
  165. }
  166. var fragmentShaderUrl;
  167. if (fragment[0] === "." || fragment[0] === "/" || fragment.indexOf("http") > -1) {
  168. fragmentShaderUrl = fragment;
  169. }
  170. else {
  171. fragmentShaderUrl = BABYLON.Engine.ShadersRepository + fragment;
  172. }
  173. // Fragment shader
  174. BABYLON.Tools.LoadFile(fragmentShaderUrl + ".fragment.fx", callback);
  175. };
  176. Effect.prototype._dumpShadersName = function () {
  177. if (this.name.vertexElement) {
  178. BABYLON.Tools.Error("Vertex shader:" + this.name.vertexElement);
  179. BABYLON.Tools.Error("Fragment shader:" + this.name.fragmentElement);
  180. }
  181. else if (this.name.vertex) {
  182. BABYLON.Tools.Error("Vertex shader:" + this.name.vertex);
  183. BABYLON.Tools.Error("Fragment shader:" + this.name.fragment);
  184. }
  185. else {
  186. BABYLON.Tools.Error("Vertex shader:" + this.name);
  187. BABYLON.Tools.Error("Fragment shader:" + this.name);
  188. }
  189. };
  190. Effect.prototype._processIncludes = function (sourceCode, callback) {
  191. var _this = this;
  192. var regex = /#include<(.+)>(\((.*)\))*(\[(.*)\])*/g;
  193. var match = regex.exec(sourceCode);
  194. var returnValue = new String(sourceCode);
  195. while (match != null) {
  196. var includeFile = match[1];
  197. if (Effect.IncludesShadersStore[includeFile]) {
  198. // Substitution
  199. var includeContent = Effect.IncludesShadersStore[includeFile];
  200. if (match[2]) {
  201. var splits = match[3].split(",");
  202. for (var index = 0; index < splits.length; index += 2) {
  203. var source = new RegExp(splits[index], "g");
  204. var dest = splits[index + 1];
  205. includeContent = includeContent.replace(source, dest);
  206. }
  207. }
  208. if (match[4]) {
  209. includeContent = includeContent.replace(/\{X\}/g, match[5]);
  210. }
  211. // Replace
  212. returnValue = returnValue.replace(match[0], includeContent);
  213. }
  214. else {
  215. var includeShaderUrl = BABYLON.Engine.ShadersRepository + "ShadersInclude/" + includeFile + ".fx";
  216. BABYLON.Tools.LoadFile(includeShaderUrl, function (fileContent) {
  217. Effect.IncludesShadersStore[includeFile] = fileContent;
  218. _this._processIncludes(returnValue, callback);
  219. });
  220. return;
  221. }
  222. match = regex.exec(sourceCode);
  223. }
  224. callback(returnValue);
  225. };
  226. Effect.prototype._processPrecision = function (source) {
  227. if (source.indexOf("precision highp float") === -1) {
  228. if (!this._engine.getCaps().highPrecisionShaderSupported) {
  229. source = "precision mediump float;\n" + source;
  230. }
  231. else {
  232. source = "precision highp float;\n" + source;
  233. }
  234. }
  235. else {
  236. if (!this._engine.getCaps().highPrecisionShaderSupported) {
  237. source = source.replace("precision highp float", "precision mediump float");
  238. }
  239. }
  240. return source;
  241. };
  242. Effect.prototype._prepareEffect = function (vertexSourceCode, fragmentSourceCode, attributesNames, defines, fallbacks) {
  243. try {
  244. var engine = this._engine;
  245. // Precision
  246. vertexSourceCode = this._processPrecision(vertexSourceCode);
  247. fragmentSourceCode = this._processPrecision(fragmentSourceCode);
  248. this._program = engine.createShaderProgram(vertexSourceCode, fragmentSourceCode, defines);
  249. this._uniforms = engine.getUniforms(this._program, this._uniformsNames);
  250. this._attributes = engine.getAttributes(this._program, attributesNames);
  251. for (var index = 0; index < this._samplers.length; index++) {
  252. var sampler = this.getUniform(this._samplers[index]);
  253. if (sampler == null) {
  254. this._samplers.splice(index, 1);
  255. index--;
  256. }
  257. }
  258. engine.bindSamplers(this);
  259. this._isReady = true;
  260. if (this.onCompiled) {
  261. this.onCompiled(this);
  262. }
  263. }
  264. catch (e) {
  265. // Is it a problem with precision?
  266. if (e.message.indexOf("highp") !== -1) {
  267. vertexSourceCode = vertexSourceCode.replace("precision highp float", "precision mediump float");
  268. fragmentSourceCode = fragmentSourceCode.replace("precision highp float", "precision mediump float");
  269. this._prepareEffect(vertexSourceCode, fragmentSourceCode, attributesNames, defines, fallbacks);
  270. return;
  271. }
  272. // Let's go through fallbacks then
  273. if (fallbacks && fallbacks.isMoreFallbacks) {
  274. BABYLON.Tools.Error("Unable to compile effect with current defines. Trying next fallback.");
  275. this._dumpShadersName();
  276. defines = fallbacks.reduce(defines);
  277. this._prepareEffect(vertexSourceCode, fragmentSourceCode, attributesNames, defines, fallbacks);
  278. }
  279. else {
  280. BABYLON.Tools.Error("Unable to compile effect: ");
  281. this._dumpShadersName();
  282. BABYLON.Tools.Error("Defines: " + defines);
  283. BABYLON.Tools.Error("Error: " + e.message);
  284. this._compilationError = e.message;
  285. if (this.onError) {
  286. this.onError(this, this._compilationError);
  287. }
  288. }
  289. }
  290. };
  291. Object.defineProperty(Effect.prototype, "isSupported", {
  292. get: function () {
  293. return this._compilationError === "";
  294. },
  295. enumerable: true,
  296. configurable: true
  297. });
  298. Effect.prototype._bindTexture = function (channel, texture) {
  299. this._engine._bindTexture(this._samplers.indexOf(channel), texture);
  300. };
  301. Effect.prototype.setTexture = function (channel, texture) {
  302. this._engine.setTexture(this._samplers.indexOf(channel), texture);
  303. };
  304. Effect.prototype.setTextureFromPostProcess = function (channel, postProcess) {
  305. this._engine.setTextureFromPostProcess(this._samplers.indexOf(channel), postProcess);
  306. };
  307. Effect.prototype._cacheMatrix = function (uniformName, matrix) {
  308. if (!this._valueCache[uniformName]) {
  309. this._valueCache[uniformName] = new BABYLON.Matrix();
  310. }
  311. for (var index = 0; index < 16; index++) {
  312. this._valueCache[uniformName].m[index] = matrix.m[index];
  313. }
  314. };
  315. Effect.prototype._cacheFloat2 = function (uniformName, x, y) {
  316. if (!this._valueCache[uniformName]) {
  317. this._valueCache[uniformName] = [x, y];
  318. return;
  319. }
  320. this._valueCache[uniformName][0] = x;
  321. this._valueCache[uniformName][1] = y;
  322. };
  323. Effect.prototype._cacheFloat3 = function (uniformName, x, y, z) {
  324. if (!this._valueCache[uniformName]) {
  325. this._valueCache[uniformName] = [x, y, z];
  326. return;
  327. }
  328. this._valueCache[uniformName][0] = x;
  329. this._valueCache[uniformName][1] = y;
  330. this._valueCache[uniformName][2] = z;
  331. };
  332. Effect.prototype._cacheFloat4 = function (uniformName, x, y, z, w) {
  333. if (!this._valueCache[uniformName]) {
  334. this._valueCache[uniformName] = [x, y, z, w];
  335. return;
  336. }
  337. this._valueCache[uniformName][0] = x;
  338. this._valueCache[uniformName][1] = y;
  339. this._valueCache[uniformName][2] = z;
  340. this._valueCache[uniformName][3] = w;
  341. };
  342. Effect.prototype.setArray = function (uniformName, array) {
  343. this._engine.setArray(this.getUniform(uniformName), array);
  344. return this;
  345. };
  346. Effect.prototype.setArray2 = function (uniformName, array) {
  347. this._engine.setArray2(this.getUniform(uniformName), array);
  348. return this;
  349. };
  350. Effect.prototype.setArray3 = function (uniformName, array) {
  351. this._engine.setArray3(this.getUniform(uniformName), array);
  352. return this;
  353. };
  354. Effect.prototype.setArray4 = function (uniformName, array) {
  355. this._engine.setArray4(this.getUniform(uniformName), array);
  356. return this;
  357. };
  358. Effect.prototype.setMatrices = function (uniformName, matrices) {
  359. this._engine.setMatrices(this.getUniform(uniformName), matrices);
  360. return this;
  361. };
  362. Effect.prototype.setMatrix = function (uniformName, matrix) {
  363. //if (this._valueCache[uniformName] && this._valueCache[uniformName].equals(matrix))
  364. // return this;
  365. // this._cacheMatrix(uniformName, matrix);
  366. this._engine.setMatrix(this.getUniform(uniformName), matrix);
  367. return this;
  368. };
  369. Effect.prototype.setMatrix3x3 = function (uniformName, matrix) {
  370. this._engine.setMatrix3x3(this.getUniform(uniformName), matrix);
  371. return this;
  372. };
  373. Effect.prototype.setMatrix2x2 = function (uniformname, matrix) {
  374. this._engine.setMatrix2x2(this.getUniform(uniformname), matrix);
  375. return this;
  376. };
  377. Effect.prototype.setFloat = function (uniformName, value) {
  378. if (this._valueCache[uniformName] && this._valueCache[uniformName] === value)
  379. return this;
  380. this._valueCache[uniformName] = value;
  381. this._engine.setFloat(this.getUniform(uniformName), value);
  382. return this;
  383. };
  384. Effect.prototype.setBool = function (uniformName, bool) {
  385. if (this._valueCache[uniformName] && this._valueCache[uniformName] === bool)
  386. return this;
  387. this._valueCache[uniformName] = bool;
  388. this._engine.setBool(this.getUniform(uniformName), bool ? 1 : 0);
  389. return this;
  390. };
  391. Effect.prototype.setVector2 = function (uniformName, vector2) {
  392. if (this._valueCache[uniformName] && this._valueCache[uniformName][0] === vector2.x && this._valueCache[uniformName][1] === vector2.y)
  393. return this;
  394. this._cacheFloat2(uniformName, vector2.x, vector2.y);
  395. this._engine.setFloat2(this.getUniform(uniformName), vector2.x, vector2.y);
  396. return this;
  397. };
  398. Effect.prototype.setFloat2 = function (uniformName, x, y) {
  399. if (this._valueCache[uniformName] && this._valueCache[uniformName][0] === x && this._valueCache[uniformName][1] === y)
  400. return this;
  401. this._cacheFloat2(uniformName, x, y);
  402. this._engine.setFloat2(this.getUniform(uniformName), x, y);
  403. return this;
  404. };
  405. Effect.prototype.setVector3 = function (uniformName, vector3) {
  406. if (this._valueCache[uniformName] && this._valueCache[uniformName][0] === vector3.x && this._valueCache[uniformName][1] === vector3.y && this._valueCache[uniformName][2] === vector3.z)
  407. return this;
  408. this._cacheFloat3(uniformName, vector3.x, vector3.y, vector3.z);
  409. this._engine.setFloat3(this.getUniform(uniformName), vector3.x, vector3.y, vector3.z);
  410. return this;
  411. };
  412. Effect.prototype.setFloat3 = function (uniformName, x, y, z) {
  413. if (this._valueCache[uniformName] && this._valueCache[uniformName][0] === x && this._valueCache[uniformName][1] === y && this._valueCache[uniformName][2] === z)
  414. return this;
  415. this._cacheFloat3(uniformName, x, y, z);
  416. this._engine.setFloat3(this.getUniform(uniformName), x, y, z);
  417. return this;
  418. };
  419. Effect.prototype.setVector4 = function (uniformName, vector4) {
  420. 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)
  421. return this;
  422. this._cacheFloat4(uniformName, vector4.x, vector4.y, vector4.z, vector4.w);
  423. this._engine.setFloat4(this.getUniform(uniformName), vector4.x, vector4.y, vector4.z, vector4.w);
  424. return this;
  425. };
  426. Effect.prototype.setFloat4 = function (uniformName, x, y, z, w) {
  427. if (this._valueCache[uniformName] && this._valueCache[uniformName][0] === x && this._valueCache[uniformName][1] === y && this._valueCache[uniformName][2] === z && this._valueCache[uniformName][3] === w)
  428. return this;
  429. this._cacheFloat4(uniformName, x, y, z, w);
  430. this._engine.setFloat4(this.getUniform(uniformName), x, y, z, w);
  431. return this;
  432. };
  433. Effect.prototype.setColor3 = function (uniformName, color3) {
  434. if (this._valueCache[uniformName] && this._valueCache[uniformName][0] === color3.r && this._valueCache[uniformName][1] === color3.g && this._valueCache[uniformName][2] === color3.b)
  435. return this;
  436. this._cacheFloat3(uniformName, color3.r, color3.g, color3.b);
  437. this._engine.setColor3(this.getUniform(uniformName), color3);
  438. return this;
  439. };
  440. Effect.prototype.setColor4 = function (uniformName, color3, alpha) {
  441. 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)
  442. return this;
  443. this._cacheFloat4(uniformName, color3.r, color3.g, color3.b, alpha);
  444. this._engine.setColor4(this.getUniform(uniformName), color3, alpha);
  445. return this;
  446. };
  447. // Statics
  448. Effect.ShadersStore = {};
  449. Effect.IncludesShadersStore = {};
  450. return Effect;
  451. })();
  452. BABYLON.Effect = Effect;
  453. })(BABYLON || (BABYLON = {}));