Merge nibu/rtd-visuals: Brokkr mech_paint shader, POM decal atlas/shader, and vertex weathering baker
This commit is contained in:
commit
0fa1de2dce
12 changed files with 2419 additions and 0 deletions
140
client/decal_pom.gdshader
Normal file
140
client/decal_pom.gdshader
Normal file
|
|
@ -0,0 +1,140 @@
|
|||
shader_type spatial;
|
||||
render_mode blend_mix, depth_draw_never, cull_back;
|
||||
|
||||
// OpenGL 3.3 (gl_compatibility) Parallax Occlusion Mapping (POM) decal shader for MultiMeshInstance3D.
|
||||
// Renders hundreds of hard-surface vents, hatches, stencils, and live combat hits in 1 draw call.
|
||||
//
|
||||
// INSTANCE_CUSTOM layout:
|
||||
// x = tile_index (0..15 in 4x4 trim-sheet atlas)
|
||||
// y = heat_glow (0..1, incandescent glow on rolled rim & core of fresh combat hits)
|
||||
// z = opacity (0..1)
|
||||
// w = depth_scale (default ~0.06 when 0)
|
||||
//
|
||||
// Atlas textures (assets/decals/):
|
||||
// albedo_tex : RGBA albedo + alpha mask
|
||||
// normal_tex : tangent-space normal map (+X right, +Y down, +Z out)
|
||||
// ormh_tex : R=AO, G=Roughness, B=Metallic, A=Height (1.0=flush surface, 0.0=deep recess)
|
||||
|
||||
uniform sampler2D albedo_tex : source_color, filter_linear_mipmap, repeat_disable;
|
||||
uniform sampler2D normal_tex : hint_normal, filter_linear_mipmap, repeat_disable;
|
||||
uniform sampler2D ormh_tex : filter_linear_mipmap, repeat_disable;
|
||||
|
||||
uniform bool use_instance_custom = true;
|
||||
uniform float fallback_tile_index : hint_range(0.0, 15.0) = 0.0;
|
||||
uniform float fallback_heat_glow : hint_range(0.0, 1.0) = 0.0;
|
||||
uniform float fallback_opacity : hint_range(0.0, 1.0) = 1.0;
|
||||
uniform float default_depth_scale : hint_range(0.0, 0.25) = 0.06;
|
||||
uniform float normal_offset : hint_range(0.0, 0.02) = 0.004;
|
||||
uniform float normal_depth : hint_range(0.0, 3.0) = 1.15;
|
||||
uniform float heat_emission_energy : hint_range(0.0, 10.0) = 2.6;
|
||||
|
||||
varying vec4 v_custom;
|
||||
|
||||
void vertex() {
|
||||
VERTEX += NORMAL * normal_offset;
|
||||
if (use_instance_custom) {
|
||||
v_custom = INSTANCE_CUSTOM;
|
||||
} else {
|
||||
v_custom = vec4(fallback_tile_index, fallback_heat_glow, fallback_opacity, default_depth_scale);
|
||||
}
|
||||
}
|
||||
|
||||
void fragment() {
|
||||
const int POM_STEPS = 10;
|
||||
const float CELL_SIZE = 0.25;
|
||||
|
||||
float tile_idx = floor(clamp(v_custom.x + 0.5, 0.0, 15.0));
|
||||
float heat_glow = clamp(v_custom.y, 0.0, 1.0);
|
||||
float opacity = clamp(v_custom.z, 0.0, 1.0);
|
||||
float depth_scale = v_custom.w > 0.0001 ? v_custom.w : default_depth_scale;
|
||||
|
||||
float col_idx = mod(tile_idx, 4.0);
|
||||
float row_idx = floor(tile_idx * 0.25);
|
||||
vec2 cell_origin = vec2(col_idx, row_idx) * CELL_SIZE;
|
||||
|
||||
// Hoist screen-space derivatives above any non-uniform control flow / discard (GLSL 330 safety)
|
||||
vec2 d_uv_dx = dFdx(UV * CELL_SIZE);
|
||||
vec2 d_uv_dy = dFdy(UV * CELL_SIZE);
|
||||
|
||||
// Soft outer edge fade on local quad UV
|
||||
float edge_fade = smoothstep(0.0, 0.04, UV.x)
|
||||
* smoothstep(1.0, 0.96, UV.x)
|
||||
* smoothstep(0.0, 0.04, UV.y)
|
||||
* smoothstep(1.0, 0.96, UV.y);
|
||||
|
||||
// Tangent-space view ray (Godot fragment VIEW, TANGENT, BINORMAL, NORMAL are all in view space)
|
||||
vec3 t_dir = normalize(TANGENT);
|
||||
vec3 b_dir = normalize(BINORMAL);
|
||||
vec3 n_dir = normalize(NORMAL);
|
||||
vec3 view_ts = normalize(vec3(dot(VIEW, t_dir), dot(VIEW, b_dir), dot(VIEW, n_dir)));
|
||||
|
||||
// Clamp grazing Z so oblique rays stay well-behaved without sliding out of the tile
|
||||
float vz = max(view_ts.z, 0.22);
|
||||
vec2 p_dir = -(view_ts.xy / vz) * depth_scale;
|
||||
|
||||
// 10 linear POM steps from surface (layer_depth=0, height=1) into recess (layer_depth=1, height=0)
|
||||
float step_size = 1.0 / float(POM_STEPS);
|
||||
vec2 delta_uv = p_dir * step_size;
|
||||
|
||||
vec2 cur_uv = UV;
|
||||
float cur_layer_depth = 0.0;
|
||||
float cur_tex_depth = 1.0 - textureLod(ormh_tex, cell_origin + clamp(cur_uv, 0.002, 0.998) * CELL_SIZE, 0.0).a;
|
||||
|
||||
vec2 prev_uv = cur_uv;
|
||||
float prev_layer_depth = cur_layer_depth;
|
||||
float prev_tex_depth = cur_tex_depth;
|
||||
|
||||
for (int i = 0; i < POM_STEPS; i++) {
|
||||
if (cur_layer_depth >= cur_tex_depth) {
|
||||
break;
|
||||
}
|
||||
prev_uv = cur_uv;
|
||||
prev_layer_depth = cur_layer_depth;
|
||||
prev_tex_depth = cur_tex_depth;
|
||||
|
||||
cur_uv += delta_uv;
|
||||
cur_layer_depth += step_size;
|
||||
cur_tex_depth = 1.0 - textureLod(ormh_tex, cell_origin + clamp(cur_uv, 0.002, 0.998) * CELL_SIZE, 0.0).a;
|
||||
}
|
||||
|
||||
// 1 secant interpolation step
|
||||
float after_d = cur_tex_depth - cur_layer_depth;
|
||||
float before_d = prev_tex_depth - prev_layer_depth;
|
||||
float denom = after_d - before_d;
|
||||
float weight = abs(denom) > 1e-5 ? clamp(after_d / denom, 0.0, 1.0) : 0.0;
|
||||
vec2 final_uv = mix(cur_uv, prev_uv, weight);
|
||||
|
||||
// Discard cleanly outside tile bounds so adjacent atlas cells never bleed
|
||||
if (final_uv.x < 0.002 || final_uv.x > 0.998 || final_uv.y < 0.002 || final_uv.y > 0.998) {
|
||||
discard;
|
||||
}
|
||||
|
||||
vec2 atlas_uv = cell_origin + final_uv * CELL_SIZE;
|
||||
vec4 alb = textureGrad(albedo_tex, atlas_uv, d_uv_dx, d_uv_dy);
|
||||
vec3 nrm_sample = textureGrad(normal_tex, atlas_uv, d_uv_dx, d_uv_dy).rgb;
|
||||
vec4 ormh = textureGrad(ormh_tex, atlas_uv, d_uv_dx, d_uv_dy);
|
||||
|
||||
float out_alpha = alb.a * edge_fade * opacity;
|
||||
if (out_alpha < 0.015) {
|
||||
discard;
|
||||
}
|
||||
|
||||
// Emissive rim & molten crater mask for combat hits (Row 3: tiles 12..15)
|
||||
float is_combat = step(11.5, tile_idx);
|
||||
// Rolled metallic lip & crater wall glow hot while keeping pitch-black bore contrast
|
||||
float rim_band = smoothstep(0.94, 0.50, ormh.a) * smoothstep(0.08, 0.55, ormh.b);
|
||||
float bore_inner_ring = smoothstep(0.35, 0.10, ormh.a) * smoothstep(0.01, 0.16, ormh.a);
|
||||
float hot_mask = clamp((rim_band * 1.15 + bore_inner_ring * 0.45) * is_combat, 0.0, 1.0);
|
||||
float hot_intensity = heat_glow * hot_mask;
|
||||
vec3 hot_col = mix(vec3(0.95, 0.18, 0.02), vec3(1.0, 0.58, 0.14), smoothstep(0.25, 0.90, hot_intensity));
|
||||
|
||||
ALBEDO = alb.rgb;
|
||||
ALPHA = out_alpha;
|
||||
NORMAL_MAP = nrm_sample;
|
||||
NORMAL_MAP_DEPTH = normal_depth;
|
||||
AO = ormh.r;
|
||||
AO_LIGHT_AFFECT = 0.85;
|
||||
ROUGHNESS = clamp(ormh.g, 0.12, 0.98);
|
||||
METALLIC = clamp(ormh.b, 0.0, 1.0);
|
||||
EMISSION = hot_col * (hot_intensity * heat_emission_energy);
|
||||
}
|
||||
225
client/mech_paint.gdshader
Normal file
225
client/mech_paint.gdshader
Normal file
|
|
@ -0,0 +1,225 @@
|
|||
shader_type spatial;
|
||||
render_mode cull_back, depth_draw_opaque;
|
||||
|
||||
// Real-time GLSL 330 (gl_compatibility) port of Brokkr's 6-layer MechPaint.
|
||||
// Samples 3D procedural noise in rest-pose object space (v_local_pos, v_local_nrm)
|
||||
// so weathering never swims during skeletal/joint animation or chassis motion.
|
||||
//
|
||||
// Expects baked vertex COLOR_0:
|
||||
// R = geometric cavity AO (0 = deep crevice, 1 = open exposed face)
|
||||
// G = convex dihedral edge curvature (0 = flat, 1 = sharp exposed edge)
|
||||
// B = per-island connected-component hash (0..1)
|
||||
// A = rest-pose height above ground (meters / 6.0, clamped 0..1)
|
||||
//
|
||||
// Live combat channels (via INSTANCE_CUSTOM or uniform fallback for non-MultiMesh):
|
||||
// INSTANCE_CUSTOM.x / scorch (0..1) -> char/soot darkening + matte roughness
|
||||
// INSTANCE_CUSTOM.y / heat_glow (0..1) -> incandescent orange-red emission without cloning materials
|
||||
|
||||
uniform vec4 base_color : source_color = vec4(0.306, 0.341, 0.251, 1.0);
|
||||
uniform float roughness : hint_range(0.0, 1.0) = 0.58;
|
||||
uniform float metallic : hint_range(0.0, 1.0) = 0.28;
|
||||
uniform float tone_variation : hint_range(0.0, 1.0) = 0.30;
|
||||
uniform float mottle : hint_range(0.0, 2.0) = 1.0;
|
||||
uniform float noise_scale : hint_range(0.1, 10.0) = 1.0;
|
||||
|
||||
uniform float grime : hint_range(0.0, 2.0) = 0.90;
|
||||
uniform vec4 grime_color : source_color = vec4(0.145, 0.133, 0.114, 1.0);
|
||||
|
||||
uniform float streaks : hint_range(0.0, 2.0) = 0.95;
|
||||
uniform float dust : hint_range(0.0, 2.0) = 0.55;
|
||||
uniform vec4 dust_color : source_color = vec4(0.451, 0.431, 0.388, 1.0);
|
||||
|
||||
uniform float wear : hint_range(0.0, 2.5) = 0.95;
|
||||
uniform vec4 chip_color : source_color = vec4(0.460, 0.470, 0.465, 1.0);
|
||||
uniform float chip_metallic : hint_range(0.0, 1.0) = 0.85;
|
||||
uniform float chip_roughness : hint_range(0.0, 1.0) = 0.28;
|
||||
uniform vec4 primer_color : source_color = vec4(0.345, 0.325, 0.286, 1.0);
|
||||
|
||||
uniform float mud : hint_range(0.0, 2.0) = 1.0;
|
||||
uniform float mud_top : hint_range(0.1, 5.0) = 1.45;
|
||||
uniform vec4 mud_color : source_color = vec4(0.180, 0.148, 0.118, 1.0);
|
||||
uniform float wet : hint_range(0.0, 2.0) = 0.60;
|
||||
uniform float wet_top : hint_range(0.1, 6.0) = 2.80;
|
||||
|
||||
uniform float scorch : hint_range(0.0, 1.0) = 0.0;
|
||||
uniform float heat_glow : hint_range(0.0, 1.0) = 0.0;
|
||||
uniform float heat_emission_energy : hint_range(0.0, 8.0) = 2.6;
|
||||
|
||||
varying vec3 v_local_pos;
|
||||
varying vec3 v_local_nrm;
|
||||
varying vec4 v_color;
|
||||
varying float v_scorch;
|
||||
varying float v_heat;
|
||||
|
||||
// Safe remapping helper supporting inverted bounds (lo > hi) without GLSL smoothstep UB.
|
||||
float map_range(float v, float lo, float hi, float out_lo, float out_hi) {
|
||||
float denom = hi - lo;
|
||||
float t = abs(denom) > 1e-6 ? clamp((v - lo) / denom, 0.0, 1.0) : 0.0;
|
||||
t = t * t * (3.0 - 2.0 * t);
|
||||
return mix(out_lo, out_hi, t);
|
||||
}
|
||||
|
||||
float hash31(vec3 p) {
|
||||
p = fract(p * vec3(0.1031, 0.1030, 0.0973));
|
||||
p += dot(p, p.yxz + 33.33);
|
||||
return fract((p.x + p.y) * p.z);
|
||||
}
|
||||
|
||||
float noise3d(vec3 p) {
|
||||
vec3 i = floor(p);
|
||||
vec3 f = fract(p);
|
||||
vec3 u = f * f * (3.0 - 2.0 * f);
|
||||
|
||||
float n000 = hash31(i + vec3(0.0, 0.0, 0.0));
|
||||
float n100 = hash31(i + vec3(1.0, 0.0, 0.0));
|
||||
float n010 = hash31(i + vec3(0.0, 1.0, 0.0));
|
||||
float n110 = hash31(i + vec3(1.0, 1.0, 0.0));
|
||||
float n001 = hash31(i + vec3(0.0, 0.0, 1.0));
|
||||
float n101 = hash31(i + vec3(1.0, 0.0, 1.0));
|
||||
float n011 = hash31(i + vec3(0.0, 1.0, 1.0));
|
||||
float n111 = hash31(i + vec3(1.0, 1.0, 1.0));
|
||||
|
||||
float nx00 = mix(n000, n100, u.x);
|
||||
float nx10 = mix(n010, n110, u.x);
|
||||
float nx01 = mix(n001, n101, u.x);
|
||||
float nx11 = mix(n011, n111, u.x);
|
||||
float nxy0 = mix(nx00, nx10, u.y);
|
||||
float nxy1 = mix(nx01, nx11, u.y);
|
||||
return mix(nxy0, nxy1, u.z);
|
||||
}
|
||||
|
||||
float fbm2(vec3 p) {
|
||||
// Rotate second octave to eliminate cubic lattice grid alignment
|
||||
const mat3 rot = mat3(
|
||||
vec3(0.80, -0.48, 0.36),
|
||||
vec3(0.60, 0.64, -0.48),
|
||||
vec3(0.00, 0.60, 0.80)
|
||||
);
|
||||
float a = noise3d(p);
|
||||
float b = noise3d((rot * p) * 2.17 + vec3(17.3, 9.1, 23.7));
|
||||
float c = noise3d((rot * p) * 4.61 + vec3(5.7, 31.4, 11.2));
|
||||
return a * 0.55 + b * 0.30 + c * 0.15;
|
||||
}
|
||||
|
||||
vec3 rgb_to_hsv(vec3 c) {
|
||||
vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0);
|
||||
vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g));
|
||||
vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r));
|
||||
float d = q.x - min(q.w, q.y);
|
||||
float e = 1.0e-6;
|
||||
return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x);
|
||||
}
|
||||
|
||||
vec3 hsv_to_rgb(vec3 c) {
|
||||
vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);
|
||||
vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www);
|
||||
return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y);
|
||||
}
|
||||
|
||||
void vertex() {
|
||||
v_local_pos = VERTEX;
|
||||
v_local_nrm = NORMAL;
|
||||
v_color = COLOR;
|
||||
v_scorch = clamp(max(INSTANCE_CUSTOM.x, scorch), 0.0, 1.0);
|
||||
v_heat = clamp(max(INSTANCE_CUSTOM.y, heat_glow), 0.0, 1.0);
|
||||
}
|
||||
|
||||
void fragment() {
|
||||
vec3 pos = v_local_pos * noise_scale;
|
||||
vec3 nrm = normalize(v_local_nrm);
|
||||
float up = nrm.y;
|
||||
|
||||
float occ = clamp(v_color.r, 0.0, 1.0);
|
||||
float edge = clamp(v_color.g, 0.0, 1.0);
|
||||
float island_rand = clamp(v_color.b, 0.0, 1.0);
|
||||
float height_m = clamp(v_color.a, 0.0, 1.0) * 6.0;
|
||||
|
||||
// Layer 1: Base paint + per-island tone variation + 2-octave 3D object-space mottle
|
||||
float rand2 = fract(island_rand * 7.31 + 0.37);
|
||||
vec3 hsv = rgb_to_hsv(base_color.rgb);
|
||||
hsv.x = fract(hsv.x + (rand2 - 0.5) * tone_variation * 0.14);
|
||||
hsv.y = clamp(hsv.y * (1.0 + (rand2 - 0.5) * tone_variation * 0.75), 0.0, 1.0);
|
||||
hsv.z = clamp(hsv.z * (1.0 + (island_rand * 2.0 - 1.0) * tone_variation * 0.65), 0.01, 2.0);
|
||||
vec3 col = hsv_to_rgb(hsv);
|
||||
|
||||
float blotch = fbm2(pos * 0.75);
|
||||
float speck = fbm2(pos * 26.0);
|
||||
float mid_n = fbm2(pos * 4.5);
|
||||
float shade = map_range(blotch, 0.25, 0.75, -0.14, 0.14)
|
||||
+ map_range(speck, 0.25, 0.75, -0.07, 0.07)
|
||||
+ map_range(mid_n, 0.25, 0.75, -0.09, 0.09);
|
||||
col *= clamp(1.0 + shade * mottle, 0.55, 1.35);
|
||||
|
||||
// Layer 2: Cavity grime darkening + roughness boost from (1 - AO)
|
||||
float cavity = clamp(map_range(occ, 0.90, 0.42, 0.0, 1.0) * map_range(fbm2(pos * 5.0), 0.30, 0.65, 0.45, 1.0) * grime, 0.0, 1.0);
|
||||
float film = clamp(map_range(fbm2(pos * 2.1), 0.54, 0.72, 0.0, 1.0) * 0.30 * grime, 0.0, 1.0);
|
||||
float grime_amt = max(cavity, film);
|
||||
col = mix(col, grime_color.rgb, min(grime_amt, 0.85));
|
||||
|
||||
// Layer 3: Vertical rain/oil streaks on upright surfaces boosted by cavity
|
||||
vec3 stretch = pos * vec3(13.0, 0.5, 13.0);
|
||||
float upright = map_range(abs(up), 0.80, 0.35, 0.0, 1.0);
|
||||
float under = map_range(occ, 0.96, 0.65, 0.40, 1.0);
|
||||
float broken = map_range(noise3d(pos * 1.4), 0.35, 0.55, 0.15, 1.0);
|
||||
float dark_run = clamp(map_range(fbm2(stretch), 0.52, 0.68, 0.0, 1.0) * upright * under * broken * streaks, 0.0, 1.0);
|
||||
float pale_run = clamp(map_range(fbm2(stretch + vec3(31.0, 5.0, 17.0)), 0.62, 0.76, 0.0, 1.0) * upright * 0.28 * streaks, 0.0, 1.0);
|
||||
col = mix(col, dust_color.rgb, pale_run);
|
||||
col = mix(col, grime_color.rgb, dark_run * 0.85);
|
||||
|
||||
// Layer 4: Pale dust on upward-facing normals (v_local_nrm.y)
|
||||
float dust_amt = clamp(map_range(up, 0.45, 0.95, 0.0, 1.0) * map_range(fbm2(pos * 3.0), 0.25, 0.70, 0.25, 1.0) * dust * 0.55, 0.0, 1.0);
|
||||
col = mix(col, dust_color.rgb, dust_amt);
|
||||
|
||||
// Layer 5: Metallic edge chipping + thin primer_color halo from edge_curvature * noise
|
||||
float exposed = map_range(occ, 0.42, 0.88, 0.0, 1.0);
|
||||
float edge_sharp = pow(edge, 1.85);
|
||||
float chip_n = map_range(fbm2(pos * 6.5), 0.30, 0.72, 0.0, 0.44)
|
||||
+ map_range(noise3d(pos * 46.0), 0.30, 0.70, -0.06, 0.06);
|
||||
float wear_level = chip_n + edge_sharp * exposed * wear * 0.56;
|
||||
float scuff = map_range(fbm2(pos * 4.5), 0.68, 0.72, 0.0, 1.0) * clamp(wear / 1.8, 0.0, 1.0) * 0.55;
|
||||
float chip_metal_mask = clamp(max(map_range(wear_level, 0.76, 0.80, 0.0, 1.0), scuff * 0.85), 0.0, 1.0);
|
||||
float primer_mask = clamp(max(map_range(wear_level, 0.71, 0.75, 0.0, 1.0), scuff), 0.0, 1.0);
|
||||
col = mix(col, primer_color.rgb, primer_mask * 0.75);
|
||||
col = mix(col, chip_color.rgb, chip_metal_mask);
|
||||
|
||||
// Layer 6: Lower-hull/foot mud & wet gloss driven by rest_height (COLOR.a)
|
||||
float wet_amt = clamp(map_range(height_m, wet_top, 0.0, 0.0, 1.0) * wet, 0.0, 1.0);
|
||||
col *= (1.0 - 0.50 * wet_amt);
|
||||
float mud_n = fbm2(pos * 3.2);
|
||||
float thick = map_range(height_m, mud_top, 0.0, 0.0, 1.0)
|
||||
* map_range(mud_n, 0.35, 0.62, 0.1, 1.5)
|
||||
* map_range(occ, 0.95, 0.50, 0.7, 1.6) * mud;
|
||||
float splash = map_range(fbm2(pos * 11.0), 0.62, 0.66, 0.0, 1.0)
|
||||
* map_range(height_m, mud_top * 2.2, mud_top * 0.4, 0.0, 1.0) * mud * 0.85;
|
||||
float mud_amt = clamp(max(map_range(thick, 0.20, 0.50, 0.0, 1.0), splash), 0.0, 1.0);
|
||||
col = mix(col, mud_color.rgb * map_range(mud_n, 0.30, 0.70, 0.70, 1.30), mud_amt);
|
||||
|
||||
// PBR metallic & roughness synthesis
|
||||
float out_metallic = mix(metallic, chip_metallic, chip_metal_mask);
|
||||
out_metallic *= (1.0 - mud_amt) * (1.0 - grime_amt * 0.70);
|
||||
|
||||
float out_rough = roughness + grime_amt * 0.22 + (1.0 - occ) * 0.12 * clamp(grime, 0.0, 1.0);
|
||||
out_rough = mix(out_rough, chip_roughness, chip_metal_mask);
|
||||
out_rough -= wet_amt * 0.18;
|
||||
out_rough += mud_amt * 0.18;
|
||||
out_rough = clamp(out_rough, 0.12, 0.98);
|
||||
|
||||
// Live combat channel 1: scorch (char/soot darkening + matte roughness)
|
||||
float soot_pat = mix(0.65, 1.0, fbm2(pos * 4.5));
|
||||
float scorch_mask = clamp(v_scorch * soot_pat, 0.0, 1.0);
|
||||
col = mix(col, vec3(0.022, 0.018, 0.016), scorch_mask * 0.88);
|
||||
out_metallic *= (1.0 - scorch_mask * 0.75);
|
||||
out_rough = mix(out_rough, 0.94, scorch_mask);
|
||||
|
||||
// Live combat channel 2: heat_glow (incandescent orange-red emission without cloning material)
|
||||
float heat_mod = mix(0.25, 1.15, pow(edge, 1.4) * 0.6 + fbm2(pos * 3.8) * 0.4);
|
||||
float h_val = clamp(v_heat * heat_mod, 0.0, 1.0);
|
||||
vec3 heat_col = mix(vec3(0.85, 0.12, 0.01), vec3(1.0, 0.52, 0.10), smoothstep(0.25, 0.95, h_val));
|
||||
|
||||
ALBEDO = col;
|
||||
METALLIC = clamp(out_metallic, 0.0, 1.0);
|
||||
ROUGHNESS = out_rough;
|
||||
AO = mix(1.0, occ, 0.65);
|
||||
AO_LIGHT_AFFECT = 0.65;
|
||||
EMISSION = heat_col * (pow(v_heat, 1.35) * heat_mod * heat_emission_energy);
|
||||
}
|
||||
553
client/tests/VisualProbe.cs
Normal file
553
client/tests/VisualProbe.cs
Normal file
|
|
@ -0,0 +1,553 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Threading.Tasks;
|
||||
using Godot;
|
||||
|
||||
namespace RustToDust.Client.Tests;
|
||||
|
||||
/// <summary>
|
||||
/// Live OpenGL 3.3 (gl_compatibility) visual verification probe for:
|
||||
/// - Baked COLOR_0 vertex weathering (AO, edge curvature, per-island hash, rest height)
|
||||
/// - Brokkr 6-layer MechPaint spatial shader (mech_paint.gdshader) + INSTANCE_CUSTOM scorch/heat
|
||||
/// - 4x4 POM MultiMesh decal atlas + decal_pom.gdshader in 1 draw call
|
||||
/// Captures a 1280x720 preview PNG to tools/decals/previews/visual_substrate_preview.png and %TEMP%.
|
||||
/// </summary>
|
||||
public partial class VisualProbe : Node3D
|
||||
{
|
||||
private readonly List<string> _failures = new();
|
||||
private readonly List<IDisposable> _disposables = new();
|
||||
|
||||
public override void _Ready() => _ = RunAsync();
|
||||
|
||||
public override void _ExitTree()
|
||||
{
|
||||
foreach (var d in _disposables)
|
||||
{
|
||||
d.Dispose();
|
||||
}
|
||||
_disposables.Clear();
|
||||
}
|
||||
|
||||
private void Check(bool cond, string msg)
|
||||
{
|
||||
if (!cond)
|
||||
{
|
||||
_failures.Add(msg);
|
||||
GD.PushError("VISUAL_PROBE FAIL: " + msg);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task RunAsync()
|
||||
{
|
||||
int exitCode = 0;
|
||||
try
|
||||
{
|
||||
string repoRoot = Path.GetFullPath(Path.Combine(ProjectSettings.GlobalizePath("res://"), ".."));
|
||||
string decalsDir = Path.Combine(repoRoot, "assets", "decals");
|
||||
string previewRepoPath = Path.Combine(repoRoot, "tools", "decals", "previews", "visual_substrate_preview.png");
|
||||
string previewTempPath = Path.Combine(Path.GetTempPath(), "visual_substrate_preview.png");
|
||||
|
||||
SetupEnvironment();
|
||||
|
||||
var mechShader = GD.Load<Shader>("res://mech_paint.gdshader");
|
||||
var decalShader = GD.Load<Shader>("res://decal_pom.gdshader");
|
||||
Check(mechShader is not null, "Failed to load res://mech_paint.gdshader");
|
||||
Check(decalShader is not null, "Failed to load res://decal_pom.gdshader");
|
||||
if (mechShader is null || decalShader is null)
|
||||
{
|
||||
Finish(1);
|
||||
return;
|
||||
}
|
||||
|
||||
ImageTexture albedoTex = LoadTextureWithMips(Path.Combine(decalsDir, "decal_atlas_albedo.png"));
|
||||
ImageTexture normalTex = LoadTextureWithMips(Path.Combine(decalsDir, "decal_atlas_normal.png"));
|
||||
ImageTexture ormhTex = LoadTextureWithMips(Path.Combine(decalsDir, "decal_atlas_ormh.png"));
|
||||
|
||||
var cache = new VehicleAssetCache();
|
||||
_disposables.Add(cache);
|
||||
var paladinTpl = cache.GetOrLoadVehicle("paladin");
|
||||
var jackalTpl = cache.GetOrLoadVehicle("jackal");
|
||||
|
||||
VerifyBakedVertexColors("paladin", paladinTpl.BakedStaticMesh);
|
||||
VerifyBakedVertexColors("jackal", jackalTpl.BakedStaticMesh);
|
||||
|
||||
// Ground desert pad
|
||||
var groundMesh = new PlaneMesh { Size = new Vector2(48f, 48f) };
|
||||
var groundMat = new StandardMaterial3D
|
||||
{
|
||||
AlbedoColor = new Color(0.42f, 0.34f, 0.24f),
|
||||
Roughness = 0.94f,
|
||||
Metallic = 0.02f
|
||||
};
|
||||
AddChild(new MeshInstance3D { Name = "DesertPad", Mesh = groundMesh, MaterialOverride = groundMat });
|
||||
|
||||
// Shared MechPaint ShaderMaterial variants (zero per-hit cloning)
|
||||
var paladinArmorMat = MakeMechPaintMat(mechShader, new Color(0.306f, 0.341f, 0.251f), metallic: 0.22f, roughness: 0.58f, wear: 1.05f, grime: 0.95f, mud: 1.15f);
|
||||
var jackalArmorMat = MakeMechPaintMat(mechShader, new Color(0.388f, 0.345f, 0.255f), metallic: 0.20f, roughness: 0.56f, wear: 1.15f, grime: 0.95f, mud: 1.25f, mudTop: 0.95f);
|
||||
var gunmetalMat = MakeMechPaintMat(mechShader, new Color(0.173f, 0.188f, 0.180f), metallic: 0.78f, roughness: 0.42f, wear: 0.80f, grime: 1.10f, mud: 1.05f);
|
||||
var copperMat = MakeMechPaintMat(mechShader, new Color(0.612f, 0.353f, 0.204f), metallic: 0.86f, roughness: 0.34f, wear: 0.65f, grime: 0.90f, mud: 0.80f);
|
||||
var orangeMat = MakeMechPaintMat(mechShader, new Color(0.851f, 0.424f, 0.153f), metallic: 0.18f, roughness: 0.44f, wear: 1.10f, grime: 0.85f, mud: 0.90f);
|
||||
var markMat = MakeMechPaintMat(mechShader, new Color(0.760f, 0.730f, 0.640f), metallic: 0.10f, roughness: 0.62f, wear: 0.95f, grime: 0.80f, mud: 0.80f);
|
||||
|
||||
// Spawn Paladin (left-center, 3/4 view toward camera)
|
||||
var paladinMesh = (ArrayMesh)paladinTpl.BakedStaticMesh.Duplicate();
|
||||
ApplyMechPaintSurfaces(paladinMesh, paladinArmorMat, gunmetalMat, copperMat, orangeMat, markMat);
|
||||
var paladinXf = new Transform3D(new Basis(Vector3.Up, Mathf.DegToRad(24f)), new Vector3(-2.8f, 0f, -2.4f));
|
||||
var paladinMi = new MeshInstance3D
|
||||
{
|
||||
Name = "PaladinWeathered",
|
||||
Mesh = paladinMesh,
|
||||
Transform = paladinXf
|
||||
};
|
||||
AddChild(paladinMi);
|
||||
|
||||
// Spawn Jackal (right-midground, angled 3/4 toward camera)
|
||||
var jackalMesh = (ArrayMesh)jackalTpl.BakedStaticMesh.Duplicate();
|
||||
ApplyMechPaintSurfaces(jackalMesh, jackalArmorMat, gunmetalMat, copperMat, orangeMat, markMat);
|
||||
var jackalXf = new Transform3D(new Basis(Vector3.Up, Mathf.DegToRad(-38f)), new Vector3(3.4f, 0f, 1.2f));
|
||||
var jackalMi = new MeshInstance3D
|
||||
{
|
||||
Name = "JackalWeathered",
|
||||
Mesh = jackalMesh,
|
||||
Transform = jackalXf
|
||||
};
|
||||
AddChild(jackalMi);
|
||||
|
||||
// Foreground angled Armor Showcase Slab (MechPaint via MultiMesh showing clean vs INSTANCE_CUSTOM scorch+heat)
|
||||
var slabMesh = BuildBeveledShowcaseSlab(3.2f, 1.26f, 0.14f);
|
||||
var slabMm = new MultiMesh
|
||||
{
|
||||
TransformFormat = MultiMesh.TransformFormatEnum.Transform3D,
|
||||
UseCustomData = true,
|
||||
Mesh = slabMesh,
|
||||
InstanceCount = 2
|
||||
};
|
||||
var slabXf0 = new Transform3D(new Basis(Vector3.Up, Mathf.DegToRad(14f)) * new Basis(Vector3.Right, Mathf.DegToRad(-22f)), new Vector3(1.15f, 1.85f, 6.2f));
|
||||
var slabXf1 = new Transform3D(new Basis(Vector3.Up, Mathf.DegToRad(-18f)) * new Basis(Vector3.Right, Mathf.DegToRad(-18f)) * Basis.FromScale(new Vector3(0.45f, 0.55f, 1.0f)), new Vector3(5.35f, 0.85f, 3.8f));
|
||||
slabMm.SetInstanceTransform(0, slabXf0);
|
||||
slabMm.SetInstanceCustomData(0, new Color(0.0f, 0.0f, 0f, 0f)); // clean weathered chest-height plate
|
||||
slabMm.SetInstanceTransform(1, slabXf1);
|
||||
slabMm.SetInstanceCustomData(1, new Color(0.90f, 0.48f, 0f, 0f)); // live combat scorch + heat_glow without cloning!
|
||||
|
||||
var slabMmi = new MultiMeshInstance3D
|
||||
{
|
||||
Name = "ShowcaseArmorPlates",
|
||||
Multimesh = slabMm,
|
||||
MaterialOverride = paladinArmorMat
|
||||
};
|
||||
AddChild(slabMmi);
|
||||
|
||||
// Build 1 single MultiMeshInstance3D batch of POM decals across Paladin, Jackal, and the Showcase Slab
|
||||
var decalMat = new ShaderMaterial { Shader = decalShader };
|
||||
decalMat.SetShaderParameter("albedo_tex", albedoTex);
|
||||
decalMat.SetShaderParameter("normal_tex", normalTex);
|
||||
decalMat.SetShaderParameter("ormh_tex", ormhTex);
|
||||
decalMat.SetShaderParameter("use_instance_custom", true);
|
||||
decalMat.SetShaderParameter("default_depth_scale", 0.06f);
|
||||
decalMat.SetShaderParameter("normal_offset", 0.004f);
|
||||
|
||||
var decalPlacements = new List<(Transform3D Xform, Color Custom)>();
|
||||
|
||||
// Showcase slab POM decals (close 3/4 grazing angle so 3D depth & hot rims are unmistakable)
|
||||
AddSlabDecal(decalPlacements, slabXf0, -1.16f, 0.30f, 0.07f, 0.48f, tile: 0, heat: 0.0f, opacity: 1.0f, depth: 0.080f); // louvered vent
|
||||
AddSlabDecal(decalPlacements, slabXf0, -0.58f, 0.30f, 0.07f, 0.48f, tile: 1, heat: 0.0f, opacity: 1.0f, depth: 0.080f); // bolted hatch
|
||||
AddSlabDecal(decalPlacements, slabXf0, 0.00f, 0.30f, 0.07f, 0.48f, tile: 3, heat: 0.0f, opacity: 1.0f, depth: 0.080f); // slotted intake grille
|
||||
AddSlabDecal(decalPlacements, slabXf0, 0.58f, 0.30f, 0.07f, 0.48f, tile: 5, heat: 0.0f, opacity: 1.0f, depth: 0.070f); // welded patch
|
||||
AddSlabDecal(decalPlacements, slabXf0, 1.16f, 0.30f, 0.07f, 0.46f, tile: 8, heat: 0.0f, opacity: 0.96f, depth: 0.03f); // stencil "03"
|
||||
|
||||
AddSlabDecal(decalPlacements, slabXf0, -1.16f, -0.30f, 0.07f, 0.48f, tile: 6, heat: 0.0f, opacity: 1.0f, depth: 0.085f); // lift-lug pocket
|
||||
AddSlabDecal(decalPlacements, slabXf0, -0.58f, -0.30f, 0.07f, 0.48f, tile: 7, heat: 0.0f, opacity: 0.96f, depth: 0.04f); // hazard chevron
|
||||
AddSlabDecal(decalPlacements, slabXf0, 0.00f, -0.30f, 0.07f, 0.50f, tile: 12, heat: 0.95f, opacity: 1.0f, depth: 0.095f); // glowing AP hole
|
||||
AddSlabDecal(decalPlacements, slabXf0, 0.58f, -0.30f, 0.07f, 0.50f, tile: 13, heat: 0.75f, opacity: 1.0f, depth: 0.085f); // shrapnel gouge
|
||||
AddSlabDecal(decalPlacements, slabXf0, 1.16f, -0.30f, 0.07f, 0.50f, tile: 14, heat: 0.92f, opacity: 1.0f, depth: 0.090f); // molten plasma crater
|
||||
|
||||
// Raycast flush decals directly onto Paladin armor surfaces
|
||||
TryAddRayDecal(decalPlacements, paladinMesh, paladinXf, new Vector3(-2.6f, 8.2f, 3.0f), new Vector3(0f, -0.05f, -1f), 0.72f, tile: 0, heat: 0.0f, opacity: 1.0f, depth: 0.07f);
|
||||
TryAddRayDecal(decalPlacements, paladinMesh, paladinXf, new Vector3(-1.5f, 8.8f, 3.0f), new Vector3(-0.2f, -0.05f, -1f), 0.65f, tile: 8, heat: 0.0f, opacity: 0.95f, depth: 0.03f);
|
||||
TryAddRayDecal(decalPlacements, paladinMesh, paladinXf, new Vector3(-2.8f, 7.1f, 3.0f), new Vector3(0.05f, 0f, -1f), 0.72f, tile: 12, heat: 0.95f, opacity: 1.0f, depth: 0.09f);
|
||||
TryAddRayDecal(decalPlacements, paladinMesh, paladinXf, new Vector3(-3.6f, 7.8f, 3.0f), new Vector3(0.25f, 0f, -1f), 0.65f, tile: 10, heat: 0.0f, opacity: 0.95f, depth: 0.03f);
|
||||
|
||||
// Raycast flush decals onto Jackal armor surfaces
|
||||
TryAddRayDecal(decalPlacements, jackalMesh, jackalXf, new Vector3(3.4f, 3.0f, 1.5f), new Vector3(0f, -1f, 0.1f), 0.55f, tile: 9, heat: 0.0f, opacity: 0.95f, depth: 0.03f);
|
||||
TryAddRayDecal(decalPlacements, jackalMesh, jackalXf, new Vector3(3.1f, 2.5f, 2.1f), new Vector3(0.2f, -0.8f, -0.5f), 0.50f, tile: 14, heat: 0.85f, opacity: 1.0f, depth: 0.08f);
|
||||
|
||||
var decalQuad = new QuadMesh { Size = Vector2.One };
|
||||
var decalMm = new MultiMesh
|
||||
{
|
||||
TransformFormat = MultiMesh.TransformFormatEnum.Transform3D,
|
||||
UseCustomData = true,
|
||||
Mesh = decalQuad,
|
||||
InstanceCount = decalPlacements.Count
|
||||
};
|
||||
for (int i = 0; i < decalPlacements.Count; i++)
|
||||
{
|
||||
decalMm.SetInstanceTransform(i, decalPlacements[i].Xform);
|
||||
decalMm.SetInstanceCustomData(i, decalPlacements[i].Custom);
|
||||
}
|
||||
var decalMmi = new MultiMeshInstance3D
|
||||
{
|
||||
Name = "PomDecalsBatch",
|
||||
Multimesh = decalMm,
|
||||
MaterialOverride = decalMat,
|
||||
CastShadow = GeometryInstance3D.ShadowCastingSetting.Off
|
||||
};
|
||||
AddChild(decalMmi);
|
||||
|
||||
// Camera framing full Paladin (head to feet) + Jackal + close grazing showcase slab
|
||||
var cam = new Camera3D
|
||||
{
|
||||
Name = "ProbeCamera",
|
||||
Current = true,
|
||||
Fov = 48f,
|
||||
Near = 0.08f,
|
||||
Far = 200f
|
||||
};
|
||||
AddChild(cam);
|
||||
cam.GlobalPosition = new Vector3(2.8f, 4.3f, 13.4f);
|
||||
cam.LookAt(new Vector3(0.0f, 4.4f, 0.2f), Vector3.Up);
|
||||
|
||||
// Wait several rendered frames so OpenGL 3.3 compiles all shaders and finishes drawing
|
||||
for (int f = 0; f < 6; f++)
|
||||
{
|
||||
await ToSignal(GetTree(), SceneTree.SignalName.ProcessFrame);
|
||||
}
|
||||
|
||||
if (DisplayServer.GetName() != "headless")
|
||||
{
|
||||
Image? cap = GetViewport().GetTexture()?.GetImage();
|
||||
Check(cap is not null && cap.GetWidth() == 1280 && cap.GetHeight() == 720,
|
||||
$"Expected 1280x720 viewport capture, got {cap?.GetWidth()}x{cap?.GetHeight()}");
|
||||
if (cap is not null)
|
||||
{
|
||||
Directory.CreateDirectory(Path.GetDirectoryName(previewRepoPath)!);
|
||||
cap.SavePng(previewRepoPath);
|
||||
cap.SavePng(previewTempPath);
|
||||
GD.Print($"VISUAL_PROBE SAVED: {previewRepoPath}");
|
||||
GD.Print($"VISUAL_PROBE SAVED: {previewTempPath}");
|
||||
}
|
||||
}
|
||||
|
||||
double drawCalls = RenderingServer.GetRenderingInfo(RenderingServer.RenderingInfo.TotalDrawCallsInFrame);
|
||||
GD.Print($"VISUAL_PROBE INFO: renderer={RenderingServer.GetVideoAdapterName()} drawCalls={drawCalls} pomDecals={decalPlacements.Count}");
|
||||
Check(_failures.Count == 0, $"Encountered {_failures.Count} failures");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Check(false, ex.ToString());
|
||||
}
|
||||
|
||||
exitCode = _failures.Count == 0 ? 0 : 1;
|
||||
GD.Print(exitCode == 0 ? "VISUAL_PROBE RESULT: PASS" : $"VISUAL_PROBE RESULT: FAIL ({_failures.Count})");
|
||||
Finish(exitCode);
|
||||
}
|
||||
|
||||
private void Finish(int code)
|
||||
{
|
||||
GetTree().Quit(code);
|
||||
}
|
||||
|
||||
private void SetupEnvironment()
|
||||
{
|
||||
var sun = new DirectionalLight3D
|
||||
{
|
||||
Name = "DesertSun",
|
||||
LightColor = new Color(1.0f, 0.93f, 0.82f),
|
||||
LightEnergy = 1.85f,
|
||||
ShadowEnabled = true,
|
||||
DirectionalShadowMode = DirectionalLight3D.ShadowMode.Parallel2Splits,
|
||||
RotationDegrees = new Vector3(-38f, 36f, 0f)
|
||||
};
|
||||
AddChild(sun);
|
||||
|
||||
// Cool oblique fill light so POM recesses & metal rims catch clear specular highlights
|
||||
var fill = new DirectionalLight3D
|
||||
{
|
||||
Name = "SkyFill",
|
||||
LightColor = new Color(0.68f, 0.80f, 0.98f),
|
||||
LightEnergy = 0.55f,
|
||||
ShadowEnabled = false,
|
||||
RotationDegrees = new Vector3(-25f, -125f, 0f)
|
||||
};
|
||||
AddChild(fill);
|
||||
|
||||
var skyMat = new ProceduralSkyMaterial
|
||||
{
|
||||
SkyTopColor = new Color(0.24f, 0.34f, 0.44f),
|
||||
SkyHorizonColor = new Color(0.76f, 0.56f, 0.37f),
|
||||
GroundBottomColor = new Color(0.28f, 0.20f, 0.14f),
|
||||
GroundHorizonColor = new Color(0.68f, 0.48f, 0.32f)
|
||||
};
|
||||
var env = new Godot.Environment
|
||||
{
|
||||
BackgroundMode = Godot.Environment.BGMode.Sky,
|
||||
Sky = new Sky { SkyMaterial = skyMat },
|
||||
AmbientLightSource = Godot.Environment.AmbientSource.Sky,
|
||||
AmbientLightEnergy = 0.65f
|
||||
};
|
||||
AddChild(new WorldEnvironment { Name = "WorldEnv", Environment = env });
|
||||
}
|
||||
|
||||
private void VerifyBakedVertexColors(string name, ArrayMesh mesh)
|
||||
{
|
||||
int surfCount = mesh.GetSurfaceCount();
|
||||
bool foundColor = false;
|
||||
float minR = 1f, maxR = 0f, minG = 1f, maxG = 0f, minB = 1f, maxB = 0f, minA = 1f, maxA = 0f;
|
||||
int totalVerts = 0;
|
||||
for (int s = 0; s < surfCount; s++)
|
||||
{
|
||||
var arrays = mesh.SurfaceGetArrays(s);
|
||||
if (arrays.Count > (int)Mesh.ArrayType.Color &&
|
||||
arrays[(int)Mesh.ArrayType.Color].VariantType == Variant.Type.PackedColorArray)
|
||||
{
|
||||
Color[] cols = arrays[(int)Mesh.ArrayType.Color].AsColorArray();
|
||||
foundColor = true;
|
||||
totalVerts += cols.Length;
|
||||
foreach (Color c in cols)
|
||||
{
|
||||
if (c.R < minR) minR = c.R; if (c.R > maxR) maxR = c.R;
|
||||
if (c.G < minG) minG = c.G; if (c.G > maxG) maxG = c.G;
|
||||
if (c.B < minB) minB = c.B; if (c.B > maxB) maxB = c.B;
|
||||
if (c.A < minA) minA = c.A; if (c.A > maxA) maxA = c.A;
|
||||
}
|
||||
}
|
||||
}
|
||||
GD.Print($"COLOR_0 [{name}]: verts={totalVerts} AO=[{minR:F2},{maxR:F2}] Edge=[{minG:F2},{maxG:F2}] Island=[{minB:F2},{maxB:F2}] Height=[{minA:F2},{maxA:F2}]");
|
||||
Check(foundColor && totalVerts > 0, $"{name} missing baked COLOR_0 array");
|
||||
Check(minR < 0.35f && maxR > 0.85f, $"{name} AO range too narrow [{minR:F2},{maxR:F2}]");
|
||||
Check(maxG > 0.65f, $"{name} Edge curvature max too low ({maxG:F2})");
|
||||
Check(maxA - minA > 0.15f, $"{name} Rest height span too small [{minA:F2},{maxA:F2}]");
|
||||
}
|
||||
|
||||
private static ImageTexture LoadTextureWithMips(string path)
|
||||
{
|
||||
var img = Image.LoadFromFile(path);
|
||||
if (img is null)
|
||||
{
|
||||
throw new FileNotFoundException($"Could not load texture '{path}'", path);
|
||||
}
|
||||
img.GenerateMipmaps();
|
||||
return ImageTexture.CreateFromImage(img);
|
||||
}
|
||||
|
||||
private static ShaderMaterial MakeMechPaintMat(
|
||||
Shader shader,
|
||||
Color baseColor,
|
||||
float metallic,
|
||||
float roughness,
|
||||
float wear,
|
||||
float grime,
|
||||
float mud,
|
||||
float mudTop = 1.45f)
|
||||
{
|
||||
var mat = new ShaderMaterial { Shader = shader };
|
||||
mat.SetShaderParameter("base_color", baseColor);
|
||||
mat.SetShaderParameter("metallic", metallic);
|
||||
mat.SetShaderParameter("roughness", roughness);
|
||||
mat.SetShaderParameter("wear", wear);
|
||||
mat.SetShaderParameter("grime", grime);
|
||||
mat.SetShaderParameter("mud", mud);
|
||||
mat.SetShaderParameter("mud_top", mudTop);
|
||||
return mat;
|
||||
}
|
||||
|
||||
private static void ApplyMechPaintSurfaces(
|
||||
ArrayMesh mesh,
|
||||
ShaderMaterial armorMat,
|
||||
ShaderMaterial gunmetalMat,
|
||||
ShaderMaterial copperMat,
|
||||
ShaderMaterial orangeMat,
|
||||
ShaderMaterial markMat)
|
||||
{
|
||||
int n = mesh.GetSurfaceCount();
|
||||
for (int s = 0; s < n; s++)
|
||||
{
|
||||
Material? orig = mesh.SurfaceGetMaterial(s);
|
||||
string mName = (orig?.ResourceName ?? string.Empty).ToLowerInvariant();
|
||||
if (mName.Contains("glass") || mName.Contains("lamp") || mName.Contains("glow") || mName.Contains("cyan"))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
if (mName.Contains("copper"))
|
||||
{
|
||||
mesh.SurfaceSetMaterial(s, copperMat);
|
||||
}
|
||||
else if (mName.Contains("signal") || mName.Contains("orange"))
|
||||
{
|
||||
mesh.SurfaceSetMaterial(s, orangeMat);
|
||||
}
|
||||
else if (mName.Contains("mark") || mName.Contains("stencil"))
|
||||
{
|
||||
mesh.SurfaceSetMaterial(s, markMat);
|
||||
}
|
||||
else if (mName.Contains("gunmetal") || mName.Contains("metal") || mName.Contains("steel") || mName.Contains("socket") || mName.Contains("pit") || mName.Contains("rubber"))
|
||||
{
|
||||
mesh.SurfaceSetMaterial(s, gunmetalMat);
|
||||
}
|
||||
else
|
||||
{
|
||||
mesh.SurfaceSetMaterial(s, armorMat);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static ArrayMesh BuildBeveledShowcaseSlab(float w, float h, float d)
|
||||
{
|
||||
// Build a subdivided box with baked COLOR_0 (flat faces G=0, sharp edges G=1, AO=0.9, island=0.42)
|
||||
float hw = w * 0.5f, hh = h * 0.5f, hd = d * 0.5f;
|
||||
var st = new SurfaceTool();
|
||||
st.Begin(Mesh.PrimitiveType.Triangles);
|
||||
|
||||
void AddQuad(Vector3 a, Vector3 b, Vector3 c, Vector3 dPt, Vector3 nrm, float edgeA, float edgeB, float edgeC, float edgeD)
|
||||
{
|
||||
void V(Vector3 p, Vector2 uv, float edge)
|
||||
{
|
||||
float restH = Math.Clamp((p.Y + 4.2f) / 6.0f, 0f, 1f);
|
||||
st.SetColor(new Color(0.92f, edge, 0.42f, restH));
|
||||
st.SetNormal(nrm);
|
||||
st.SetUV(uv);
|
||||
st.AddVertex(p);
|
||||
}
|
||||
// Clockwise front-face winding for Godot Spatial cull_back
|
||||
V(a, new Vector2(0, 1), edgeA);
|
||||
V(c, new Vector2(1, 0), edgeC);
|
||||
V(b, new Vector2(1, 1), edgeB);
|
||||
V(a, new Vector2(0, 1), edgeA);
|
||||
V(dPt, new Vector2(0, 0), edgeD);
|
||||
V(c, new Vector2(1, 0), edgeC);
|
||||
}
|
||||
|
||||
// Front (+Z) subdivided 3x1 so center interior vertices have edge=0 and outer corners have edge=0.95
|
||||
float x0 = -hw, x1 = -hw * 0.72f, x2 = hw * 0.72f, x3 = hw;
|
||||
float y0 = -hh, y1 = -hh * 0.65f, y2 = hh * 0.65f, y3 = hh;
|
||||
float[] xs = { x0, x1, x2, x3 };
|
||||
float[] ys = { y0, y1, y2, y3 };
|
||||
float[,] edges = {
|
||||
{ 0.95f, 0.85f, 0.85f, 0.95f },
|
||||
{ 0.85f, 0.00f, 0.00f, 0.85f },
|
||||
{ 0.85f, 0.00f, 0.00f, 0.85f },
|
||||
{ 0.95f, 0.85f, 0.85f, 0.95f },
|
||||
};
|
||||
for (int iy = 0; iy < 3; iy++)
|
||||
{
|
||||
for (int ix = 0; ix < 3; ix++)
|
||||
{
|
||||
AddQuad(
|
||||
new Vector3(xs[ix], ys[iy], hd),
|
||||
new Vector3(xs[ix + 1], ys[iy], hd),
|
||||
new Vector3(xs[ix + 1], ys[iy + 1], hd),
|
||||
new Vector3(xs[ix], ys[iy + 1], hd),
|
||||
Vector3.Back,
|
||||
edges[iy, ix], edges[iy, ix + 1], edges[iy + 1, ix + 1], edges[iy + 1, ix]);
|
||||
}
|
||||
}
|
||||
// Back & sides
|
||||
AddQuad(new Vector3(hw, -hh, -hd), new Vector3(-hw, -hh, -hd), new Vector3(-hw, hh, -hd), new Vector3(hw, hh, -hd), Vector3.Forward, 0.9f, 0.9f, 0.9f, 0.9f);
|
||||
AddQuad(new Vector3(-hw, hh, hd), new Vector3(hw, hh, hd), new Vector3(hw, hh, -hd), new Vector3(-hw, hh, -hd), Vector3.Up, 0.95f, 0.95f, 0.95f, 0.95f);
|
||||
AddQuad(new Vector3(-hw, -hh, -hd), new Vector3(hw, -hh, -hd), new Vector3(hw, -hh, hd), new Vector3(-hw, -hh, hd), Vector3.Down, 0.95f, 0.95f, 0.95f, 0.95f);
|
||||
AddQuad(new Vector3(-hw, -hh, -hd), new Vector3(-hw, -hh, hd), new Vector3(-hw, hh, hd), new Vector3(-hw, hh, -hd), Vector3.Left, 0.95f, 0.95f, 0.95f, 0.95f);
|
||||
AddQuad(new Vector3(hw, -hh, hd), new Vector3(hw, -hh, -hd), new Vector3(hw, hh, -hd), new Vector3(hw, hh, hd), Vector3.Right, 0.95f, 0.95f, 0.95f, 0.95f);
|
||||
st.GenerateTangents();
|
||||
return st.Commit();
|
||||
}
|
||||
|
||||
private static void AddSlabDecal(
|
||||
List<(Transform3D, Color)> list,
|
||||
Transform3D slabXf,
|
||||
float lx,
|
||||
float ly,
|
||||
float lz,
|
||||
float size,
|
||||
int tile,
|
||||
float heat,
|
||||
float opacity,
|
||||
float depth)
|
||||
{
|
||||
var localXf = new Transform3D(Basis.FromScale(new Vector3(size, size, 1f)), new Vector3(lx, ly, lz));
|
||||
list.Add((slabXf * localXf, new Color(tile, heat, opacity, depth)));
|
||||
}
|
||||
|
||||
private static void TryAddRayDecal(
|
||||
List<(Transform3D, Color)> list,
|
||||
ArrayMesh mesh,
|
||||
Transform3D meshXf,
|
||||
Vector3 worldOrigin,
|
||||
Vector3 worldDir,
|
||||
float size,
|
||||
int tile,
|
||||
float heat,
|
||||
float opacity,
|
||||
float depth)
|
||||
{
|
||||
Transform3D inv = meshXf.AffineInverse();
|
||||
Vector3 localOrigin = inv * worldOrigin;
|
||||
Vector3 localDir = (inv.Basis * worldDir).Normalized();
|
||||
|
||||
float bestT = float.PositiveInfinity;
|
||||
Vector3 bestN = Vector3.Back;
|
||||
|
||||
for (int s = 0; s < mesh.GetSurfaceCount(); s++)
|
||||
{
|
||||
var arr = mesh.SurfaceGetArrays(s);
|
||||
if (arr.Count <= (int)Mesh.ArrayType.Vertex) continue;
|
||||
Vector3[] verts = arr[(int)Mesh.ArrayType.Vertex].AsVector3Array();
|
||||
int[]? indices = arr.Count > (int)Mesh.ArrayType.Index && arr[(int)Mesh.ArrayType.Index].VariantType == Variant.Type.PackedInt32Array
|
||||
? arr[(int)Mesh.ArrayType.Index].AsInt32Array()
|
||||
: null;
|
||||
int triCount = (indices is not null ? indices.Length : verts.Length) / 3;
|
||||
for (int t = 0; t < triCount; t++)
|
||||
{
|
||||
int i0 = indices is not null ? indices[t * 3] : t * 3;
|
||||
int i1 = indices is not null ? indices[t * 3 + 1] : t * 3 + 1;
|
||||
int i2 = indices is not null ? indices[t * 3 + 2] : t * 3 + 2;
|
||||
Vector3 v0 = verts[i0], v1 = verts[i1], v2 = verts[i2];
|
||||
if (RayTriangle(localOrigin, localDir, v0, v1, v2, out float hitT))
|
||||
{
|
||||
if (hitT < bestT)
|
||||
{
|
||||
Vector3 fn = (v1 - v0).Cross(v2 - v0);
|
||||
if (fn.LengthSquared() > 1e-10f)
|
||||
{
|
||||
fn = fn.Normalized();
|
||||
if (fn.Dot(-localDir) < 0f) fn = -fn;
|
||||
bestT = hitT;
|
||||
bestN = fn;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!float.IsFinite(bestT))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Vector3 worldHit = meshXf * (localOrigin + localDir * bestT);
|
||||
Vector3 worldN = (meshXf.Basis.Inverse().Transposed() * bestN).Normalized();
|
||||
Vector3 upRef = MathF.Abs(worldN.Y) > 0.92f ? Vector3.Forward : Vector3.Up;
|
||||
Vector3 right = upRef.Cross(worldN).Normalized();
|
||||
Vector3 up = worldN.Cross(right).Normalized();
|
||||
var basis = new Basis(right * size, up * size, worldN);
|
||||
list.Add((new Transform3D(basis, worldHit), new Color(tile, heat, opacity, depth)));
|
||||
}
|
||||
|
||||
private static bool RayTriangle(Vector3 orig, Vector3 dir, Vector3 v0, Vector3 v1, Vector3 v2, out float t)
|
||||
{
|
||||
t = 0f;
|
||||
Vector3 e1 = v1 - v0;
|
||||
Vector3 e2 = v2 - v0;
|
||||
Vector3 pvec = dir.Cross(e2);
|
||||
float det = e1.Dot(pvec);
|
||||
if (MathF.Abs(det) < 1e-7f) return false;
|
||||
float invDet = 1f / det;
|
||||
Vector3 tvec = orig - v0;
|
||||
float u = tvec.Dot(pvec) * invDet;
|
||||
if (u < 0f || u > 1f) return false;
|
||||
Vector3 qvec = tvec.Cross(e1);
|
||||
float v = dir.Dot(qvec) * invDet;
|
||||
if (v < 0f || u + v > 1f) return false;
|
||||
t = e2.Dot(qvec) * invDet;
|
||||
return t > 0.01f;
|
||||
}
|
||||
}
|
||||
6
client/tests/VisualProbe.tscn
Normal file
6
client/tests/VisualProbe.tscn
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
[gd_scene load_steps=2 format=3]
|
||||
|
||||
[ext_resource type="Script" path="res://tests/VisualProbe.cs" id="1"]
|
||||
|
||||
[node name="VisualProbe" type="Node3D"]
|
||||
script = ExtResource("1")
|
||||
Loading…
Reference in a new issue