# Help with Matrices and View Stuff

So I am hot on the trail of volumetric systems for us though after this weekend I will have to shift focus to paying work… but that’s another story.

ANYWAYS I got this:
https://playground.babylonjs.com/#6Z7SIZ

Where it like 95% works… I am having some trouble with my maths…

If you notice the raymached sphere (the blue one) scales differently then the rest of the scene. I have a sneaking suspicion it has to do with:

``````    vec3 ssToPos(){
vec4 ndc = vec4(
(vUV.x - 0.5) * 2.0,
(vUV.y - 0.5) * 2.0,
1.0,
1.0
);

mat4 invMat =  inverse(camProjection*camView);
vec4 clip = invMat * ndc;
return (clip / clip.w).xyz;
}
``````

which is added to the direction of my camera to give me a ray direction, which without it seems to really extend the distortion effect. Maybe I need a transform calculation too or something?

I have narrowed it down to Camera MinZ and Camera MaxZ being changed.

Is there a Matrix that reflects those changes that I need to apply?
https://playground.babylonjs.com/#6Z7SIZ#1

Here’s something that should be ok:

https://playground.babylonjs.com/#6Z7SIZ#2

I did not really understand your `getRayDir` + `ssToPos` functions, so I grabbed a `getRayFromScreenSpace` from here that seems to fit the bill

1 Like

https://playground.babylonjs.com/#6Z7SIZ#7

all right, now I need to combine in the BJS standard materials Lighting calculations and this is promising!

You have a problem with the depth you calculate in the ray marching function.
If I set `camMinZ=10` I get:

``````        return vec4(col, (t-camMinZ)/(camMaxZ - camMinZ));
``````

https://playground.babylonjs.com/#6Z7SIZ#6

Note that there’s still some problems with the depth computation even with that change, because if you rotate the scene, you have a bad intersection between the two spheres (I moved the second sphere 1 unit below so that it intersects the first sphere):

It should be something like that instead (it’s the scene when first rendered, without moving/rotation):

Looks like there is some depth error as you approach the edges of the screen as well.

https://playground.babylonjs.com/#6Z7SIZ#12

got the lighting started

Here’s a PG with corrected depth calculation:

https://playground.babylonjs.com/#6Z7SIZ#16

The key point was to notice that all calculations are done in world space, whereas the depth read from the depth map against which we check our calculated depth is in screen space. So we have to project the world coordinate point we find through ray marching to screen space to be able to extract the right z value to compare with.

What I did:

• removed references to `camMinZ` and `camMaxZ` from `rayMarch`, because calculations are done in world space whereas camMinZ / camMaxZ are data given in view space.
• modified `render` to calculate the right z value for comparison with `depth` in the `main` function
• modified `applyFog` to take the distance value “as is” (dist is now already between 0 and 1)
2 Likes

So when we opening a game studio? hahah you are totally the jelly to my peanut butter!

Most people do not understand all this as well as you do! We are blessed to have you in the community.

Not really, I struggled to make this work, whereas it was not so difficult once you see the solution!

Also, I should have said “clip space” and not “screen space” in my explanation.

1 Like

I was really close with my script too, just was not making the correct shifts to accommodate for the cliping! You have opened my eyes to which matrix is which now though.