Issues with Vertical Stretching and Horizontal Movement in Metal Shading Language Ray Tracing Shader

36 Views Asked by At

I'm currently learning Metal Shading Language and have been experimenting with a custom shader for ray tracing. The shader compiles and runs without errors, but I've encountered two issues that seem to be related to the calculation of ray direction in camera and world space. The rendered image appears stretched a bit vertically, and when moving the camera horizontally (either through rotation or translation), the movement is slower than expected. I suspect the problem lies in how rayDirCameraSpace or rayDirWorldSpace is calculated but I'm not entirely sure.

Below is the relevant part of my shader code:

// Vertex shader code...

fragment half4 shader_fragment(Vertex in [[stage_in]],
                               constant SCNSceneBuffer& scn_frame [[buffer(0)]],
                               constant NodeBuffer& scn_node [[buffer(1)]],
                               constant Resolution& resolution [[buffer(3)]],
                               constant float& fov [[buffer(2)]]) {
    // Get the current time
    float time = scn_frame.time;
    
    // Calculate UV coordinate
    float2 resolutionVector = float2(resolution.width, resolution.height);
    float2 uv = float2(in.position.x, resolutionVector.y - in.position.y) / resolutionVector.xy;
    
    // Convert UV to NDC | Convert from 0->1 to -1->1 range
    float2 ndc = uv * 2.0 - 1.0;
    float3 rayDirCameraSpace = normalize(float3(ndc.x, ndc.y, -1));
    
    // Transform Ray to World Space
    float4x4 invViewMatrix = scn_frame.inverseViewTransform;
    float3 rayDirWorldSpace = (invViewMatrix * float4(rayDirCameraSpace, 0.0)).xyz;
    float3 rayOrigin = (invViewMatrix * float4(0, 0, 0, 1)).xyz; // Camera position in world space
    // Additional shader code...
}

To address the horizontal camera movement issue, I experimented with adjusting the depth value in the rayDirCameraSpace calculation. Initially, the depth was set to -1. I changed this value to -3, hoping it would affect the camera's movement speed and the perceived stretching of the scene. This adjustment indeed made the scene move at the desired "speed" when the camera moves horizontally, which partially solved one of my problems.

Expected Outcome:

I hoped that by adjusting the depth value, I could correct both the horizontal movement speed and the vertical stretching issue without introducing any new problems.

Actual Result:

Changing the depth value to -3 corrected the movement speed of the scene, but it introduced a new issue: everything in the scene now appears stretched and as if it was very "close" to the camera. While the horizontal movement now feels more natural, the adjustment exacerbated the problem of distortion in the scene's rendering, making objects appear unnaturally elongated and closer than intended.

0

There are 0 best solutions below