babylon.customMaterial.js 9.6 KB

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