babylon.customMaterial.js 9.5 KB

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