Orient evrey face of a mesh toward Camera
Asked Answered
G

4

0

Hi there,

I'm new here and i'm trying to replicate the effet described in this tutorial.

I was able to create the model, unwarp it as described (each face covers the entirty of the UV space) and displacement according to the UV unwrap.

However I'm stuck at the point where we need to orient each faces toward camera. In the tutorial he simply multiplies the offset vertex vector by the view vector. However in godot, it seems that we don't have cacces to the VIEW vector in the vertex shader. We have the MODELVIEW, but when i multiply the model view matrix, my quad simply disparear...

Here is my code :

```
void vertex() {
	vec2 centered_uv= UV *2f -1f;
	VERTEX += vec3(centered_uv,0);
//	The following line is wrong :
	VERTEX =  (MODELVIEW_MATRIX * vec4(VERTEX, 0.0)).xyz;
	COLOR = vec4(UV,0,1);
}
```



Is there any chance I could retrieve the view vector in the vertex shader ? Or in other words, is there a know way to always "billboard" all the quads of a mesh ? 

Thanks for your help,
Grimalkin answered 15/8, 2021 at 9:24 Comment(0)
B
0

Welcome to the forums @Riton!

I think it should be possible, as there is a setting in the Spatial Material that makes quad meshes face the camera at all times and I think it runs in the vertex shader.

What I would recommend doing is making a new MeshInstance with a SpatialMaterial, going to the parameters and selecting the billboard option (I don't remember the name right off, sorry!), and then once it's set you can click the little dropdown on the SpatialMaterial and in the menu that appears there should be an option that says "convert to shader material". If you click this, it should convert the SpatialMaterial to a shader you can see, and in the vertex function I believe there will be the code to make it face the camera.

Bipetalous answered 16/8, 2021 at 13:10 Comment(0)
G
0

Thanks @TwistedTwigleg for your welcome and for the hint.

I've managed to find the solution and get the expected result. I made two mistake in the previous code. The first one is that I needed to inverse the Y channel of my UV channel.

The second is more tricky, and i've spend some time before understanding that what is commonly called view matrix is called INV_CAMERA_MATRIX in Godot.

So, to get each vertex facing toward camera, you simply need to multiply your centered UV by the view matrix and add the result to the vertex position.

Here is the code for anyone is interested :

	shader_type spatial ;
	uniform float offset_multiply = 1f;
void vertex() {
vec2 centered_uv= UV;
        	
centered_uv= (UV *2f -1f);
centered_uv.y *=-1f;

vec4 offset_UV = vec4(centered_uv,0,0) * INV_CAMERA_MATRIX;
        	
VERTEX += offset_UV.xyz *offset_multiply;
COLOR = vec4(UV,0,1);
}
        
void fragment() {
	ALBEDO = COLOR.xyz;
}

Thanks again,

Grimalkin answered 17/8, 2021 at 19:36 Comment(0)
M
0

@Riton , I know its an old post, but just wanted to say thanks so much for posting this code. I have been trying to convert the same tutorial to Godot and was struggling. This is exactly what I was looking for.

Moldavia answered 25/2, 2022 at 3:58 Comment(0)
A
0

Grimalkin
Hey! Firstly Huge thanks for posting your code! There's just this one thing that doesn't work for me.. faces do get distorted if you look at them from front/back.
Do you have any idea how to fix this? I've been trying to find a solution for a very long time, but can't fix it myself 🙁
Here's how the distortion looks..

Thanks!

Aldis answered 24/11, 2023 at 18:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.