babylon.gridMaterial.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  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.ts":
  148. /*!***********************************!*\
  149. !*** ./src/grid/grid.fragment.ts ***!
  150. \***********************************/
  151. /*! no static exports found */
  152. /***/ (function(module, exports, __webpack_require__) {
  153. "use strict";
  154. Object.defineProperty(exports, "__esModule", { value: true });
  155. var babylonjs_1 = __webpack_require__(/*! babylonjs */ "babylonjs");
  156. var name = 'gridPixelShader';
  157. exports.name = name;
  158. var shader = "#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}";
  159. exports.shader = shader;
  160. babylonjs_1.Effect.ShadersStore[name] = shader;
  161. /***/ }),
  162. /***/ "./src/grid/grid.vertex.ts":
  163. /*!*********************************!*\
  164. !*** ./src/grid/grid.vertex.ts ***!
  165. \*********************************/
  166. /*! no static exports found */
  167. /***/ (function(module, exports, __webpack_require__) {
  168. "use strict";
  169. Object.defineProperty(exports, "__esModule", { value: true });
  170. var babylonjs_1 = __webpack_require__(/*! babylonjs */ "babylonjs");
  171. var name = 'gridVertexShader';
  172. exports.name = name;
  173. var shader = "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}";
  174. exports.shader = shader;
  175. babylonjs_1.Effect.ShadersStore[name] = shader;
  176. /***/ }),
  177. /***/ "./src/grid/gridMaterial.ts":
  178. /*!**********************************!*\
  179. !*** ./src/grid/gridMaterial.ts ***!
  180. \**********************************/
  181. /*! no static exports found */
  182. /***/ (function(module, exports, __webpack_require__) {
  183. "use strict";
  184. var __extends = (this && this.__extends) || (function () {
  185. var extendStatics = function (d, b) {
  186. extendStatics = Object.setPrototypeOf ||
  187. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  188. function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  189. return extendStatics(d, b);
  190. }
  191. return function (d, b) {
  192. extendStatics(d, b);
  193. function __() { this.constructor = d; }
  194. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  195. };
  196. })();
  197. var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
  198. var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  199. if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
  200. 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;
  201. return c > 3 && r && Object.defineProperty(target, key, r), r;
  202. };
  203. Object.defineProperty(exports, "__esModule", { value: true });
  204. var babylonjs_1 = __webpack_require__(/*! babylonjs */ "babylonjs");
  205. __webpack_require__(/*! ./grid.fragment */ "./src/grid/grid.fragment.ts");
  206. __webpack_require__(/*! ./grid.vertex */ "./src/grid/grid.vertex.ts");
  207. var GridMaterialDefines = /** @class */ (function (_super) {
  208. __extends(GridMaterialDefines, _super);
  209. function GridMaterialDefines() {
  210. var _this = _super.call(this) || this;
  211. _this.TRANSPARENT = false;
  212. _this.FOG = false;
  213. _this.PREMULTIPLYALPHA = false;
  214. _this.rebuild();
  215. return _this;
  216. }
  217. return GridMaterialDefines;
  218. }(babylonjs_1.MaterialDefines));
  219. /**
  220. * The grid materials allows you to wrap any shape with a grid.
  221. * Colors are customizable.
  222. */
  223. var GridMaterial = /** @class */ (function (_super) {
  224. __extends(GridMaterial, _super);
  225. /**
  226. * constructor
  227. * @param name The name given to the material in order to identify it afterwards.
  228. * @param scene The scene the material is used in.
  229. */
  230. function GridMaterial(name, scene) {
  231. var _this = _super.call(this, name, scene) || this;
  232. /**
  233. * Main color of the grid (e.g. between lines)
  234. */
  235. _this.mainColor = babylonjs_1.Color3.Black();
  236. /**
  237. * Color of the grid lines.
  238. */
  239. _this.lineColor = babylonjs_1.Color3.Teal();
  240. /**
  241. * The scale of the grid compared to unit.
  242. */
  243. _this.gridRatio = 1.0;
  244. /**
  245. * Allows setting an offset for the grid lines.
  246. */
  247. _this.gridOffset = babylonjs_1.Vector3.Zero();
  248. /**
  249. * The frequency of thicker lines.
  250. */
  251. _this.majorUnitFrequency = 10;
  252. /**
  253. * The visibility of minor units in the grid.
  254. */
  255. _this.minorUnitVisibility = 0.33;
  256. /**
  257. * The grid opacity outside of the lines.
  258. */
  259. _this.opacity = 1.0;
  260. /**
  261. * Determine RBG output is premultiplied by alpha value.
  262. */
  263. _this.preMultiplyAlpha = false;
  264. _this._gridControl = new babylonjs_1.Vector4(_this.gridRatio, _this.majorUnitFrequency, _this.minorUnitVisibility, _this.opacity);
  265. return _this;
  266. }
  267. /**
  268. * Returns wehter or not the grid requires alpha blending.
  269. */
  270. GridMaterial.prototype.needAlphaBlending = function () {
  271. return this.opacity < 1.0;
  272. };
  273. GridMaterial.prototype.needAlphaBlendingForMesh = function (mesh) {
  274. return this.needAlphaBlending();
  275. };
  276. GridMaterial.prototype.isReadyForSubMesh = function (mesh, subMesh, useInstances) {
  277. if (this.isFrozen) {
  278. if (this._wasPreviouslyReady && subMesh.effect) {
  279. return true;
  280. }
  281. }
  282. if (!subMesh._materialDefines) {
  283. subMesh._materialDefines = new GridMaterialDefines();
  284. }
  285. var defines = subMesh._materialDefines;
  286. var scene = this.getScene();
  287. if (!this.checkReadyOnEveryCall && subMesh.effect) {
  288. if (this._renderId === scene.getRenderId()) {
  289. return true;
  290. }
  291. }
  292. if (defines.TRANSPARENT !== (this.opacity < 1.0)) {
  293. defines.TRANSPARENT = !defines.TRANSPARENT;
  294. defines.markAsUnprocessed();
  295. }
  296. if (defines.PREMULTIPLYALPHA != this.preMultiplyAlpha) {
  297. defines.PREMULTIPLYALPHA = !defines.PREMULTIPLYALPHA;
  298. defines.markAsUnprocessed();
  299. }
  300. babylonjs_1.MaterialHelper.PrepareDefinesForMisc(mesh, scene, false, false, this.fogEnabled, false, defines);
  301. // Get correct effect
  302. if (defines.isDirty) {
  303. defines.markAsProcessed();
  304. scene.resetCachedMaterial();
  305. // Attributes
  306. var attribs = [babylonjs_1.VertexBuffer.PositionKind, babylonjs_1.VertexBuffer.NormalKind];
  307. // Defines
  308. var join = defines.toString();
  309. subMesh.setEffect(scene.getEngine().createEffect("grid", attribs, ["projection", "worldView", "mainColor", "lineColor", "gridControl", "gridOffset", "vFogInfos", "vFogColor", "world", "view"], [], join, undefined, this.onCompiled, this.onError), defines);
  310. }
  311. if (!subMesh.effect || !subMesh.effect.isReady()) {
  312. return false;
  313. }
  314. this._renderId = scene.getRenderId();
  315. this._wasPreviouslyReady = true;
  316. return true;
  317. };
  318. GridMaterial.prototype.bindForSubMesh = function (world, mesh, subMesh) {
  319. var scene = this.getScene();
  320. var defines = subMesh._materialDefines;
  321. if (!defines) {
  322. return;
  323. }
  324. var effect = subMesh.effect;
  325. if (!effect) {
  326. return;
  327. }
  328. this._activeEffect = effect;
  329. // Matrices
  330. this.bindOnlyWorldMatrix(world);
  331. this._activeEffect.setMatrix("worldView", world.multiply(scene.getViewMatrix()));
  332. this._activeEffect.setMatrix("view", scene.getViewMatrix());
  333. this._activeEffect.setMatrix("projection", scene.getProjectionMatrix());
  334. // Uniforms
  335. if (this._mustRebind(scene, effect)) {
  336. this._activeEffect.setColor3("mainColor", this.mainColor);
  337. this._activeEffect.setColor3("lineColor", this.lineColor);
  338. this._activeEffect.setVector3("gridOffset", this.gridOffset);
  339. this._gridControl.x = this.gridRatio;
  340. this._gridControl.y = Math.round(this.majorUnitFrequency);
  341. this._gridControl.z = this.minorUnitVisibility;
  342. this._gridControl.w = this.opacity;
  343. this._activeEffect.setVector4("gridControl", this._gridControl);
  344. }
  345. // Fog
  346. babylonjs_1.MaterialHelper.BindFogParameters(scene, mesh, this._activeEffect);
  347. this._afterBind(mesh, this._activeEffect);
  348. };
  349. /**
  350. * Dispose the material and its associated resources.
  351. * @param forceDisposeEffect will also dispose the used effect when true
  352. */
  353. GridMaterial.prototype.dispose = function (forceDisposeEffect) {
  354. _super.prototype.dispose.call(this, forceDisposeEffect);
  355. };
  356. GridMaterial.prototype.clone = function (name) {
  357. var _this = this;
  358. return babylonjs_1.SerializationHelper.Clone(function () { return new GridMaterial(name, _this.getScene()); }, this);
  359. };
  360. GridMaterial.prototype.serialize = function () {
  361. var serializationObject = babylonjs_1.SerializationHelper.Serialize(this);
  362. serializationObject.customType = "BABYLON.GridMaterial";
  363. return serializationObject;
  364. };
  365. GridMaterial.prototype.getClassName = function () {
  366. return "GridMaterial";
  367. };
  368. GridMaterial.Parse = function (source, scene, rootUrl) {
  369. return babylonjs_1.SerializationHelper.Parse(function () { return new GridMaterial(source.name, scene); }, source, scene, rootUrl);
  370. };
  371. __decorate([
  372. babylonjs_1.serializeAsColor3()
  373. ], GridMaterial.prototype, "mainColor", void 0);
  374. __decorate([
  375. babylonjs_1.serializeAsColor3()
  376. ], GridMaterial.prototype, "lineColor", void 0);
  377. __decorate([
  378. babylonjs_1.serialize()
  379. ], GridMaterial.prototype, "gridRatio", void 0);
  380. __decorate([
  381. babylonjs_1.serializeAsColor3()
  382. ], GridMaterial.prototype, "gridOffset", void 0);
  383. __decorate([
  384. babylonjs_1.serialize()
  385. ], GridMaterial.prototype, "majorUnitFrequency", void 0);
  386. __decorate([
  387. babylonjs_1.serialize()
  388. ], GridMaterial.prototype, "minorUnitVisibility", void 0);
  389. __decorate([
  390. babylonjs_1.serialize()
  391. ], GridMaterial.prototype, "opacity", void 0);
  392. __decorate([
  393. babylonjs_1.serialize()
  394. ], GridMaterial.prototype, "preMultiplyAlpha", void 0);
  395. return GridMaterial;
  396. }(BABYLON.PushMaterial));
  397. exports.GridMaterial = GridMaterial;
  398. /***/ }),
  399. /***/ "./src/grid/index.ts":
  400. /*!***************************!*\
  401. !*** ./src/grid/index.ts ***!
  402. \***************************/
  403. /*! no static exports found */
  404. /***/ (function(module, exports, __webpack_require__) {
  405. "use strict";
  406. function __export(m) {
  407. for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
  408. }
  409. Object.defineProperty(exports, "__esModule", { value: true });
  410. __export(__webpack_require__(/*! ./gridMaterial */ "./src/grid/gridMaterial.ts"));
  411. /***/ }),
  412. /***/ "babylonjs":
  413. /*!****************************************************************************************************!*\
  414. !*** external {"root":"BABYLON","commonjs":"babylonjs","commonjs2":"babylonjs","amd":"babylonjs"} ***!
  415. \****************************************************************************************************/
  416. /*! no static exports found */
  417. /***/ (function(module, exports) {
  418. module.exports = __WEBPACK_EXTERNAL_MODULE_babylonjs__;
  419. /***/ })
  420. /******/ });
  421. });
  422. //# sourceMappingURL=babylon.gridMaterial.js.map