251 lines
9.5 KiB
Text
251 lines
9.5 KiB
Text
shader_type canvas_item;
|
|
render_mode blend_premul_alpha;
|
|
|
|
// Port of Aetheria's UI/Inventory Grid shader to Godot 4 gl_compatibility (OpenGL 3.3).
|
|
// Evaluates analytical corner-folded texel boxes for hull/hardpoint silhouettes, L-brackets,
|
|
// hairline rules, and 2D Euclidean SDF thermal conduits with RoundUnion fillets.
|
|
//
|
|
// data_tex (3W x H, filter_nearest):
|
|
// 3x + 0 : cell/item fill & armor tint (RGBA)
|
|
// 3x + 1 : border/bracket/pipe accent color (RGBA)
|
|
// 3x + 2 : R = 8-bit neighbor mismatch mask (N,E,S,W,NE,SE,SW,NW)
|
|
// G = flags (bit0=present, bit1=item, bit2=hover, bit3=damaged/stripped, bit4=compatible, bit5=border_radiator)
|
|
// B = 4-bit N/E/S/W thermal conductivity pipe mask
|
|
// A = hit-pulse flash intensity (0..1)
|
|
// temp_tex (W x H, filter_linear):
|
|
// R = normalized temperature [20C..200C -> 0..1]
|
|
// G = thermal plateau / overheat stress [0..0.5 optimal, 0.5..1.0 overheating]
|
|
// B = active cell mask
|
|
|
|
uniform sampler2D data_tex : filter_nearest, repeat_disable;
|
|
uniform sampler2D temp_tex : filter_linear, repeat_disable;
|
|
|
|
uniform vec2 grid_size = vec2(4.0, 5.0);
|
|
uniform vec2 rect_size = vec2(180.0, 225.0);
|
|
uniform float thermal_mode : hint_range(0.0, 1.0) = 0.0;
|
|
|
|
// Look parameters in Aetheria texel units
|
|
uniform float icon_texel = 1.51515;
|
|
uniform float bg_texel = 1.0;
|
|
uniform float rule_inset = 1.5;
|
|
uniform float rule_width = 1.0;
|
|
uniform float arm_texels = 1.25;
|
|
uniform float fill_inset = 2.6;
|
|
uniform float softness = 1.0;
|
|
uniform float rule_alpha = 0.34;
|
|
uniform float bracket_alpha = 1.0;
|
|
uniform float fill_alpha = 0.14;
|
|
|
|
// Thermal pipe SDF parameters (in cell fractions, edge ramp in pixels)
|
|
uniform float blob_radius = 0.1272;
|
|
uniform float pipe_half_width = 0.1261;
|
|
uniform float fillet = 0.1172;
|
|
uniform float pipe_soft = 1.212;
|
|
|
|
const float FAR = 1000.0;
|
|
|
|
float box_profile(float x, float lo, float hi, float soft) {
|
|
if (hi < lo) {
|
|
return 0.0;
|
|
}
|
|
float outside = max(max(lo - x, x - hi), 0.0);
|
|
return clamp(1.0 - outside / soft, 0.0, 1.0);
|
|
}
|
|
|
|
float eval_layer(vec2 p, float cell_px, float texel, uint mask, bool is_item, bool is_icon, float aa_px) {
|
|
float t = min(texel, cell_px / 8.0);
|
|
bool left = p.x < cell_px * 0.5;
|
|
bool top = p.y >= cell_px * 0.5;
|
|
float max_t = max(cell_px * 0.5 / t, 4.0);
|
|
float x = clamp((left ? p.x : cell_px - p.x) / t, 0.5, max_t);
|
|
float y = clamp((top ? cell_px - p.y : p.y) / t, 0.5, max_t);
|
|
|
|
uint v_shift = top ? 0u : 2u;
|
|
uint h_shift = left ? 3u : 1u;
|
|
uint d_shift = top ? (left ? 7u : 4u) : (left ? 6u : 5u);
|
|
bool v = ((mask >> v_shift) & 1u) != 0u;
|
|
bool h = ((mask >> h_shift) & 1u) != 0u;
|
|
bool d = ((mask >> d_shift) & 1u) != 0u;
|
|
bool concave = !v && !h && d;
|
|
|
|
float soft = clamp(softness * aa_px / max(t, 0.25), 0.15, 2.0);
|
|
float fill_lo = fill_inset + 0.5;
|
|
float fx = h ? box_profile(x, fill_lo, FAR, soft) : 1.0;
|
|
float fy = v ? box_profile(y, fill_lo, FAR, soft) : 1.0;
|
|
float f = fx * fy;
|
|
if (concave) {
|
|
f -= box_profile(x, -FAR, fill_inset - 0.5, soft) * box_profile(y, -FAR, fill_inset - 0.5, soft);
|
|
}
|
|
f = clamp(f, 0.0, 1.0);
|
|
if (!is_icon) {
|
|
return f;
|
|
}
|
|
|
|
float a = is_item ? fill_alpha * f : 0.0;
|
|
float ri = rule_inset;
|
|
float rlo = ri - (rule_width - 1.0) * 0.5;
|
|
float rhi = ri + (rule_width - 1.0) * 0.5;
|
|
float rx = box_profile(x, rlo, rhi, soft);
|
|
float ry = box_profile(y, rlo, rhi, soft);
|
|
float arms = arm_texels >= 1.0 ? 1.0 : 0.0;
|
|
|
|
if (v) {
|
|
a += rule_alpha * ry * (h ? box_profile(x, ri, FAR, soft) : 1.0);
|
|
}
|
|
if (h) {
|
|
a += rule_alpha * rx * (v ? box_profile(y, ri + 1.0, FAR, soft) : 1.0);
|
|
}
|
|
if (v && h) {
|
|
a += (bracket_alpha - rule_alpha) * (ry * box_profile(x, ri, ri + arm_texels, soft) + arms * rx * box_profile(y, ri + 1.0, ri + arm_texels, soft));
|
|
} else if (concave) {
|
|
a += bracket_alpha * (ry * box_profile(x, ri - arm_texels, ri, soft) + arms * rx * box_profile(y, ri - arm_texels, ri - 1.0, soft));
|
|
} else if (!v && !h) {
|
|
// Faint cell subdivision hairline across matched internal cell boundaries
|
|
float sub_x = box_profile(x, 0.0, 0.65, soft);
|
|
float sub_y = box_profile(y, 0.0, 0.65, soft);
|
|
a += 0.14 * max(sub_x, sub_y);
|
|
}
|
|
return clamp(a, 0.0, 1.0);
|
|
}
|
|
|
|
float round_union(float a, float b, float r) {
|
|
vec2 u = max(vec2(r - a, r - b), vec2(0.0));
|
|
return max(r, min(a, b)) - length(u);
|
|
}
|
|
|
|
float eval_pipes(vec2 p, float cell_px, uint cond, float aa_px) {
|
|
vec2 q = p / cell_px - 0.5;
|
|
float d = length(q) - blob_radius;
|
|
vec2 dirs[4];
|
|
dirs[0] = vec2(0.0, 1.0);
|
|
dirs[1] = vec2(1.0, 0.0);
|
|
dirs[2] = vec2(0.0, -1.0);
|
|
dirs[3] = vec2(-1.0, 0.0);
|
|
|
|
float bars[4];
|
|
for (int b = 0; b < 4; b++) {
|
|
float along = dot(q, dirs[b]);
|
|
float perp = abs(dot(q, vec2(dirs[b].y, -dirs[b].x)));
|
|
bool connected = ((cond >> uint(b)) & 1u) != 0u;
|
|
bars[b] = !connected
|
|
? FAR
|
|
: (along >= 0.0 ? perp - pipe_half_width : length(vec2(perp, along)) - pipe_half_width);
|
|
d = min(d, bars[b]);
|
|
}
|
|
for (int c = 0; c < 4; c++) {
|
|
int n = (c + 1) % 4;
|
|
if (bars[c] < FAR && bars[n] < FAR && dot(q, dirs[c] + dirs[n]) > 0.0) {
|
|
d = min(d, round_union(bars[c], bars[n], fillet));
|
|
}
|
|
}
|
|
return clamp(0.5 - d * cell_px / max(pipe_soft * aa_px, 0.25), 0.0, 1.0);
|
|
}
|
|
|
|
vec3 thermal_ramp(float t) {
|
|
vec3 c0 = vec3(0.11, 0.18, 0.24); // ambient cool slate
|
|
vec3 c1 = vec3(0.24, 0.48, 0.34); // warm olive
|
|
vec3 c2 = vec3(0.86, 0.64, 0.18); // optimal plateau amber-gold
|
|
vec3 c3 = vec3(0.92, 0.28, 0.14); // hot crimson-orange
|
|
vec3 c4 = vec3(1.00, 0.85, 0.58); // critical incandescent
|
|
if (t < 0.25) return mix(c0, c1, t / 0.25);
|
|
if (t < 0.55) return mix(c1, c2, (t - 0.25) / 0.30);
|
|
if (t < 0.85) return mix(c2, c3, (t - 0.55) / 0.30);
|
|
return mix(c3, c4, (t - 0.85) / 0.15);
|
|
}
|
|
|
|
void fragment() {
|
|
vec2 safe_grid = max(grid_size, vec2(1.0));
|
|
vec2 cell_wh = rect_size / safe_grid;
|
|
float S = min(cell_wh.x, cell_wh.y);
|
|
vec2 grid_px = safe_grid * S;
|
|
vec2 origin = (rect_size - grid_px) * 0.5;
|
|
vec2 local_px = UV * rect_size - origin;
|
|
|
|
// Hoist derivatives and implicit-LOD texture lookup above non-uniform branches (GLSL 330 helper quads)
|
|
float aa_px = clamp(max(fwidth(UV.x) * rect_size.x, fwidth(UV.y) * rect_size.y), 0.2, 2.0);
|
|
vec4 temp_sample = texture(temp_tex, clamp(local_px / max(grid_px, vec2(1.0)), vec2(0.0), vec2(1.0)));
|
|
|
|
vec2 cellf = floor(local_px / S);
|
|
if (cellf.x < 0.0 || cellf.y < 0.0 || cellf.x >= safe_grid.x || cellf.y >= safe_grid.y) {
|
|
COLOR = vec4(0.0);
|
|
} else {
|
|
vec2 p_godot = local_px - cellf * S;
|
|
// Flip Y inside cell to match Aetheria's y-up Layer / Pipes convention
|
|
vec2 p = vec2(p_godot.x, S - p_godot.y);
|
|
|
|
float tex_w = safe_grid.x * 3.0;
|
|
float ty = (cellf.y + 0.5) / safe_grid.y;
|
|
vec4 cell_fill = textureLod(data_tex, vec2((cellf.x * 3.0 + 0.5) / tex_w, ty), 0.0);
|
|
vec4 border_accent = textureLod(data_tex, vec2((cellf.x * 3.0 + 1.5) / tex_w, ty), 0.0);
|
|
vec4 meta = textureLod(data_tex, vec2((cellf.x * 3.0 + 2.5) / tex_w, ty), 0.0);
|
|
|
|
uint mask = uint(round(meta.r * 255.0));
|
|
uint flags = uint(round(meta.g * 255.0));
|
|
uint cond = uint(round(meta.b * 255.0));
|
|
float hit_pulse = meta.a;
|
|
|
|
if ((flags & 1u) == 0u) {
|
|
COLOR = vec4(0.0);
|
|
} else {
|
|
bool is_item = (flags & 2u) != 0u;
|
|
bool is_hover = (flags & 4u) != 0u;
|
|
bool is_damaged = (flags & 8u) != 0u;
|
|
bool is_compatible = (flags & 16u) != 0u;
|
|
|
|
float norm_temp = temp_sample.r;
|
|
float plateau_stress = temp_sample.g;
|
|
vec3 heat_col = thermal_ramp(norm_temp);
|
|
|
|
float a_bg = eval_layer(p, S, bg_texel, mask, false, false, aa_px) * cell_fill.a;
|
|
float a_rules = eval_layer(p, S, icon_texel, mask, is_item, true, aa_px);
|
|
float a_pipes = eval_pipes(p, S, cond, aa_px);
|
|
|
|
// Intact vs stripped (Armor == 0) interior fill
|
|
vec3 base_rgb = mix(cell_fill.rgb, heat_col, mix(0.24, 0.84, thermal_mode));
|
|
if (is_damaged) {
|
|
// Diagonal fracture/exposed-core hatching at 45 deg
|
|
float hatch_coord = (p_godot.x + p_godot.y) / max(S * 0.22, 4.0);
|
|
float hatch_dist = abs(fract(hatch_coord) - 0.5);
|
|
float hatch = 1.0 - smoothstep(0.10, 0.22, hatch_dist);
|
|
base_rgb = mix(base_rgb * 0.55, vec3(0.82, 0.20, 0.14), hatch * 0.65);
|
|
}
|
|
|
|
// Thermal plateau glow over hot cells
|
|
float overheat_glow = max(plateau_stress - 0.5, 0.0) * 2.0;
|
|
base_rgb += heat_col * (0.12 * norm_temp + 0.22 * overheat_glow);
|
|
base_rgb = clamp(base_rgb, vec3(0.0), vec3(1.0));
|
|
|
|
if (is_hover) {
|
|
base_rgb = mix(base_rgb, vec3(0.95, 0.94, 0.88), 0.16);
|
|
}
|
|
|
|
// Active hit-pulse impact flash
|
|
vec3 flash_col = vec3(1.0, 0.76, 0.38);
|
|
base_rgb = clamp(mix(base_rgb, flash_col, hit_pulse * 0.85), vec3(0.0), vec3(1.0));
|
|
|
|
vec3 accent_rgb = border_accent.rgb;
|
|
if (is_damaged) {
|
|
accent_rgb = mix(accent_rgb, vec3(0.92, 0.26, 0.18), 0.70);
|
|
}
|
|
if (is_compatible) {
|
|
float pulse = 0.72 + 0.28 * sin(TIME * 6.5);
|
|
accent_rgb = mix(accent_rgb, vec3(0.96, 0.82, 0.30), pulse);
|
|
a_rules = clamp(a_rules * 1.35, 0.0, 1.0);
|
|
}
|
|
if (is_hover) {
|
|
accent_rgb = mix(accent_rgb, vec3(1.0), 0.35);
|
|
a_rules = clamp(a_rules * 1.25, 0.0, 1.0);
|
|
}
|
|
accent_rgb = clamp(mix(accent_rgb, flash_col, hit_pulse * 0.90), vec3(0.0), vec3(1.0));
|
|
|
|
float icon_alpha = clamp(mix(a_rules * border_accent.a, max(a_rules * 0.26, a_pipes), thermal_mode), 0.0, 1.0);
|
|
vec3 pipe_rgb = clamp(mix(accent_rgb, heat_col * 1.2 + vec3(0.14), 0.72), vec3(0.0), vec3(1.0));
|
|
vec3 icon_rgb = clamp(mix(accent_rgb, pipe_rgb, thermal_mode * clamp(a_pipes * 1.2, 0.0, 1.0)), vec3(0.0), vec3(1.0));
|
|
|
|
float out_a = clamp(icon_alpha + a_bg * (1.0 - icon_alpha), 0.0, 1.0);
|
|
vec3 out_rgb = min(icon_rgb * icon_alpha + base_rgb * a_bg * (1.0 - icon_alpha), vec3(out_a));
|
|
out_rgb = max(out_rgb, vec3(0.0));
|
|
COLOR = vec4(out_rgb, out_a);
|
|
}
|
|
}
|
|
}
|