babylon.customMaterial.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /// <reference path="../../../dist/preview release/babylon.d.ts"/>
  2. var __extends = (this && this.__extends) || (function () {
  3. var extendStatics = Object.setPrototypeOf ||
  4. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  5. function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  6. return function (d, b) {
  7. extendStatics(d, b);
  8. function __() { this.constructor = d; }
  9. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  10. };
  11. })();
  12. var BABYLON;
  13. (function (BABYLON) {
  14. // old version of standard material updated every 3 months
  15. var CustomShaderStructure = /** @class */ (function () {
  16. function CustomShaderStructure() {
  17. }
  18. return CustomShaderStructure;
  19. }());
  20. BABYLON.CustomShaderStructure = CustomShaderStructure;
  21. var ShaderSpecialParts = /** @class */ (function () {
  22. function ShaderSpecialParts() {
  23. }
  24. return ShaderSpecialParts;
  25. }());
  26. BABYLON.ShaderSpecialParts = ShaderSpecialParts;
  27. var CustomMaterial = /** @class */ (function (_super) {
  28. __extends(CustomMaterial, _super);
  29. function CustomMaterial(name, scene) {
  30. var _this = _super.call(this, name, scene) || this;
  31. _this.CustomParts = new ShaderSpecialParts();
  32. _this.customShaderNameResolve = _this.Builder;
  33. _this.FragmentShader = BABYLON.Effect.ShadersStore["defaultPixelShader"];
  34. _this.VertexShader = BABYLON.Effect.ShadersStore["defaultVertexShader"];
  35. return _this;
  36. }
  37. CustomMaterial.prototype.AttachAfterBind = function (mesh, effect) {
  38. for (var el in this._newUniformInstances) {
  39. var ea = el.toString().split('-');
  40. if (ea[0] == 'vec2')
  41. effect.setVector2(ea[1], this._newUniformInstances[el]);
  42. else if (ea[0] == 'vec3')
  43. effect.setVector3(ea[1], this._newUniformInstances[el]);
  44. else if (ea[0] == 'vec4')
  45. effect.setVector4(ea[1], this._newUniformInstances[el]);
  46. else if (ea[0] == 'mat4')
  47. effect.setMatrix(ea[1], this._newUniformInstances[el]);
  48. else if (ea[0] == 'float')
  49. effect.setFloat(ea[1], this._newUniformInstances[el]);
  50. }
  51. for (var el in this._newSamplerInstances) {
  52. var ea = el.toString().split('-');
  53. if (ea[0] == 'sampler2D' && this._newSamplerInstances[el].isReady && this._newSamplerInstances[el].isReady())
  54. effect.setTexture(ea[1], this._newSamplerInstances[el]);
  55. }
  56. };
  57. CustomMaterial.prototype.ReviewUniform = function (name, arr) {
  58. if (name == "uniform") {
  59. for (var ind in this._newUniforms)
  60. if (this._customUniform[ind].indexOf('sampler') == -1)
  61. arr.push(this._newUniforms[ind]);
  62. }
  63. if (name == "sampler") {
  64. for (var ind in this._newUniforms)
  65. if (this._customUniform[ind].indexOf('sampler') != -1)
  66. arr.push(this._newUniforms[ind]);
  67. }
  68. return arr;
  69. };
  70. CustomMaterial.prototype.Builder = function (shaderName, uniforms, uniformBuffers, samplers, defines) {
  71. var _this = this;
  72. if (this._isCreatedShader)
  73. return this._createdShaderName;
  74. this._isCreatedShader = false;
  75. CustomMaterial.ShaderIndexer++;
  76. var name = "custom_" + CustomMaterial.ShaderIndexer;
  77. this.ReviewUniform("uniform", uniforms);
  78. this.ReviewUniform("sampler", samplers);
  79. var fn_afterBind = this._afterBind.bind(this);
  80. this._afterBind = function (m, e) {
  81. if (!e) {
  82. return;
  83. }
  84. _this.AttachAfterBind(m, e);
  85. try {
  86. fn_afterBind(m, e);
  87. }
  88. catch (e) { }
  89. ;
  90. };
  91. BABYLON.Effect.ShadersStore[name + "VertexShader"] = this.VertexShader
  92. .replace('#define CUSTOM_VERTEX_BEGIN', (this.CustomParts.Vertex_Begin ? this.CustomParts.Vertex_Begin : ""))
  93. .replace('#define CUSTOM_VERTEX_DEFINITIONS', (this._customUniform ? this._customUniform.join("\n") : "") + (this.CustomParts.Vertex_Definitions ? this.CustomParts.Vertex_Definitions : ""))
  94. .replace('#define CUSTOM_VERTEX_MAIN_BEGIN', (this.CustomParts.Vertex_MainBegin ? this.CustomParts.Vertex_MainBegin : ""))
  95. .replace('#define CUSTOM_VERTEX_UPDATE_POSITION', (this.CustomParts.Vertex_Before_PositionUpdated ? this.CustomParts.Vertex_Before_PositionUpdated : ""))
  96. .replace('#define CUSTOM_VERTEX_UPDATE_NORMAL', (this.CustomParts.Vertex_Before_NormalUpdated ? this.CustomParts.Vertex_Before_NormalUpdated : ""));
  97. // #define CUSTOM_VERTEX_MAIN_END
  98. BABYLON.Effect.ShadersStore[name + "PixelShader"] = this.FragmentShader
  99. .replace('#define CUSTOM_FRAGMENT_BEGIN', (this.CustomParts.Fragment_Begin ? this.CustomParts.Fragment_Begin : ""))
  100. .replace('#define CUSTOM_FRAGMENT_MAIN_BEGIN', (this.CustomParts.Fragment_MainBegin ? this.CustomParts.Fragment_MainBegin : ""))
  101. .replace('#define CUSTOM_FRAGMENT_DEFINITIONS', (this._customUniform ? this._customUniform.join("\n") : "") + (this.CustomParts.Fragment_Definitions ? this.CustomParts.Fragment_Definitions : ""))
  102. .replace('#define CUSTOM_FRAGMENT_UPDATE_DIFFUSE', (this.CustomParts.Fragment_Custom_Diffuse ? this.CustomParts.Fragment_Custom_Diffuse : ""))
  103. .replace('#define CUSTOM_FRAGMENT_UPDATE_ALPHA', (this.CustomParts.Fragment_Custom_Alpha ? this.CustomParts.Fragment_Custom_Alpha : ""))
  104. .replace('#define CUSTOM_FRAGMENT_BEFORE_FRAGCOLOR', (this.CustomParts.Fragment_Before_FragColor ? this.CustomParts.Fragment_Before_FragColor : ""));
  105. // #define CUSTOM_FRAGMENT_BEFORE_LIGHTS
  106. // #define CUSTOM_FRAGMENT_BEFORE_FOG
  107. this._isCreatedShader = true;
  108. this._createdShaderName = name;
  109. return name;
  110. };
  111. CustomMaterial.prototype.AddUniform = function (name, kind, param) {
  112. if (!this._customUniform) {
  113. this._customUniform = new Array();
  114. this._newUniforms = new Array();
  115. this._newSamplerInstances = new Array();
  116. this._newUniformInstances = new Array();
  117. }
  118. if (param) {
  119. if (kind.indexOf("sampler") == -1) {
  120. this._newUniformInstances[kind + "-" + name] = param;
  121. }
  122. else {
  123. this._newUniformInstances[kind + "-" + name] = param;
  124. }
  125. }
  126. this._customUniform.push("uniform " + kind + " " + name + ";");
  127. this._newUniforms.push(name);
  128. return this;
  129. };
  130. CustomMaterial.prototype.Fragment_Begin = function (shaderPart) {
  131. this.CustomParts.Fragment_Begin = shaderPart;
  132. return this;
  133. };
  134. CustomMaterial.prototype.Fragment_Definitions = function (shaderPart) {
  135. this.CustomParts.Fragment_Definitions = shaderPart;
  136. return this;
  137. };
  138. CustomMaterial.prototype.Fragment_MainBegin = function (shaderPart) {
  139. this.CustomParts.Fragment_MainBegin = shaderPart;
  140. return this;
  141. };
  142. CustomMaterial.prototype.Fragment_Custom_Diffuse = function (shaderPart) {
  143. this.CustomParts.Fragment_Custom_Diffuse = shaderPart.replace("result", "diffuseColor");
  144. return this;
  145. };
  146. CustomMaterial.prototype.Fragment_Custom_Alpha = function (shaderPart) {
  147. this.CustomParts.Fragment_Custom_Alpha = shaderPart.replace("result", "alpha");
  148. return this;
  149. };
  150. CustomMaterial.prototype.Fragment_Before_FragColor = function (shaderPart) {
  151. this.CustomParts.Fragment_Before_FragColor = shaderPart.replace("result", "color");
  152. return this;
  153. };
  154. CustomMaterial.prototype.Vertex_Begin = function (shaderPart) {
  155. this.CustomParts.Vertex_Begin = shaderPart;
  156. return this;
  157. };
  158. CustomMaterial.prototype.Vertex_Definitions = function (shaderPart) {
  159. this.CustomParts.Vertex_Definitions = shaderPart;
  160. return this;
  161. };
  162. CustomMaterial.prototype.Vertex_MainBegin = function (shaderPart) {
  163. this.CustomParts.Vertex_MainBegin = shaderPart;
  164. return this;
  165. };
  166. CustomMaterial.prototype.Vertex_Before_PositionUpdated = function (shaderPart) {
  167. this.CustomParts.Vertex_Before_PositionUpdated = shaderPart.replace("result", "positionUpdated");
  168. return this;
  169. };
  170. CustomMaterial.prototype.Vertex_Before_NormalUpdated = function (shaderPart) {
  171. this.CustomParts.Vertex_Before_NormalUpdated = shaderPart.replace("result", "normalUpdated");
  172. return this;
  173. };
  174. CustomMaterial.ShaderIndexer = 1;
  175. return CustomMaterial;
  176. }(BABYLON.StandardMaterial));
  177. BABYLON.CustomMaterial = CustomMaterial;
  178. })(BABYLON || (BABYLON = {}));
  179. //# sourceMappingURL=babylon.customMaterial.js.map