babylon.fireProceduralTexture.min.js 3.8 KB

1
  1. var BABYLON;!function(e){var r=function(r){function o(n,t,i,l,s){r.call(this,n,t,"fireProceduralTexture",i,l,s),this._time=0,this._speed=new e.Vector2(.5,.3),this._autoGenerateTime=!0,this._alphaThreshold=.5,this._fireColors=o.RedFireColors,this.updateShaderUniforms()}return __extends(o,r),o.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)},o.prototype.render=function(e){this._autoGenerateTime&&(this._time+=.03*this.getScene().getAnimationRatio(),this.updateShaderUniforms()),r.prototype.render.call(this,e)},Object.defineProperty(o,"PurpleFireColors",{get:function(){return[new e.Color3(.5,0,1),new e.Color3(.9,0,1),new e.Color3(.2,0,1),new e.Color3(1,.9,1),new e.Color3(.1,.1,1),new e.Color3(.9,.9,1)]},enumerable:!0,configurable:!0}),Object.defineProperty(o,"GreenFireColors",{get:function(){return[new e.Color3(.5,1,0),new e.Color3(.5,1,0),new e.Color3(.3,.4,0),new e.Color3(.5,1,0),new e.Color3(.2,0,0),new e.Color3(.5,1,0)]},enumerable:!0,configurable:!0}),Object.defineProperty(o,"RedFireColors",{get:function(){return[new e.Color3(.5,0,.1),new e.Color3(.9,0,0),new e.Color3(.2,0,0),new e.Color3(1,.9,0),new e.Color3(.1,.1,.1),new e.Color3(.9,.9,.9)]},enumerable:!0,configurable:!0}),Object.defineProperty(o,"BlueFireColors",{get:function(){return[new e.Color3(.1,0,.5),new e.Color3(0,0,.5),new e.Color3(.1,0,.2),new e.Color3(0,0,1),new e.Color3(.1,.2,.3),new e.Color3(0,.2,.9)]},enumerable:!0,configurable:!0}),Object.defineProperty(o.prototype,"fireColors",{get:function(){return this._fireColors},set:function(e){this._fireColors=e,this.updateShaderUniforms()},enumerable:!0,configurable:!0}),Object.defineProperty(o.prototype,"time",{get:function(){return this._time},set:function(e){this._time=e,this.updateShaderUniforms()},enumerable:!0,configurable:!0}),Object.defineProperty(o.prototype,"speed",{get:function(){return this._speed},set:function(e){this._speed=e,this.updateShaderUniforms()},enumerable:!0,configurable:!0}),Object.defineProperty(o.prototype,"alphaThreshold",{get:function(){return this._alphaThreshold},set:function(e){this._alphaThreshold=e,this.updateShaderUniforms()},enumerable:!0,configurable:!0}),o}(e.ProceduralTexture);e.FireProceduralTexture=r}(BABYLON||(BABYLON={})),BABYLON.Effect.ShadersStore.fireProceduralTexturePixelShader="precision highp float;\r\n\r\nuniform float time;\r\nuniform vec3 c1;\r\nuniform vec3 c2;\r\nuniform vec3 c3;\r\nuniform vec3 c4;\r\nuniform vec3 c5;\r\nuniform vec3 c6;\r\nuniform vec2 speed;\r\nuniform float shift;\r\nuniform float alphaThreshold;\r\n\r\nvarying vec2 vUV;\r\n\r\nfloat rand(vec2 n) {\r\n return fract(cos(dot(n, vec2(12.9898, 4.1414))) * 43758.5453);\r\n}\r\n\r\nfloat noise(vec2 n) {\r\n const vec2 d = vec2(0.0, 1.0);\r\n vec2 b = floor(n), f = smoothstep(vec2(0.0), vec2(1.0), fract(n));\r\n return mix(mix(rand(b), rand(b + d.yx), f.x), mix(rand(b + d.xy), rand(b + d.yy), f.x), f.y);\r\n}\r\n\r\nfloat fbm(vec2 n) {\r\n float total = 0.0, amplitude = 1.0;\r\n for (int i = 0; i < 4; i++) {\r\n total += noise(n) * amplitude;\r\n n += n;\r\n amplitude *= 0.5;\r\n }\r\n return total;\r\n}\r\n\r\nvoid main() {\r\n vec2 p = vUV * 8.0;\r\n float q = fbm(p - time * 0.1);\r\n vec2 r = vec2(fbm(p + q + time * speed.x - p.x - p.y), fbm(p + q - time * speed.y));\r\n vec3 c = mix(c1, c2, fbm(p + r)) + mix(c3, c4, r.x) - mix(c5, c6, r.y);\r\n vec3 color = c * cos(shift * vUV.y);\r\n float luminance = dot(color.rgb, vec3(0.3, 0.59, 0.11));\r\n\r\n gl_FragColor = vec4(color, luminance * alphaThreshold + (1.0 - alphaThreshold));\r\n}";