babylon.shaderMaterial.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. var __extends = (this && this.__extends) || function (d, b) {
  2. for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
  3. function __() { this.constructor = d; }
  4. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  5. };
  6. var BABYLON;
  7. (function (BABYLON) {
  8. var ShaderMaterial = (function (_super) {
  9. __extends(ShaderMaterial, _super);
  10. function ShaderMaterial(name, scene, shaderPath, options) {
  11. _super.call(this, name, scene);
  12. this._textures = {};
  13. this._floats = {};
  14. this._floatsArrays = {};
  15. this._colors3 = {};
  16. this._colors4 = {};
  17. this._vectors2 = {};
  18. this._vectors3 = {};
  19. this._vectors4 = {};
  20. this._matrices = {};
  21. this._matrices3x3 = {};
  22. this._matrices2x2 = {};
  23. this._cachedWorldViewMatrix = new BABYLON.Matrix();
  24. this._shaderPath = shaderPath;
  25. options.needAlphaBlending = options.needAlphaBlending || false;
  26. options.needAlphaTesting = options.needAlphaTesting || false;
  27. options.attributes = options.attributes || ["position", "normal", "uv"];
  28. options.uniforms = options.uniforms || ["worldViewProjection"];
  29. options.samplers = options.samplers || [];
  30. options.defines = options.defines || [];
  31. this._options = options;
  32. }
  33. ShaderMaterial.prototype.needAlphaBlending = function () {
  34. return this._options.needAlphaBlending;
  35. };
  36. ShaderMaterial.prototype.needAlphaTesting = function () {
  37. return this._options.needAlphaTesting;
  38. };
  39. ShaderMaterial.prototype._checkUniform = function (uniformName) {
  40. if (this._options.uniforms.indexOf(uniformName) === -1) {
  41. this._options.uniforms.push(uniformName);
  42. }
  43. };
  44. ShaderMaterial.prototype.setTexture = function (name, texture) {
  45. if (this._options.samplers.indexOf(name) === -1) {
  46. this._options.samplers.push(name);
  47. }
  48. this._textures[name] = texture;
  49. return this;
  50. };
  51. ShaderMaterial.prototype.setFloat = function (name, value) {
  52. this._checkUniform(name);
  53. this._floats[name] = value;
  54. return this;
  55. };
  56. ShaderMaterial.prototype.setFloats = function (name, value) {
  57. this._checkUniform(name);
  58. this._floatsArrays[name] = value;
  59. return this;
  60. };
  61. ShaderMaterial.prototype.setColor3 = function (name, value) {
  62. this._checkUniform(name);
  63. this._colors3[name] = value;
  64. return this;
  65. };
  66. ShaderMaterial.prototype.setColor4 = function (name, value) {
  67. this._checkUniform(name);
  68. this._colors4[name] = value;
  69. return this;
  70. };
  71. ShaderMaterial.prototype.setVector2 = function (name, value) {
  72. this._checkUniform(name);
  73. this._vectors2[name] = value;
  74. return this;
  75. };
  76. ShaderMaterial.prototype.setVector3 = function (name, value) {
  77. this._checkUniform(name);
  78. this._vectors3[name] = value;
  79. return this;
  80. };
  81. ShaderMaterial.prototype.setVector4 = function (name, value) {
  82. this._checkUniform(name);
  83. this._vectors4[name] = value;
  84. return this;
  85. };
  86. ShaderMaterial.prototype.setMatrix = function (name, value) {
  87. this._checkUniform(name);
  88. this._matrices[name] = value;
  89. return this;
  90. };
  91. ShaderMaterial.prototype.setMatrix3x3 = function (name, value) {
  92. this._checkUniform(name);
  93. this._matrices3x3[name] = value;
  94. return this;
  95. };
  96. ShaderMaterial.prototype.setMatrix2x2 = function (name, value) {
  97. this._checkUniform(name);
  98. this._matrices2x2[name] = value;
  99. return this;
  100. };
  101. ShaderMaterial.prototype.isReady = function (mesh, useInstances) {
  102. var scene = this.getScene();
  103. var engine = scene.getEngine();
  104. if (!this.checkReadyOnEveryCall) {
  105. if (this._renderId === scene.getRenderId()) {
  106. return true;
  107. }
  108. }
  109. // Instances
  110. var defines = [];
  111. var fallbacks = new BABYLON.EffectFallbacks();
  112. if (useInstances) {
  113. defines.push("#define INSTANCES");
  114. }
  115. for (var index = 0; index < this._options.defines.length; index++) {
  116. defines.push(this._options.defines[index]);
  117. }
  118. // Bones
  119. if (mesh && mesh.useBones && mesh.computeBonesUsingShaders) {
  120. defines.push("#define NUM_BONE_INFLUENCERS " + mesh.numBoneInfluencers);
  121. defines.push("#define BonesPerMesh " + (mesh.skeleton.bones.length + 1));
  122. fallbacks.addCPUSkinningFallback(0, mesh);
  123. }
  124. // Alpha test
  125. if (engine.getAlphaTesting()) {
  126. defines.push("#define ALPHATEST");
  127. }
  128. var previousEffect = this._effect;
  129. var join = defines.join("\n");
  130. this._effect = engine.createEffect(this._shaderPath, this._options.attributes, this._options.uniforms, this._options.samplers, join, fallbacks, this.onCompiled, this.onError);
  131. if (!this._effect.isReady()) {
  132. return false;
  133. }
  134. if (previousEffect !== this._effect) {
  135. scene.resetCachedMaterial();
  136. }
  137. this._renderId = scene.getRenderId();
  138. return true;
  139. };
  140. ShaderMaterial.prototype.bindOnlyWorldMatrix = function (world) {
  141. var scene = this.getScene();
  142. if (this._options.uniforms.indexOf("world") !== -1) {
  143. this._effect.setMatrix("world", world);
  144. }
  145. if (this._options.uniforms.indexOf("worldView") !== -1) {
  146. world.multiplyToRef(scene.getViewMatrix(), this._cachedWorldViewMatrix);
  147. this._effect.setMatrix("worldView", this._cachedWorldViewMatrix);
  148. }
  149. if (this._options.uniforms.indexOf("worldViewProjection") !== -1) {
  150. this._effect.setMatrix("worldViewProjection", world.multiply(scene.getTransformMatrix()));
  151. }
  152. };
  153. ShaderMaterial.prototype.bind = function (world, mesh) {
  154. // Std values
  155. this.bindOnlyWorldMatrix(world);
  156. if (this.getScene().getCachedMaterial() !== this) {
  157. if (this._options.uniforms.indexOf("view") !== -1) {
  158. this._effect.setMatrix("view", this.getScene().getViewMatrix());
  159. }
  160. if (this._options.uniforms.indexOf("projection") !== -1) {
  161. this._effect.setMatrix("projection", this.getScene().getProjectionMatrix());
  162. }
  163. if (this._options.uniforms.indexOf("viewProjection") !== -1) {
  164. this._effect.setMatrix("viewProjection", this.getScene().getTransformMatrix());
  165. }
  166. // Bones
  167. if (mesh && mesh.useBones && mesh.computeBonesUsingShaders) {
  168. this._effect.setMatrices("mBones", mesh.skeleton.getTransformMatrices(mesh));
  169. }
  170. // Texture
  171. for (var name in this._textures) {
  172. this._effect.setTexture(name, this._textures[name]);
  173. }
  174. // Float
  175. for (name in this._floats) {
  176. this._effect.setFloat(name, this._floats[name]);
  177. }
  178. // Float s
  179. for (name in this._floatsArrays) {
  180. this._effect.setArray(name, this._floatsArrays[name]);
  181. }
  182. // Color3
  183. for (name in this._colors3) {
  184. this._effect.setColor3(name, this._colors3[name]);
  185. }
  186. // Color4
  187. for (name in this._colors4) {
  188. var color = this._colors4[name];
  189. this._effect.setFloat4(name, color.r, color.g, color.b, color.a);
  190. }
  191. // Vector2
  192. for (name in this._vectors2) {
  193. this._effect.setVector2(name, this._vectors2[name]);
  194. }
  195. // Vector3
  196. for (name in this._vectors3) {
  197. this._effect.setVector3(name, this._vectors3[name]);
  198. }
  199. // Vector4
  200. for (name in this._vectors4) {
  201. this._effect.setVector4(name, this._vectors4[name]);
  202. }
  203. // Matrix
  204. for (name in this._matrices) {
  205. this._effect.setMatrix(name, this._matrices[name]);
  206. }
  207. // Matrix 3x3
  208. for (name in this._matrices3x3) {
  209. this._effect.setMatrix3x3(name, this._matrices3x3[name]);
  210. }
  211. // Matrix 2x2
  212. for (name in this._matrices2x2) {
  213. this._effect.setMatrix2x2(name, this._matrices2x2[name]);
  214. }
  215. }
  216. _super.prototype.bind.call(this, world, mesh);
  217. };
  218. ShaderMaterial.prototype.clone = function (name) {
  219. var newShaderMaterial = new ShaderMaterial(name, this.getScene(), this._shaderPath, this._options);
  220. return newShaderMaterial;
  221. };
  222. ShaderMaterial.prototype.dispose = function (forceDisposeEffect) {
  223. for (var name in this._textures) {
  224. this._textures[name].dispose();
  225. }
  226. this._textures = {};
  227. _super.prototype.dispose.call(this, forceDisposeEffect);
  228. };
  229. ShaderMaterial.prototype.serialize = function () {
  230. var serializationObject = _super.prototype.serialize.call(this);
  231. serializationObject.options = this._options;
  232. serializationObject.shaderPath = this._shaderPath;
  233. serializationObject.customType = "BABYLON.ShaderMaterial";
  234. // Texture
  235. serializationObject.textures = {};
  236. for (var name in this._textures) {
  237. serializationObject.textures[name] = this._textures[name].serialize();
  238. }
  239. // Float
  240. serializationObject.floats = {};
  241. for (name in this._floats) {
  242. serializationObject.floats[name] = this._floats[name];
  243. }
  244. // Float s
  245. serializationObject.floatArrays = {};
  246. for (name in this._floatsArrays) {
  247. serializationObject.floatArrays[name] = this._floatsArrays[name];
  248. }
  249. // Color3
  250. serializationObject.colors3 = {};
  251. for (name in this._colors3) {
  252. serializationObject.colors3[name] = this._colors3[name].asArray();
  253. }
  254. // Color4
  255. serializationObject.colors4 = {};
  256. for (name in this._colors4) {
  257. serializationObject.colors4[name] = this._colors4[name].asArray();
  258. }
  259. // Vector2
  260. serializationObject.vectors2 = {};
  261. for (name in this._vectors2) {
  262. serializationObject.vectors2[name] = this._vectors2[name].asArray();
  263. }
  264. // Vector3
  265. serializationObject.vectors3 = {};
  266. for (name in this._vectors3) {
  267. serializationObject.vectors3[name] = this._vectors3[name].asArray();
  268. }
  269. // Vector4
  270. serializationObject.vectors4 = {};
  271. for (name in this._vectors4) {
  272. serializationObject.vectors4[name] = this._vectors4[name].asArray();
  273. }
  274. // Matrix
  275. serializationObject.matrices = {};
  276. for (name in this._matrices) {
  277. serializationObject.matrices[name] = this._matrices[name].asArray();
  278. }
  279. // Matrix 3x3
  280. serializationObject.matrices3x3 = {};
  281. for (name in this._matrices3x3) {
  282. serializationObject.matrices3x3[name] = this._matrices3x3[name];
  283. }
  284. // Matrix 2x2
  285. serializationObject.matrices2x2 = {};
  286. for (name in this._matrices2x2) {
  287. serializationObject.matrices2x2[name] = this._matrices2x2[name];
  288. }
  289. return serializationObject;
  290. };
  291. ShaderMaterial.Parse = function (source, scene, rootUrl) {
  292. var material = new ShaderMaterial(source.name, scene, source.shaderPath, source.options);
  293. // Texture
  294. for (var name in source.textures) {
  295. material.setTexture(name, BABYLON.Texture.Parse(source.textures[name], scene, rootUrl));
  296. }
  297. // Float
  298. for (name in source.floats) {
  299. material.setFloat(name, source.floats[name]);
  300. }
  301. // Float s
  302. for (name in source.floatsArrays) {
  303. material.setFloats(name, source.floatsArrays[name]);
  304. }
  305. // Color3
  306. for (name in source.colors3) {
  307. material.setColor3(name, BABYLON.Color3.FromArray(source.colors3[name]));
  308. }
  309. // Color4
  310. for (name in source.colors4) {
  311. material.setColor4(name, BABYLON.Color4.FromArray(source.colors4[name]));
  312. }
  313. // Vector2
  314. for (name in source.vectors2) {
  315. material.setVector2(name, BABYLON.Vector2.FromArray(source.vectors2[name]));
  316. }
  317. // Vector3
  318. for (name in source.vectors3) {
  319. material.setVector3(name, BABYLON.Vector3.FromArray(source.vectors3[name]));
  320. }
  321. // Vector4
  322. for (name in source.vectors4) {
  323. material.setVector4(name, BABYLON.Vector4.FromArray(source.vectors4[name]));
  324. }
  325. // Matrix
  326. for (name in source.matrices) {
  327. material.setMatrix(name, BABYLON.Matrix.FromArray(source.matrices[name]));
  328. }
  329. // Matrix 3x3
  330. for (name in source.matrices3x3) {
  331. material.setMatrix3x3(name, source.matrices3x3[name]);
  332. }
  333. // Matrix 2x2
  334. for (name in source.matrices2x2) {
  335. material.setMatrix2x2(name, source.matrices2x2[name]);
  336. }
  337. return material;
  338. };
  339. return ShaderMaterial;
  340. })(BABYLON.Material);
  341. BABYLON.ShaderMaterial = ShaderMaterial;
  342. })(BABYLON || (BABYLON = {}));
  343. //# sourceMappingURL=babylon.shaderMaterial.js.map