UNPKG

@babylonjs/materials

Version:

Babylon.js Materials Library =====================

243 lines 10.2 kB
import { SerializationHelper } from "@babylonjs/core/Misc/decorators.serialization.js"; import { Color3 } from "@babylonjs/core/Maths/math.color.js"; import { MaterialDefines } from "@babylonjs/core/Materials/materialDefines.js"; import { PushMaterial } from "@babylonjs/core/Materials/pushMaterial.js"; import { VertexBuffer } from "@babylonjs/core/Buffers/buffer.js"; import { Scene } from "@babylonjs/core/scene.js"; import { RegisterClass } from "@babylonjs/core/Misc/typeStore.js"; import "./shadowOnly.fragment.js"; import "./shadowOnly.vertex.js"; import { EffectFallbacks } from "@babylonjs/core/Materials/effectFallbacks.js"; import { addClipPlaneUniforms, bindClipPlane } from "@babylonjs/core/Materials/clipPlaneMaterialHelper.js"; import { BindBonesParameters, BindFogParameters, BindLights, BindLogDepth, HandleFallbacksForShadows, PrepareAttributesForBones, PrepareAttributesForInstances, PrepareDefinesForAttributes, PrepareDefinesForFrameBoundValues, PrepareDefinesForLights, PrepareDefinesForMisc, PrepareUniformsAndSamplersList, } from "@babylonjs/core/Materials/materialHelper.functions.js"; class ShadowOnlyMaterialDefines extends MaterialDefines { constructor() { super(); this.CLIPPLANE = false; this.CLIPPLANE2 = false; this.CLIPPLANE3 = false; this.CLIPPLANE4 = false; this.CLIPPLANE5 = false; this.CLIPPLANE6 = false; this.POINTSIZE = false; this.FOG = false; this.NORMAL = false; this.NUM_BONE_INFLUENCERS = 0; this.BonesPerMesh = 0; this.INSTANCES = false; this.IMAGEPROCESSINGPOSTPROCESS = false; this.SKIPFINALCOLORCLAMP = false; this.LOGARITHMICDEPTH = false; this.rebuild(); } } export class ShadowOnlyMaterial extends PushMaterial { constructor(name, scene) { super(name, scene); this._needAlphaBlending = true; this.shadowColor = Color3.Black(); } needAlphaBlending() { return this._needAlphaBlending; } needAlphaTesting() { return false; } getAlphaTestTexture() { return null; } get activeLight() { return this._activeLight; } set activeLight(light) { this._activeLight = light; } _getFirstShadowLightForMesh(mesh) { for (const light of mesh.lightSources) { if (light.shadowEnabled) { return light; } } return null; } // Methods isReadyForSubMesh(mesh, subMesh, useInstances) { const drawWrapper = subMesh._drawWrapper; if (this.isFrozen) { if (drawWrapper.effect && drawWrapper._wasPreviouslyReady && drawWrapper._wasPreviouslyUsingInstances === useInstances) { return true; } } if (!subMesh.materialDefines) { subMesh.materialDefines = new ShadowOnlyMaterialDefines(); } const defines = subMesh.materialDefines; const scene = this.getScene(); if (this._isReadyForSubMesh(subMesh)) { return true; } const engine = scene.getEngine(); // Ensure that active light is the first shadow light if (this._activeLight) { for (const light of mesh.lightSources) { if (light.shadowEnabled) { if (this._activeLight === light) { break; // We are good } const lightPosition = mesh.lightSources.indexOf(this._activeLight); if (lightPosition !== -1) { mesh.lightSources.splice(lightPosition, 1); mesh.lightSources.splice(0, 0, this._activeLight); } break; } } } PrepareDefinesForFrameBoundValues(scene, engine, this, defines, useInstances ? true : false); PrepareDefinesForMisc(mesh, scene, this._useLogarithmicDepth, this.pointsCloud, this.fogEnabled, this.needAlphaTestingForMesh(mesh), defines); defines._needNormals = PrepareDefinesForLights(scene, mesh, defines, false, 1); const shadowGenerator = this._getFirstShadowLightForMesh(mesh)?.getShadowGenerator(); this._needAlphaBlending = true; if (shadowGenerator && shadowGenerator.getClassName && shadowGenerator.getClassName() === "CascadedShadowGenerator") { const csg = shadowGenerator; this._needAlphaBlending = !csg.autoCalcDepthBounds; } // Attribs PrepareDefinesForAttributes(mesh, defines, false, true); // Get correct effect if (defines.isDirty) { defines.markAsProcessed(); scene.resetCachedMaterial(); // Fallbacks const fallbacks = new EffectFallbacks(); if (defines.FOG) { fallbacks.addFallback(1, "FOG"); } HandleFallbacksForShadows(defines, fallbacks, 1); if (defines.NUM_BONE_INFLUENCERS > 0) { fallbacks.addCPUSkinningFallback(0, mesh); } defines.IMAGEPROCESSINGPOSTPROCESS = scene.imageProcessingConfiguration.applyByPostProcess; //Attributes const attribs = [VertexBuffer.PositionKind]; if (defines.NORMAL) { attribs.push(VertexBuffer.NormalKind); } PrepareAttributesForBones(attribs, mesh, defines, fallbacks); PrepareAttributesForInstances(attribs, defines); const shaderName = "shadowOnly"; const join = defines.toString(); const uniforms = [ "world", "view", "viewProjection", "vEyePosition", "vLightsType", "vFogInfos", "vFogColor", "pointSize", "alpha", "shadowColor", "mBones", "logarithmicDepthConstant", ]; const samplers = []; const uniformBuffers = ["Scene"]; addClipPlaneUniforms(uniforms); PrepareUniformsAndSamplersList({ uniformsNames: uniforms, uniformBuffersNames: uniformBuffers, samplers: samplers, defines: defines, maxSimultaneousLights: 1, }); subMesh.setEffect(scene.getEngine().createEffect(shaderName, { attributes: attribs, uniformsNames: uniforms, uniformBuffersNames: uniformBuffers, samplers: samplers, defines: join, fallbacks: fallbacks, onCompiled: this.onCompiled, onError: this.onError, indexParameters: { maxSimultaneousLights: 1 }, }, engine), defines, this._materialContext); } if (!subMesh.effect || !subMesh.effect.isReady()) { return false; } defines._renderId = scene.getRenderId(); drawWrapper._wasPreviouslyReady = true; drawWrapper._wasPreviouslyUsingInstances = !!useInstances; return true; } bindForSubMesh(world, mesh, subMesh) { const scene = this.getScene(); const defines = subMesh.materialDefines; if (!defines) { return; } const effect = subMesh.effect; if (!effect) { return; } this._activeEffect = effect; // Matrices this.bindOnlyWorldMatrix(world); this.bindViewProjection(effect); // Bones BindBonesParameters(mesh, this._activeEffect); if (this._mustRebind(scene, effect, subMesh)) { // Clip plane bindClipPlane(effect, this, scene); // Point size if (this.pointsCloud) { this._activeEffect.setFloat("pointSize", this.pointSize); } this._activeEffect.setFloat("alpha", this.alpha); this._activeEffect.setColor3("shadowColor", this.shadowColor); // Log. depth if (this._useLogarithmicDepth) { BindLogDepth(defines, effect, scene); } scene.bindEyePosition(effect); } // Lights if (scene.lightsEnabled) { BindLights(scene, mesh, this._activeEffect, defines, 1); const light = this._getFirstShadowLightForMesh(mesh); if (light) { // Make sure the uniforms for this light will be rebound for other materials using this light when rendering the current frame. // Indeed, there is an optimization in Light that binds the light uniforms only once per frame for a given light (if using ubo). // Doing this way assumes that all uses of this light are the same, meaning all parameters passed to Light._bindLlight // are the same, notably useSpecular. However, isReadyForSubMesh (see above) is passing false for this parameter, which may not be // the value the other materials may pass. light._renderId = -1; } } // View if ((scene.fogEnabled && mesh.applyFog && scene.fogMode !== Scene.FOGMODE_NONE) || defines["SHADOWCSM0"]) { this.bindView(effect); } // Fog BindFogParameters(scene, mesh, this._activeEffect); this._afterBind(mesh, this._activeEffect, subMesh); } clone(name) { return SerializationHelper.Clone(() => new ShadowOnlyMaterial(name, this.getScene()), this); } serialize() { const serializationObject = super.serialize(); serializationObject.customType = "BABYLON.ShadowOnlyMaterial"; return serializationObject; } getClassName() { return "ShadowOnlyMaterial"; } // Statics static Parse(source, scene, rootUrl) { return SerializationHelper.Parse(() => new ShadowOnlyMaterial(source.name, scene), source, scene, rootUrl); } } RegisterClass("BABYLON.ShadowOnlyMaterial", ShadowOnlyMaterial); //# sourceMappingURL=shadowOnlyMaterial.js.map