Files
tgcc-artest/Shaders/Grain_Test.gdshader
Matteo Sonaglioni f7b334b21c first commit
2026-03-10 23:54:57 +01:00

96 lines
3.2 KiB
Plaintext

shader_type spatial;
// Rimosso 'unshaded' per ricevere le ombre.
// Aggiunto 'ambient_light_disabled' per proteggere la tua palette dai riflessi del cielo.
render_mode specular_disabled, cull_disabled, ambient_light_disabled;
// --- GLOBAL UNIFORMS (Vento) ---
global uniform float wind_scale;
global uniform float wind_speed;
global uniform float wind_strength;
global uniform vec3 wind_direction;
global uniform sampler2D wind_noise : filter_linear_mipmap;
// --- PARAMETRI ESTETICI ---
uniform vec4 base_color : source_color = vec4(0.2, 0.6, 0.3, 1.0);
uniform sampler2D alpha_texture : source_color, filter_nearest;
uniform float leaves_scale = 1.0;
uniform float rotation_degrees = 0.0;
// --- CONTROLLO GRADIENTE E RANDOM ---
uniform float height_min = 0.0;
uniform float height_max = 5.0;
uniform float shadow_intensity : hint_range(0.0, 1.0) = 0.5;
uniform float highlight_intensity : hint_range(0.0, 1.0) = 0.3;
uniform float light_steps : hint_range(1.0, 10.0) = 4.0;
uniform float random_mix : hint_range(0.0, 1.0) = 0.3;
// NUOVO: Regola quanto è scura l'ombra proiettata (es. dal tronco) sull'erba
uniform float cast_shadow_strength : hint_range(0.0, 1.0) = 0.6;
varying vec3 v_final_color;
// Funzione hash per generare casualità basata sulla posizione
float hash(vec3 p) {
p = fract(p * 0.1031);
p += dot(p, p.yzx + 33.33);
return fract((p.x + p.y) * p.z);
}
void vertex() {
// 1. POSIZIONE DELL'ISTANZA
vec3 instance_pos = MODEL_MATRIX[3].xyz;
// --- 2. LOGICA VENTO ---
float time = TIME * wind_speed;
vec2 noise_uv = (instance_pos.xz * wind_scale) + (time * wind_direction.xz * 0.5);
float noise_val = textureLod(wind_noise, noise_uv, 2.0).r;
float sway = sin(time + (noise_val * 10.0));
float total_angle = radians(rotation_degrees) + (sway * wind_strength);
// --- 3. CALCOLO COLORE (LOGICA MIXATA) ---
float h_factor = clamp((instance_pos.y - height_min) / (height_max - height_min), 0.0, 1.0);
float leaf_rand = hash(instance_pos);
float combined_factor = mix(h_factor, leaf_rand, random_mix);
combined_factor = ceil(combined_factor * light_steps) / light_steps;
vec3 dark_color = base_color.rgb * (1.0 - shadow_intensity);
vec3 light_color = base_color.rgb + (vec3(1.0) - base_color.rgb) * highlight_intensity;
v_final_color = mix(dark_color, light_color, combined_factor);
// --- 4. LOGICA BILLBOARD ---
vec3 view_pos_origin = (VIEW_MATRIX * vec4(instance_pos, 1.0)).xyz;
vec2 offset = (UV - 0.5) * leaves_scale;
float c = cos(total_angle);
float s = sin(total_angle);
vec2 rotated_offset = vec2(offset.x * c - offset.y * s, offset.x * s + offset.y * c);
vec3 final_pos = view_pos_origin + vec3(rotated_offset.x, rotated_offset.y, 0.0);
MODELVIEW_MATRIX = mat4(1.0);
MODELVIEW_MATRIX[3].xyz = final_pos;
VERTEX = vec3(0.0);
}
void fragment() {
vec4 tex = texture(alpha_texture, UV);
ALBEDO = v_final_color;
ALPHA = tex.r;
ALPHA_SCISSOR_THRESHOLD = 0.5;
}
// NUOVO: Calcolo luce custom che simula l'effetto "unshaded" (e ora fixa i quadrati!)
void light() {
float shadow_factor = mix(1.0 - cast_shadow_strength, 1.0, ATTENUATION);
// Maschera morbida per nascondere i quadrati
float cutoff_mask = smoothstep(0.0, 0.05, ATTENUATION);
DIFFUSE_LIGHT += (shadow_factor * LIGHT_COLOR) * cutoff_mask;
}