Download

View matrix modification

Hi, I want to modify the viewFrustum inside the FrustumCull function in SceneVisibility.cpp (Line #466). Basically, I want a bigger frustum by moving backwards a little bit (from the current view location) and a larger FOV. But for some reason, it doesn’t seem to be working. Here is my code. Am I missing something. Can someone point me in the right direction ? Here viewTranslationDistance and enlargedFOV are the distance I want to move backward from current view location and larger FOV size respectively.


FMatrix viewRotationMatrix = FInverseRotationMatrix::FInverseRotationMatrix(View.ViewRotation);
    viewRotationMatrix = viewRotationMatrix * FMatrix(
        FPlane(0, 0, 1, 0),
        FPlane(1, 0, 0, 0),
        FPlane(0, 1, 0, 0),
        FPlane(0, 0, 0, 1));

    FMatrix newViewMatrix = FTranslationMatrix::FTranslationMatrix(FVector(-viewTranslationDistance, 0.0, 0.0) - View.ViewLocation) * viewRotationMatrix;

    // Assuming viewport is wider than taller
    float XAxisMultiplier = 1.0f;
    float YAxisMultiplier = viewportWidth / viewportHeight;

    FMatrix newProjectionMatrix = FReversedZPerspectiveMatrix::FReversedZPerspectiveMatrix(enlargedFOV * PI / 360.0, enlargedFOV * PI / 360.0, XAxisMultiplier, YAxisMultiplier, nearClipPlane, farClipPlane);

    // Adjust projection matrix for appropriate for underlying RHI layer
    newProjectionMatrix = AdjustProjectionMatrixForRHI(newProjectionMatrix);

    // FMatrix newViewProjectionMatrix = newViewMatrix * newProjectionMatrix;

    FPlane Temp;
    FConvexVolume ModifiedFrustum;
    ModifiedFrustum.Planes.Empty(6);
    if (newViewProjectionMatrix.GetFrustumNearPlane(Temp))
    {
        ModifiedFrustum.Planes.Add(Temp);
    }
    if (newViewProjectionMatrix.GetFrustumLeftPlane(Temp))
    {
        ModifiedFrustum.Planes.Add(Temp);
    }
    if (newViewProjectionMatrix.GetFrustumRightPlane(Temp))
    {
        ModifiedFrustum.Planes.Add(Temp);
    }
    if (newViewProjectionMatrix.GetFrustumTopPlane(Temp))
    {
        ModifiedFrustum.Planes.Add(Temp);
    }
    if (newViewProjectionMatrix.GetFrustumBottomPlane(Temp))
    {
        ModifiedFrustum.Planes.Add(Temp);
    }
    if (newViewProjectionMatrix.GetFrustumFarPlane(Temp))
    {
        ModifiedFrustum.Planes.Add(Temp);
    }

    ModifiedFrustum.Init();

    // Check intersection with mesh box
    if (ModifiedFrustum.IntersectBox(Origin, Extent) || ModifiedFrustum.IntersectSphere(Origin, Radius))
    {
        return true;
    }
    else
    {
        return false;
    }