From c24f5a77f37f080d2a8a6b1fae3c31bec3f1af94 Mon Sep 17 00:00:00 2001 From: cary <39078800+caryliu1999@users.noreply.github.com> Date: Wed, 19 Aug 2020 11:20:54 +0800 Subject: [PATCH] Fix a problem of shadow map. (#7168) --- .../core/pipeline/forward/forward-pipeline.ts | 2 +- cocos/core/renderer/scene/planar-shadows.ts | 20 +++++++++++-------- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/cocos/core/pipeline/forward/forward-pipeline.ts b/cocos/core/pipeline/forward/forward-pipeline.ts index 65639934b1..3c2a917c73 100644 --- a/cocos/core/pipeline/forward/forward-pipeline.ts +++ b/cocos/core/pipeline/forward/forward-pipeline.ts @@ -199,7 +199,7 @@ export class ForwardPipeline extends RenderPipeline { Mat4.invert(matShadowView, mainLight!.node!.worldMatrix); // light proj - const x = shadowInfo.near * shadowInfo.aspect; + const x = shadowInfo.orthoSize * shadowInfo.aspect; const y = shadowInfo.orthoSize; const projectionSignY = device.screenSpaceSignY * device.UVSpaceSignY; Mat4.ortho(matShadowViewProj, -x, x, -y, y, shadowInfo.near, shadowInfo.far, diff --git a/cocos/core/renderer/scene/planar-shadows.ts b/cocos/core/renderer/scene/planar-shadows.ts index 8b21d51eef..a775c6f56c 100644 --- a/cocos/core/renderer/scene/planar-shadows.ts +++ b/cocos/core/renderer/scene/planar-shadows.ts @@ -130,19 +130,23 @@ export class PlanarShadows { protected _dirty: boolean = true; public activate () { - if (!this._enabled) { - return; - } - this._dirty = true; const pipeline = legacyCC.director.root.pipeline; this._globalDescriptorSet = pipeline.descriptorSet; this._data = (pipeline as ForwardPipeline).shadowUBO; Color.toArray(this._data, this._shadowColor, UBOShadow.SHADOW_COLOR_OFFSET); this._globalDescriptorSet!.getBuffer(UBOShadow.BLOCK.binding).update(this.data); - this._material = new Material(); - this._material.initialize({ effectName: 'pipeline/planar-shadow' }); - this._instancingMaterial = new Material(); - this._instancingMaterial.initialize({ effectName: 'pipeline/planar-shadow', defines: { USE_INSTANCING: true } }); + if (!this._enabled) { + return; + } + this._dirty = true; + if (!this._material) { + this._material = new Material(); + this._material.initialize({ effectName: 'pipeline/planar-shadow' }); + } + if (!this._instancingMaterial) { + this._instancingMaterial = new Material(); + this._instancingMaterial.initialize({ effectName: 'pipeline/planar-shadow', defines: { USE_INSTANCING: true } }); + } } public updateSphereLight (light: SphereLight) {