diff --git a/cocos/pipeline/culling.ts b/cocos/pipeline/culling.ts index d8fa1f230e..b699321278 100644 --- a/cocos/pipeline/culling.ts +++ b/cocos/pipeline/culling.ts @@ -25,8 +25,8 @@ export function cullSpotLight (light: SpotLight, model: Model) { export const cullSceneWithDirectionalLight = (() => { const lightFrustum: frustum = new frustum(); lightFrustum.accurate = true; - return (out: Model[], modelToCull: Model[], sceneCamera: Camera, light: DirectionalLight, near: number, far: number) => { - calcDirectionalLightCullFrustum(lightFrustum, sceneCamera, light, near, far); + return (out: Model[], modelToCull: Model[], sceneCamera: Camera, light: DirectionalLight, near: number, far: number, nearBias: number) => { + calcDirectionalLightCullFrustum(lightFrustum, sceneCamera, light, near, far, nearBias); for (const m of modelToCull) { if (!m.enabled || !m.worldBounds) { continue; @@ -51,7 +51,7 @@ export const calcDirectionalLightCullFrustum = (() => { const lightVeiwMatInv = cc.mat4(); const minBoxCorner = cc.v3(); const maxBoxCorner = cc.v3(); - return (out: frustum, sceneCamera: Camera, light: DirectionalLight, near: number, far: number) => { + return (out: frustum, sceneCamera: Camera, light: DirectionalLight, near: number, far: number, nearBias: number) => { mat4.fromRT(lightViewMat, light.node.getWorldRotation(lightRot), sceneCamera.node.getWorldPosition(lightPos)); mat4.invert(lightVeiwMatInv, lightViewMat); sceneCamera.getSplitFrustum(camFrustum, near, far); @@ -70,9 +70,10 @@ export const calcDirectionalLightCullFrustum = (() => { } // calc the light world transform ,suppose that the light's position is at the camera's location. vec3.set(lightViewCenter, (minBoxCorner.x + maxBoxCorner.x) / 2, (minBoxCorner.y + maxBoxCorner.y) / 2, maxBoxCorner.z); + lightViewCenter.z += nearBias; vec3.transformMat4(lightPos, lightViewCenter, lightViewMat); mat4.fromRT(lightViewMat, light.node.getWorldRotation(lightRot), lightPos); // calc the light's frustum - frustum.createOrtho(out, maxBoxCorner.x - minBoxCorner.x, maxBoxCorner.y - minBoxCorner.y, 0, minBoxCorner.z - maxBoxCorner.z, lightViewMat); + frustum.createOrtho(out, maxBoxCorner.x - minBoxCorner.x, maxBoxCorner.y - minBoxCorner.y, 0, minBoxCorner.z - nearBias - maxBoxCorner.z, lightViewMat); }; })(); diff --git a/cocos/pipeline/forward/forward-stage.ts b/cocos/pipeline/forward/forward-stage.ts index 3af25e9211..e1013b447c 100644 --- a/cocos/pipeline/forward/forward-stage.ts +++ b/cocos/pipeline/forward/forward-stage.ts @@ -89,7 +89,7 @@ export class ForwardStage extends RenderStage { this._castShadowObjects.splice(0); const camera = view.camera; const scene = camera.scene; - cullSceneWithDirectionalLight(this._castShadowObjects, scene.models, camera, scene.mainLight, camera.nearClip, camera.farClip); + cullSceneWithDirectionalLight(this._castShadowObjects, scene.models, camera, scene.mainLight, camera.nearClip, camera.farClip, 10); for (const m of this._castShadowObjects) { for (let i = 0; i < m.subModelNum; i++) { for (let j = 0; j < m.getSubModel(i).passes.length; j++) {