look_at() function in shader code?
Asked Answered
L

6

0

I'd like to make my shader material behave like a billboard so that it always faces my camera regardless of where the camera is. I.e. I'd like the shader to work the same way as an animatedSprite3D behaves when in billboard mode.

I tried this vertex shader code: MODELVIEW_MATRIX = INV_CAMERA_MATRIX * mat4(vec4(normalize(cross(vec3(0.0, 1.0, 0.0), CAMERA_MATRIX[2].xyz)),0.0),vec4(0.0, 1.0, 0.0, 0.0),vec4(normalize(cross(CAMERA_MATRIX[0].xyz, vec3(0.0, 1.0, 0.0))),0.0),WORLD_MATRIX[3]); Which kind of works, but when I rotate my camera it moves my object. I want my object to remain in the same place in world_space when I rotate my camera.

Thanks!

Loesceke answered 20/1, 2022 at 19:29 Comment(0)
A
0

Make a 3d sprite, assign spatial material, enable billboarding in material properties, convert material to shader material and copy matrix calculations from the vertex function.

Ascender answered 20/1, 2022 at 19:33 Comment(0)
T
0

Use a mat3, that removes the positional portion of the transform.

Tallis answered 20/1, 2022 at 22:13 Comment(0)
L
0

@xyz unfortunately when I convert it seems to erase the hidden billboard code. @cybereality I actually have no idea what the above code is trying to do. My understanding is that we have: Model space which is all points defined relative to the models center at 0, 0, 0. World space which is all points defined relative tot he world's center at 0, 0, 0. View space which is all points defined relative to the camera at 0, 0, 0. And finally Clip space which is perspective and other stuff applied.

To go from Model space to World space we multiply by the Model matrix. In Godot the Model matrix is called WORLD_MATRIX.

To go from World space to View space we multiply by the View matrix. In Godot the View matrix is called CAMERA_MATRIX.

In Godot the Model matrix and View matrix have been premultiplied to allow a direct translation from Model space to View space, this matrix is called the MODELVIEW_MATRIX.

To make a billboard shader the MODELVIEW_MATRIX must be changed such that it does not apply rotation, but still applies translation and scaling.

MODELVIEW_MATRIX = WORLD_MATRIX * CAMERA_MATRIX.

I believe the CAMERA_MATRIX = translation_matrix rotation_matrix scaling_matrix

Which gives us this:

MODELVIEW_MATRIX = WORLD_MATRIX (translation_matrix rotation_matrix * scaling_matrix)

Now we want to remove the rotation part so we get this:

MODELVIEW_MATRIX = WORLD_MATRIX (translation_matrix scaling_matrix)

I'm not sure where to get translation_matrix and scaling_matrix? I'm sure I'm making a lot of other mistakes in logic. Can you fill in the gaps?

Loesceke answered 22/1, 2022 at 19:37 Comment(0)
T
0

Maybe try @xyz method. The code isn't that bad, but no point in reinventing the wheel.

Tallis answered 22/1, 2022 at 19:42 Comment(0)
A
0

@itgiawa said: @xyz unfortunately when I convert it seems to erase the hidden billboard code

Not sure what you mean by that. If you do what I've written you'll get billboarding code in shader's vertex function. Note that you need to enable billboarding in the material parameters, not in sprite properties. The line you'll get in the vertex shader is:

MODELVIEW_MATRIX = INV_CAMERA_MATRIX * mat4(CAMERA_MATRIX[0],CAMERA_MATRIX[1],CAMERA_MATRIX[2],WORLD_MATRIX[3]);
Ascender answered 22/1, 2022 at 20:23 Comment(0)
L
0

Thanks that code worked!

Loesceke answered 23/1, 2022 at 17:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.