Godot Version
4.6.1
Question
I want to draw an inifity grid with a single quad mesh.
I’m using MeshInstance2D with a rectangle mesh, the mesh is shown successfully, but the position (‘world_pos‘ in shader) looks always the screen space position, it doesn’t move or scale when camera is moving or scaling. Also I can’t find anything like VIEW_MATRIX or CAMERA_MATRIX. What’s the proper way to do so?
code:
shader_type canvas_item;
uniform vec2 grid_size = vec2(64.0, 64.0);
uniform float line_width_px = 2.0;
uniform vec4 grid_color : source_color = vec4(0.5,0.5,0.5,0.5);
void vertex() {
VERTEX = (MODEL_MATRIX * vec4(VERTEX, 0.0, 1.0)).xy;
}
void fragment() {
vec2 world_pos = VERTEX;
vec2 coord = world_pos / grid_size;
vec2 grid = abs(fract(coord - 0.5) - 0.5);
vec2 fw = fwidth(coord);
float line = min(grid.x / fw.x, grid.y / fw.y);
float alpha = 1.0 - smoothstep(0.0, line_width_px, line);
COLOR = vec4(grid_color.rgb, grid_color.a * alpha);
}