mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-12-01 19:57:06 +01:00
115c13b274
- A node of a model made anew (legacy_flags 0) with a transform gets 0x8000, as a converted one has it: the walk and posed_transform() left such nodes out, so a skin was drawn off its place in the bind pose (the tube: the upper half 2 m off) and jumped when an animation set 0x4000 on the joint. - Normals are skinned by the cofactors of the blended rotation (the direction of its inverse transpose), with the sign of its determinant, not by the rotation: a joint scaled unevenly tilted the normals of its part. - rest_pose() takes the translation within the tolerance times the distance from the origin of the mesh and the joints (palette_scale()): the float chain rounds in proportion to it, and a model a few kilometres out counted as posed in the bind pose, with a geometry copy and a skinning pass of its own. Tests: the rest pose far from the origin, the normals of a stretched and of a mirroring joint.
151 lines
4.6 KiB
C++
151 lines
4.6 KiB
C++
/*
|
|
This Source Code Form is subject to the
|
|
terms of the Mozilla Public License, v.
|
|
2.0. If a copy of the MPL was not
|
|
distributed with this file, You can
|
|
obtain one at
|
|
http://mozilla.org/MPL/2.0/.
|
|
*/
|
|
|
|
#include "stdafx.h"
|
|
|
|
#include "model/skinning.h"
|
|
|
|
#include <algorithm>
|
|
#include <cmath>
|
|
#include <limits>
|
|
|
|
namespace skinning
|
|
{
|
|
|
|
template <typename Weight> vertex_weights unpack(std::array<std::uint16_t, 4> const &Joints, std::array<Weight, 4> const &Weights)
|
|
{
|
|
vertex_weights result;
|
|
result.joints = Joints;
|
|
auto total{0.f};
|
|
for (std::size_t idx = 0; idx < 4; ++idx)
|
|
{
|
|
result.weights[idx] = static_cast<float>(Weights[idx]) / static_cast<float>(std::numeric_limits<Weight>::max());
|
|
total += result.weights[idx];
|
|
}
|
|
if (total <= 0.f)
|
|
{
|
|
result.joints = {};
|
|
result.weights = {1.f, 0.f, 0.f, 0.f};
|
|
return result;
|
|
}
|
|
// the weights of the file add up to the largest value of the type; the rounding of the division is made up for here
|
|
for (auto &weight : result.weights)
|
|
{
|
|
weight /= total;
|
|
}
|
|
return result;
|
|
}
|
|
|
|
template vertex_weights unpack<std::uint8_t>(std::array<std::uint16_t, 4> const &, std::array<std::uint8_t, 4> const &);
|
|
template vertex_weights unpack<std::uint16_t>(std::array<std::uint16_t, 4> const &, std::array<std::uint16_t, 4> const &);
|
|
|
|
std::vector<glm::mat4> palette(glm::mat4 const &Mesh, std::span<glm::mat4 const> const Joints, std::span<glm::mat4 const> const InverseBind)
|
|
{
|
|
auto const tomesh{glm::inverse(Mesh)};
|
|
std::vector<glm::mat4> result;
|
|
result.reserve(Joints.size());
|
|
for (std::size_t idx = 0; idx < Joints.size(); ++idx)
|
|
{
|
|
auto const inversebind{idx < InverseBind.size() ? InverseBind[idx] : glm::mat4{1.f}};
|
|
result.emplace_back(tomesh * Joints[idx] * inversebind);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
bool rest_pose(std::span<glm::mat4 const> const Palette, float const Tolerance, float const Scale)
|
|
{
|
|
glm::mat4 const identity{1.f};
|
|
// the translation is rounded in proportion to the distances the palette was made of
|
|
auto const translation{Tolerance * std::max(1.f, Scale)};
|
|
return std::ranges::all_of(Palette, [&identity, Tolerance, translation](glm::mat4 const &Matrix) {
|
|
for (int column = 0; column < 4; ++column)
|
|
{
|
|
auto const limit{column == 3 ? translation : Tolerance};
|
|
for (int row = 0; row < 4; ++row)
|
|
{
|
|
if (std::abs(Matrix[column][row] - identity[column][row]) > limit)
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
return true;
|
|
});
|
|
}
|
|
|
|
float palette_scale(glm::mat4 const &Mesh, std::span<glm::mat4 const> const Joints)
|
|
{
|
|
auto const reach{[](glm::mat4 const &Matrix) { return std::max({std::abs(Matrix[3][0]), std::abs(Matrix[3][1]), std::abs(Matrix[3][2])}); }};
|
|
auto scale{reach(Mesh)};
|
|
for (auto const &joint : Joints)
|
|
{
|
|
scale = std::max(scale, reach(joint));
|
|
}
|
|
return scale;
|
|
}
|
|
|
|
namespace
|
|
{
|
|
|
|
glm::mat4 blended(vertex_weights const &Weights, std::span<glm::mat4 const> const Palette)
|
|
{
|
|
glm::mat4 result{0.f};
|
|
for (std::size_t idx = 0; idx < 4; ++idx)
|
|
{
|
|
auto const weight{Weights.weights[idx]};
|
|
if (weight <= 0.f)
|
|
{
|
|
continue;
|
|
}
|
|
auto const joint{static_cast<std::size_t>(Weights.joints[idx])};
|
|
result += weight * (joint < Palette.size() ? Palette[joint] : glm::mat4{1.f});
|
|
}
|
|
return result;
|
|
}
|
|
|
|
glm::vec3 direction(glm::mat3 const &Rotation, glm::vec3 const &Vector)
|
|
{
|
|
auto const moved{Rotation * Vector};
|
|
auto const length{glm::length(moved)};
|
|
return length > 0.f ? moved / length : Vector;
|
|
}
|
|
|
|
// what moves the normals: the inverse transpose of the rotation, here as its cofactors (the same direction, without the
|
|
// division), with the sign of the determinant so a mirroring matrix keeps the normals facing out. the rotation itself would
|
|
// tilt the normals of a part squashed by a scale
|
|
glm::mat3 normal_matrix(glm::mat3 const &Rotation)
|
|
{
|
|
glm::mat3 const cofactors{glm::cross(Rotation[1], Rotation[2]), glm::cross(Rotation[2], Rotation[0]), glm::cross(Rotation[0], Rotation[1])};
|
|
return glm::dot(Rotation[0], cofactors[0]) < 0.f ? -cofactors : cofactors;
|
|
}
|
|
|
|
} // namespace
|
|
|
|
void skin_vertices(std::span<gfx::basic_vertex const> const Rest, std::span<vertex_weights const> const Weights, std::span<glm::mat4 const> const Palette, std::span<gfx::basic_vertex> const Out)
|
|
{
|
|
auto const count{std::min(Rest.size(), Out.size())};
|
|
for (std::size_t idx = 0; idx < count; ++idx)
|
|
{
|
|
auto const &rest{Rest[idx]};
|
|
auto &out{Out[idx]};
|
|
out = rest;
|
|
if (idx >= Weights.size())
|
|
{
|
|
continue;
|
|
}
|
|
auto const matrix{blended(Weights[idx], Palette)};
|
|
glm::mat3 const rotation{matrix};
|
|
out.position = glm::vec3{matrix * glm::vec4{rest.position, 1.f}};
|
|
out.normal = direction(normal_matrix(rotation), rest.normal);
|
|
out.tangent = glm::vec4{direction(rotation, glm::vec3{rest.tangent}), rest.tangent.w};
|
|
}
|
|
}
|
|
|
|
} // namespace skinning
|