How to get vertex world positions in fragment shader

I am trying to recreate this shader:

However, I am having trouble as early as 4:40, when I am trying to draw a box to my view with an ImageEffectShader.

My shader looks like this:

 v2f vert (appdata v)
            {
                v2f o;
                o.pos = UnityObjectToClipPos(v.pos);
                o.uv = v.uv;
                return o;
            }

            float2 rayBoxDist (float3 boundsMin, float3 boundsMax, float3 rayOrigin, float3 rayDir)
            {
                float3 t0 = (boundsMin - rayOrigin) / rayDir;
                float3 t1 = (boundsMax - rayOrigin) / rayDir;
                float3 tmin = min(t0, t1);
                float3 tmax = max(t0, t1);

                float distA = max(max(tmin.x, tmin.y), tmin.z);
                float distB = min(tmax.x, min(tmax.x, tmax.y));

                float distToBox = max(0, distA);
                float distInsideBox = max(0, distB - distToBox);
                return float2(distToBox, distInsideBox);
            }

            float3 BoundsMin;
            float3 BoundsMax;
            float3 CameraPos;

            sampler2D _MainTex;

            fixed4 frag (v2f i) : SV_Target
            {
                fixed4 col = tex2D(_MainTex, i.uv);


                float3 rayOrigin = CameraPos;

                float3 worldPos = mul(unity_ObjectToWorld, float4(i.pos.xyz, 1)).xyz;
                float3 rayDir = normalize(worldPos - CameraPos);

                float2 rayBoxInfo = rayBoxDist(BoundsMin, BoundsMax, rayOrigin, rayDir);

                float distToBox = rayBoxInfo.x;
                float distInsideBox = rayBoxInfo.y;

                bool rayBoxHit = distInsideBox > 0;
                if (rayBoxHit) {
                    return col;
                }

                return float4(0,0,0,1);
            }

The shader renders an all-black image, indicating that no ray has hit the box.

I have already confirmed that rayBoxDist works with this line of code:
float2 rayBoxInfo = rayBoxDist(float3(0,0,0), float3(2,2,2), float3(1,1.9,1), 1 / float3(-1,0,0)); Which returns the expected rayBoxInfo.

I assume that I am somehow confusing different coordinate systems, but I don’t know where my error is.

If I get the world space position of a vertex in the shader, I should be able to infer the view direction, so how can I get the position of a vertex in world space?

Your vertex shader already transforms your object space position into clip space. So your fragment shader does not get the object space vector so transforming the clip space vector again through the object to world matrix makes no sense ^^. You need to also pass along either the object space vertex position or better, calculate the worldspace position of the vertex in the vertex shader and pass the worldspace position along. For this you need to add the worldPos to your “v2f” struct so you can carry it over from your vertex to fragment shader.

However since this ia an image effect shader, the object or worldspace position of the fullscreen quad that is actually rendered with this shader it pretty much irrelevant.

There are so many differences between the shader code in the video and your own. So it’s hard to follow what’s actually happening. In the original code he calculates the actual view / ray direction from the uv coordinates on the fullscreen quad which he “unprojects” into camera space by using the inverse projection matrix and then convert the camera space direction vector into a worldspace direction through the camera to world matrix. You’re doing something completely different here.