babylon.perlinNoiseProceduralTexture.min.js 3.5 KB

1
  1. var BABYLON,__extends=this&&this.__extends||(function(){var r=function(e,n){return(r=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(e,n){e.__proto__=n}||function(e,n){for(var t in n)n.hasOwnProperty(t)&&(e[t]=n[t])})(e,n)};return function(e,n){function t(){this.constructor=e}r(e,n),e.prototype=null===n?Object.create(n):(t.prototype=n.prototype,new t)}})(),__decorate=this&&this.__decorate||function(e,n,t,r){var i,o=arguments.length,a=o<3?n:null===r?r=Object.getOwnPropertyDescriptor(n,t):r;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)a=Reflect.decorate(e,n,t,r);else for(var c=e.length-1;0<=c;c--)(i=e[c])&&(a=(o<3?i(a):3<o?i(n,t,a):i(n,t))||a);return 3<o&&a&&Object.defineProperty(n,t,a),a};!(function(i){var e=(function(a){function r(e,n,t,r,i){var o=a.call(this,e,n,"perlinNoiseProceduralTexture",t,r,i)||this;return o.time=0,o.timeScale=1,o.translationSpeed=1,o._currentTranslation=0,o.updateShaderUniforms(),o}return __extends(r,a),r.prototype.updateShaderUniforms=function(){this.setFloat("size",this.getRenderSize());var e=this.getScene();if(e){var n=e.getEngine().getDeltaTime();this.time+=n,this.setFloat("time",this.time*this.timeScale/1e3),this._currentTranslation+=n*this.translationSpeed/1e3,this.setFloat("translationSpeed",this._currentTranslation)}},r.prototype.render=function(e){this.updateShaderUniforms(),a.prototype.render.call(this,e)},r.prototype.resize=function(e,n){a.prototype.resize.call(this,e,n)},r.prototype.serialize=function(){var e=i.SerializationHelper.Serialize(this,a.prototype.serialize.call(this));return e.customType="BABYLON.PerlinNoiseProceduralTexture",e},r.Parse=function(e,n,t){return i.SerializationHelper.Parse((function(){return new r(e.name,e._size,n,void 0,e._generateMipMaps)}),e,n,t)},__decorate([i.serialize()],r.prototype,"time",void 0),__decorate([i.serialize()],r.prototype,"timeScale",void 0),__decorate([i.serialize()],r.prototype,"translationSpeed",void 0),r})(i.ProceduralTexture);i.PerlinNoiseProceduralTexture=e})(BABYLON||(BABYLON={})),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";