babylon.effect.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. var BABYLON;
  2. (function (BABYLON) {
  3. var Effect = (function () {
  4. function Effect(baseName, attributesNames, uniformsNames, samplers, engine, defines, optionalDefines, onCompiled, onError) {
  5. var _this = this;
  6. this._isReady = false;
  7. this._compilationError = "";
  8. this._valueCache = [];
  9. this._engine = engine;
  10. this.name = baseName;
  11. this.defines = defines;
  12. this._uniformsNames = uniformsNames.concat(samplers);
  13. this._samplers = samplers;
  14. this._attributesNames = attributesNames;
  15. this.onError = onError;
  16. this.onCompiled = onCompiled;
  17. var vertexSource;
  18. var fragmentSource;
  19. if (baseName.vertexElement) {
  20. vertexSource = document.getElementById(baseName.vertexElement);
  21. fragmentSource = document.getElementById(baseName.fragmentElement);
  22. } else {
  23. vertexSource = baseName.vertexElement || baseName.vertex || baseName;
  24. fragmentSource = baseName.fragmentElement || baseName.fragment || baseName;
  25. }
  26. this._loadVertexShader(vertexSource, function (vertexCode) {
  27. _this._loadFragmentShader(fragmentSource, function (fragmentCode) {
  28. _this._prepareEffect(vertexCode, fragmentCode, attributesNames, defines, optionalDefines);
  29. });
  30. });
  31. }
  32. // Properties
  33. Effect.prototype.isReady = function () {
  34. return this._isReady;
  35. };
  36. Effect.prototype.getProgram = function () {
  37. return this._program;
  38. };
  39. Effect.prototype.getAttributesNames = function () {
  40. return this._attributesNames;
  41. };
  42. Effect.prototype.getAttribute = function (index) {
  43. return this._attributes[index];
  44. };
  45. Effect.prototype.getAttributesCount = function () {
  46. return this._attributes.length;
  47. };
  48. Effect.prototype.getUniformIndex = function (uniformName) {
  49. return this._uniformsNames.indexOf(uniformName);
  50. };
  51. Effect.prototype.getUniform = function (uniformName) {
  52. return this._uniforms[this._uniformsNames.indexOf(uniformName)];
  53. };
  54. Effect.prototype.getSamplers = function () {
  55. return this._samplers;
  56. };
  57. Effect.prototype.getCompilationError = function () {
  58. return this._compilationError;
  59. };
  60. // Methods
  61. Effect.prototype._loadVertexShader = function (vertex, callback) {
  62. // DOM element ?
  63. if (vertex instanceof HTMLElement) {
  64. var vertexCode = BABYLON.Tools.GetDOMTextContent(vertex);
  65. callback(vertexCode);
  66. return;
  67. }
  68. // Is in local store ?
  69. if (BABYLON.Effect.ShadersStore[vertex + "VertexShader"]) {
  70. callback(BABYLON.Effect.ShadersStore[vertex + "VertexShader"]);
  71. return;
  72. }
  73. var vertexShaderUrl;
  74. if (vertex[0] === ".") {
  75. vertexShaderUrl = vertex;
  76. } else {
  77. vertexShaderUrl = BABYLON.Engine.ShadersRepository + vertex;
  78. }
  79. // Vertex shader
  80. BABYLON.Tools.LoadFile(vertexShaderUrl + ".vertex.fx", callback);
  81. };
  82. Effect.prototype._loadFragmentShader = function (fragment, callback) {
  83. // DOM element ?
  84. if (fragment instanceof HTMLElement) {
  85. var fragmentCode = BABYLON.Tools.GetDOMTextContent(fragment);
  86. callback(fragmentCode);
  87. return;
  88. }
  89. // Is in local store ?
  90. if (BABYLON.Effect.ShadersStore[fragment + "PixelShader"]) {
  91. callback(BABYLON.Effect.ShadersStore[fragment + "PixelShader"]);
  92. return;
  93. }
  94. var fragmentShaderUrl;
  95. if (fragment[0] === ".") {
  96. fragmentShaderUrl = fragment;
  97. } else {
  98. fragmentShaderUrl = BABYLON.Engine.ShadersRepository + fragment;
  99. }
  100. // Fragment shader
  101. BABYLON.Tools.LoadFile(fragmentShaderUrl + ".fragment.fx", callback);
  102. };
  103. Effect.prototype._prepareEffect = function (vertexSourceCode, fragmentSourceCode, attributesNames, defines, optionalDefines, useFallback) {
  104. try {
  105. var engine = this._engine;
  106. this._program = engine.createShaderProgram(vertexSourceCode, fragmentSourceCode, defines);
  107. this._uniforms = engine.getUniforms(this._program, this._uniformsNames);
  108. this._attributes = engine.getAttributes(this._program, attributesNames);
  109. for (var index = 0; index < this._samplers.length; index++) {
  110. var sampler = this.getUniform(this._samplers[index]);
  111. if (sampler == null) {
  112. this._samplers.splice(index, 1);
  113. index--;
  114. }
  115. }
  116. engine.bindSamplers(this);
  117. this._isReady = true;
  118. if (this.onCompiled) {
  119. this.onCompiled(this);
  120. }
  121. } catch (e) {
  122. if (!useFallback && optionalDefines) {
  123. for (index = 0; index < optionalDefines.length; index++) {
  124. defines = defines.replace(optionalDefines[index], "");
  125. }
  126. this._prepareEffect(vertexSourceCode, fragmentSourceCode, attributesNames, defines, optionalDefines, true);
  127. } else {
  128. console.error("Unable to compile effect: " + this.name);
  129. console.error("Defines: " + defines);
  130. console.error("Optional defines: " + optionalDefines);
  131. console.error("Error: " + e.message);
  132. this._compilationError = e.message;
  133. if (this.onError) {
  134. this.onError(this, this._compilationError);
  135. }
  136. }
  137. }
  138. };
  139. Effect.prototype._bindTexture = function (channel, texture) {
  140. this._engine._bindTexture(this._samplers.indexOf(channel), texture);
  141. };
  142. Effect.prototype.setTexture = function (channel, texture) {
  143. this._engine.setTexture(this._samplers.indexOf(channel), texture);
  144. };
  145. Effect.prototype.setTextureFromPostProcess = function (channel, postProcess) {
  146. this._engine.setTextureFromPostProcess(this._samplers.indexOf(channel), postProcess);
  147. };
  148. //public _cacheMatrix(uniformName, matrix) {
  149. // if (!this._valueCache[uniformName]) {
  150. // this._valueCache[uniformName] = new BABYLON.Matrix();
  151. // }
  152. // for (var index = 0; index < 16; index++) {
  153. // this._valueCache[uniformName].m[index] = matrix.m[index];
  154. // }
  155. //};
  156. Effect.prototype._cacheFloat2 = function (uniformName, x, y) {
  157. if (!this._valueCache[uniformName]) {
  158. this._valueCache[uniformName] = [x, y];
  159. return;
  160. }
  161. this._valueCache[uniformName][0] = x;
  162. this._valueCache[uniformName][1] = y;
  163. };
  164. Effect.prototype._cacheFloat3 = function (uniformName, x, y, z) {
  165. if (!this._valueCache[uniformName]) {
  166. this._valueCache[uniformName] = [x, y, z];
  167. return;
  168. }
  169. this._valueCache[uniformName][0] = x;
  170. this._valueCache[uniformName][1] = y;
  171. this._valueCache[uniformName][2] = z;
  172. };
  173. Effect.prototype._cacheFloat4 = function (uniformName, x, y, z, w) {
  174. if (!this._valueCache[uniformName]) {
  175. this._valueCache[uniformName] = [x, y, z, w];
  176. return;
  177. }
  178. this._valueCache[uniformName][0] = x;
  179. this._valueCache[uniformName][1] = y;
  180. this._valueCache[uniformName][2] = z;
  181. this._valueCache[uniformName][3] = w;
  182. };
  183. Effect.prototype.setArray = function (uniformName, array) {
  184. this._engine.setArray(this.getUniform(uniformName), array);
  185. return this;
  186. };
  187. Effect.prototype.setMatrices = function (uniformName, matrices) {
  188. this._engine.setMatrices(this.getUniform(uniformName), matrices);
  189. return this;
  190. };
  191. Effect.prototype.setMatrix = function (uniformName, matrix) {
  192. //if (this._valueCache[uniformName] && this._valueCache[uniformName].equals(matrix))
  193. // return;
  194. //this._cacheMatrix(uniformName, matrix);
  195. this._engine.setMatrix(this.getUniform(uniformName), matrix);
  196. return this;
  197. };
  198. Effect.prototype.setFloat = function (uniformName, value) {
  199. if (this._valueCache[uniformName] && this._valueCache[uniformName] === value)
  200. return this;
  201. this._valueCache[uniformName] = value;
  202. this._engine.setFloat(this.getUniform(uniformName), value);
  203. return this;
  204. };
  205. Effect.prototype.setBool = function (uniformName, bool) {
  206. if (this._valueCache[uniformName] && this._valueCache[uniformName] === bool)
  207. return this;
  208. this._valueCache[uniformName] = bool;
  209. this._engine.setBool(this.getUniform(uniformName), bool ? 1 : 0);
  210. return this;
  211. };
  212. Effect.prototype.setVector2 = function (uniformName, vector2) {
  213. if (this._valueCache[uniformName] && this._valueCache[uniformName][0] == vector2.x && this._valueCache[uniformName][1] == vector2.y)
  214. return this;
  215. this._cacheFloat2(uniformName, vector2.x, vector2.y);
  216. this._engine.setFloat2(this.getUniform(uniformName), vector2.x, vector2.y);
  217. return this;
  218. };
  219. Effect.prototype.setFloat2 = function (uniformName, x, y) {
  220. if (this._valueCache[uniformName] && this._valueCache[uniformName][0] == x && this._valueCache[uniformName][1] == y)
  221. return this;
  222. this._cacheFloat2(uniformName, x, y);
  223. this._engine.setFloat2(this.getUniform(uniformName), x, y);
  224. return this;
  225. };
  226. Effect.prototype.setVector3 = function (uniformName, vector3) {
  227. if (this._valueCache[uniformName] && this._valueCache[uniformName][0] == vector3.x && this._valueCache[uniformName][1] == vector3.y && this._valueCache[uniformName][2] == vector3.z)
  228. return this;
  229. this._cacheFloat3(uniformName, vector3.x, vector3.y, vector3.z);
  230. this._engine.setFloat3(this.getUniform(uniformName), vector3.x, vector3.y, vector3.z);
  231. return this;
  232. };
  233. Effect.prototype.setFloat3 = function (uniformName, x, y, z) {
  234. if (this._valueCache[uniformName] && this._valueCache[uniformName][0] == x && this._valueCache[uniformName][1] == y && this._valueCache[uniformName][2] == z)
  235. return this;
  236. this._cacheFloat3(uniformName, x, y, z);
  237. this._engine.setFloat3(this.getUniform(uniformName), x, y, z);
  238. return this;
  239. };
  240. Effect.prototype.setFloat4 = function (uniformName, x, y, z, w) {
  241. if (this._valueCache[uniformName] && this._valueCache[uniformName][0] == x && this._valueCache[uniformName][1] == y && this._valueCache[uniformName][2] == z && this._valueCache[uniformName][3] == w)
  242. return this;
  243. this._cacheFloat4(uniformName, x, y, z, w);
  244. this._engine.setFloat4(this.getUniform(uniformName), x, y, z, w);
  245. return this;
  246. };
  247. Effect.prototype.setColor3 = function (uniformName, color3) {
  248. if (this._valueCache[uniformName] && this._valueCache[uniformName][0] == color3.r && this._valueCache[uniformName][1] == color3.g && this._valueCache[uniformName][2] == color3.b)
  249. return this;
  250. this._cacheFloat3(uniformName, color3.r, color3.g, color3.b);
  251. this._engine.setColor3(this.getUniform(uniformName), color3);
  252. return this;
  253. };
  254. Effect.prototype.setColor4 = function (uniformName, color3, alpha) {
  255. 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)
  256. return this;
  257. this._cacheFloat4(uniformName, color3.r, color3.g, color3.b, alpha);
  258. this._engine.setColor4(this.getUniform(uniformName), color3, alpha);
  259. return this;
  260. };
  261. Effect.ShadersStore = {};
  262. return Effect;
  263. })();
  264. BABYLON.Effect = Effect;
  265. })(BABYLON || (BABYLON = {}));
  266. //# sourceMappingURL=babylon.effect.js.map