babylon.perlinNoiseProceduralTexture.js 6.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /// <reference path="../../../dist/preview release/babylon.d.ts"/>
  2. var __extends = (this && this.__extends) || (function () {
  3. var extendStatics = Object.setPrototypeOf ||
  4. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  5. function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  6. return function (d, b) {
  7. extendStatics(d, b);
  8. function __() { this.constructor = d; }
  9. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  10. };
  11. })();
  12. var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
  13. var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  14. if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
  15. 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;
  16. return c > 3 && r && Object.defineProperty(target, key, r), r;
  17. };
  18. var BABYLON;
  19. (function (BABYLON) {
  20. var PerlinNoiseProceduralTexture = /** @class */ (function (_super) {
  21. __extends(PerlinNoiseProceduralTexture, _super);
  22. function PerlinNoiseProceduralTexture(name, size, scene, fallbackTexture, generateMipMaps) {
  23. var _this = _super.call(this, name, size, "perlinNoiseProceduralTexture", scene, fallbackTexture, generateMipMaps) || this;
  24. _this.time = 0.0;
  25. _this.timeScale = 1.0;
  26. _this.translationSpeed = 1.0;
  27. _this._currentTranslation = 0;
  28. _this.updateShaderUniforms();
  29. return _this;
  30. }
  31. PerlinNoiseProceduralTexture.prototype.updateShaderUniforms = function () {
  32. this.setFloat("size", this.getRenderSize());
  33. var scene = this.getScene();
  34. if (!scene) {
  35. return;
  36. }
  37. var deltaTime = scene.getEngine().getDeltaTime();
  38. this.time += deltaTime;
  39. this.setFloat("time", this.time * this.timeScale / 1000);
  40. this._currentTranslation += deltaTime * this.translationSpeed / 1000.0;
  41. this.setFloat("translationSpeed", this._currentTranslation);
  42. };
  43. PerlinNoiseProceduralTexture.prototype.render = function (useCameraPostProcess) {
  44. this.updateShaderUniforms();
  45. _super.prototype.render.call(this, useCameraPostProcess);
  46. };
  47. PerlinNoiseProceduralTexture.prototype.resize = function (size, generateMipMaps) {
  48. _super.prototype.resize.call(this, size, generateMipMaps);
  49. };
  50. /**
  51. * Serializes this perlin noise procedural texture
  52. * @returns a serialized perlin noise procedural texture object
  53. */
  54. PerlinNoiseProceduralTexture.prototype.serialize = function () {
  55. var serializationObject = BABYLON.SerializationHelper.Serialize(this, _super.prototype.serialize.call(this));
  56. serializationObject.customType = "BABYLON.PerlinNoiseProceduralTexture";
  57. return serializationObject;
  58. };
  59. /**
  60. * Creates a Perlin Noise Procedural Texture from parsed perlin noise procedural texture data
  61. * @param parsedTexture defines parsed texture data
  62. * @param scene defines the current scene
  63. * @param rootUrl defines the root URL containing perlin noise procedural texture information
  64. * @returns a parsed Perlin Noise Procedural Texture
  65. */
  66. PerlinNoiseProceduralTexture.Parse = function (parsedTexture, scene, rootUrl) {
  67. var texture = BABYLON.SerializationHelper.Parse(function () { return new PerlinNoiseProceduralTexture(parsedTexture.name, parsedTexture._size, scene, undefined, parsedTexture._generateMipMaps); }, parsedTexture, scene, rootUrl);
  68. return texture;
  69. };
  70. __decorate([
  71. BABYLON.serialize()
  72. ], PerlinNoiseProceduralTexture.prototype, "time", void 0);
  73. __decorate([
  74. BABYLON.serialize()
  75. ], PerlinNoiseProceduralTexture.prototype, "timeScale", void 0);
  76. __decorate([
  77. BABYLON.serialize()
  78. ], PerlinNoiseProceduralTexture.prototype, "translationSpeed", void 0);
  79. return PerlinNoiseProceduralTexture;
  80. }(BABYLON.ProceduralTexture));
  81. BABYLON.PerlinNoiseProceduralTexture = PerlinNoiseProceduralTexture;
  82. })(BABYLON || (BABYLON = {}));
  83. //# sourceMappingURL=babylon.perlinNoiseProceduralTexture.js.map
  84. BABYLON.Effect.ShadersStore['perlinNoiseProceduralTexturePixelShader'] = "\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";