/// 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 VERTEXCOLOR = false; public VERTEXALPHA = false; public BonesPerMesh = 0; public NUM_BONE_INFLUENCERS = 0; public INSTANCES = false; constructor() { super(); this._keys = Object.keys(this); } } export class FireMaterial extends Material { @serializeAsTexture() public diffuseTexture: BaseTexture; @serializeAsTexture() public distortionTexture: BaseTexture; @serializeAsTexture() public opacityTexture: BaseTexture; @serialize("diffuseColor") public diffuseColor = new Color3(1, 1, 1); @serialize() 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 (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.NUM_BONE_INFLUENCERS = mesh.numBoneInfluencers; this._defines.BonesPerMesh = (mesh.skeleton.bones.length + 1); } // 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.NUM_BONE_INFLUENCERS > 0) { fallbacks.addCPUSkinningFallback(0, mesh); } //Attributes var attribs = [VertexBuffer.PositionKind]; if (this._defines.UV1) { attribs.push(VertexBuffer.UVKind); } if (this._defines.VERTEXCOLOR) { attribs.push(VertexBuffer.ColorKind); } MaterialHelper.PrepareAttributesForBones(attribs, mesh, this._defines, fallbacks); MaterialHelper.PrepareAttributesForInstances(attribs, this._defines); // 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 MaterialHelper.BindBonesParameters(mesh, this._effect); 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 MaterialHelper.BindFogParameters(scene, mesh, this._effect); // 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 { return SerializationHelper.Clone(() => new FireMaterial(name, this.getScene()), this); } public serialize(): any { var serializationObject = super.serialize(); serializationObject.customType = "BABYLON.FireMaterial"; serializationObject.diffuseColor = this.diffuseColor.asArray(); serializationObject.speed = this.speed; if (this.diffuseTexture) { serializationObject.diffuseTexture = this.diffuseTexture.serialize(); } if (this.distortionTexture) { serializationObject.distortionTexture = this.distortionTexture.serialize(); } if (this.opacityTexture) { serializationObject.opacityTexture = this.opacityTexture.serialize(); } return serializationObject; } public static Parse(source: any, scene: Scene, rootUrl: string): FireMaterial { var material = new FireMaterial(source.name, scene); material.diffuseColor = Color3.FromArray(source.diffuseColor); material.speed = source.speed; material.alpha = source.alpha; material.id = source.id; Tags.AddTagsTo(material, source.tags); material.backFaceCulling = source.backFaceCulling; material.wireframe = source.wireframe; if (source.diffuseTexture) { material.diffuseTexture = Texture.Parse(source.diffuseTexture, scene, rootUrl); } if (source.distortionTexture) { material.distortionTexture = Texture.Parse(source.distortionTexture, scene, rootUrl); } if (source.opacityTexture) { material.opacityTexture = Texture.Parse(source.opacityTexture, scene, rootUrl); } if (source.checkReadyOnlyOnce) { material.checkReadyOnlyOnce = source.checkReadyOnlyOnce; } return material; } } }