McAwl / Explosional

MIT License
2 stars 0 forks source link

Add more layers to terrain texture shader #189

Open McAwl opened 2 years ago

McAwl commented 9 months ago

This vid Shows how to blend two colours using the visual shader. Says it works for textures as well. So might also work for more shaders.

McAwl commented 9 months ago

This guy Shows how to mix 3 textures in shader code:


shader_type spatial;
uniform sampler2D grass_texture;
uniform sampler2D rock_texture;
uniform sampler2D sand_texture;

uniform float min_rock_slope:hint_range(0f,1f) = 0.5;
uniform float max_grass_slope:hint_range(0f,1f) = 0.9;
uniform float min_rockgrass_height = -8f;
uniform float max_sand_height = -6f;

varying float normal_y;
varying float vertex_y;

void vertex(){
    normal_y = NORMAL.y;
    vertex_y = VERTEX.y;
}

void fragment(){
    //Albedo Values
    vec3 grass_albedo = texture(grass_texture,UV*4f).xyz;
    vec3 rock_albedo = texture(rock_texture,UV*4f).xyz;
    vec3 sand_albedo = texture(sand_texture,UV*4f).xyz;
    //Weights
    float rock_grass_weight = normal_y;
    float sand_rockgrass_weight = vertex_y;
    //Calculating Rock/Grass Weight
    rock_grass_weight = max(min_rock_slope, rock_grass_weight);
    rock_grass_weight = min(max_grass_slope, rock_grass_weight);
    rock_grass_weight -= min_rock_slope;
    rock_grass_weight /= max_grass_slope - min_rock_slope;
    //Calculating Sand/RockGrass Weight
    sand_rockgrass_weight = max(min_rockgrass_height, sand_rockgrass_weight);
    sand_rockgrass_weight = min(max_sand_height, sand_rockgrass_weight);
    sand_rockgrass_weight -= min_rockgrass_height;
    sand_rockgrass_weight /= max_sand_height - min_rockgrass_height;
    //Mixing and Assigning Albedo
    vec3 rockgrass_albedo = mix(rock_albedo, grass_albedo, rock_grass_weight);
    ALBEDO = mix(sand_albedo, rockgrass_albedo, sand_rockgrass_weight);
McAwl commented 9 months ago

There's also this one from here


shader_type spatial;
uniform float max_grass_height = 2.0;
uniform float min_snow_height = 18.0;
uniform float slope_factor = 8.0;
uniform sampler2D grass_tex;
uniform vec2 grass_scale;
uniform sampler2D dirt_tex;
uniform vec2 dirt_scale;
uniform sampler2D snow_tex;
uniform vec2 snow_scale;
varying float height_val;
varying vec3 normal;
void vertex(){
    height_val = VERTEX.y;
    normal = NORMAL;
}
float get_slope_of_terrain(float height_normal){
    float slope = 1.0-height_normal;
    slope *= slope;
    return (slope*slope_factor);
}
float get_snow_and_grass_mix(float curr_height){
    if (curr_height > min_snow_height){
        return 1.0;
    }
    if (curr_height  < max_grass_height){
        return 0.0;
    }
    float mix_height = (curr_height-max_grass_height) / (min_snow_height-max_grass_height);
    return mix_height;
}
void fragment(){
    vec3 dirt = vec3(texture(dirt_tex, UV*dirt_scale).rgb)*0.35;
    vec3 grass = vec3(texture(grass_tex, UV*grass_scale).rgb)*0.5;
    vec3 snow = vec3(texture(snow_tex, UV*snow_scale).rgb);

    float slope = clamp(get_slope_of_terrain(normal.y), 0.0, 1.0);
    float snow_mix = clamp(get_snow_and_grass_mix(height_val), 0.0, 1.0);

    vec3 grass_mixin = mix(grass, dirt, slope);
    vec3 dirt_mixin = mix(dirt, snow, snow_mix/slope);
    vec3 snow_mixin = mix(snow, dirt_mixin, slope);
    vec3 mixin = mix(grass_mixin, snow_mixin, snow_mix); ```