babylon.effect.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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._prepareEffect = function (vertexSourceCode, fragmentSourceCode, attributesNames, defines, fallbacks) {
  227. try {
  228. var engine = this._engine;
  229. // Precision
  230. if (!engine.getCaps().highPrecisionShaderSupported) {
  231. vertexSourceCode = vertexSourceCode.replace("precision highp float", "precision mediump float");
  232. fragmentSourceCode = fragmentSourceCode.replace("precision highp float", "precision mediump float");
  233. }
  234. this._program = engine.createShaderProgram(vertexSourceCode, fragmentSourceCode, defines);
  235. this._uniforms = engine.getUniforms(this._program, this._uniformsNames);
  236. this._attributes = engine.getAttributes(this._program, attributesNames);
  237. for (var index = 0; index < this._samplers.length; index++) {
  238. var sampler = this.getUniform(this._samplers[index]);
  239. if (sampler == null) {
  240. this._samplers.splice(index, 1);
  241. index--;
  242. }
  243. }
  244. engine.bindSamplers(this);
  245. this._isReady = true;
  246. if (this.onCompiled) {
  247. this.onCompiled(this);
  248. }
  249. }
  250. catch (e) {
  251. // Is it a problem with precision?
  252. if (e.message.indexOf("highp") !== -1) {
  253. vertexSourceCode = vertexSourceCode.replace("precision highp float", "precision mediump float");
  254. fragmentSourceCode = fragmentSourceCode.replace("precision highp float", "precision mediump float");
  255. this._prepareEffect(vertexSourceCode, fragmentSourceCode, attributesNames, defines, fallbacks);
  256. return;
  257. }
  258. // Let's go through fallbacks then
  259. if (fallbacks && fallbacks.isMoreFallbacks) {
  260. BABYLON.Tools.Error("Unable to compile effect with current defines. Trying next fallback.");
  261. this._dumpShadersName();
  262. defines = fallbacks.reduce(defines);
  263. this._prepareEffect(vertexSourceCode, fragmentSourceCode, attributesNames, defines, fallbacks);
  264. }
  265. else {
  266. BABYLON.Tools.Error("Unable to compile effect: ");
  267. this._dumpShadersName();
  268. BABYLON.Tools.Error("Defines: " + defines);
  269. BABYLON.Tools.Error("Error: " + e.message);
  270. this._compilationError = e.message;
  271. if (this.onError) {
  272. this.onError(this, this._compilationError);
  273. }
  274. }
  275. }
  276. };
  277. Object.defineProperty(Effect.prototype, "isSupported", {
  278. get: function () {
  279. return this._compilationError === "";
  280. },
  281. enumerable: true,
  282. configurable: true
  283. });
  284. Effect.prototype._bindTexture = function (channel, texture) {
  285. this._engine._bindTexture(this._samplers.indexOf(channel), texture);
  286. };
  287. Effect.prototype.setTexture = function (channel, texture) {
  288. this._engine.setTexture(this._samplers.indexOf(channel), texture);
  289. };
  290. Effect.prototype.setTextureFromPostProcess = function (channel, postProcess) {
  291. this._engine.setTextureFromPostProcess(this._samplers.indexOf(channel), postProcess);
  292. };
  293. Effect.prototype._cacheMatrix = function (uniformName, matrix) {
  294. if (!this._valueCache[uniformName]) {
  295. this._valueCache[uniformName] = new BABYLON.Matrix();
  296. }
  297. for (var index = 0; index < 16; index++) {
  298. this._valueCache[uniformName].m[index] = matrix.m[index];
  299. }
  300. };
  301. Effect.prototype._cacheFloat2 = function (uniformName, x, y) {
  302. if (!this._valueCache[uniformName]) {
  303. this._valueCache[uniformName] = [x, y];
  304. return;
  305. }
  306. this._valueCache[uniformName][0] = x;
  307. this._valueCache[uniformName][1] = y;
  308. };
  309. Effect.prototype._cacheFloat3 = function (uniformName, x, y, z) {
  310. if (!this._valueCache[uniformName]) {
  311. this._valueCache[uniformName] = [x, y, z];
  312. return;
  313. }
  314. this._valueCache[uniformName][0] = x;
  315. this._valueCache[uniformName][1] = y;
  316. this._valueCache[uniformName][2] = z;
  317. };
  318. Effect.prototype._cacheFloat4 = function (uniformName, x, y, z, w) {
  319. if (!this._valueCache[uniformName]) {
  320. this._valueCache[uniformName] = [x, y, z, w];
  321. return;
  322. }
  323. this._valueCache[uniformName][0] = x;
  324. this._valueCache[uniformName][1] = y;
  325. this._valueCache[uniformName][2] = z;
  326. this._valueCache[uniformName][3] = w;
  327. };
  328. Effect.prototype.setArray = function (uniformName, array) {
  329. this._engine.setArray(this.getUniform(uniformName), array);
  330. return this;
  331. };
  332. Effect.prototype.setArray2 = function (uniformName, array) {
  333. this._engine.setArray2(this.getUniform(uniformName), array);
  334. return this;
  335. };
  336. Effect.prototype.setArray3 = function (uniformName, array) {
  337. this._engine.setArray3(this.getUniform(uniformName), array);
  338. return this;
  339. };
  340. Effect.prototype.setArray4 = function (uniformName, array) {
  341. this._engine.setArray4(this.getUniform(uniformName), array);
  342. return this;
  343. };
  344. Effect.prototype.setMatrices = function (uniformName, matrices) {
  345. this._engine.setMatrices(this.getUniform(uniformName), matrices);
  346. return this;
  347. };
  348. Effect.prototype.setMatrix = function (uniformName, matrix) {
  349. //if (this._valueCache[uniformName] && this._valueCache[uniformName].equals(matrix))
  350. // return this;
  351. // this._cacheMatrix(uniformName, matrix);
  352. this._engine.setMatrix(this.getUniform(uniformName), matrix);
  353. return this;
  354. };
  355. Effect.prototype.setMatrix3x3 = function (uniformName, matrix) {
  356. this._engine.setMatrix3x3(this.getUniform(uniformName), matrix);
  357. return this;
  358. };
  359. Effect.prototype.setMatrix2x2 = function (uniformname, matrix) {
  360. this._engine.setMatrix2x2(this.getUniform(uniformname), matrix);
  361. return this;
  362. };
  363. Effect.prototype.setFloat = function (uniformName, value) {
  364. if (this._valueCache[uniformName] && this._valueCache[uniformName] === value)
  365. return this;
  366. this._valueCache[uniformName] = value;
  367. this._engine.setFloat(this.getUniform(uniformName), value);
  368. return this;
  369. };
  370. Effect.prototype.setBool = function (uniformName, bool) {
  371. if (this._valueCache[uniformName] && this._valueCache[uniformName] === bool)
  372. return this;
  373. this._valueCache[uniformName] = bool;
  374. this._engine.setBool(this.getUniform(uniformName), bool ? 1 : 0);
  375. return this;
  376. };
  377. Effect.prototype.setVector2 = function (uniformName, vector2) {
  378. if (this._valueCache[uniformName] && this._valueCache[uniformName][0] === vector2.x && this._valueCache[uniformName][1] === vector2.y)
  379. return this;
  380. this._cacheFloat2(uniformName, vector2.x, vector2.y);
  381. this._engine.setFloat2(this.getUniform(uniformName), vector2.x, vector2.y);
  382. return this;
  383. };
  384. Effect.prototype.setFloat2 = function (uniformName, x, y) {
  385. if (this._valueCache[uniformName] && this._valueCache[uniformName][0] === x && this._valueCache[uniformName][1] === y)
  386. return this;
  387. this._cacheFloat2(uniformName, x, y);
  388. this._engine.setFloat2(this.getUniform(uniformName), x, y);
  389. return this;
  390. };
  391. Effect.prototype.setVector3 = function (uniformName, vector3) {
  392. if (this._valueCache[uniformName] && this._valueCache[uniformName][0] === vector3.x && this._valueCache[uniformName][1] === vector3.y && this._valueCache[uniformName][2] === vector3.z)
  393. return this;
  394. this._cacheFloat3(uniformName, vector3.x, vector3.y, vector3.z);
  395. this._engine.setFloat3(this.getUniform(uniformName), vector3.x, vector3.y, vector3.z);
  396. return this;
  397. };
  398. Effect.prototype.setFloat3 = function (uniformName, x, y, z) {
  399. if (this._valueCache[uniformName] && this._valueCache[uniformName][0] === x && this._valueCache[uniformName][1] === y && this._valueCache[uniformName][2] === z)
  400. return this;
  401. this._cacheFloat3(uniformName, x, y, z);
  402. this._engine.setFloat3(this.getUniform(uniformName), x, y, z);
  403. return this;
  404. };
  405. Effect.prototype.setVector4 = function (uniformName, vector4) {
  406. 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)
  407. return this;
  408. this._cacheFloat4(uniformName, vector4.x, vector4.y, vector4.z, vector4.w);
  409. this._engine.setFloat4(this.getUniform(uniformName), vector4.x, vector4.y, vector4.z, vector4.w);
  410. return this;
  411. };
  412. Effect.prototype.setFloat4 = function (uniformName, x, y, z, w) {
  413. if (this._valueCache[uniformName] && this._valueCache[uniformName][0] === x && this._valueCache[uniformName][1] === y && this._valueCache[uniformName][2] === z && this._valueCache[uniformName][3] === w)
  414. return this;
  415. this._cacheFloat4(uniformName, x, y, z, w);
  416. this._engine.setFloat4(this.getUniform(uniformName), x, y, z, w);
  417. return this;
  418. };
  419. Effect.prototype.setColor3 = function (uniformName, color3) {
  420. if (this._valueCache[uniformName] && this._valueCache[uniformName][0] === color3.r && this._valueCache[uniformName][1] === color3.g && this._valueCache[uniformName][2] === color3.b)
  421. return this;
  422. this._cacheFloat3(uniformName, color3.r, color3.g, color3.b);
  423. this._engine.setColor3(this.getUniform(uniformName), color3);
  424. return this;
  425. };
  426. Effect.prototype.setColor4 = function (uniformName, color3, alpha) {
  427. 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)
  428. return this;
  429. this._cacheFloat4(uniformName, color3.r, color3.g, color3.b, alpha);
  430. this._engine.setColor4(this.getUniform(uniformName), color3, alpha);
  431. return this;
  432. };
  433. // Statics
  434. Effect.ShadersStore = {};
  435. Effect.IncludesShadersStore = {};
  436. return Effect;
  437. })();
  438. BABYLON.Effect = Effect;
  439. })(BABYLON || (BABYLON = {}));