|
@@ -5,7 +5,6 @@ var __extends = (this && this.__extends) || function (d, b) {
|
|
|
};
|
|
|
var BABYLON;
|
|
|
(function (BABYLON) {
|
|
|
- var maxSimultaneousLights = 4;
|
|
|
var FresnelParameters = (function () {
|
|
|
function FresnelParameters() {
|
|
|
this.isEnabled = true;
|
|
@@ -203,174 +202,6 @@ var BABYLON;
|
|
|
}
|
|
|
return false;
|
|
|
};
|
|
|
- StandardMaterial.PrepareDefinesForLights = function (scene, mesh, defines) {
|
|
|
- var lightIndex = 0;
|
|
|
- var needNormals = false;
|
|
|
- for (var index = 0; index < scene.lights.length; index++) {
|
|
|
- var light = scene.lights[index];
|
|
|
- if (!light.isEnabled()) {
|
|
|
- continue;
|
|
|
- }
|
|
|
- // Excluded check
|
|
|
- if (light._excludedMeshesIds.length > 0) {
|
|
|
- for (var excludedIndex = 0; excludedIndex < light._excludedMeshesIds.length; excludedIndex++) {
|
|
|
- var excludedMesh = scene.getMeshByID(light._excludedMeshesIds[excludedIndex]);
|
|
|
- if (excludedMesh) {
|
|
|
- light.excludedMeshes.push(excludedMesh);
|
|
|
- }
|
|
|
- }
|
|
|
- light._excludedMeshesIds = [];
|
|
|
- }
|
|
|
- // Included check
|
|
|
- if (light._includedOnlyMeshesIds.length > 0) {
|
|
|
- for (var includedOnlyIndex = 0; includedOnlyIndex < light._includedOnlyMeshesIds.length; includedOnlyIndex++) {
|
|
|
- var includedOnlyMesh = scene.getMeshByID(light._includedOnlyMeshesIds[includedOnlyIndex]);
|
|
|
- if (includedOnlyMesh) {
|
|
|
- light.includedOnlyMeshes.push(includedOnlyMesh);
|
|
|
- }
|
|
|
- }
|
|
|
- light._includedOnlyMeshesIds = [];
|
|
|
- }
|
|
|
- if (!light.canAffectMesh(mesh)) {
|
|
|
- continue;
|
|
|
- }
|
|
|
- needNormals = true;
|
|
|
- defines["LIGHT" + lightIndex] = true;
|
|
|
- var type;
|
|
|
- if (light instanceof BABYLON.SpotLight) {
|
|
|
- type = "SPOTLIGHT" + lightIndex;
|
|
|
- }
|
|
|
- else if (light instanceof BABYLON.HemisphericLight) {
|
|
|
- type = "HEMILIGHT" + lightIndex;
|
|
|
- }
|
|
|
- else if (light instanceof BABYLON.PointLight) {
|
|
|
- type = "POINTLIGHT" + lightIndex;
|
|
|
- }
|
|
|
- else {
|
|
|
- type = "DIRLIGHT" + lightIndex;
|
|
|
- }
|
|
|
- defines[type] = true;
|
|
|
- // Specular
|
|
|
- if (!light.specular.equalsFloats(0, 0, 0) && defines["SPECULARTERM"] !== undefined) {
|
|
|
- defines["SPECULARTERM"] = true;
|
|
|
- }
|
|
|
- // Shadows
|
|
|
- if (scene.shadowsEnabled) {
|
|
|
- var shadowGenerator = light.getShadowGenerator();
|
|
|
- if (mesh && mesh.receiveShadows && shadowGenerator) {
|
|
|
- defines["SHADOW" + lightIndex] = true;
|
|
|
- defines["SHADOWS"] = true;
|
|
|
- if (shadowGenerator.useVarianceShadowMap || shadowGenerator.useBlurVarianceShadowMap) {
|
|
|
- defines["SHADOWVSM" + lightIndex] = true;
|
|
|
- }
|
|
|
- if (shadowGenerator.usePoissonSampling) {
|
|
|
- defines["SHADOWPCF" + lightIndex] = true;
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- lightIndex++;
|
|
|
- if (lightIndex === maxSimultaneousLights)
|
|
|
- break;
|
|
|
- }
|
|
|
- return needNormals;
|
|
|
- };
|
|
|
- StandardMaterial.BindLightShadow = function (light, scene, mesh, lightIndex, effect, depthValuesAlreadySet) {
|
|
|
- var shadowGenerator = light.getShadowGenerator();
|
|
|
- if (mesh.receiveShadows && shadowGenerator) {
|
|
|
- if (!light.needCube()) {
|
|
|
- effect.setMatrix("lightMatrix" + lightIndex, shadowGenerator.getTransformMatrix());
|
|
|
- }
|
|
|
- else {
|
|
|
- if (!depthValuesAlreadySet) {
|
|
|
- depthValuesAlreadySet = true;
|
|
|
- effect.setFloat2("depthValues", scene.activeCamera.minZ, scene.activeCamera.maxZ);
|
|
|
- }
|
|
|
- }
|
|
|
- effect.setTexture("shadowSampler" + lightIndex, shadowGenerator.getShadowMapForRendering());
|
|
|
- effect.setFloat3("shadowsInfo" + lightIndex, shadowGenerator.getDarkness(), shadowGenerator.blurScale / shadowGenerator.getShadowMap().getSize().width, shadowGenerator.bias);
|
|
|
- }
|
|
|
- return depthValuesAlreadySet;
|
|
|
- };
|
|
|
- StandardMaterial.BindLights = function (scene, mesh, effect, defines) {
|
|
|
- var lightIndex = 0;
|
|
|
- var depthValuesAlreadySet = false;
|
|
|
- for (var index = 0; index < scene.lights.length; index++) {
|
|
|
- var light = scene.lights[index];
|
|
|
- if (!light.isEnabled()) {
|
|
|
- continue;
|
|
|
- }
|
|
|
- if (!light.canAffectMesh(mesh)) {
|
|
|
- continue;
|
|
|
- }
|
|
|
- if (light instanceof BABYLON.PointLight) {
|
|
|
- // Point Light
|
|
|
- light.transferToEffect(effect, "vLightData" + lightIndex);
|
|
|
- }
|
|
|
- else if (light instanceof BABYLON.DirectionalLight) {
|
|
|
- // Directional Light
|
|
|
- light.transferToEffect(effect, "vLightData" + lightIndex);
|
|
|
- }
|
|
|
- else if (light instanceof BABYLON.SpotLight) {
|
|
|
- // Spot Light
|
|
|
- light.transferToEffect(effect, "vLightData" + lightIndex, "vLightDirection" + lightIndex);
|
|
|
- }
|
|
|
- else if (light instanceof BABYLON.HemisphericLight) {
|
|
|
- // Hemispheric Light
|
|
|
- light.transferToEffect(effect, "vLightData" + lightIndex, "vLightGround" + lightIndex);
|
|
|
- }
|
|
|
- light.diffuse.scaleToRef(light.intensity, StandardMaterial._scaledDiffuse);
|
|
|
- effect.setColor4("vLightDiffuse" + lightIndex, StandardMaterial._scaledDiffuse, light.range);
|
|
|
- if (defines["SPECULARTERM"]) {
|
|
|
- light.specular.scaleToRef(light.intensity, StandardMaterial._scaledSpecular);
|
|
|
- effect.setColor3("vLightSpecular" + lightIndex, StandardMaterial._scaledSpecular);
|
|
|
- }
|
|
|
- // Shadows
|
|
|
- if (scene.shadowsEnabled) {
|
|
|
- depthValuesAlreadySet = this.BindLightShadow(light, scene, mesh, lightIndex, effect, depthValuesAlreadySet);
|
|
|
- }
|
|
|
- lightIndex++;
|
|
|
- if (lightIndex === maxSimultaneousLights)
|
|
|
- break;
|
|
|
- }
|
|
|
- };
|
|
|
- StandardMaterial.HandleFallbacksForShadows = function (defines, fallbacks) {
|
|
|
- for (var lightIndex = 0; lightIndex < maxSimultaneousLights; lightIndex++) {
|
|
|
- if (!defines["LIGHT" + lightIndex]) {
|
|
|
- continue;
|
|
|
- }
|
|
|
- if (lightIndex > 0) {
|
|
|
- fallbacks.addFallback(lightIndex, "LIGHT" + lightIndex);
|
|
|
- }
|
|
|
- if (defines["SHADOW" + lightIndex]) {
|
|
|
- fallbacks.addFallback(0, "SHADOW" + lightIndex);
|
|
|
- }
|
|
|
- if (defines["SHADOWPCF" + lightIndex]) {
|
|
|
- fallbacks.addFallback(0, "SHADOWPCF" + lightIndex);
|
|
|
- }
|
|
|
- if (defines["SHADOWVSM" + lightIndex]) {
|
|
|
- fallbacks.addFallback(0, "SHADOWVSM" + lightIndex);
|
|
|
- }
|
|
|
- }
|
|
|
- };
|
|
|
- StandardMaterial.PrepareAttributesForBones = function (attribs, mesh, defines, fallbacks) {
|
|
|
- if (defines["NUM_BONE_INFLUENCERS"] > 0) {
|
|
|
- fallbacks.addCPUSkinningFallback(0, mesh);
|
|
|
- attribs.push(BABYLON.VertexBuffer.MatricesIndicesKind);
|
|
|
- attribs.push(BABYLON.VertexBuffer.MatricesWeightsKind);
|
|
|
- if (defines["NUM_BONE_INFLUENCERS"] > 4) {
|
|
|
- attribs.push(BABYLON.VertexBuffer.MatricesIndicesExtraKind);
|
|
|
- attribs.push(BABYLON.VertexBuffer.MatricesWeightsExtraKind);
|
|
|
- }
|
|
|
- }
|
|
|
- };
|
|
|
- StandardMaterial.PrepareAttributesForInstances = function (attribs, defines) {
|
|
|
- if (defines["INSTANCES"]) {
|
|
|
- attribs.push("world0");
|
|
|
- attribs.push("world1");
|
|
|
- attribs.push("world2");
|
|
|
- attribs.push("world3");
|
|
|
- }
|
|
|
- };
|
|
|
StandardMaterial.prototype.isReady = function (mesh, useInstances) {
|
|
|
if (this.isFrozen) {
|
|
|
if (this._wasPreviouslyReady) {
|
|
@@ -544,7 +375,7 @@ var BABYLON;
|
|
|
this._defines.FOG = true;
|
|
|
}
|
|
|
if (scene.lightsEnabled && !this.disableLighting) {
|
|
|
- needNormals = StandardMaterial.PrepareDefinesForLights(scene, mesh, this._defines);
|
|
|
+ needNormals = BABYLON.MaterialHelper.PrepareDefinesForLights(scene, mesh, this._defines);
|
|
|
}
|
|
|
if (StandardMaterial.FresnelEnabled) {
|
|
|
// Fresnel
|
|
@@ -630,7 +461,7 @@ var BABYLON;
|
|
|
if (this._defines.LOGARITHMICDEPTH) {
|
|
|
fallbacks.addFallback(0, "LOGARITHMICDEPTH");
|
|
|
}
|
|
|
- StandardMaterial.HandleFallbacksForShadows(this._defines, fallbacks);
|
|
|
+ BABYLON.MaterialHelper.HandleFallbacksForShadows(this._defines, fallbacks);
|
|
|
if (this._defines.SPECULARTERM) {
|
|
|
fallbacks.addFallback(0, "SPECULARTERM");
|
|
|
}
|
|
@@ -663,8 +494,8 @@ var BABYLON;
|
|
|
if (this._defines.VERTEXCOLOR) {
|
|
|
attribs.push(BABYLON.VertexBuffer.ColorKind);
|
|
|
}
|
|
|
- StandardMaterial.PrepareAttributesForBones(attribs, mesh, this._defines, fallbacks);
|
|
|
- StandardMaterial.PrepareAttributesForInstances(attribs, this._defines);
|
|
|
+ BABYLON.MaterialHelper.PrepareAttributesForBones(attribs, mesh, this._defines, fallbacks);
|
|
|
+ BABYLON.MaterialHelper.PrepareAttributesForInstances(attribs, this._defines);
|
|
|
// Legacy browser patch
|
|
|
var shaderName = "default";
|
|
|
if (!scene.getEngine().getCaps().standardDerivatives) {
|
|
@@ -717,7 +548,7 @@ var BABYLON;
|
|
|
// Matrices
|
|
|
this.bindOnlyWorldMatrix(world);
|
|
|
// Bones
|
|
|
- StandardMaterial.BindBonesParameters(mesh, this._effect);
|
|
|
+ BABYLON.MaterialHelper.BindBonesParameters(mesh, this._effect);
|
|
|
if (scene.getCachedMaterial() !== this) {
|
|
|
this._effect.setMatrix("viewProjection", scene.getTransformMatrix());
|
|
|
if (StandardMaterial.FresnelEnabled) {
|
|
@@ -827,14 +658,14 @@ var BABYLON;
|
|
|
this._effect.setColor4("vDiffuseColor", this.diffuseColor, this.alpha * mesh.visibility);
|
|
|
// Lights
|
|
|
if (scene.lightsEnabled && !this.disableLighting) {
|
|
|
- StandardMaterial.BindLights(scene, mesh, this._effect, this._defines);
|
|
|
+ BABYLON.MaterialHelper.BindLights(scene, mesh, this._effect, this._defines);
|
|
|
}
|
|
|
// View
|
|
|
if (scene.fogEnabled && mesh.applyFog && scene.fogMode !== BABYLON.Scene.FOGMODE_NONE || this.reflectionTexture || this.refractionTexture) {
|
|
|
this._effect.setMatrix("view", scene.getViewMatrix());
|
|
|
}
|
|
|
// Fog
|
|
|
- StandardMaterial.BindFogParameters(scene, mesh, this._effect);
|
|
|
+ BABYLON.MaterialHelper.BindFogParameters(scene, mesh, this._effect);
|
|
|
// Log. depth
|
|
|
if (this._defines.LOGARITHMICDEPTH) {
|
|
|
this._effect.setFloat("logarithmicDepthConstant", 2.0 / (Math.log(scene.activeCamera.maxZ + 1.0) / Math.LN2));
|
|
@@ -842,17 +673,6 @@ var BABYLON;
|
|
|
}
|
|
|
_super.prototype.bind.call(this, world, mesh);
|
|
|
};
|
|
|
- StandardMaterial.BindFogParameters = function (scene, mesh, effect) {
|
|
|
- if (scene.fogEnabled && mesh.applyFog && scene.fogMode !== BABYLON.Scene.FOGMODE_NONE) {
|
|
|
- effect.setFloat4("vFogInfos", scene.fogMode, scene.fogStart, scene.fogEnd, scene.fogDensity);
|
|
|
- effect.setColor3("vFogColor", scene.fogColor);
|
|
|
- }
|
|
|
- };
|
|
|
- StandardMaterial.BindBonesParameters = function (mesh, effect) {
|
|
|
- if (mesh && mesh.useBones && mesh.computeBonesUsingShaders) {
|
|
|
- effect.setMatrices("mBones", mesh.skeleton.getTransformMatrices(mesh));
|
|
|
- }
|
|
|
- };
|
|
|
StandardMaterial.prototype.getAnimatables = function () {
|
|
|
var results = [];
|
|
|
if (this.diffuseTexture && this.diffuseTexture.animations && this.diffuseTexture.animations.length > 0) {
|
|
@@ -1100,8 +920,6 @@ var BABYLON;
|
|
|
}
|
|
|
return material;
|
|
|
};
|
|
|
- StandardMaterial._scaledDiffuse = new BABYLON.Color3();
|
|
|
- StandardMaterial._scaledSpecular = new BABYLON.Color3();
|
|
|
// Statics
|
|
|
// Flags used to enable or disable a type of texture for all Standard Materials
|
|
|
StandardMaterial.DiffuseTextureEnabled = true;
|