1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-07-02 16:38:41 +00:00

Fix handling of skinned meshes for nodes

Second try after the revert in 8a28339 due to an unexpected regression.

- Rigidly animated models (e.g. the glTF frog node) were not working correctly,
  since cloning the mesh ignored the transformation matrices.
  Note that scaling the mesh needs to occur *after* transforming the vertices.
- Visual scale did not apply to skinned models,
  as resetting the animation overwrote scaled vertex data with static positions & normals.
  For backwards compatibility, we now apply a 10x scale to static, non-glTF models.

We now do scale static meshes, as the bug that caused meshes not to be scaled was limited to skeletally animated meshes,
hence we ought not to reproduce it for skinned meshes that do not take advantage of skeletal animations (e.g. current MTG doors).

However, glTF models (e.g. Wuzzy's eyeballs) up until recently were always affected due to technical reasons
(using skeletal animation for rigid animation).

Thus, to preserve behavior, we:

1. Do not apply 10x scale to glTF models.
2. Apply 10x scale to obj models.
3. Apply 10x scale to static x or b3d models, but not to animated ones.

See also: #16141
This commit is contained in:
Lars Müller 2025-05-20 18:37:33 +02:00 committed by GitHub
parent 30e33d71cc
commit 7ac5502fdf
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
12 changed files with 118 additions and 75 deletions

View file

@ -4,6 +4,7 @@
#include "nodedef.h"
#include "SAnimatedMesh.h"
#include "itemdef.h"
#if CHECK_CLIENT_BUILD()
#include "client/mesh.h"
@ -13,6 +14,7 @@
#include "client/texturesource.h"
#include "client/tile.h"
#include <IMeshManipulator.h>
#include <SMesh.h>
#include <SkinnedMesh.h>
#endif
#include "log.h"
@ -959,23 +961,44 @@ void ContentFeatures::updateTextures(ITextureSource *tsrc, IShaderSource *shdsrc
palette = tsrc->getPalette(palette_name);
if (drawtype == NDT_MESH && !mesh.empty()) {
// Read the mesh and apply scale
mesh_ptr = client->getMesh(mesh);
if (mesh_ptr) {
v3f scale = v3f(BS) * visual_scale;
scaleMesh(mesh_ptr, scale);
// Note: By freshly reading, we get an unencumbered mesh.
if (scene::IMesh *src_mesh = client->getMesh(mesh)) {
bool apply_bs = false;
// For frame-animated meshes, always get the first frame,
// which holds a model for which we can eventually get the static pose.
while (auto *src_meshes = dynamic_cast<scene::SAnimatedMesh *>(src_mesh)) {
src_mesh = src_meshes->getMesh(0.0f);
src_mesh->grab();
src_meshes->drop();
}
if (auto *skinned_mesh = dynamic_cast<scene::SkinnedMesh *>(src_mesh)) {
// Compatibility: Animated meshes, as well as static gltf meshes, are not scaled by BS.
// See https://github.com/luanti-org/luanti/pull/16112#issuecomment-2881860329
bool is_gltf = skinned_mesh->getSourceFormat() ==
scene::SkinnedMesh::SourceFormat::GLTF;
apply_bs = skinned_mesh->isStatic() && !is_gltf;
// Nodes do not support mesh animation, so we clone the static pose.
// This simplifies working with the mesh: We can just scale the vertices
// as transformations have already been applied.
mesh_ptr = cloneStaticMesh(src_mesh);
src_mesh->drop();
} else {
auto *static_mesh = dynamic_cast<scene::SMesh *>(src_mesh);
assert(static_mesh);
mesh_ptr = static_mesh;
// Compatibility: Apply BS scaling to static meshes (.obj). See #15811.
apply_bs = true;
}
scaleMesh(mesh_ptr, v3f((apply_bs ? BS : 1.0f) * visual_scale));
recalculateBoundingBox(mesh_ptr);
if (!checkMeshNormals(mesh_ptr)) {
// TODO this should be done consistently when the mesh is loaded
infostream << "ContentFeatures: recalculating normals for mesh "
<< mesh << std::endl;
meshmanip->recalculateNormals(mesh_ptr, true, false);
} else {
// Animation is not supported, but we need to reset it to
// default state if it is animated.
// Note: recalculateNormals() also does this hence the else-block
if (mesh_ptr->getMeshType() == scene::EAMT_SKINNED)
((scene::SkinnedMesh*) mesh_ptr)->resetAnimation();
}
} else {
mesh_ptr = nullptr;
}
}
}