babylon.customMaterial.js 9.5 KB

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