+1 vote

So, I'm creating a special shader to render some objects using raymarching!

I succeded to render everything normally on a cube (MeshInstance),
however, the camera inside the shader used a different transform than the one in the actual scene.

In order to fully render, let's say, a sphere inside the cube I'd need to have the camera position (which I have) and the fragment's position in the world (similarly to how SCREEN_UV gives a vec2 for the fragment's position on screen, I want a vec3 for the fragment's position in the world):

color

Also, if possible as well, the fragment's position related to the mesh itself (e.g., how UV is relative to the node and SCREEN_UV is relative to the screen).

Thank you in advance <3!

in Engine by (24 points)

2 Answers

+2 votes
Best answer

Ok VERTEX will give you (by default) the local coordinates inside the vertex shader.
These coordinates will be relative to the mesh.

And VERTEX will give you the camera-relative coordinates inside the fragment shader.

You should be able to get world coordinates inside the fragment shader by applying the CAMERA_MATRIX to the VERTEX.

vec3 wrld_vertex = CAMERA_MATRIX * vec4(VERTEX, 1.0)

You can pass the (local) VERTEX from the vertex shader to the fragment shader by using a varying variable. Such variables will be interpolated between the corners of a face to match the pixel. Probably, there's also a way to calculate the local VERTEX inside the fragment shader but I don't see a convenient way (like i.e. an INV_MODELVIEW_MATRIX)

by (3,244 points)
selected by
0 votes
// from camera space to world space
vec3 VERTEX_WORLD = CAMERA_MATRIX * vec4(VERTEX, 1.0);

// from world space to object space
vec3 VERTEX_LOCAL = inverse(WORLD_MATRIX) * vec4(VERTEX_LOCAL, 1.0);

edit:
combine them for efficiency

// pixel's position in object space
VERTEX = inverse(WORLD_MATRIX) * CAMERA_MATRIX * vec4(VERTEX, 1.0);
by (31 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.