140 lines
5.4 KiB
Text
140 lines
5.4 KiB
Text
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);
|
|
}
|