GodotComponentTest/materials/shader/VoronoiBasedBiomes2D.gdshader

166 lines
4.3 KiB
Plaintext

shader_type canvas_item;
uniform vec4 DeepWaterColor : hint_color = vec4(0, 0, 0.6, 1);
uniform vec4 WaterColor : hint_color = vec4(0, 0, 0.7, 1);
uniform vec4 LightWaterColor : hint_color = vec4(0, 0, 1, 1);
uniform vec4 SandColor : hint_color = vec4(0.8, 0.8, 0.1, 1);
uniform vec4 GrassColor : hint_color = vec4(0, 0.6, 0, 1);
uniform vec4 ForestColor : hint_color = vec4(0, 0.4, 0, 1);
uniform vec4 RockColor : hint_color = vec4(0.5, 0.5, 0.4, 1);
uniform vec4 SnowColor : hint_color = vec4(1);
vec2 rand2d(vec2 uv) {
return vec2(fract(sin(dot(uv.xy,
vec2(12.9898,78.233))) * 43758.5453123));
}
float rand1d(vec2 uv){
return fract(sin(dot(uv, vec2(12.9898, 78.233))) * 43758.5453);
}
vec2 voronoi_segment(vec2 uv, float columns, float rows) {
vec2 index_uv = floor(vec2(uv.x * columns, uv.y * rows));
vec2 fract_uv = fract(vec2(uv.x * columns, uv.y * rows));
float minimum_dist = 1.0;
vec2 minimum_point;
vec2 minimum_neighbour;
for (int y = -1; y <= 1; y++) {
for (int x = -1; x <= 1; x++) {
vec2 neighbour = vec2(float(x), float(y));
vec2 point = rand2d(index_uv + neighbour);
vec2 diff = neighbour + point - fract_uv;
float dist = length(diff);
if (dist < minimum_dist) {
minimum_dist = dist;
minimum_point = point;
minimum_neighbour = neighbour;
}
}
}
return minimum_point;
}
ivec2 voronoiCellId(vec2 uv, vec2 grid_size) {
vec2 index_uv = floor(uv * grid_size);
vec2 fract_uv = fract(uv * grid_size);
float minimum_dist = 1.0;
vec2 minimum_point;
vec2 minimum_neighbour;
for (int y = -1; y <= 1; y++) {
for (int x = -1; x <= 1; x++) {
vec2 neighbour = vec2(float(x), float(y));
vec2 point = rand2d(index_uv + neighbour);
vec2 diff = neighbour + point - fract_uv;
float dist = length(diff);
if (dist < minimum_dist) {
minimum_dist = dist;
minimum_point = point;
minimum_neighbour = index_uv + neighbour;
}
}
}
return ivec2(minimum_neighbour);
}
vec3 voronoi_cell_id_and_border_dist(vec2 uv, vec2 grid_size) {
vec2 index_uv = floor(uv * grid_size);
vec2 fract_uv = fract(uv * grid_size);
float minimum_dist = 1.0;
vec2 minimum_neighbour;
vec2 cell_point = rand2d(index_uv);
float border_dist = 1.0;
for (int y = -1; y <= 1; y++) {
for (int x = -1; x <= 1; x++) {
vec2 neighbour = vec2(float(x), float(y));
vec2 point = rand2d(index_uv + neighbour);
vec2 diff = neighbour + point - fract_uv;
float dist = length(diff);
if (dist < minimum_dist) {
minimum_dist = dist;
minimum_neighbour = index_uv + neighbour;
float cell_point_dist = length(neighbour + point - cell_point);
border_dist = (cell_point_dist * 0.5 - dist);
border_dist = 1.0 - dist;
}
}
}
return vec3(minimum_neighbour, border_dist);
}
vec4 biomeValue (vec2 uv, vec2 grid_size) {
vec2 index_uv = floor(uv * grid_size);
vec2 fract_uv = fract(uv * grid_size);
float minimum_dist = 1.0;
vec2 minimum_point;
vec2 minimum_neighbour;
for (int y = -1; y <= 1; y++) {
for (int x = -1; x <= 1; x++) {
vec2 neighbour = vec2(float(x), float(y));
vec2 point = rand2d(index_uv + neighbour);
vec2 diff = neighbour + point - fract_uv;
float dist = length(diff);
if (dist < minimum_dist) {
minimum_dist = dist;
minimum_point = point;
minimum_neighbour = index_uv + neighbour;
}
}
}
float biomeId = rand1d(minimum_neighbour);
if (biomeId < 0.2) {
return SandColor;
} else if (biomeId < 0.4) {
return GrassColor;
} else if (biomeId < 0.6) {
return RockColor;
} else if (biomeId < 0.8) {
return SnowColor;
} else {
return ForestColor;
}
}
void fragment() {
vec2 uv = UV / 0.01;
vec2 offset = vec2(sin(TIME * 0.6), cos(TIME * 0.6)) * 14.0;
ivec2 cellId = voronoiCellId(uv + offset, vec2(0.125, 0.125));
vec4 water_land_value = rand1d(vec2(cellId)) < 0.66 ? DeepWaterColor : GrassColor;
vec4 biome_id_value = biomeValue(uv + offset, vec2(0.14, 0.14));
// COLOR = vec4(water_land_value.xyz, 0.4) + vec4 (biome_id_value.xyz, 0.3);
if (water_land_value == DeepWaterColor) {
COLOR = water_land_value;
} else {
COLOR = biome_id_value;
}
vec3 cellIdAndBorderDist = voronoi_cell_id_and_border_dist(uv + offset, vec2(0.125, 0.125));
//COLOR = vec4(rand2d(vec2(cellIdAndBorderDist.xy)), 0.0, cellIdAndBorderDist.z);
COLOR = vec4(cellIdAndBorderDist.z, 0., 0., 1.0);
}