babylon.effect.js 13 KB

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