Skip to content

Commit

Permalink
restore blend weight function
Browse files Browse the repository at this point in the history
  • Loading branch information
Xtarsia committed Oct 31, 2024
1 parent 31d4330 commit fa489e4
Showing 1 changed file with 15 additions and 11 deletions.
26 changes: 15 additions & 11 deletions src/shaders/main.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,13 @@ vec2 project_uv_from_normal(vec3 normal) {
return vec2(dot(v_vertex, p_tangent), dot(v_vertex, normalize(cross(p_tangent, p_normal)))) * scale;
}

float blend_weights(float weight, float detail) {
weight = smoothstep(0.0, 1.0, weight);
weight = sqrt(weight * 0.5);
float result = max(0.1 * weight, fma(10.0, (weight + detail), 1.0f - (detail + 10.0)));
return weight;
}

void fragment() {
// Recover UVs
vec2 uv = UV + v_uv_offset;
Expand Down Expand Up @@ -402,19 +409,16 @@ void fragment() {
get_material(project_uv_from_normal(index_normal[2]), base_derivatives, control[2], indexUV[2], w_normal, mat[2]);

// rebuild weights for detail and noise blending
float noise3 = (0.333 - texture(noise_texture, uv * noise3_scale).r) * blend_sharpness;
#define PARABOLA(x) (4.0*x*(1.0-x))
weight.x += PARABOLA(weight.x) * noise3;
weight.y += PARABOLA(weight.y) * noise3;
#undef PARABOLA
weight = smoothstep(vec2(0.0), vec2(1.0), weight);
invert = 1.0 - weight;
float noise3 = texture(noise_texture, uv * noise3_scale).r * blend_sharpness;
#define PARABOLA(x) (4.0 * x * (1.0 - x))
weights = smoothstep(0, 1, weights);
weights = vec4(
(mat[0].alb_ht.a + 1e-6) * invert.x * weight.y,
(mat[1].alb_ht.a + 1e-6) * weight.x * weight.y,
(mat[2].alb_ht.a + 1e-6) * weight.x * invert.y,
(mat[3].alb_ht.a + 1e-6) * invert.x * invert.y
blend_weights(weights.x + PARABOLA(weights.x) * noise3, mat[0].alb_ht.a),
blend_weights(weights.y + PARABOLA(weights.y) * noise3, mat[1].alb_ht.a),
blend_weights(weights.z + PARABOLA(weights.z) * noise3, mat[2].alb_ht.a),
blend_weights(weights.w + PARABOLA(weights.w) * noise3, mat[3].alb_ht.a)
);
#undef PARABOLA
// renormalize weights
weights *= 1.0 / (weights.x + weights.y + weights.z + weights.w);

Expand Down

0 comments on commit fa489e4

Please sign in to comment.