Really hoping someone can see where I’ve gone wrong here, I will try to list reasoning and relevant code.
The aim is to use a SceneCaptureComponent2D with RenderTarget to capture a Shadowmap, and then use that to mask out an effect in a material later on, however, I am stuck at the first step. The RT is definately capturing correct depth, so the issue is in reading the depth map then applying it to fragments.
The first place i feel i may have a mistake is getting the perspective matrix for the SceneCaptureComponent2D. I could not find a way of getting the matrix used for actual rendering so instead decided to recreate it as needed, with the following code:
void MyFuncLib::GetSceneCaptureCameraMatrix(const USceneCaptureComponent2D* captureComponent, FMatrix& cameraMatrix)
{
const FTransform& worldTransform = captureComponent->GetComponentToWorld();
FMatrix viewMatrix = worldTransform.ToInverseMatrixWithScale();
// swap axis st. x=z,y=x,z=y (unreal coord space) so that z is up
viewMatrix = viewMatrix * FMatrix(
FPlane(0, 0, 1, 0),
FPlane(1, 0, 0, 0),
FPlane(0, 1, 0, 0),
FPlane(0, 0, 0, 1));
const float halfFOV = 0.5f * captureComponent->FOVAngle * (float)PI / 360.0f;
FIntPoint captureSize(captureComponent->TextureTarget->GetSurfaceWidth(), captureComponent->TextureTarget->GetSurfaceHeight());
FMatrix projectionMatrix;
float maxViewDistOverride = captureComponent->MaxViewDistanceOverride;
if (maxViewDistOverride > 0)
{
projectionMatrix = FReversedZPerspectiveMatrix(halfFOV, captureSize.X, captureSize.Y, GNearClippingPlane, maxViewDistOverride);
}
else
{
projectionMatrix = FReversedZPerspectiveMatrix(halfFOV, captureSize.X, captureSize.Y, GNearClippingPlane);
}
cameraMatrix = projectionMatrix * viewMatrix;
}
Essentially it gets the component, gets its inverse matrix, then applies a projection matrix to get the ‘camera matrix’…or at least thats the idea?
The other point i think i may be failing is in the material that samples the shadowmap. This is a test Material i was using to (try and) visualise the shadowing:
This just results in black in my test case, I’m not sure if i have the maths right (first time ive had to do this with vec’s rather than a mat4)…or if I’ve made a silly mistake elsewhere.
Really appreciate any help with this.
Thanks!