Skip to content

Instantly share code, notes, and snippets.

@padreputativo
Last active September 22, 2021 12:04
Show Gist options
  • Save padreputativo/4deaa7d29895e1d195245df9235977a0 to your computer and use it in GitHub Desktop.
Save padreputativo/4deaa7d29895e1d195245df9235977a0 to your computer and use it in GitHub Desktop.
Prototiping GridBox Texture Shader
shader_type spatial;
render_mode ensure_correct_normals;
//render_mode world_vertex_coords;
// Based in : https://godotshaders.com/shader/world-coordinates-grid-bw-shader/
uniform vec4 lightColor : hint_color;
uniform vec4 darkColor : hint_color;
uniform vec4 borderColor : hint_color;
const float gridSize = 4f;
const float half = gridSize / 2f;
const float border = gridSize / 1000f;
bool borde(float pos) {
return mod(pos, 1f) < border || 1f - mod(pos, 1f) < border || mod(pos, 0.5f) < border || 0.5f - mod(pos, 0.5f) < border;
}
void fragment(){
vec4 world = CAMERA_MATRIX * vec4(VERTEX, 1.0);
vec3 pos = world.xyz;
// This is to avoid the zero incoherence
if (pos.x <= 0f) pos.x = abs(pos.x - half);
if (pos.y <= 0f) pos.y = abs(pos.y - half);
if (pos.z <= 0f) pos.z = abs(pos.z - half);
pos /= gridSize;
//pos += gridSize * half;
// border color
vec3 col = vec3(0f);
if (borde(pos.x)) col -= 1f - borderColor.rgb;
if (borde(pos.y)) col -= 1f - borderColor.rgb;
if (borde(pos.z)) col -= 1f - borderColor.rgb;
pos.y += float(fract(float(int(pos.x*half))/half));
pos.z += float(fract(float(int(pos.y*half))/half));
if (vec3(fract(float(int(pos.z*half))/half)) == vec3(0f)) {
col += lightColor.rgb;
} else {
col += darkColor.rgb;
}
ROUGHNESS = col.x / half + 0.5;
ALBEDO = col;
// alpha changes they rendering order
//ALPHA = 0.5;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment