babylon.fireProceduralTexture.min.js 5.2 KB

1
  1. var BABYLON,__extends=this&&this.__extends||(function(){var t=function(e,r){return(t=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(e,r){e.__proto__=r}||function(e,r){for(var o in r)r.hasOwnProperty(o)&&(e[o]=r[o])})(e,r)};return function(e,r){function o(){this.constructor=e}t(e,r),e.prototype=null===r?Object.create(r):(o.prototype=r.prototype,new o)}})(),__decorate=this&&this.__decorate||function(e,r,o,t){var n,i=arguments.length,l=i<3?r:null===t?t=Object.getOwnPropertyDescriptor(r,o):t;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)l=Reflect.decorate(e,r,o,t);else for(var a=e.length-1;0<=a;a--)(n=e[a])&&(l=(i<3?n(l):3<i?n(r,o,l):n(r,o))||l);return 3<i&&l&&Object.defineProperty(r,o,l),l};!(function(s){var e=(function(l){function a(e,r,o,t,n){var i=l.call(this,e,r,"fireProceduralTexture",o,t,n)||this;return i._time=0,i._speed=new s.Vector2(.5,.3),i._autoGenerateTime=!0,i._alphaThreshold=.5,i._fireColors=a.RedFireColors,i.updateShaderUniforms(),i}return __extends(a,l),a.prototype.updateShaderUniforms=function(){this.setFloat("time",this._time),this.setVector2("speed",this._speed),this.setColor3("c1",this._fireColors[0]),this.setColor3("c2",this._fireColors[1]),this.setColor3("c3",this._fireColors[2]),this.setColor3("c4",this._fireColors[3]),this.setColor3("c5",this._fireColors[4]),this.setColor3("c6",this._fireColors[5]),this.setFloat("alphaThreshold",this._alphaThreshold)},a.prototype.render=function(e){var r=this.getScene();this._autoGenerateTime&&r&&(this._time+=.03*r.getAnimationRatio(),this.updateShaderUniforms()),l.prototype.render.call(this,e)},Object.defineProperty(a,"PurpleFireColors",{get:function(){return[new s.Color3(.5,0,1),new s.Color3(.9,0,1),new s.Color3(.2,0,1),new s.Color3(1,.9,1),new s.Color3(.1,.1,1),new s.Color3(.9,.9,1)]},enumerable:!0,configurable:!0}),Object.defineProperty(a,"GreenFireColors",{get:function(){return[new s.Color3(.5,1,0),new s.Color3(.5,1,0),new s.Color3(.3,.4,0),new s.Color3(.5,1,0),new s.Color3(.2,0,0),new s.Color3(.5,1,0)]},enumerable:!0,configurable:!0}),Object.defineProperty(a,"RedFireColors",{get:function(){return[new s.Color3(.5,0,.1),new s.Color3(.9,0,0),new s.Color3(.2,0,0),new s.Color3(1,.9,0),new s.Color3(.1,.1,.1),new s.Color3(.9,.9,.9)]},enumerable:!0,configurable:!0}),Object.defineProperty(a,"BlueFireColors",{get:function(){return[new s.Color3(.1,0,.5),new s.Color3(0,0,.5),new s.Color3(.1,0,.2),new s.Color3(0,0,1),new s.Color3(.1,.2,.3),new s.Color3(0,.2,.9)]},enumerable:!0,configurable:!0}),Object.defineProperty(a.prototype,"autoGenerateTime",{get:function(){return this._autoGenerateTime},set:function(e){this._autoGenerateTime=e},enumerable:!0,configurable:!0}),Object.defineProperty(a.prototype,"fireColors",{get:function(){return this._fireColors},set:function(e){this._fireColors=e,this.updateShaderUniforms()},enumerable:!0,configurable:!0}),Object.defineProperty(a.prototype,"time",{get:function(){return this._time},set:function(e){this._time=e,this.updateShaderUniforms()},enumerable:!0,configurable:!0}),Object.defineProperty(a.prototype,"speed",{get:function(){return this._speed},set:function(e){this._speed=e,this.updateShaderUniforms()},enumerable:!0,configurable:!0}),Object.defineProperty(a.prototype,"alphaThreshold",{get:function(){return this._alphaThreshold},set:function(e){this._alphaThreshold=e,this.updateShaderUniforms()},enumerable:!0,configurable:!0}),a.prototype.serialize=function(){var e=s.SerializationHelper.Serialize(this,l.prototype.serialize.call(this));e.customType="BABYLON.FireProceduralTexture",e.fireColors=[];for(var r=0;r<this._fireColors.length;r++)e.fireColors.push(this._fireColors[r].asArray());return e},a.Parse=function(e,r,o){for(var t=s.SerializationHelper.Parse((function(){return new a(e.name,e._size,r,void 0,e._generateMipMaps)}),e,r,o),n=[],i=0;i<e.fireColors.length;i++)n.push(s.Color3.FromArray(e.fireColors[i]));return t.fireColors=n,t},__decorate([s.serialize()],a.prototype,"autoGenerateTime",null),__decorate([s.serialize()],a.prototype,"time",null),__decorate([s.serializeAsVector2()],a.prototype,"speed",null),__decorate([s.serialize()],a.prototype,"alphaThreshold",null),a})(s.ProceduralTexture);s.FireProceduralTexture=e})(BABYLON||(BABYLON={})),BABYLON.Effect.ShadersStore.fireProceduralTexturePixelShader="precision highp float;\nuniform float time;\nuniform vec3 c1;\nuniform vec3 c2;\nuniform vec3 c3;\nuniform vec3 c4;\nuniform vec3 c5;\nuniform vec3 c6;\nuniform vec2 speed;\nuniform float shift;\nuniform float alphaThreshold;\nvarying vec2 vUV;\nfloat rand(vec2 n) {\nreturn fract(cos(dot(n,vec2(12.9898,4.1414)))*43758.5453);\n}\nfloat noise(vec2 n) {\nconst vec2 d=vec2(0.0,1.0);\nvec2 b=floor(n),f=smoothstep(vec2(0.0),vec2(1.0),fract(n));\nreturn mix(mix(rand(b),rand(b+d.yx),f.x),mix(rand(b+d.xy),rand(b+d.yy),f.x),f.y);\n}\nfloat fbm(vec2 n) {\nfloat total=0.0,amplitude=1.0;\nfor (int i=0; i<4; i++) {\ntotal+=noise(n)*amplitude;\nn+=n;\namplitude*=0.5;\n}\nreturn total;\n}\nvoid main() {\nvec2 p=vUV*8.0;\nfloat q=fbm(p-time*0.1);\nvec2 r=vec2(fbm(p+q+time*speed.x-p.x-p.y),fbm(p+q-time*speed.y));\nvec3 c=mix(c1,c2,fbm(p+r))+mix(c3,c4,r.x)-mix(c5,c6,r.y);\nvec3 color=c*cos(shift*vUV.y);\nfloat luminance=dot(color.rgb,vec3(0.3,0.59,0.11));\ngl_FragColor=vec4(color,luminance*alphaThreshold+(1.0-alphaThreshold));\n}";