babylon.fireMaterial.min.js 13 KB

1
  1. var BABYLON;!function(e){var n=function(e){function n(){e.call(this),this.DIFFUSE=!1,this.CLIPPLANE=!1,this.ALPHATEST=!1,this.POINTSIZE=!1,this.FOG=!1,this.UV1=!1,this.NORMAL=!1,this.VERTEXCOLOR=!1,this.VERTEXALPHA=!1,this.NUM_BONE_INFLUENCERS=0,this.BonesPerMesh=0,this.INSTANCES=!1,this._keys=Object.keys(this)}return __extends(n,e),n}(e.MaterialDefines),r=function(r){function i(i,t){r.call(this,i,t),this.diffuseColor=new e.Color3(1,1,1),this.disableLighting=!1,this.speed=1,this._scaledDiffuse=new e.Color3,this._defines=new n,this._cachedDefines=new n,this._lastTime=0,this._cachedDefines.BonesPerMesh=-1}return __extends(i,r),i.prototype.needAlphaBlending=function(){return this.alpha<1},i.prototype.needAlphaTesting=function(){return!1},i.prototype.getAlphaTestTexture=function(){return null},i.prototype._checkCache=function(e,n,r){return n?this._defines.INSTANCES!==r?!1:n._materialDefines&&n._materialDefines.isEqual(this._defines)?!0:!1:!0},i.prototype.isReady=function(r,i){if(this.checkReadyOnlyOnce&&this._wasPreviouslyReady)return!0;var t=this.getScene();if(!this.checkReadyOnEveryCall&&this._renderId===t.getRenderId()&&this._checkCache(t,r,i))return!0;var o=(t.getEngine(),!1),s=!1;if(this._defines.reset(),t.texturesEnabled&&this.diffuseTexture&&e.StandardMaterial.DiffuseTextureEnabled){if(!this.diffuseTexture.isReady())return!1;s=!0,this._defines.DIFFUSE=!0}if(t.clipPlane&&(this._defines.CLIPPLANE=!0),this._defines.ALPHATEST=!0,(this.pointsCloud||t.forcePointsCloud)&&(this._defines.POINTSIZE=!0),t.fogEnabled&&r&&r.applyFog&&t.fogMode!==e.Scene.FOGMODE_NONE&&this.fogEnabled&&(this._defines.FOG=!0),r&&(o&&r.isVerticesDataPresent(e.VertexBuffer.NormalKind)&&(this._defines.NORMAL=!0),s&&r.isVerticesDataPresent(e.VertexBuffer.UVKind)&&(this._defines.UV1=!0),r.useVertexColors&&r.isVerticesDataPresent(e.VertexBuffer.ColorKind)&&(this._defines.VERTEXCOLOR=!0,r.hasVertexAlpha&&(this._defines.VERTEXALPHA=!0)),r.useBones&&r.computeBonesUsingShaders&&(this._defines.NUM_BONE_INFLUENCERS=r.numBoneInfluencers,this._defines.BonesPerMesh=r.skeleton.bones.length+1),i&&(this._defines.INSTANCES=!0)),!this._defines.isEqual(this._cachedDefines)){this._defines.cloneTo(this._cachedDefines),t.resetCachedMaterial();var f=new e.EffectFallbacks;this._defines.FOG&&f.addFallback(1,"FOG"),this._defines.NUM_BONE_INFLUENCERS>0&&f.addCPUSkinningFallback(0,r);var a=[e.VertexBuffer.PositionKind];this._defines.NORMAL&&a.push(e.VertexBuffer.NormalKind),this._defines.UV1&&a.push(e.VertexBuffer.UVKind),this._defines.VERTEXCOLOR&&a.push(e.VertexBuffer.ColorKind),this._defines.NUM_BONE_INFLUENCERS>0&&(a.push(e.VertexBuffer.MatricesIndicesKind),a.push(e.VertexBuffer.MatricesWeightsKind),this._defines.NUM_BONE_INFLUENCERS>4&&(a.push(e.VertexBuffer.MatricesIndicesExtraKind),a.push(e.VertexBuffer.MatricesWeightsExtraKind))),this._defines.INSTANCES&&(a.push("world0"),a.push("world1"),a.push("world2"),a.push("world3"));var d="fire",l=this._defines.toString();this._effect=t.getEngine().createEffect(d,a,["world","view","viewProjection","vEyePosition","vFogInfos","vFogColor","pointSize","vDiffuseInfos","mBones","vClipPlane","diffuseMatrix","time","speed"],["diffuseSampler","distortionSampler","opacitySampler"],l,f,this.onCompiled,this.onError)}return this._effect.isReady()?(this._renderId=t.getRenderId(),this._wasPreviouslyReady=!0,r&&(r._materialDefines||(r._materialDefines=new n),this._defines.cloneTo(r._materialDefines)),!0):!1},i.prototype.bindOnlyWorldMatrix=function(e){this._effect.setMatrix("world",e)},i.prototype.bind=function(n,i){var t=this.getScene();if(this.bindOnlyWorldMatrix(n),this._effect.setMatrix("viewProjection",t.getTransformMatrix()),i&&i.useBones&&i.computeBonesUsingShaders&&this._effect.setMatrices("mBones",i.skeleton.getTransformMatrices()),t.getCachedMaterial()!==this){if(this.diffuseTexture&&e.StandardMaterial.DiffuseTextureEnabled&&(this._effect.setTexture("diffuseSampler",this.diffuseTexture),this._effect.setFloat2("vDiffuseInfos",this.diffuseTexture.coordinatesIndex,this.diffuseTexture.level),this._effect.setMatrix("diffuseMatrix",this.diffuseTexture.getTextureMatrix()),this._effect.setTexture("distortionSampler",this.distortionTexture),this._effect.setTexture("opacitySampler",this.opacityTexture)),t.clipPlane){var o=t.clipPlane;this._effect.setFloat4("vClipPlane",o.normal.x,o.normal.y,o.normal.z,o.d)}this.pointsCloud&&this._effect.setFloat("pointSize",this.pointSize),this._effect.setVector3("vEyePosition",t._mirroredCameraPosition?t._mirroredCameraPosition:t.activeCamera.position)}this._effect.setColor4("vDiffuseColor",this._scaledDiffuse,this.alpha*i.visibility),t.fogEnabled&&i.applyFog&&t.fogMode!==e.Scene.FOGMODE_NONE&&this._effect.setMatrix("view",t.getViewMatrix()),t.fogEnabled&&i.applyFog&&t.fogMode!==e.Scene.FOGMODE_NONE&&(this._effect.setFloat4("vFogInfos",t.fogMode,t.fogStart,t.fogEnd,t.fogDensity),this._effect.setColor3("vFogColor",t.fogColor)),this._lastTime+=t.getEngine().getDeltaTime(),this._effect.setFloat("time",this._lastTime),this._effect.setFloat("speed",this.speed),r.prototype.bind.call(this,n,i)},i.prototype.getAnimatables=function(){var e=[];return this.diffuseTexture&&this.diffuseTexture.animations&&this.diffuseTexture.animations.length>0&&e.push(this.diffuseTexture),this.distortionTexture&&this.distortionTexture.animations&&this.distortionTexture.animations.length>0&&e.push(this.distortionTexture),this.opacityTexture&&this.opacityTexture.animations&&this.opacityTexture.animations.length>0&&e.push(this.opacityTexture),e},i.prototype.dispose=function(e){this.diffuseTexture&&this.diffuseTexture.dispose(),this.distortionTexture&&this.distortionTexture.dispose(),r.prototype.dispose.call(this,e)},i.prototype.clone=function(e){var n=new i(e,this.getScene());return this.copyTo(n),this.diffuseTexture&&this.diffuseTexture.clone&&(n.diffuseTexture=this.diffuseTexture.clone()),this.distortionTexture&&this.distortionTexture.clone&&(n.distortionTexture=this.distortionTexture.clone()),this.opacityTexture&&this.opacityTexture.clone&&(n.opacityTexture=this.opacityTexture.clone()),n.diffuseColor=this.diffuseColor.clone(),n},i}(e.Material);e.FireMaterial=r}(BABYLON||(BABYLON={})),BABYLON.Effect.ShadersStore.fireVertexShader="precision highp float;\r\n\r\n// Attributes\r\nattribute vec3 position;\r\n#ifdef NORMAL\r\nattribute vec3 normal;\r\n#endif\r\n#ifdef UV1\r\nattribute vec2 uv;\r\n#endif\r\n#ifdef UV2\r\nattribute vec2 uv2;\r\n#endif\r\n#ifdef VERTEXCOLOR\r\nattribute vec4 color;\r\n#endif\r\n#ifdef BONES\r\nattribute vec4 matricesIndices;\r\nattribute vec4 matricesWeights;\r\n#endif\r\n\r\n// Uniforms\r\n\r\n#ifdef INSTANCES\r\nattribute vec4 world0;\r\nattribute vec4 world1;\r\nattribute vec4 world2;\r\nattribute vec4 world3;\r\n#else\r\nuniform mat4 world;\r\n#endif\r\n\r\nuniform mat4 view;\r\nuniform mat4 viewProjection;\r\n\r\n#ifdef DIFFUSE\r\nvarying vec2 vDiffuseUV;\r\n#endif\r\n\r\n#if NUM_BONE_INFLUENCERS > 0\r\n uniform mat4 mBones[BonesPerMesh];\r\n\r\n attribute vec4 matricesIndices;\r\n attribute vec4 matricesWeights;\r\n #if NUM_BONE_INFLUENCERS > 4\r\n attribute vec4 matricesIndicesExtra;\r\n attribute vec4 matricesWeightsExtra;\r\n #endif\r\n#endif\r\n\r\n#ifdef POINTSIZE\r\nuniform float pointSize;\r\n#endif\r\n\r\n// Output\r\nvarying vec3 vPositionW;\r\n#ifdef NORMAL\r\nvarying vec3 vNormalW;\r\n#endif\r\n\r\n#ifdef VERTEXCOLOR\r\nvarying vec4 vColor;\r\n#endif\r\n\r\n#ifdef CLIPPLANE\r\nuniform vec4 vClipPlane;\r\nvarying float fClipDistance;\r\n#endif\r\n\r\n#ifdef FOG\r\nvarying float fFogDistance;\r\n#endif\r\n\r\n// Fire\r\nuniform float time;\r\nuniform float speed;\r\n\r\nvarying vec2 vDistortionCoords1;\r\nvarying vec2 vDistortionCoords2;\r\nvarying vec2 vDistortionCoords3;\r\n\r\nvoid main(void) {\r\n mat4 finalWorld;\r\n\r\n#ifdef INSTANCES\r\n finalWorld = mat4(world0, world1, world2, world3);\r\n#else\r\n finalWorld = world;\r\n#endif\r\n\r\n#if NUM_BONE_INFLUENCERS > 0\r\n mat4 influence;\r\n influence = mBones[int(matricesIndices[0])] * matricesWeights[0];\r\n\r\n #if NUM_BONE_INFLUENCERS > 1\r\n influence += mBones[int(matricesIndices[1])] * matricesWeights[1];\r\n #endif \r\n #if NUM_BONE_INFLUENCERS > 2\r\n influence += mBones[int(matricesIndices[2])] * matricesWeights[2];\r\n #endif \r\n #if NUM_BONE_INFLUENCERS > 3\r\n influence += mBones[int(matricesIndices[3])] * matricesWeights[3];\r\n #endif \r\n\r\n #if NUM_BONE_INFLUENCERS > 4\r\n influence += mBones[int(matricesIndicesExtra[0])] * matricesWeightsExtra[0];\r\n #endif\r\n #if NUM_BONE_INFLUENCERS > 5\r\n influence += mBones[int(matricesIndicesExtra[1])] * matricesWeightsExtra[1];\r\n #endif \r\n #if NUM_BONE_INFLUENCERS > 6\r\n influence += mBones[int(matricesIndicesExtra[2])] * matricesWeightsExtra[2];\r\n #endif \r\n #if NUM_BONE_INFLUENCERS > 7\r\n influence += mBones[int(matricesIndicesExtra[3])] * matricesWeightsExtra[3];\r\n #endif \r\n\r\n finalWorld = finalWorld * influence;\r\n#endif\r\n\r\n gl_Position = viewProjection * finalWorld * vec4(position, 1.0);\r\n\r\n vec4 worldPos = finalWorld * vec4(position, 1.0);\r\n vPositionW = vec3(worldPos);\r\n\r\n#ifdef NORMAL\r\n vNormalW = normalize(vec3(finalWorld * vec4(normal, 0.0)));\r\n#endif\r\n\r\n // Texture coordinates\r\n#ifdef DIFFUSE\r\n vDiffuseUV = uv;\r\n vDiffuseUV.y -= 0.2;\r\n#endif\r\n\r\n // Clip plane\r\n#ifdef CLIPPLANE\r\n fClipDistance = dot(worldPos, vClipPlane);\r\n#endif\r\n\r\n // Fog\r\n#ifdef FOG\r\n fFogDistance = (view * worldPos).z;\r\n#endif\r\n\r\n // Vertex color\r\n#ifdef VERTEXCOLOR\r\n vColor = color;\r\n#endif\r\n\r\n // Point size\r\n#ifdef POINTSIZE\r\n gl_PointSize = pointSize;\r\n#endif\r\n\r\n // Fire\r\n vec3 layerSpeed = vec3(-0.2, -0.52, -0.1) * speed;\r\n \r\n vDistortionCoords1.x = uv.x;\r\n vDistortionCoords1.y = uv.y + layerSpeed.x * time / 1000.0;\r\n \r\n vDistortionCoords2.x = uv.x;\r\n vDistortionCoords2.y = uv.y + layerSpeed.y * time / 1000.0;\r\n \r\n vDistortionCoords3.x = uv.x;\r\n vDistortionCoords3.y = uv.y + layerSpeed.z * time / 1000.0;\r\n}\r\n",BABYLON.Effect.ShadersStore.firePixelShader="precision highp float;\r\n\r\n// Constants\r\nuniform vec3 vEyePosition;\r\n\r\n// Input\r\nvarying vec3 vPositionW;\r\n\r\n#ifdef NORMAL\r\nvarying vec3 vNormalW;\r\n#endif\r\n\r\n#ifdef VERTEXCOLOR\r\nvarying vec4 vColor;\r\n#endif\r\n\r\n// Samplers\r\n#ifdef DIFFUSE\r\nvarying vec2 vDiffuseUV;\r\nuniform sampler2D diffuseSampler;\r\nuniform vec2 vDiffuseInfos;\r\n#endif\r\n\r\n// Fire\r\nuniform sampler2D distortionSampler;\r\nuniform sampler2D opacitySampler;\r\n\r\nvarying vec2 vDistortionCoords1;\r\nvarying vec2 vDistortionCoords2;\r\nvarying vec2 vDistortionCoords3;\r\n\r\n#ifdef CLIPPLANE\r\nvarying float fClipDistance;\r\n#endif\r\n\r\n// Fog\r\n#ifdef FOG\r\n\r\n#define FOGMODE_NONE 0.\r\n#define FOGMODE_EXP 1.\r\n#define FOGMODE_EXP2 2.\r\n#define FOGMODE_LINEAR 3.\r\n#define E 2.71828\r\n\r\nuniform vec4 vFogInfos;\r\nuniform vec3 vFogColor;\r\nvarying float fFogDistance;\r\n\r\nfloat CalcFogFactor()\r\n{\r\n float fogCoeff = 1.0;\r\n float fogStart = vFogInfos.y;\r\n float fogEnd = vFogInfos.z;\r\n float fogDensity = vFogInfos.w;\r\n\r\n if (FOGMODE_LINEAR == vFogInfos.x)\r\n {\r\n fogCoeff = (fogEnd - fFogDistance) / (fogEnd - fogStart);\r\n }\r\n else if (FOGMODE_EXP == vFogInfos.x)\r\n {\r\n fogCoeff = 1.0 / pow(E, fFogDistance * fogDensity);\r\n }\r\n else if (FOGMODE_EXP2 == vFogInfos.x)\r\n {\r\n fogCoeff = 1.0 / pow(E, fFogDistance * fFogDistance * fogDensity * fogDensity);\r\n }\r\n\r\n return clamp(fogCoeff, 0.0, 1.0);\r\n}\r\n#endif\r\n\r\nvec4 bx2(vec4 x)\r\n{\r\n return vec4(2.0) * x - vec4(1.0);\r\n}\r\n\r\nvoid main(void) {\r\n // Clip plane\r\n#ifdef CLIPPLANE\r\n if (fClipDistance > 0.0)\r\n discard;\r\n#endif\r\n\r\n vec3 viewDirectionW = normalize(vEyePosition - vPositionW);\r\n\r\n // Base color\r\n vec4 baseColor = vec4(1., 1., 1., 1.);\r\n\r\n // Alpha\r\n float alpha = 1.0;\r\n\r\n#ifdef DIFFUSE\r\n // Fire\r\n const float distortionAmount0 = 0.092;\r\n const float distortionAmount1 = 0.092;\r\n const float distortionAmount2 = 0.092;\r\n \r\n vec2 heightAttenuation = vec2(0.3, 0.39);\r\n \r\n vec4 noise0 = texture2D(distortionSampler, vDistortionCoords1);\r\n vec4 noise1 = texture2D(distortionSampler, vDistortionCoords2);\r\n vec4 noise2 = texture2D(distortionSampler, vDistortionCoords3);\r\n \r\n vec4 noiseSum = bx2(noise0) * distortionAmount0 + bx2(noise1) * distortionAmount1 + bx2(noise2) * distortionAmount2;\r\n \r\n vec4 perturbedBaseCoords = vec4(vDiffuseUV, 0.0, 1.0) + noiseSum * (vDiffuseUV.y * heightAttenuation.x + heightAttenuation.y);\r\n \r\n vec4 opacityColor = texture2D(opacitySampler, perturbedBaseCoords.xy);\r\n \r\n#ifdef ALPHATEST\r\n if (opacityColor.r < 0.1)\r\n discard;\r\n#endif\r\n \r\n baseColor = texture2D(diffuseSampler, perturbedBaseCoords.xy) * 2.0;\r\n baseColor *= opacityColor;\r\n\r\n baseColor.rgb *= vDiffuseInfos.y;\r\n#endif\r\n\r\n#ifdef VERTEXCOLOR\r\n baseColor.rgb *= vColor.rgb;\r\n#endif\r\n\r\n // Bump\r\n#ifdef NORMAL\r\n vec3 normalW = normalize(vNormalW);\r\n#else\r\n vec3 normalW = vec3(1.0, 1.0, 1.0);\r\n#endif\r\n\r\n // Lighting\r\n vec3 diffuseBase = vec3(1.0, 1.0, 1.0);\r\n\r\n#ifdef VERTEXALPHA\r\n alpha *= vColor.a;\r\n#endif\r\n\r\n // Composition\r\n vec4 color = vec4(baseColor.rgb, alpha);\r\n\r\n#ifdef FOG\r\n float fog = CalcFogFactor();\r\n color.rgb = fog * color.rgb + (1.0 - fog) * vFogColor;\r\n#endif\r\n\r\n gl_FragColor = color;\r\n}";