Fix: EEVEE: Wrong thickness for rotated objects

The previous code was using matrix multiplication to
get the local thickness to world thickness.

The correct way is to multiply the local thickness
by the scale of the object (length of each columns
of the object_to_world matrix).
This commit is contained in:
Clément Foucault 2024-06-17 19:12:24 +02:00
parent 8014b9cbb4
commit 6d375cf7f0
2 changed files with 13 additions and 4 deletions

View file

@ -721,8 +721,14 @@ void ShaderModule::material_create_info_ammend(GPUMaterial *gpumat, GPUCodegenOu
if (info.additional_infos_.first_index_of_try("draw_object_infos_new") == -1) {
info.additional_info("draw_object_infos_new");
}
/* TODO(fclem): Should use `to_scale` but the gpu_shader_math_matrix_lib.glsl isn't
* included everywhere yet. */
frag_gen << "vec3 ob_scale;\n";
frag_gen << "ob_scale.x = length(ModelMatrix[0].xyz);\n";
frag_gen << "ob_scale.y = length(ModelMatrix[1].xyz);\n";
frag_gen << "ob_scale.z = length(ModelMatrix[2].xyz);\n";
frag_gen << "vec3 ls_dimensions = safe_rcp(abs(OrcoTexCoFactors[1].xyz));\n";
frag_gen << "vec3 ws_dimensions = mat3x3(ModelMatrix) * ls_dimensions;\n";
frag_gen << "vec3 ws_dimensions = ob_scale * ls_dimensions;\n";
/* Choose the minimum axis so that cuboids are better represented. */
frag_gen << "return reduce_min(ws_dimensions);\n";
}

View file

@ -21,9 +21,12 @@ void node_output_material_displacement(vec3 displacement, out vec3 out_displacem
void node_output_material_thickness(float thickness, out float out_thickness)
{
vec3 thickness_vec;
direction_transform_object_to_world(vec3(max(thickness, 0.0)), thickness_vec);
thickness_vec = abs(thickness_vec);
vec3 ob_scale;
ob_scale.x = length(ModelMatrix[0].xyz);
ob_scale.y = length(ModelMatrix[1].xyz);
ob_scale.z = length(ModelMatrix[2].xyz);
vec3 thickness_vec = abs(max(thickness, 0.0) * ob_scale);
/* Contrary to displacement we need to output a scalar quantity.
* We arbitrarily choose to output the axis with the minimum extent since it is the axis along
* which the object is usually viewed at. */