/// module BABYLON { var maxSimultaneousLights = 4; class FireMaterialDefines extends MaterialDefines { public DIFFUSE = false; public CLIPPLANE = false; public ALPHATEST = false; public POINTSIZE = false; public FOG = false; public UV1 = false; public NORMAL = false; public VERTEXCOLOR = false; public VERTEXALPHA = false; public BONES = false; public BONES4 = false; public BonesPerMesh = 0; public INSTANCES = false; constructor() { super(); this._keys = Object.keys(this); } } export class FireMaterial extends Material { public diffuseTexture: BaseTexture; public distortionTexture: BaseTexture; public opacityTexture: BaseTexture; public diffuseColor = new Color3(1, 1, 1); public disableLighting = false; public speed = 1.0; private _scaledDiffuse = new Color3(); private _renderId: number; private _defines = new FireMaterialDefines(); private _cachedDefines = new FireMaterialDefines(); private _lastTime: number = 0; constructor(name: string, scene: Scene) { super(name, scene); this._cachedDefines.BonesPerMesh = -1; } public needAlphaBlending(): boolean { return (this.alpha < 1.0); } public needAlphaTesting(): boolean { return false; } public getAlphaTestTexture(): BaseTexture { return null; } // Methods private _checkCache(scene: Scene, mesh?: AbstractMesh, useInstances?: boolean): boolean { if (!mesh) { return true; } if (this._defines.INSTANCES !== useInstances) { return false; } if (mesh._materialDefines && mesh._materialDefines.isEqual(this._defines)) { return true; } return false; } public isReady(mesh?: AbstractMesh, useInstances?: boolean): boolean { if (this.checkReadyOnlyOnce) { if (this._wasPreviouslyReady) { return true; } } var scene = this.getScene(); if (!this.checkReadyOnEveryCall) { if (this._renderId === scene.getRenderId()) { if (this._checkCache(scene, mesh, useInstances)) { return true; } } } var engine = scene.getEngine(); var needNormals = false; var needUVs = false; this._defines.reset(); // Textures if (scene.texturesEnabled) { if (this.diffuseTexture && StandardMaterial.DiffuseTextureEnabled) { if (!this.diffuseTexture.isReady()) { return false; } else { needUVs = true; this._defines.DIFFUSE = true; } } } // Effect if (scene.clipPlane) { this._defines.CLIPPLANE = true; } this._defines.ALPHATEST = true; // Point size if (this.pointsCloud || scene.forcePointsCloud) { this._defines.POINTSIZE = true; } // Fog if (scene.fogEnabled && mesh && mesh.applyFog && scene.fogMode !== Scene.FOGMODE_NONE && this.fogEnabled) { this._defines.FOG = true; } // Attribs if (mesh) { if (needNormals && mesh.isVerticesDataPresent(VertexBuffer.NormalKind)) { this._defines.NORMAL = true; } if (needUVs) { if (mesh.isVerticesDataPresent(VertexBuffer.UVKind)) { this._defines.UV1 = true; } } if (mesh.useVertexColors && mesh.isVerticesDataPresent(VertexBuffer.ColorKind)) { this._defines.VERTEXCOLOR = true; if (mesh.hasVertexAlpha) { this._defines.VERTEXALPHA = true; } } if (mesh.useBones && mesh.computeBonesUsingShaders) { this._defines.BONES = true; this._defines.BonesPerMesh = (mesh.skeleton.bones.length + 1); this._defines.BONES4 = true; } // Instances if (useInstances) { this._defines.INSTANCES = true; } } // Get correct effect if (!this._defines.isEqual(this._cachedDefines)) { this._defines.cloneTo(this._cachedDefines); scene.resetCachedMaterial(); // Fallbacks var fallbacks = new EffectFallbacks(); if (this._defines.FOG) { fallbacks.addFallback(1, "FOG"); } if (this._defines.BONES4) { fallbacks.addFallback(0, "BONES4"); } //Attributes var attribs = [VertexBuffer.PositionKind]; if (this._defines.NORMAL) { attribs.push(VertexBuffer.NormalKind); } if (this._defines.UV1) { attribs.push(VertexBuffer.UVKind); } if (this._defines.VERTEXCOLOR) { attribs.push(VertexBuffer.ColorKind); } if (this._defines.BONES) { attribs.push(VertexBuffer.MatricesIndicesKind); attribs.push(VertexBuffer.MatricesWeightsKind); } if (this._defines.INSTANCES) { attribs.push("world0"); attribs.push("world1"); attribs.push("world2"); attribs.push("world3"); } // Legacy browser patch var shaderName = "fire"; var join = this._defines.toString(); this._effect = scene.getEngine().createEffect(shaderName, attribs, ["world", "view", "viewProjection", "vEyePosition", "vFogInfos", "vFogColor", "pointSize", "vDiffuseInfos", "mBones", "vClipPlane", "diffuseMatrix", // Fire "time", "speed" ], ["diffuseSampler", // Fire "distortionSampler", "opacitySampler" ], join, fallbacks, this.onCompiled, this.onError); } if (!this._effect.isReady()) { return false; } this._renderId = scene.getRenderId(); this._wasPreviouslyReady = true; if (mesh) { if (!mesh._materialDefines) { mesh._materialDefines = new FireMaterialDefines(); } this._defines.cloneTo(mesh._materialDefines); } return true; } public bindOnlyWorldMatrix(world: Matrix): void { this._effect.setMatrix("world", world); } public bind(world: Matrix, mesh?: Mesh): void { var scene = this.getScene(); // Matrices this.bindOnlyWorldMatrix(world); this._effect.setMatrix("viewProjection", scene.getTransformMatrix()); // Bones if (mesh && mesh.useBones && mesh.computeBonesUsingShaders) { this._effect.setMatrices("mBones", mesh.skeleton.getTransformMatrices()); } if (scene.getCachedMaterial() !== this) { // Textures if (this.diffuseTexture && 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); } // Clip plane if (scene.clipPlane) { var clipPlane = scene.clipPlane; this._effect.setFloat4("vClipPlane", clipPlane.normal.x, clipPlane.normal.y, clipPlane.normal.z, clipPlane.d); } // Point size if (this.pointsCloud) { this._effect.setFloat("pointSize", this.pointSize); } this._effect.setVector3("vEyePosition", scene._mirroredCameraPosition ? scene._mirroredCameraPosition : scene.activeCamera.position); } this._effect.setColor4("vDiffuseColor", this._scaledDiffuse, this.alpha * mesh.visibility); // View if (scene.fogEnabled && mesh.applyFog && scene.fogMode !== Scene.FOGMODE_NONE) { this._effect.setMatrix("view", scene.getViewMatrix()); } // Fog if (scene.fogEnabled && mesh.applyFog && scene.fogMode !== Scene.FOGMODE_NONE) { this._effect.setFloat4("vFogInfos", scene.fogMode, scene.fogStart, scene.fogEnd, scene.fogDensity); this._effect.setColor3("vFogColor", scene.fogColor); } // Time this._lastTime += scene.getEngine().getDeltaTime(); this._effect.setFloat("time", this._lastTime); // Speed this._effect.setFloat("speed", this.speed); super.bind(world, mesh); } public getAnimatables(): IAnimatable[] { var results = []; if (this.diffuseTexture && this.diffuseTexture.animations && this.diffuseTexture.animations.length > 0) { results.push(this.diffuseTexture); } if (this.distortionTexture && this.distortionTexture.animations && this.distortionTexture.animations.length > 0) { results.push(this.distortionTexture); } if (this.opacityTexture && this.opacityTexture.animations && this.opacityTexture.animations.length > 0) { results.push(this.opacityTexture); } return results; } public dispose(forceDisposeEffect?: boolean): void { if (this.diffuseTexture) { this.diffuseTexture.dispose(); } if (this.distortionTexture) { this.distortionTexture.dispose(); } super.dispose(forceDisposeEffect); } public clone(name: string): FireMaterial { var newMaterial = new FireMaterial(name, this.getScene()); // Base material this.copyTo(newMaterial); // Fire material if (this.diffuseTexture && this.diffuseTexture.clone) { newMaterial.diffuseTexture = this.diffuseTexture.clone(); } if (this.distortionTexture && this.distortionTexture.clone) { newMaterial.distortionTexture = this.distortionTexture.clone(); } if (this.opacityTexture && this.opacityTexture.clone) { newMaterial.opacityTexture = this.opacityTexture.clone(); } newMaterial.diffuseColor = this.diffuseColor.clone(); return newMaterial; } } }