Skip to content

Instantly share code, notes, and snippets.

@levidavidmurray
Created June 9, 2024 04:10
Show Gist options
  • Save levidavidmurray/57cd71d79c8593e9b12cce4eaa768153 to your computer and use it in GitHub Desktop.
Save levidavidmurray/57cd71d79c8593e9b12cce4eaa768153 to your computer and use it in GitHub Desktop.

Revisions

  1. levidavidmurray created this gist Jun 9, 2024.
    41 changes: 41 additions & 0 deletions tiles.gdshader
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,41 @@
    shader_type spatial;
    render_mode unshaded;

    uniform vec2 target_uv = vec2(0.5, 0.5); // The UV coordinate to affect
    uniform float radius = 0.265; // Radius of the effect
    uniform float min_cell_size = 0.02; // Minimum cell size
    uniform float max_cell_size = 0.07; // Maximum cell size
    uniform float falloff = 1.0; // Falloff factor for the size transition
    uniform vec4 grid_color : source_color = vec4(1.0, 1.0, 1.0, 1.0);
    uniform vec4 background_color : source_color = vec4(0.0, 0.0, 0.0, 1.0);

    void fragment() {
    // Calculate the center of the current cell based on the max cell size
    vec2 cell_coord = floor(UV / max_cell_size);
    vec2 cell_center = cell_coord * max_cell_size + max_cell_size * 0.5;

    // Calculate the distance from the cell center to the target UV
    float dist = distance(cell_center, target_uv);
    float dist_t = dist / radius;

    // Determine the cell size based on the distance
    float cell_size = (dist < radius) ? mix(min_cell_size, max_cell_size, dist_t) : max_cell_size;

    // Calculate the scaled UV coordinates based on the max cell size for alignment
    vec2 aligned_uv = UV / max_cell_size;
    vec2 grid_uv = fract(aligned_uv);

    // Adjust the grid UV based on the calculated cell size to maintain uniform scaling
    vec2 adjusted_uv = (grid_uv - 0.5) * (max_cell_size / cell_size) + 0.5;

    // Define the gutter size relative to the cell size
    float gutter_size = 0.1 * max_cell_size / cell_size;
    float gutter_half = gutter_size * 0.5;

    // Determine the color based on the position within the cell
    if (adjusted_uv.x < gutter_half || adjusted_uv.x > 1.0 - gutter_half || adjusted_uv.y < gutter_half || adjusted_uv.y > 1.0 - gutter_half) {
    ALBEDO = grid_color.rgb;
    } else {
    ALBEDO = background_color.rgb;
    }
    }