const FMatrix WorldToShadowMatrix = // Translate to the origin of the shadow's translated world space FTranslationMatrix(PreShadowTranslation) * // Transform into the shadow's post projection space // This has to be the same transform used to render the shadow depths SubjectAndReceiverMatrix * // Scale and translate x and y to be texture coordinates into the ShadowInfo's rectangle in the shadow depth buffer // Normalize z by MaxSubjectDepth, as was done when writing shadow depths FMatrix( FPlane(ShadowResolutionFractionX,0, 0, 0), FPlane(0, -ShadowResolutionFractionY,0, 0), FPlane(0, 0, InvMaxSubjectDepth, 0), FPlane( (X + BorderSize) * InvBufferResolutionX + ShadowResolutionFractionX, //第一级CasadeX偏移就是(2048 + 4) * 1 / 6144 + 0级在ShadowMap的所在UV.X (Y + BorderSize) * InvBufferResolutionY + ShadowResolutionFractionY, //第一级CasadeY偏移就是(0 + 4) * 1 / 2048 + 0级在ShadowMap的所在UV.Y 0, 1 ) );
FMatrix ScreenToShadow; FMatrix ViewDependentTransform = // Z of the position being transformed is actually view space Z, // Transform it into post projection space by applying the projection matrix, // Which is the required space before applying View.InvTranslatedViewProjectionMatrix FMatrix( FPlane(1,0,0,0), FPlane(0,1,0,0), FPlane(0,0,View.ViewMatrices.GetProjectionMatrix().M[2][2],1), FPlane(0,0,View.ViewMatrices.GetProjectionMatrix().M[3][2],0)) * // Transform the post projection space position into translated world space // Translated world space is normal world space translated to the view's origin, // Which prevents floating point imprecision far from the world origin. View.ViewMatrices.GetInvTranslatedViewProjectionMatrix() * FTranslationMatrix(-View.ViewMatrices.GetPreViewTranslation());
FMatrix ShadowMapDependentTransform = // Translate to the origin of the shadow's translated world space FTranslationMatrix(PreShadowTranslation) * // Transform into the shadow's post projection space // This has to be the same transform used to render the shadow depths FMatrix(TranslatedWorldToClipInnerMatrix) * // Scale and translate x and y to be texture coordinates into the ShadowInfo's rectangle in the shadow depth buffer // Normalize z by MaxSubjectDepth, as was done when writing shadow depths FMatrix( FPlane(ShadowResolutionFractionX,0, 0, 0), FPlane(0, -ShadowResolutionFractionY,0, 0), FPlane(0, 0, InvMaxSubjectDepth, 0), FPlane( (TileOffsetX + BorderSize) * InvBufferResolutionX + ShadowResolutionFractionX, (TileOffsetY + BorderSize) * InvBufferResolutionY + ShadowResolutionFractionY, 0, 1 ) );
if (View.bIsMobileMultiViewEnabled && View.Family->Views.Num() > 0) { // In Multiview, we split ViewDependentTransform out into ViewUniformShaderParameters.MobileMultiviewShadowTransform // So we can multiply it later in shader. ScreenToShadow = ShadowMapDependentTransform; } else { ScreenToShadow = ViewDependentTransform * ShadowMapDependentTransform; } return ScreenToShadow; }