babylon.perlinNoiseProceduralTexture.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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-procedural-textures", ["babylonjs"], factory);
  6. else if(typeof exports === 'object')
  7. exports["babylonjs-procedural-textures"] = factory(require("babylonjs"));
  8. else
  9. root["PTLIB"] = 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-perlinNoise.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-perlinNoise.ts":
  122. /*!**************************************!*\
  123. !*** ./legacy/legacy-perlinNoise.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 proceduralTexture = __webpack_require__(/*! ../src/perlinNoise */ "./src/perlinNoise/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 proceduralTexture) {
  141. globalObject.BABYLON[key] = proceduralTexture[key];
  142. }
  143. }
  144. __export(__webpack_require__(/*! ../src/perlinNoise */ "./src/perlinNoise/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/perlinNoise/index.ts":
  148. /*!**********************************!*\
  149. !*** ./src/perlinNoise/index.ts ***!
  150. \**********************************/
  151. /*! no static exports found */
  152. /***/ (function(module, exports, __webpack_require__) {
  153. "use strict";
  154. function __export(m) {
  155. for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
  156. }
  157. Object.defineProperty(exports, "__esModule", { value: true });
  158. __export(__webpack_require__(/*! ./perlinNoiseProceduralTexture */ "./src/perlinNoise/perlinNoiseProceduralTexture.ts"));
  159. /***/ }),
  160. /***/ "./src/perlinNoise/perlinNoiseProceduralTexture.fragment.fx":
  161. /*!******************************************************************!*\
  162. !*** ./src/perlinNoise/perlinNoiseProceduralTexture.fragment.fx ***!
  163. \******************************************************************/
  164. /*! no static exports found */
  165. /***/ (function(module, exports) {
  166. module.exports = "\nprecision highp float;\n\nuniform float size;\nuniform float time;\nuniform float translationSpeed;\n\nvarying vec2 vUV;\n\nfloat r(float n)\n{\nreturn fract(cos(n*89.42)*343.42);\n}\nvec2 r(vec2 n)\n{\nreturn vec2(r(n.x*23.62-300.0+n.y*34.35),r(n.x*45.13+256.0+n.y*38.89)); \n}\nfloat worley(vec2 n,float s)\n{\nfloat dis=1.0;\nfor(int x=-1; x<=1; x++)\n{\nfor(int y=-1; y<=1; y++)\n{\nvec2 p=floor(n/s)+vec2(x,y);\nfloat d=length(r(p)+vec2(x,y)-fract(n/s));\nif (dis>d)\ndis=d;\n}\n}\nreturn 1.0-dis;\n}\nvec3 hash33(vec3 p3)\n{\np3=fract(p3*vec3(0.1031,0.11369,0.13787));\np3+=dot(p3,p3.yxz+19.19);\nreturn -1.0+2.0*fract(vec3((p3.x+p3.y)*p3.z,(p3.x+p3.z)*p3.y,(p3.y+p3.z)*p3.x));\n}\nfloat perlinNoise(vec3 p)\n{\nvec3 pi=floor(p);\nvec3 pf=p-pi;\nvec3 w=pf*pf*(3.0-2.0*pf);\nreturn mix(\nmix(\nmix(\ndot(pf-vec3(0,0,0),hash33(pi+vec3(0,0,0))),\ndot(pf-vec3(1,0,0),hash33(pi+vec3(1,0,0))),\nw.x\n),\nmix(\ndot(pf-vec3(0,0,1),hash33(pi+vec3(0,0,1))),\ndot(pf-vec3(1,0,1),hash33(pi+vec3(1,0,1))),\nw.x\n),\nw.z\n),\nmix(\nmix(\ndot(pf-vec3(0,1,0),hash33(pi+vec3(0,1,0))),\ndot(pf-vec3(1,1,0),hash33(pi+vec3(1,1,0))),\nw.x\n),\nmix(\ndot(pf-vec3(0,1,1),hash33(pi+vec3(0,1,1))),\ndot(pf-vec3(1,1,1),hash33(pi+vec3(1,1,1))),\nw.x\n),\nw.z\n),\nw.y\n);\n}\n\nvoid main(void)\n{\nvec2 uv=gl_FragCoord.xy+translationSpeed;\nfloat dis=(\n1.0+perlinNoise(vec3(uv/vec2(size,size),time*0.05)*8.0))\n*(1.0+(worley(uv,32.0)+ 0.5*worley(2.0*uv,32.0)+0.25*worley(4.0*uv,32.0))\n);\ngl_FragColor=vec4(vec3(dis/4.0),1.0);\n}\n"
  167. /***/ }),
  168. /***/ "./src/perlinNoise/perlinNoiseProceduralTexture.ts":
  169. /*!*********************************************************!*\
  170. !*** ./src/perlinNoise/perlinNoiseProceduralTexture.ts ***!
  171. \*********************************************************/
  172. /*! no static exports found */
  173. /***/ (function(module, exports, __webpack_require__) {
  174. "use strict";
  175. var __extends = (this && this.__extends) || (function () {
  176. var extendStatics = function (d, b) {
  177. extendStatics = Object.setPrototypeOf ||
  178. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  179. function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  180. return extendStatics(d, b);
  181. }
  182. return function (d, b) {
  183. extendStatics(d, b);
  184. function __() { this.constructor = d; }
  185. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  186. };
  187. })();
  188. var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
  189. var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  190. if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
  191. 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;
  192. return c > 3 && r && Object.defineProperty(target, key, r), r;
  193. };
  194. Object.defineProperty(exports, "__esModule", { value: true });
  195. var babylonjs_1 = __webpack_require__(/*! babylonjs */ "babylonjs");
  196. babylonjs_1.Effect.ShadersStore["perlinNoiseProceduralTexturePixelShader"] = __webpack_require__(/*! ./perlinNoiseProceduralTexture.fragment.fx */ "./src/perlinNoise/perlinNoiseProceduralTexture.fragment.fx");
  197. var PerlinNoiseProceduralTexture = /** @class */ (function (_super) {
  198. __extends(PerlinNoiseProceduralTexture, _super);
  199. function PerlinNoiseProceduralTexture(name, size, scene, fallbackTexture, generateMipMaps) {
  200. var _this = _super.call(this, name, size, "perlinNoiseProceduralTexture", scene, fallbackTexture, generateMipMaps) || this;
  201. _this.time = 0.0;
  202. _this.timeScale = 1.0;
  203. _this.translationSpeed = 1.0;
  204. _this._currentTranslation = 0;
  205. _this.updateShaderUniforms();
  206. return _this;
  207. }
  208. PerlinNoiseProceduralTexture.prototype.updateShaderUniforms = function () {
  209. this.setFloat("size", this.getRenderSize());
  210. var scene = this.getScene();
  211. if (!scene) {
  212. return;
  213. }
  214. var deltaTime = scene.getEngine().getDeltaTime();
  215. this.time += deltaTime;
  216. this.setFloat("time", this.time * this.timeScale / 1000);
  217. this._currentTranslation += deltaTime * this.translationSpeed / 1000.0;
  218. this.setFloat("translationSpeed", this._currentTranslation);
  219. };
  220. PerlinNoiseProceduralTexture.prototype.render = function (useCameraPostProcess) {
  221. this.updateShaderUniforms();
  222. _super.prototype.render.call(this, useCameraPostProcess);
  223. };
  224. PerlinNoiseProceduralTexture.prototype.resize = function (size, generateMipMaps) {
  225. _super.prototype.resize.call(this, size, generateMipMaps);
  226. };
  227. /**
  228. * Serializes this perlin noise procedural texture
  229. * @returns a serialized perlin noise procedural texture object
  230. */
  231. PerlinNoiseProceduralTexture.prototype.serialize = function () {
  232. var serializationObject = babylonjs_1.SerializationHelper.Serialize(this, _super.prototype.serialize.call(this));
  233. serializationObject.customType = "BABYLON.PerlinNoiseProceduralTexture";
  234. return serializationObject;
  235. };
  236. /**
  237. * Creates a Perlin Noise Procedural Texture from parsed perlin noise procedural texture data
  238. * @param parsedTexture defines parsed texture data
  239. * @param scene defines the current scene
  240. * @param rootUrl defines the root URL containing perlin noise procedural texture information
  241. * @returns a parsed Perlin Noise Procedural Texture
  242. */
  243. PerlinNoiseProceduralTexture.Parse = function (parsedTexture, scene, rootUrl) {
  244. var texture = babylonjs_1.SerializationHelper.Parse(function () { return new PerlinNoiseProceduralTexture(parsedTexture.name, parsedTexture._size, scene, undefined, parsedTexture._generateMipMaps); }, parsedTexture, scene, rootUrl);
  245. return texture;
  246. };
  247. __decorate([
  248. babylonjs_1.serialize()
  249. ], PerlinNoiseProceduralTexture.prototype, "time", void 0);
  250. __decorate([
  251. babylonjs_1.serialize()
  252. ], PerlinNoiseProceduralTexture.prototype, "timeScale", void 0);
  253. __decorate([
  254. babylonjs_1.serialize()
  255. ], PerlinNoiseProceduralTexture.prototype, "translationSpeed", void 0);
  256. return PerlinNoiseProceduralTexture;
  257. }(babylonjs_1.ProceduralTexture));
  258. exports.PerlinNoiseProceduralTexture = PerlinNoiseProceduralTexture;
  259. /***/ }),
  260. /***/ "babylonjs":
  261. /*!****************************************************************************************************!*\
  262. !*** external {"root":"BABYLON","commonjs":"babylonjs","commonjs2":"babylonjs","amd":"babylonjs"} ***!
  263. \****************************************************************************************************/
  264. /*! no static exports found */
  265. /***/ (function(module, exports) {
  266. module.exports = __WEBPACK_EXTERNAL_MODULE_babylonjs__;
  267. /***/ })
  268. /******/ });
  269. });
  270. //# sourceMappingURL=babylon.perlinNoiseProceduralTexture.js.map