Skip to content

Instantly share code, notes, and snippets.

@PranavSK
Last active July 11, 2022 22:15
Show Gist options
  • Save PranavSK/b0c3547aeaa3ce2643e7123781b5430f to your computer and use it in GitHub Desktop.
Save PranavSK/b0c3547aeaa3ce2643e7123781b5430f to your computer and use it in GitHub Desktop.
shader_type spatial;
render_mode unshaded;
uniform sampler2D pattern_map: hint_albedo;
uniform vec2 uv_offset = vec2(0.5, 0.5);
uniform vec2 uv_tiling = vec2(1.0, 1.0);
float when_lt(float x, float y) {
return max(sign(y - x), 0.0);
}
void vertex() {
// Extract scale from the WORLD_MATRIX.
vec3 object_scale = vec3(
length(WORLD_MATRIX[0].xyz),
-length(WORLD_MATRIX[1].xyz),
-length(WORLD_MATRIX[2].xyz)
);
// Get scaled object positions.
vec3 scaled_position = VERTEX * object_scale;
// Invert UV depending on side for mapping.
vec2 temp_zx = scaled_position.zx * vec2(0.5 * NORMAL.y, 0.5);
vec2 temp_zy = scaled_position.zy * vec2(0.5 * NORMAL.x, 0.5);
vec2 temp_xy = scaled_position.xy * vec2(0.5 * NORMAL.z, 0.5);
vec3 normal_abs = abs(NORMAL);
float cond1 = when_lt(max(normal_abs.x,normal_abs.y), normal_abs.z);
float cond2 = when_lt(normal_abs.x, normal_abs.y);
UV = cond1 * temp_xy + (1.0 - cond1) * (cond2 * temp_zx + (1.0 - cond2) * temp_zy);
// Shift and tile UVs.
UV *= uv_tiling;
UV += uv_offset;
}
void fragment() {
ALBEDO = texture(pattern_map, UV).rgb;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment