Enhance checks and user experience around tangents.
Ensure `ensure_tangents` option actually creates tangent array. Even if it is just a dummy array. Allow mesh to generate its own tangents when using compression. This allows users to compress meshes without tangents. Warn users if they are trying to read from tangents without providing tangents.
This commit is contained in:
@ -2797,9 +2797,26 @@ Error GLTFDocument::_parse_meshes(Ref<GLTFState> p_state) {
|
||||
array[Mesh::ARRAY_INDEX] = indices;
|
||||
}
|
||||
|
||||
bool generate_tangents = p_state->force_generate_tangents && (primitive == Mesh::PRIMITIVE_TRIANGLES && !a.has("TANGENT") && a.has("TEXCOORD_0") && a.has("NORMAL"));
|
||||
bool generate_tangents = p_state->force_generate_tangents && (primitive == Mesh::PRIMITIVE_TRIANGLES && !a.has("TANGENT") && a.has("NORMAL"));
|
||||
|
||||
if (p_state->force_disable_compression || !a.has("POSITION") || !a.has("NORMAL") || !(a.has("TANGENT") || generate_tangents) || p.has("targets") || (a.has("JOINTS_0") || a.has("JOINTS_1"))) {
|
||||
if (generate_tangents && !a.has("TEXCOORD_0")) {
|
||||
// If we don't have UVs we provide a dummy tangent array.
|
||||
Vector<float> tangents;
|
||||
tangents.resize(vertex_num * 4);
|
||||
float *tangentsw = tangents.ptrw();
|
||||
|
||||
Vector<Vector3> normals = array[Mesh::ARRAY_NORMAL];
|
||||
for (int k = 0; k < vertex_num; k++) {
|
||||
Vector3 tan = Vector3(0.0, 1.0, 0.0).cross(normals[k]);
|
||||
tangentsw[k * 4 + 0] = tan.x;
|
||||
tangentsw[k * 4 + 1] = tan.y;
|
||||
tangentsw[k * 4 + 2] = tan.z;
|
||||
tangentsw[k * 4 + 3] = 1.0;
|
||||
}
|
||||
array[Mesh::ARRAY_TANGENT] = tangents;
|
||||
}
|
||||
|
||||
if (p_state->force_disable_compression || !a.has("POSITION") || !a.has("NORMAL") || p.has("targets") || (a.has("JOINTS_0") || a.has("JOINTS_1"))) {
|
||||
flags &= ~RS::ARRAY_FLAG_COMPRESS_ATTRIBUTES;
|
||||
}
|
||||
|
||||
@ -2810,7 +2827,7 @@ Error GLTFDocument::_parse_meshes(Ref<GLTFState> p_state) {
|
||||
mesh_surface_tool->set_skin_weight_count(SurfaceTool::SKIN_8_WEIGHTS);
|
||||
}
|
||||
mesh_surface_tool->index();
|
||||
if (generate_tangents) {
|
||||
if (generate_tangents && a.has("TEXCOORD_0")) {
|
||||
//must generate mikktspace tangents.. ergh..
|
||||
mesh_surface_tool->generate_tangents();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user