var __extends = (this && this.__extends) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; var BABYLON; (function (BABYLON) { var maxSimultaneousLights = 4; var PBRMaterialDefines = (function () { function PBRMaterialDefines() { this.ALBEDO = false; this.CLIPPLANE = false; this.ALPHATEST = false; this.FOG = false; this.NORMAL = false; this.UV1 = false; this.UV2 = false; this.VERTEXCOLOR = false; this.VERTEXALPHA = false; this.BONES = false; this.BONES4 = false; this.BonesPerMesh = 0; this.INSTANCES = false; this.POINTSIZE = false; this._keys = Object.keys(this); } PBRMaterialDefines.prototype.isEqual = function (other) { for (var index = 0; index < this._keys.length; index++) { var prop = this._keys[index]; if (this[prop] !== other[prop]) { return false; } } return true; }; PBRMaterialDefines.prototype.cloneTo = function (other) { for (var index = 0; index < this._keys.length; index++) { var prop = this._keys[index]; other[prop] = this[prop]; } }; PBRMaterialDefines.prototype.reset = function () { for (var index = 0; index < this._keys.length; index++) { var prop = this._keys[index]; if (prop === "BonesPerMesh") { this[prop] = 0; continue; } this[prop] = false; } }; PBRMaterialDefines.prototype.toString = function () { var result = ""; for (var index = 0; index < this._keys.length; index++) { var prop = this._keys[index]; if (prop === "BonesPerMesh" && this[prop] > 0) { result += "#define BonesPerMesh " + this[prop] + "\n"; continue; } if (this[prop]) { result += "#define " + prop + "\n"; } } return result; }; return PBRMaterialDefines; })(); var PBRMaterial = (function (_super) { __extends(PBRMaterial, _super); function PBRMaterial(name, scene) { _super.call(this, name, scene); this.albedoColor = new BABYLON.Color3(1, 1, 1); this._worldViewProjectionMatrix = BABYLON.Matrix.Zero(); this._globalAmbientColor = new BABYLON.Color3(0, 0, 0); this._scaledDiffuse = new BABYLON.Color3(); this._scaledSpecular = new BABYLON.Color3(); this._defines = new PBRMaterialDefines(); this._cachedDefines = new PBRMaterialDefines(); this._cachedDefines.BonesPerMesh = -1; } PBRMaterial.prototype.needAlphaBlending = function () { return this.alpha < 1.0; }; PBRMaterial.prototype.needAlphaTesting = function () { return false; }; PBRMaterial.prototype.getAlphaTestTexture = function () { return null; }; // Methods PBRMaterial.prototype.isReady = function (mesh, useInstances) { if (this.checkReadyOnlyOnce) { if (this._wasPreviouslyReady) { return true; } } var scene = this.getScene(); if (!this.checkReadyOnEveryCall) { if (this._renderId === scene.getRenderId()) { return true; } } var engine = scene.getEngine(); var needNormals = false; var needUVs = false; this._defines.reset(); // Textures if (scene.texturesEnabled) { } // Effect if (scene.clipPlane) { this._defines.CLIPPLANE = true; } if (engine.getAlphaTesting()) { this._defines.ALPHATEST = true; } // Point size if (this.pointsCloud || scene.forcePointsCloud) { this._defines.POINTSIZE = true; } // Fog if (scene.fogEnabled && mesh && mesh.applyFog && scene.fogMode !== BABYLON.Scene.FOGMODE_NONE && this.fogEnabled) { this._defines.FOG = true; } // Lights // Attribs if (mesh) { if (needNormals && mesh.isVerticesDataPresent(BABYLON.VertexBuffer.NormalKind)) { this._defines.NORMAL = true; } if (needUVs) { if (mesh.isVerticesDataPresent(BABYLON.VertexBuffer.UVKind)) { this._defines.UV1 = true; } if (mesh.isVerticesDataPresent(BABYLON.VertexBuffer.UV2Kind)) { this._defines.UV2 = true; } } if (mesh.useVertexColors && mesh.isVerticesDataPresent(BABYLON.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 BABYLON.EffectFallbacks(); if (this._defines.FOG) { fallbacks.addFallback(1, "FOG"); } if (this._defines.BONES4) { fallbacks.addFallback(0, "BONES4"); } //Attributes var attribs = [BABYLON.VertexBuffer.PositionKind]; if (this._defines.NORMAL) { attribs.push(BABYLON.VertexBuffer.NormalKind); } if (this._defines.UV1) { attribs.push(BABYLON.VertexBuffer.UVKind); } if (this._defines.UV2) { attribs.push(BABYLON.VertexBuffer.UV2Kind); } if (this._defines.VERTEXCOLOR) { attribs.push(BABYLON.VertexBuffer.ColorKind); } if (this._defines.BONES) { attribs.push(BABYLON.VertexBuffer.MatricesIndicesKind); attribs.push(BABYLON.VertexBuffer.MatricesWeightsKind); } if (this._defines.INSTANCES) { attribs.push("world0"); attribs.push("world1"); attribs.push("world2"); attribs.push("world3"); } // Legacy browser patch var join = this._defines.toString(); this._effect = scene.getEngine().createEffect("pbr", attribs, ["world", "view", "viewProjection", "vEyePosition", "vAlbedoColor", "vFogInfos", "vFogColor", "pointSize", "mBones", "vClipPlane", ], [], join, fallbacks, this.onCompiled, this.onError); } if (!this._effect.isReady()) { return false; } this._renderId = scene.getRenderId(); this._wasPreviouslyReady = true; return true; }; PBRMaterial.prototype.bindOnlyWorldMatrix = function (world) { this._effect.setMatrix("world", world); }; PBRMaterial.prototype.bind = function (world, mesh) { 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) { // Clip plane if (scene.clipPlane) { var clipPlane = scene.clipPlane; this._effect.setFloat4("vClipPlane", clipPlane.normal.x, clipPlane.normal.y, clipPlane.normal.z, clipPlane.d); } this._effect.setVector3("vEyePosition", scene.activeCamera.position); } // Point size if (this.pointsCloud) { this._effect.setFloat("pointSize", this.pointSize); } // Colors this._effect.setColor4("vAlbedoColor", this.albedoColor, this.alpha * mesh.visibility); // View if (scene.fogEnabled && mesh.applyFog && scene.fogMode !== BABYLON.Scene.FOGMODE_NONE) { this._effect.setMatrix("view", scene.getViewMatrix()); } // Fog if (scene.fogEnabled && mesh.applyFog && scene.fogMode !== BABYLON.Scene.FOGMODE_NONE) { this._effect.setFloat4("vFogInfos", scene.fogMode, scene.fogStart, scene.fogEnd, scene.fogDensity); this._effect.setColor3("vFogColor", scene.fogColor); } _super.prototype.bind.call(this, world, mesh); }; PBRMaterial.prototype.getAnimatables = function () { var results = []; return results; }; PBRMaterial.prototype.dispose = function (forceDisposeEffect) { _super.prototype.dispose.call(this, forceDisposeEffect); }; PBRMaterial.prototype.clone = function (name) { var newPBRMaterial = new PBRMaterial(name, this.getScene()); // Base material this.copyTo(newPBRMaterial); // PBRMaterial material newPBRMaterial.albedoColor = this.albedoColor.clone(); return newPBRMaterial; }; return PBRMaterial; })(BABYLON.Material); BABYLON.PBRMaterial = PBRMaterial; })(BABYLON || (BABYLON = {}));