fix shadow frustum error (#4190)

This commit is contained in:
zxg0622
2019-04-11 14:54:16 +08:00
committed by Weiliang Wang
parent 80e2200301
commit c75a164ac1
2 changed files with 42 additions and 18 deletions
+31
View File
@@ -31,6 +31,37 @@ export class frustum {
return new frustum();
}
public static createOrtho = (() => {
const _temp_v3 = cc.v3();
return (out: frustum, width: number, height: number, near: number, far: number, transform: mat4) => {
const halfWidth = width / 2;
const halfHeight = height / 2;
vec3.set(_temp_v3, halfWidth, halfHeight, near);
vec3.transformMat4(out.vertices[0], _temp_v3, transform);
vec3.set(_temp_v3, -halfWidth, halfHeight, near);
vec3.transformMat4(out.vertices[1], _temp_v3, transform);
vec3.set(_temp_v3, -halfWidth, -halfHeight, near);
vec3.transformMat4(out.vertices[2], _temp_v3, transform);
vec3.set(_temp_v3, halfWidth, -halfHeight, near);
vec3.transformMat4(out.vertices[3], _temp_v3, transform);
vec3.set(_temp_v3, halfWidth, halfHeight, far);
vec3.transformMat4(out.vertices[4], _temp_v3, transform);
vec3.set(_temp_v3, -halfWidth, halfHeight, far);
vec3.transformMat4(out.vertices[5], _temp_v3, transform);
vec3.set(_temp_v3, -halfWidth, -halfHeight, far);
vec3.transformMat4(out.vertices[6], _temp_v3, transform);
vec3.set(_temp_v3, halfWidth, -halfHeight, far);
vec3.transformMat4(out.vertices[7], _temp_v3, transform);
plane.fromPoints(out.planes[0], out.vertices[1], out.vertices[6], out.vertices[5]);
plane.fromPoints(out.planes[1], out.vertices[3], out.vertices[4], out.vertices[7]);
plane.fromPoints(out.planes[2], out.vertices[6], out.vertices[3], out.vertices[7]);
plane.fromPoints(out.planes[3], out.vertices[0], out.vertices[5], out.vertices[4]);
plane.fromPoints(out.planes[4], out.vertices[2], out.vertices[0], out.vertices[3]);
plane.fromPoints(out.planes[0], out.vertices[7], out.vertices[5], out.vertices[6]);
};
})();
/**
* Clone a frustum
*/
+11 -18
View File
@@ -43,21 +43,20 @@ export const cullSceneWithDirectionalLight = (() => {
export const calcDirectionalLightCullFrustum = (() => {
const lightPos = cc.v3();
const lightPosOffset = cc.v3();
const lightViewCenter = cc.v3();
const lightRot = cc.quat();
const camFrustum = new frustum();
camFrustum.accurate = true;
const lightViewMat = cc.mat4();
const lightProjMat = cc.mat4();
const lightVPMat = cc.mat4();
const lightVPMatInv = cc.mat4();
const lightVeiwMatInv = cc.mat4();
const minBoxCorner = cc.v3();
const maxBoxCorner = cc.v3();
return (out: frustum, sceneCamera: Camera, light: DirectionalLight, near: number, far: number) => {
mat4.invert(lightViewMat, light.node.getWorldMatrix(lightViewMat));
mat4.fromRT(lightViewMat, light.node.getWorldRotation(lightRot), sceneCamera.node.getWorldPosition(lightPos));
mat4.invert(lightVeiwMatInv, lightViewMat);
sceneCamera.getSplitFrustum(camFrustum, near, far);
// transform camera frustum to light space
camFrustum.transform(lightViewMat);
camFrustum.transform(lightVeiwMatInv);
vec3.set(minBoxCorner, camFrustum.vertices[0].x, camFrustum.vertices[0].y, camFrustum.vertices[0].z);
vec3.copy(maxBoxCorner, minBoxCorner);
// calculate the light frustum corner
@@ -69,17 +68,11 @@ export const calcDirectionalLightCullFrustum = (() => {
maxBoxCorner.y = Math.max(maxBoxCorner.y, camFrustum.vertices[i].y);
maxBoxCorner.z = Math.max(maxBoxCorner.z, camFrustum.vertices[i].z);
}
// move light to frustum near plane center
vec3.set(lightPosOffset, (minBoxCorner.x + maxBoxCorner.x) / 2, (minBoxCorner.y + maxBoxCorner.y) / 2, maxBoxCorner.z);
vec3.add(minBoxCorner, minBoxCorner, lightPosOffset);
vec3.add(maxBoxCorner, maxBoxCorner, lightPosOffset);
mat4.ortho(lightProjMat, minBoxCorner.x, maxBoxCorner.x, minBoxCorner.y, maxBoxCorner.y, -maxBoxCorner.z, -minBoxCorner.z);
light.node.getPosition(lightPos);
vec3.add(lightPos, lightPos, lightPosOffset);
mat4.fromRT(lightViewMat, light.node.getRotation(lightRot), lightPos);
mat4.mul(lightVPMat, lightProjMat, lightViewMat);
mat4.invert(lightVPMatInv, lightVPMat);
out.update(lightVPMat, lightVPMatInv);
// 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);
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);
};
})();