babylon.effect.js 21 KB

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