mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-12-01 12:57:06 +01:00
d324c53233
Stage 4 of the EU07M model format. A primitive with a skin (SKIN, SKJT, weights in VSKN or VSKW) was drawn in its bind pose. Now it follows the joints of its skin, in both renderers. - The joints are nodes of the model, so they're posed as before: animation events, the .mmd, SetRotate / SetTranslate from the vehicle code. The matrix of a joint is world(joint) * inverse bind(joint), world being the transform of the joint from the root of the model with the animations of the instance drawn (TSubModel::posed_transform()); the vertices lie in the bind pose in the space of the model, the transform of the node of the mesh left out, as in glTF. - The loader keeps the bind pose and the weights of a skinned primitive (skinning::skinned_geometry on the submodel) and the skins of the model (TModel3d::skins()). - The first pass of a frame to draw a skinned submodel of an instance works out the palette and, when it changed, moves the vertices on the CPU: positions by the blended matrix, normals and tangents by its rotation (model/skinning.cpp, gfx::skin_cache in rendering/common/skincache.cpp). The geometry of the instance is made or replaced before the passes of the next frame (flush() in the frame setup of both renderers), as nothing the passes draw may change while they draw (gfx::walk_sync): the vertices follow the joints a frame late. In the bind pose the shared geometry of the model is drawn; an instance not drawn for 600 frames lets go of its own. - Fix: a node with range_max_sq 0, which the spec gives as no limit, was never drawn (a model made anew rather than converted from E3D); it keeps the default range of a submodel now. - The editor self-test gets "animate <node> <submodel> <x> <y> <z>", which turns a submodel of an instance as an animation event does, since the editor doesn't run the events. Not in this commit: the skinning in a compute shader, bounds of the posed submodel, motion vectors and ray tracing of the moved vertices. Checked: a tube of two joints in two instances, one turned by 60 degrees at its middle, bends smoothly while the other stays straight, in Vulkan and GL 4.6, with gfx.gpucull 0 and 1 (time let run in the editor for the test only). Unit tests 1194/1194, 5 new ones for the weights, the bind pose, the blending and the file. Not built with GCC here.
128 lines
3.5 KiB
C++
128 lines
3.5 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)
|
|
{
|
|
glm::mat4 const identity{1.f};
|
|
return std::ranges::all_of(Palette, [&](glm::mat4 const &Matrix) {
|
|
for (int column = 0; column < 4; ++column)
|
|
{
|
|
for (int row = 0; row < 4; ++row)
|
|
{
|
|
if (std::abs(Matrix[column][row] - identity[column][row]) > Tolerance)
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
return true;
|
|
});
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
} // 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(rotation, rest.normal);
|
|
out.tangent = glm::vec4{direction(rotation, glm::vec3{rest.tangent}), rest.tangent.w};
|
|
}
|
|
}
|
|
|
|
} // namespace skinning
|