Raymarching in gl.gridshape Cube as texturing [solved]

tmhglnd's icon

Hi all!

I'm trying to make an interpretation of this nice tutorial from The Art of Code, where he codes a raymarching shader and uses it on a cube in Unity. https://www.youtube.com/watch?v=S8AWd66hoCo&ab_channel=TheArtofCode

However I'm getting stuck at the point where he is converting world to object coordinates to get the camera movement correct (around 20:40 in the video). unity_WorldToObject is used in a matrixmultiplication with the _WorldSpaceCameraPos. I've found that this WorldToObject should be the inverse of the world view matrix, but I have not been able to calculate this myself and apply it correctly (if tried various combinations of inverse() with mat4x4(viewMatrix), also tried with modelViewMatrix.

I've been able to import the gl.camera camera position in the shader via a <param name="campos" type="vec3" state="CAMERA_POSITION"> which seems to be similar to the _worldSpaceCameraPos in Unity, and the v.vertex seems to be the <param name="position" type="vec3" state="POSITION" /> converted to a vec4(position, 1). Rotation of the object is also not working yet, but is not necessarily my first priority, movement would already be great.

Below the patch (code is in a comment box) and some screenshots to show where I'm at. Any pointers to what would allow me to do this conversion of WorldToObject would be much appreciated!

Max Patch
Copy patch and select New From Clipboard in Max.

Works without WorldToObject as long as cube is not moved in position

Wrong perspective

With rotation and moving also wrong

tmhglnd's icon

Solved with some help from Jaromir Mulders. You can inverse the modelViewMatrix directly with <param name="modelViewInverse" type="mat4" state="MODELVIEW_MATRIX" transform="INVERSE" />. Then store the camera position as jit_out.jit_Cam_position = vec3(modelViewInverse * vec4(0, 0, 0, 1));

Max Patch
Copy patch and select New From Clipboard in Max.

👽'tW∆s ∆lienz👽's icon
tmhglnd's icon

I've put up the final result of this study on github and my gumroad/patreon page. https://github.com/tmhglnd/gridshape-raymarcher