Get world position in Vertex shader (HLSL)
I'm using Temaran's UE4 Shader Plugin demo to write a custom Shader in HLSL purely from within C++ / engine code. (but I guess the question should also apply to the custom shader node)
When I try to multiply my vertex position with Primitive.LocalToWorld the result is always a zero vector.
Am I doing it wrong? How do you go from one space to another in the vertex and/or pixel shader in HLSL in UE4?
I read this into the pixel shader and set it as the output color, and the result is pure 0 (black). If I set
Note: I am using the shader by rendering a quad:
So I guess I have to manually create a world matrix in c++ and a float4x4 attribute on my shader and set it every frame?
I'm amazed how far Unreal has managed to get with such a cumbersome and undocumented graphics programming system.
I figured something out. I'm not accepting my answer because I haven't figured out how/when any of the built in matrices work.
As everyone probably knows, UE4 is really hard to customize. UE4 Graphics programming using the engine code, is not only harder, it's also undocumented.
I tried all matrices both on the HLSL usf side and on the c++ side to no avail. So I made my own matrix from scratch every frame and uploaded it to the shader as a 4x4
I render the camera to a quad. This means that in my Vertex shader I can treat it as if it is already in View space (instead of object space aka local space) to make it be exactly the size of the camera. So to get a ray in worldspace from that, I have to multiply by the Inverse Transpose View matrix.
Here's how to construct an Inverse Transpose View matrix in UE4 in c++:
First get the Viewport of the client (the game running on your machine):
We need the
Now just make a simple reverse View Matrix:
From this you can get a float4x4 matrix in your hlsl shader which you can mul with your vertex pos to go from "viewspace" to world space.
Follow this question
Once you sign in you will be able to subscribe for any updates here