babylon.effect.js 13 KB

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