1
\$\begingroup\$

I have a shader that should do raymarching.

But I have problems converting the fragment's position to a worldspace coordinate.

This is my vertex shader:

VertexShaderOutput VS(VertexShaderInput input)
{
    VertexShaderOutput output;
    output.Position = float4(input.Position.xy*2,0.0, 1.0);
    float4 worldPos = output.Position;
    output.WorldPos = mul(worldPos, InverseViewProjection);
    output.UV = input.UV;
    return output;
}

input.Position in this case is a quad from -0.5 to 0.5, hence why I multiply it by 2.

In my pixel shader, I want to get the ray start position from output.WorldPos.

However, the shader outputs WorldPos as this:

x = -118.943700000, y = -25.704420000, z = 23.763410000

This is wrong, because the camera is at around

x = -3846 y = -810 z = 734

What am I doing wrong? I expected a world position, but it seems to be a screen space position?

Here is how I create my matrices (DirectX SimpleMath):

View = Matrix::CreateLookAt(WorldPosition, target, up);
Projection = Matrix::CreatePerspectiveFieldOfView(fov, w / (float)h, n, f);
ViewProjection = View * Projection;
ViewProjection.Invert(InverseViewProjection);
Projection.Invert(InverseProjection);
View.Invert(InverseView);

The game renders perfectly fine, So I am certain The View/Projection Matrices are not wrong.

\$\endgroup\$
0

1 Answer 1

1
\$\begingroup\$

Okay after a lot of fiddling. I needed to divide xyz with w: Here is my Vertex Shader for a fullscreen quad;

VertexShaderOutput VS(VertexShaderInput input)
{
    VertexShaderOutput output;
    output.Position = float4(input.Position.xy,0.0, 1.0);
    float4 worldPos = output.Position;
    worldPos = mul(worldPos, InverseViewProjection);
    worldPos.xyz /= worldPos.w;
    output.WorldPos = worldPos;
    output.UV = input.UV;
    return output;
}
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .