Meters Grid

This Shader is based on the world coordinates grid b&w shader by Lordinator. This new version fixes the incoherence in the zero coordinates and now works in Godot 4. It also has configurable colors. You can paste them into the .tres files.

Red
shader_parameter/lightColor = Color(1, 0, 0.219608, 1)
shader_parameter/darkColor = Color(0.556863, 0.0588235, 0.168627, 1)
shader_parameter/borderColor = Color(1, 1, 1, 1)

Purple
shader_parameter/lightColor = Color(0.615686, 0.133333, 0.980392, 1)
shader_parameter/darkColor = Color(0.364706, 0.12549, 0.54902, 1)
shader_parameter/borderColor = Color(1, 1, 1, 1)

Orange
shader_parameter/lightColor = Color(1, 0.54902, 0, 1)
shader_parameter/darkColor = Color(0.556863, 0.333333, 0.0588235, 1)
shader_parameter/borderColor = Color(1, 1, 1, 1)

Green
shader_parameter/lightColor = Color(0.105882, 0.85098, 0.466667, 1)
shader_parameter/darkColor = Color(0.109804, 0.482353, 0.290196, 1)
shader_parameter/borderColor = Color(1, 1, 1, 1)

Light
shader_parameter/lightColor = Color(0.827451, 0.839216, 0.85098, 1)
shader_parameter/darkColor = Color(0.470588, 0.478431, 0.482353, 1)
shader_parameter/borderColor = Color(0.737255, 0.741176, 0.741176, 1)

Dark
shader_parameter/lightColor = Color(0.2, 0.2, 0.207843, 1)
shader_parameter/darkColor = Color(0.156863, 0.156863, 0.160784, 1)
shader_parameter/borderColor = Color(1, 1, 1, 1)

Shader code
shader_type spatial;
render_mode ensure_correct_normals;


uniform vec4 lightColor : source_color;
uniform vec4 darkColor : source_color;
uniform vec4 borderColor : source_color;
const float gridSize = 4.0;
const float half = gridSize / 2.0;
const float border = gridSize / 1000.0;

bool borde(float pos) {
	return mod(pos, 1.0) < border || 1.0 - mod(pos, 1.0) < border || mod(pos, 0.5) < border || 0.5 - mod(pos, 0.5) < border;
}

void fragment(){
	vec4 world = INV_VIEW_MATRIX * vec4(VERTEX, 1.0);
	vec3 pos = world.xyz;
	// This is to avoid the zero incoherence
	if (pos.x <= 0.0) pos.x = abs(pos.x - half);
	if (pos.y <= 0.0) pos.y = abs(pos.y - half);
	if (pos.z <= 0.0) pos.z = abs(pos.z - half);
	pos /= gridSize;
	//pos += gridSize * half;
	
	pos.y += float(fract(float(int(pos.x*half))/half));
	pos.z += float(fract(float(int(pos.y*half))/half));
	
	vec3 col = vec3(0.0);
	if (vec3(fract(float(int(pos.z*half))/half)) == vec3(0.0)) {
		col += lightColor.rgb;
	} else {
		col += darkColor.rgb;
	}
	
	// border color
	if (borde(pos.x)) col = borderColor.rgb;
	if (borde(pos.y)) col = borderColor.rgb;
	if (borde(pos.z)) col = borderColor.rgb;
	
	ROUGHNESS = col.x / half + 1.0;
	ALBEDO = col;
	//METALLIC = 1f;
	//SPECULAR = 1f;
	
	// alpha changes the rendering order
	//ALPHA = 1f;
}
Tags
3d, grid, prototype, prototyping, Spatial, triplanar, world coordinates
The shader code and all code snippets in this post are under CC0 license and can be used freely without the author's permission. Images and videos, and assets depicted in those, do not fall under this license. For more info, see our License terms.

Related shaders

Dashed Grid (The Best Darn Grid Shader (Yet))

Dotted grid 2d [Improved]

3D Grid (with individual cell control)

Subscribe
Notify of
guest

2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
goblinhours
goblinhours
1 year ago

massive time saver for prototyping, thanks for sharing!