babylon.customMaterial.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. (function webpackUniversalModuleDefinition(root, factory) {
  2. if(typeof exports === 'object' && typeof module === 'object')
  3. module.exports = factory(require("babylonjs"));
  4. else if(typeof define === 'function' && define.amd)
  5. define("babylonjs-materials", ["babylonjs"], factory);
  6. else if(typeof exports === 'object')
  7. exports["babylonjs-materials"] = factory(require("babylonjs"));
  8. else
  9. root["MATLIB"] = factory(root["BABYLON"]);
  10. })(window, function(__WEBPACK_EXTERNAL_MODULE_babylonjs__) {
  11. return /******/ (function(modules) { // webpackBootstrap
  12. /******/ // The module cache
  13. /******/ var installedModules = {};
  14. /******/
  15. /******/ // The require function
  16. /******/ function __webpack_require__(moduleId) {
  17. /******/
  18. /******/ // Check if module is in cache
  19. /******/ if(installedModules[moduleId]) {
  20. /******/ return installedModules[moduleId].exports;
  21. /******/ }
  22. /******/ // Create a new module (and put it into the cache)
  23. /******/ var module = installedModules[moduleId] = {
  24. /******/ i: moduleId,
  25. /******/ l: false,
  26. /******/ exports: {}
  27. /******/ };
  28. /******/
  29. /******/ // Execute the module function
  30. /******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
  31. /******/
  32. /******/ // Flag the module as loaded
  33. /******/ module.l = true;
  34. /******/
  35. /******/ // Return the exports of the module
  36. /******/ return module.exports;
  37. /******/ }
  38. /******/
  39. /******/
  40. /******/ // expose the modules object (__webpack_modules__)
  41. /******/ __webpack_require__.m = modules;
  42. /******/
  43. /******/ // expose the module cache
  44. /******/ __webpack_require__.c = installedModules;
  45. /******/
  46. /******/ // define getter function for harmony exports
  47. /******/ __webpack_require__.d = function(exports, name, getter) {
  48. /******/ if(!__webpack_require__.o(exports, name)) {
  49. /******/ Object.defineProperty(exports, name, { enumerable: true, get: getter });
  50. /******/ }
  51. /******/ };
  52. /******/
  53. /******/ // define __esModule on exports
  54. /******/ __webpack_require__.r = function(exports) {
  55. /******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
  56. /******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
  57. /******/ }
  58. /******/ Object.defineProperty(exports, '__esModule', { value: true });
  59. /******/ };
  60. /******/
  61. /******/ // create a fake namespace object
  62. /******/ // mode & 1: value is a module id, require it
  63. /******/ // mode & 2: merge all properties of value into the ns
  64. /******/ // mode & 4: return value when already ns object
  65. /******/ // mode & 8|1: behave like require
  66. /******/ __webpack_require__.t = function(value, mode) {
  67. /******/ if(mode & 1) value = __webpack_require__(value);
  68. /******/ if(mode & 8) return value;
  69. /******/ if((mode & 4) && typeof value === 'object' && value && value.__esModule) return value;
  70. /******/ var ns = Object.create(null);
  71. /******/ __webpack_require__.r(ns);
  72. /******/ Object.defineProperty(ns, 'default', { enumerable: true, value: value });
  73. /******/ if(mode & 2 && typeof value != 'string') for(var key in value) __webpack_require__.d(ns, key, function(key) { return value[key]; }.bind(null, key));
  74. /******/ return ns;
  75. /******/ };
  76. /******/
  77. /******/ // getDefaultExport function for compatibility with non-harmony modules
  78. /******/ __webpack_require__.n = function(module) {
  79. /******/ var getter = module && module.__esModule ?
  80. /******/ function getDefault() { return module['default']; } :
  81. /******/ function getModuleExports() { return module; };
  82. /******/ __webpack_require__.d(getter, 'a', getter);
  83. /******/ return getter;
  84. /******/ };
  85. /******/
  86. /******/ // Object.prototype.hasOwnProperty.call
  87. /******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
  88. /******/
  89. /******/ // __webpack_public_path__
  90. /******/ __webpack_require__.p = "";
  91. /******/
  92. /******/
  93. /******/ // Load entry module and return exports
  94. /******/ return __webpack_require__(__webpack_require__.s = "./legacy-custom.ts");
  95. /******/ })
  96. /************************************************************************/
  97. /******/ ({
  98. /***/ "../../Tools/Gulp/node_modules/webpack/buildin/global.js":
  99. /*!***********************************!*\
  100. !*** (webpack)/buildin/global.js ***!
  101. \***********************************/
  102. /*! no static exports found */
  103. /***/ (function(module, exports) {
  104. var g;
  105. // This works in non-strict mode
  106. g = (function() {
  107. return this;
  108. })();
  109. try {
  110. // This works if eval is allowed (see CSP)
  111. g = g || Function("return this")() || (1, eval)("this");
  112. } catch (e) {
  113. // This works if the window reference is available
  114. if (typeof window === "object") g = window;
  115. }
  116. // g can still be undefined, but nothing to do about it...
  117. // We return undefined, instead of nothing here, so it's
  118. // easier to handle this case. if(!global) { ...}
  119. module.exports = g;
  120. /***/ }),
  121. /***/ "../src/custom/customMaterial.ts":
  122. /*!***************************************!*\
  123. !*** ../src/custom/customMaterial.ts ***!
  124. \***************************************/
  125. /*! no static exports found */
  126. /***/ (function(module, exports, __webpack_require__) {
  127. "use strict";
  128. var __extends = (this && this.__extends) || (function () {
  129. var extendStatics = function (d, b) {
  130. extendStatics = Object.setPrototypeOf ||
  131. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  132. function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  133. return extendStatics(d, b);
  134. }
  135. return function (d, b) {
  136. extendStatics(d, b);
  137. function __() { this.constructor = d; }
  138. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  139. };
  140. })();
  141. Object.defineProperty(exports, "__esModule", { value: true });
  142. var babylonjs_1 = __webpack_require__(/*! babylonjs */ "babylonjs");
  143. var CustomShaderStructure = /** @class */ (function () {
  144. function CustomShaderStructure() {
  145. }
  146. return CustomShaderStructure;
  147. }());
  148. exports.CustomShaderStructure = CustomShaderStructure;
  149. var ShaderSpecialParts = /** @class */ (function () {
  150. function ShaderSpecialParts() {
  151. }
  152. return ShaderSpecialParts;
  153. }());
  154. exports.ShaderSpecialParts = ShaderSpecialParts;
  155. var CustomMaterial = /** @class */ (function (_super) {
  156. __extends(CustomMaterial, _super);
  157. function CustomMaterial(name, scene) {
  158. var _this = _super.call(this, name, scene) || this;
  159. _this.CustomParts = new ShaderSpecialParts();
  160. _this.customShaderNameResolve = _this.Builder;
  161. _this.FragmentShader = BABYLON.Effect.ShadersStore["defaultPixelShader"];
  162. _this.VertexShader = BABYLON.Effect.ShadersStore["defaultVertexShader"];
  163. return _this;
  164. }
  165. CustomMaterial.prototype.AttachAfterBind = function (mesh, effect) {
  166. for (var el in this._newUniformInstances) {
  167. var ea = el.toString().split('-');
  168. if (ea[0] == 'vec2') {
  169. effect.setVector2(ea[1], this._newUniformInstances[el]);
  170. }
  171. else if (ea[0] == 'vec3') {
  172. effect.setVector3(ea[1], this._newUniformInstances[el]);
  173. }
  174. else if (ea[0] == 'vec4') {
  175. effect.setVector4(ea[1], this._newUniformInstances[el]);
  176. }
  177. else if (ea[0] == 'mat4') {
  178. effect.setMatrix(ea[1], this._newUniformInstances[el]);
  179. }
  180. else if (ea[0] == 'float') {
  181. effect.setFloat(ea[1], this._newUniformInstances[el]);
  182. }
  183. }
  184. for (var el in this._newSamplerInstances) {
  185. var ea = el.toString().split('-');
  186. if (ea[0] == 'sampler2D' && this._newSamplerInstances[el].isReady && this._newSamplerInstances[el].isReady()) {
  187. effect.setTexture(ea[1], this._newSamplerInstances[el]);
  188. }
  189. }
  190. };
  191. CustomMaterial.prototype.ReviewUniform = function (name, arr) {
  192. if (name == "uniform") {
  193. for (var ind in this._newUniforms) {
  194. if (this._customUniform[ind].indexOf('sampler') == -1) {
  195. arr.push(this._newUniforms[ind]);
  196. }
  197. }
  198. }
  199. if (name == "sampler") {
  200. for (var ind in this._newUniforms) {
  201. if (this._customUniform[ind].indexOf('sampler') != -1) {
  202. arr.push(this._newUniforms[ind]);
  203. }
  204. }
  205. }
  206. return arr;
  207. };
  208. CustomMaterial.prototype.Builder = function (shaderName, uniforms, uniformBuffers, samplers, defines) {
  209. var _this = this;
  210. if (this._isCreatedShader) {
  211. return this._createdShaderName;
  212. }
  213. this._isCreatedShader = false;
  214. CustomMaterial.ShaderIndexer++;
  215. var name = "custom_" + CustomMaterial.ShaderIndexer;
  216. this.ReviewUniform("uniform", uniforms);
  217. this.ReviewUniform("sampler", samplers);
  218. var fn_afterBind = this._afterBind.bind(this);
  219. this._afterBind = function (m, e) {
  220. if (!e) {
  221. return;
  222. }
  223. _this.AttachAfterBind(m, e);
  224. try {
  225. fn_afterBind(m, e);
  226. }
  227. catch (e) { }
  228. };
  229. BABYLON.Effect.ShadersStore[name + "VertexShader"] = this.VertexShader
  230. .replace('#define CUSTOM_VERTEX_BEGIN', (this.CustomParts.Vertex_Begin ? this.CustomParts.Vertex_Begin : ""))
  231. .replace('#define CUSTOM_VERTEX_DEFINITIONS', (this._customUniform ? this._customUniform.join("\n") : "") + (this.CustomParts.Vertex_Definitions ? this.CustomParts.Vertex_Definitions : ""))
  232. .replace('#define CUSTOM_VERTEX_MAIN_BEGIN', (this.CustomParts.Vertex_MainBegin ? this.CustomParts.Vertex_MainBegin : ""))
  233. .replace('#define CUSTOM_VERTEX_UPDATE_POSITION', (this.CustomParts.Vertex_Before_PositionUpdated ? this.CustomParts.Vertex_Before_PositionUpdated : ""))
  234. .replace('#define CUSTOM_VERTEX_UPDATE_NORMAL', (this.CustomParts.Vertex_Before_NormalUpdated ? this.CustomParts.Vertex_Before_NormalUpdated : ""));
  235. // #define CUSTOM_VERTEX_MAIN_END
  236. BABYLON.Effect.ShadersStore[name + "PixelShader"] = this.FragmentShader
  237. .replace('#define CUSTOM_FRAGMENT_BEGIN', (this.CustomParts.Fragment_Begin ? this.CustomParts.Fragment_Begin : ""))
  238. .replace('#define CUSTOM_FRAGMENT_MAIN_BEGIN', (this.CustomParts.Fragment_MainBegin ? this.CustomParts.Fragment_MainBegin : ""))
  239. .replace('#define CUSTOM_FRAGMENT_DEFINITIONS', (this._customUniform ? this._customUniform.join("\n") : "") + (this.CustomParts.Fragment_Definitions ? this.CustomParts.Fragment_Definitions : ""))
  240. .replace('#define CUSTOM_FRAGMENT_UPDATE_DIFFUSE', (this.CustomParts.Fragment_Custom_Diffuse ? this.CustomParts.Fragment_Custom_Diffuse : ""))
  241. .replace('#define CUSTOM_FRAGMENT_UPDATE_ALPHA', (this.CustomParts.Fragment_Custom_Alpha ? this.CustomParts.Fragment_Custom_Alpha : ""))
  242. .replace('#define CUSTOM_FRAGMENT_BEFORE_FRAGCOLOR', (this.CustomParts.Fragment_Before_FragColor ? this.CustomParts.Fragment_Before_FragColor : ""));
  243. // #define CUSTOM_FRAGMENT_BEFORE_LIGHTS
  244. // #define CUSTOM_FRAGMENT_BEFORE_FOG
  245. this._isCreatedShader = true;
  246. this._createdShaderName = name;
  247. return name;
  248. };
  249. CustomMaterial.prototype.AddUniform = function (name, kind, param) {
  250. if (!this._customUniform) {
  251. this._customUniform = new Array();
  252. this._newUniforms = new Array();
  253. this._newSamplerInstances = new Array();
  254. this._newUniformInstances = new Array();
  255. }
  256. if (param) {
  257. if (kind.indexOf("sampler") == -1) {
  258. this._newUniformInstances[kind + "-" + name] = param;
  259. }
  260. else {
  261. this._newUniformInstances[kind + "-" + name] = param;
  262. }
  263. }
  264. this._customUniform.push("uniform " + kind + " " + name + ";");
  265. this._newUniforms.push(name);
  266. return this;
  267. };
  268. CustomMaterial.prototype.Fragment_Begin = function (shaderPart) {
  269. this.CustomParts.Fragment_Begin = shaderPart;
  270. return this;
  271. };
  272. CustomMaterial.prototype.Fragment_Definitions = function (shaderPart) {
  273. this.CustomParts.Fragment_Definitions = shaderPart;
  274. return this;
  275. };
  276. CustomMaterial.prototype.Fragment_MainBegin = function (shaderPart) {
  277. this.CustomParts.Fragment_MainBegin = shaderPart;
  278. return this;
  279. };
  280. CustomMaterial.prototype.Fragment_Custom_Diffuse = function (shaderPart) {
  281. this.CustomParts.Fragment_Custom_Diffuse = shaderPart.replace("result", "diffuseColor");
  282. return this;
  283. };
  284. CustomMaterial.prototype.Fragment_Custom_Alpha = function (shaderPart) {
  285. this.CustomParts.Fragment_Custom_Alpha = shaderPart.replace("result", "alpha");
  286. return this;
  287. };
  288. CustomMaterial.prototype.Fragment_Before_FragColor = function (shaderPart) {
  289. this.CustomParts.Fragment_Before_FragColor = shaderPart.replace("result", "color");
  290. return this;
  291. };
  292. CustomMaterial.prototype.Vertex_Begin = function (shaderPart) {
  293. this.CustomParts.Vertex_Begin = shaderPart;
  294. return this;
  295. };
  296. CustomMaterial.prototype.Vertex_Definitions = function (shaderPart) {
  297. this.CustomParts.Vertex_Definitions = shaderPart;
  298. return this;
  299. };
  300. CustomMaterial.prototype.Vertex_MainBegin = function (shaderPart) {
  301. this.CustomParts.Vertex_MainBegin = shaderPart;
  302. return this;
  303. };
  304. CustomMaterial.prototype.Vertex_Before_PositionUpdated = function (shaderPart) {
  305. this.CustomParts.Vertex_Before_PositionUpdated = shaderPart.replace("result", "positionUpdated");
  306. return this;
  307. };
  308. CustomMaterial.prototype.Vertex_Before_NormalUpdated = function (shaderPart) {
  309. this.CustomParts.Vertex_Before_NormalUpdated = shaderPart.replace("result", "normalUpdated");
  310. return this;
  311. };
  312. CustomMaterial.ShaderIndexer = 1;
  313. return CustomMaterial;
  314. }(babylonjs_1.StandardMaterial));
  315. exports.CustomMaterial = CustomMaterial;
  316. /***/ }),
  317. /***/ "../src/custom/index.ts":
  318. /*!******************************!*\
  319. !*** ../src/custom/index.ts ***!
  320. \******************************/
  321. /*! no static exports found */
  322. /***/ (function(module, exports, __webpack_require__) {
  323. "use strict";
  324. function __export(m) {
  325. for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
  326. }
  327. Object.defineProperty(exports, "__esModule", { value: true });
  328. __export(__webpack_require__(/*! ./customMaterial */ "../src/custom/customMaterial.ts"));
  329. /***/ }),
  330. /***/ "./legacy-custom.ts":
  331. /*!**************************!*\
  332. !*** ./legacy-custom.ts ***!
  333. \**************************/
  334. /*! no static exports found */
  335. /***/ (function(module, exports, __webpack_require__) {
  336. "use strict";
  337. /* WEBPACK VAR INJECTION */(function(global) {
  338. function __export(m) {
  339. for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
  340. }
  341. Object.defineProperty(exports, "__esModule", { value: true });
  342. var MatLib = __webpack_require__(/*! ../src/custom/index */ "../src/custom/index.ts");
  343. /**
  344. * This is the entry point for the UMD module.
  345. * The entry point for a future ESM package should be index.ts
  346. */
  347. var globalObject = (typeof global !== 'undefined') ? global : ((typeof window !== 'undefined') ? window : undefined);
  348. if (typeof globalObject !== "undefined") {
  349. for (var key in MatLib) {
  350. globalObject.BABYLON[key] = MatLib[key];
  351. }
  352. }
  353. __export(__webpack_require__(/*! ../src/custom/index */ "../src/custom/index.ts"));
  354. /* WEBPACK VAR INJECTION */}.call(this, __webpack_require__(/*! ./../../Tools/Gulp/node_modules/webpack/buildin/global.js */ "../../Tools/Gulp/node_modules/webpack/buildin/global.js")))
  355. /***/ }),
  356. /***/ "babylonjs":
  357. /*!****************************************************************************************************!*\
  358. !*** external {"root":"BABYLON","commonjs":"babylonjs","commonjs2":"babylonjs","amd":"babylonjs"} ***!
  359. \****************************************************************************************************/
  360. /*! no static exports found */
  361. /***/ (function(module, exports) {
  362. module.exports = __WEBPACK_EXTERNAL_MODULE_babylonjs__;
  363. /***/ })
  364. /******/ });
  365. });
  366. //# sourceMappingURL=babylon.customMaterial.js.map