babylon.gridMaterial.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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/legacy-grid.ts");
  95. /******/ })
  96. /************************************************************************/
  97. /******/ ({
  98. /***/ "../node_modules/webpack/buildin/global.js":
  99. /*!*************************************************!*\
  100. !*** ../node_modules/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. /***/ "./legacy/legacy-grid.ts":
  122. /*!*******************************!*\
  123. !*** ./legacy/legacy-grid.ts ***!
  124. \*******************************/
  125. /*! no static exports found */
  126. /***/ (function(module, exports, __webpack_require__) {
  127. "use strict";
  128. /* WEBPACK VAR INJECTION */(function(global) {
  129. function __export(m) {
  130. for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
  131. }
  132. Object.defineProperty(exports, "__esModule", { value: true });
  133. var MatLib = __webpack_require__(/*! ../src/grid/index */ "./src/grid/index.ts");
  134. /**
  135. * This is the entry point for the UMD module.
  136. * The entry point for a future ESM package should be index.ts
  137. */
  138. var globalObject = (typeof global !== 'undefined') ? global : ((typeof window !== 'undefined') ? window : undefined);
  139. if (typeof globalObject !== "undefined") {
  140. for (var key in MatLib) {
  141. globalObject.BABYLON[key] = MatLib[key];
  142. }
  143. }
  144. __export(__webpack_require__(/*! ../src/grid/index */ "./src/grid/index.ts"));
  145. /* WEBPACK VAR INJECTION */}.call(this, __webpack_require__(/*! ./../../node_modules/webpack/buildin/global.js */ "../node_modules/webpack/buildin/global.js")))
  146. /***/ }),
  147. /***/ "./src/grid/grid.fragment.fx":
  148. /*!***********************************!*\
  149. !*** ./src/grid/grid.fragment.fx ***!
  150. \***********************************/
  151. /*! no static exports found */
  152. /***/ (function(module, exports) {
  153. module.exports = "#extension GL_OES_standard_derivatives : enable\n#define SQRT2 1.41421356\n#define PI 3.14159\nprecision highp float;\nuniform vec3 mainColor;\nuniform vec3 lineColor;\nuniform vec4 gridControl;\nuniform vec3 gridOffset;\n\n#ifdef TRANSPARENT\nvarying vec4 vCameraSpacePosition;\n#endif\nvarying vec3 vPosition;\nvarying vec3 vNormal;\n#include<fogFragmentDeclaration>\nfloat getVisibility(float position) {\n\nfloat majorGridFrequency=gridControl.y;\nif (floor(position+0.5) == floor(position/majorGridFrequency+0.5)*majorGridFrequency)\n{\nreturn 1.0;\n} \nreturn gridControl.z;\n}\nfloat getAnisotropicAttenuation(float differentialLength) {\nconst float maxNumberOfLines=10.0;\nreturn clamp(1.0/(differentialLength+1.0)-1.0/maxNumberOfLines,0.0,1.0);\n}\nfloat isPointOnLine(float position,float differentialLength) {\nfloat fractionPartOfPosition=position-floor(position+0.5); \nfractionPartOfPosition/=differentialLength; \nfractionPartOfPosition=clamp(fractionPartOfPosition,-1.,1.);\nfloat result=0.5+0.5*cos(fractionPartOfPosition*PI); \nreturn result; \n}\nfloat contributionOnAxis(float position) {\nfloat differentialLength=length(vec2(dFdx(position),dFdy(position)));\ndifferentialLength*=SQRT2; \n\nfloat result=isPointOnLine(position,differentialLength);\n\nfloat visibility=getVisibility(position);\nresult*=visibility;\n\nfloat anisotropicAttenuation=getAnisotropicAttenuation(differentialLength);\nresult*=anisotropicAttenuation;\nreturn result;\n}\nfloat normalImpactOnAxis(float x) {\nfloat normalImpact=clamp(1.0-3.0*abs(x*x*x),0.0,1.0);\nreturn normalImpact;\n}\nvoid main(void) {\n\nfloat gridRatio=gridControl.x;\nvec3 gridPos=(vPosition+gridOffset)/gridRatio;\n\nfloat x=contributionOnAxis(gridPos.x);\nfloat y=contributionOnAxis(gridPos.y);\nfloat z=contributionOnAxis(gridPos.z);\n\nvec3 normal=normalize(vNormal);\nx*=normalImpactOnAxis(normal.x);\ny*=normalImpactOnAxis(normal.y);\nz*=normalImpactOnAxis(normal.z);\n\nfloat grid=clamp(x+y+z,0.,1.);\n\nvec3 color=mix(mainColor,lineColor,grid);\n#ifdef FOG\n#include<fogFragment>\n#endif\n#ifdef TRANSPARENT\nfloat distanceToFragment=length(vCameraSpacePosition.xyz);\nfloat cameraPassThrough=clamp(distanceToFragment-0.25,0.0,1.0);\nfloat opacity=clamp(grid,0.08,cameraPassThrough*gridControl.w*grid);\ngl_FragColor=vec4(color.rgb,opacity);\n#ifdef PREMULTIPLYALPHA\ngl_FragColor.rgb*=opacity;\n#endif\n#else\n\ngl_FragColor=vec4(color.rgb,1.0);\n#endif\n}"
  154. /***/ }),
  155. /***/ "./src/grid/grid.vertex.fx":
  156. /*!*********************************!*\
  157. !*** ./src/grid/grid.vertex.fx ***!
  158. \*********************************/
  159. /*! no static exports found */
  160. /***/ (function(module, exports) {
  161. module.exports = "precision highp float;\n\nattribute vec3 position;\nattribute vec3 normal;\n\nuniform mat4 projection;\nuniform mat4 world;\nuniform mat4 view;\nuniform mat4 worldView;\n\n#ifdef TRANSPARENT\nvarying vec4 vCameraSpacePosition;\n#endif\nvarying vec3 vPosition;\nvarying vec3 vNormal;\n#include<fogVertexDeclaration>\nvoid main(void) {\n#ifdef FOG\nvec4 worldPos=world*vec4(position,1.0);\n#endif\n#include<fogVertex>\nvec4 cameraSpacePosition=worldView*vec4(position,1.0);\ngl_Position=projection*cameraSpacePosition;\n#ifdef TRANSPARENT\nvCameraSpacePosition=cameraSpacePosition;\n#endif\nvPosition=position;\nvNormal=normal;\n}"
  162. /***/ }),
  163. /***/ "./src/grid/gridMaterial.ts":
  164. /*!**********************************!*\
  165. !*** ./src/grid/gridMaterial.ts ***!
  166. \**********************************/
  167. /*! no static exports found */
  168. /***/ (function(module, exports, __webpack_require__) {
  169. "use strict";
  170. var __extends = (this && this.__extends) || (function () {
  171. var extendStatics = function (d, b) {
  172. extendStatics = Object.setPrototypeOf ||
  173. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  174. function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  175. return extendStatics(d, b);
  176. }
  177. return function (d, b) {
  178. extendStatics(d, b);
  179. function __() { this.constructor = d; }
  180. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  181. };
  182. })();
  183. var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
  184. var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  185. if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
  186. else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
  187. return c > 3 && r && Object.defineProperty(target, key, r), r;
  188. };
  189. Object.defineProperty(exports, "__esModule", { value: true });
  190. var babylonjs_1 = __webpack_require__(/*! babylonjs */ "babylonjs");
  191. babylonjs_1.Effect.ShadersStore["gridPixelShader"] = __webpack_require__(/*! ./grid.fragment.fx */ "./src/grid/grid.fragment.fx");
  192. babylonjs_1.Effect.ShadersStore["gridVertexShader"] = __webpack_require__(/*! ./grid.vertex.fx */ "./src/grid/grid.vertex.fx");
  193. var GridMaterialDefines = /** @class */ (function (_super) {
  194. __extends(GridMaterialDefines, _super);
  195. function GridMaterialDefines() {
  196. var _this = _super.call(this) || this;
  197. _this.TRANSPARENT = false;
  198. _this.FOG = false;
  199. _this.PREMULTIPLYALPHA = false;
  200. _this.rebuild();
  201. return _this;
  202. }
  203. return GridMaterialDefines;
  204. }(babylonjs_1.MaterialDefines));
  205. /**
  206. * The grid materials allows you to wrap any shape with a grid.
  207. * Colors are customizable.
  208. */
  209. var GridMaterial = /** @class */ (function (_super) {
  210. __extends(GridMaterial, _super);
  211. /**
  212. * constructor
  213. * @param name The name given to the material in order to identify it afterwards.
  214. * @param scene The scene the material is used in.
  215. */
  216. function GridMaterial(name, scene) {
  217. var _this = _super.call(this, name, scene) || this;
  218. /**
  219. * Main color of the grid (e.g. between lines)
  220. */
  221. _this.mainColor = babylonjs_1.Color3.Black();
  222. /**
  223. * Color of the grid lines.
  224. */
  225. _this.lineColor = babylonjs_1.Color3.Teal();
  226. /**
  227. * The scale of the grid compared to unit.
  228. */
  229. _this.gridRatio = 1.0;
  230. /**
  231. * Allows setting an offset for the grid lines.
  232. */
  233. _this.gridOffset = babylonjs_1.Vector3.Zero();
  234. /**
  235. * The frequency of thicker lines.
  236. */
  237. _this.majorUnitFrequency = 10;
  238. /**
  239. * The visibility of minor units in the grid.
  240. */
  241. _this.minorUnitVisibility = 0.33;
  242. /**
  243. * The grid opacity outside of the lines.
  244. */
  245. _this.opacity = 1.0;
  246. /**
  247. * Determine RBG output is premultiplied by alpha value.
  248. */
  249. _this.preMultiplyAlpha = false;
  250. _this._gridControl = new babylonjs_1.Vector4(_this.gridRatio, _this.majorUnitFrequency, _this.minorUnitVisibility, _this.opacity);
  251. return _this;
  252. }
  253. /**
  254. * Returns wehter or not the grid requires alpha blending.
  255. */
  256. GridMaterial.prototype.needAlphaBlending = function () {
  257. return this.opacity < 1.0;
  258. };
  259. GridMaterial.prototype.needAlphaBlendingForMesh = function (mesh) {
  260. return this.needAlphaBlending();
  261. };
  262. GridMaterial.prototype.isReadyForSubMesh = function (mesh, subMesh, useInstances) {
  263. if (this.isFrozen) {
  264. if (this._wasPreviouslyReady && subMesh.effect) {
  265. return true;
  266. }
  267. }
  268. if (!subMesh._materialDefines) {
  269. subMesh._materialDefines = new GridMaterialDefines();
  270. }
  271. var defines = subMesh._materialDefines;
  272. var scene = this.getScene();
  273. if (!this.checkReadyOnEveryCall && subMesh.effect) {
  274. if (this._renderId === scene.getRenderId()) {
  275. return true;
  276. }
  277. }
  278. if (defines.TRANSPARENT !== (this.opacity < 1.0)) {
  279. defines.TRANSPARENT = !defines.TRANSPARENT;
  280. defines.markAsUnprocessed();
  281. }
  282. if (defines.PREMULTIPLYALPHA != this.preMultiplyAlpha) {
  283. defines.PREMULTIPLYALPHA = !defines.PREMULTIPLYALPHA;
  284. defines.markAsUnprocessed();
  285. }
  286. babylonjs_1.MaterialHelper.PrepareDefinesForMisc(mesh, scene, false, false, this.fogEnabled, false, defines);
  287. // Get correct effect
  288. if (defines.isDirty) {
  289. defines.markAsProcessed();
  290. scene.resetCachedMaterial();
  291. // Attributes
  292. var attribs = [babylonjs_1.VertexBuffer.PositionKind, babylonjs_1.VertexBuffer.NormalKind];
  293. // Defines
  294. var join = defines.toString();
  295. subMesh.setEffect(scene.getEngine().createEffect("grid", attribs, ["projection", "worldView", "mainColor", "lineColor", "gridControl", "gridOffset", "vFogInfos", "vFogColor", "world", "view"], [], join, undefined, this.onCompiled, this.onError), defines);
  296. }
  297. if (!subMesh.effect || !subMesh.effect.isReady()) {
  298. return false;
  299. }
  300. this._renderId = scene.getRenderId();
  301. this._wasPreviouslyReady = true;
  302. return true;
  303. };
  304. GridMaterial.prototype.bindForSubMesh = function (world, mesh, subMesh) {
  305. var scene = this.getScene();
  306. var defines = subMesh._materialDefines;
  307. if (!defines) {
  308. return;
  309. }
  310. var effect = subMesh.effect;
  311. if (!effect) {
  312. return;
  313. }
  314. this._activeEffect = effect;
  315. // Matrices
  316. this.bindOnlyWorldMatrix(world);
  317. this._activeEffect.setMatrix("worldView", world.multiply(scene.getViewMatrix()));
  318. this._activeEffect.setMatrix("view", scene.getViewMatrix());
  319. this._activeEffect.setMatrix("projection", scene.getProjectionMatrix());
  320. // Uniforms
  321. if (this._mustRebind(scene, effect)) {
  322. this._activeEffect.setColor3("mainColor", this.mainColor);
  323. this._activeEffect.setColor3("lineColor", this.lineColor);
  324. this._activeEffect.setVector3("gridOffset", this.gridOffset);
  325. this._gridControl.x = this.gridRatio;
  326. this._gridControl.y = Math.round(this.majorUnitFrequency);
  327. this._gridControl.z = this.minorUnitVisibility;
  328. this._gridControl.w = this.opacity;
  329. this._activeEffect.setVector4("gridControl", this._gridControl);
  330. }
  331. // Fog
  332. babylonjs_1.MaterialHelper.BindFogParameters(scene, mesh, this._activeEffect);
  333. this._afterBind(mesh, this._activeEffect);
  334. };
  335. /**
  336. * Dispose the material and its associated resources.
  337. * @param forceDisposeEffect will also dispose the used effect when true
  338. */
  339. GridMaterial.prototype.dispose = function (forceDisposeEffect) {
  340. _super.prototype.dispose.call(this, forceDisposeEffect);
  341. };
  342. GridMaterial.prototype.clone = function (name) {
  343. var _this = this;
  344. return babylonjs_1.SerializationHelper.Clone(function () { return new GridMaterial(name, _this.getScene()); }, this);
  345. };
  346. GridMaterial.prototype.serialize = function () {
  347. var serializationObject = babylonjs_1.SerializationHelper.Serialize(this);
  348. serializationObject.customType = "BABYLON.GridMaterial";
  349. return serializationObject;
  350. };
  351. GridMaterial.prototype.getClassName = function () {
  352. return "GridMaterial";
  353. };
  354. GridMaterial.Parse = function (source, scene, rootUrl) {
  355. return babylonjs_1.SerializationHelper.Parse(function () { return new GridMaterial(source.name, scene); }, source, scene, rootUrl);
  356. };
  357. __decorate([
  358. babylonjs_1.serializeAsColor3()
  359. ], GridMaterial.prototype, "mainColor", void 0);
  360. __decorate([
  361. babylonjs_1.serializeAsColor3()
  362. ], GridMaterial.prototype, "lineColor", void 0);
  363. __decorate([
  364. babylonjs_1.serialize()
  365. ], GridMaterial.prototype, "gridRatio", void 0);
  366. __decorate([
  367. babylonjs_1.serializeAsColor3()
  368. ], GridMaterial.prototype, "gridOffset", void 0);
  369. __decorate([
  370. babylonjs_1.serialize()
  371. ], GridMaterial.prototype, "majorUnitFrequency", void 0);
  372. __decorate([
  373. babylonjs_1.serialize()
  374. ], GridMaterial.prototype, "minorUnitVisibility", void 0);
  375. __decorate([
  376. babylonjs_1.serialize()
  377. ], GridMaterial.prototype, "opacity", void 0);
  378. __decorate([
  379. babylonjs_1.serialize()
  380. ], GridMaterial.prototype, "preMultiplyAlpha", void 0);
  381. return GridMaterial;
  382. }(BABYLON.PushMaterial));
  383. exports.GridMaterial = GridMaterial;
  384. /***/ }),
  385. /***/ "./src/grid/index.ts":
  386. /*!***************************!*\
  387. !*** ./src/grid/index.ts ***!
  388. \***************************/
  389. /*! no static exports found */
  390. /***/ (function(module, exports, __webpack_require__) {
  391. "use strict";
  392. function __export(m) {
  393. for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
  394. }
  395. Object.defineProperty(exports, "__esModule", { value: true });
  396. __export(__webpack_require__(/*! ./gridMaterial */ "./src/grid/gridMaterial.ts"));
  397. /***/ }),
  398. /***/ "babylonjs":
  399. /*!****************************************************************************************************!*\
  400. !*** external {"root":"BABYLON","commonjs":"babylonjs","commonjs2":"babylonjs","amd":"babylonjs"} ***!
  401. \****************************************************************************************************/
  402. /*! no static exports found */
  403. /***/ (function(module, exports) {
  404. module.exports = __WEBPACK_EXTERNAL_MODULE_babylonjs__;
  405. /***/ })
  406. /******/ });
  407. });
  408. //# sourceMappingURL=babylon.gridMaterial.js.map