18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-01 09:27:10 +01:00
Files
maszyna/tests/unit/test_skinning.cpp
T
maj00r d324c53233 feat(model): skinned parts of EU07M models move with their joints, skinned on the CPU
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.
2026-11-28 23:52:35 +01:00

199 lines
8.8 KiB
C++

#include "stdafx.h"
#include "doctest.h"
#include "model/eu07m/eu07m_build.h"
#include "model/eu07m/eu07m_io.h"
#include "model/skinning.h"
#include <algorithm>
#include <glm/gtc/matrix_transform.hpp>
#include <cmath>
#include <filesystem>
#include <fstream>
#include <numbers>
namespace {
bool close_to(glm::vec3 const &A, glm::vec3 const &B, float const Tolerance = 1e-4f) {
return glm::length(A - B) <= Tolerance;
}
gfx::basic_vertex vertex_at(glm::vec3 const &Position) {
gfx::basic_vertex vertex;
vertex.position = Position;
vertex.normal = {0.f, 1.f, 0.f};
vertex.tangent = {1.f, 0.f, 0.f, 1.f};
return vertex;
}
// a tube along +z, 4 m long, skinned to two joints: the base at the origin and the tip at z = 2, child of the base. the vertices follow
// the base below z = 1.5, the tip above z = 2.5 and both in between. the tip turned bends the tube (EU07M_SKINNED_TUBE in the scratch
// folder is this model, for a look in the game)
eu07m::model skinned_tube() {
eu07m::model model;
auto const node{[&model](std::string_view const Name, std::int32_t const Parent, std::uint32_t const Flags) {
eu07m::node_record record;
record.name = model.intern(Name);
record.parent = Parent;
record.legacy_flags = Flags;
model.nodes.push_back(record);
return static_cast<std::uint32_t>(model.nodes.size() - 1);
}};
// the order of the nodes is the order of the walk: a subtree right after its node
auto const root{node("tube_root", -1, 0x8000)};
auto const tube{node("tube", static_cast<std::int32_t>(root), 0x10)};
auto const base{node("tube_base", static_cast<std::int32_t>(root), 0xC000)};
auto const tip{node("tube_tip", static_cast<std::int32_t>(base), 0xC000)};
model.nodes[root].subtree_end = 4;
model.nodes[tube].subtree_end = 2;
model.nodes[base].subtree_end = 4;
model.nodes[tip].subtree_end = 4;
model.nodes[tip].translation = {0.f, 0.f, 2.f};
eu07m::material_record material;
material.diffuse = {0.9f, 0.4f, 0.1f, 1.f};
model.materials.push_back(material);
constexpr int sides{16};
constexpr int rings{21};
constexpr float radius{0.3f};
for (int ring = 0; ring < rings; ++ring) {
auto const z{4.f * static_cast<float>(ring) / static_cast<float>(rings - 1)};
// the tip takes over between 1.5 and 2.5 m
auto const tipweight{std::clamp(z - 1.5f, 0.f, 1.f)};
auto const tipbyte{static_cast<std::uint8_t>(std::lround(tipweight * 255.f))};
for (int side = 0; side < sides; ++side) {
auto const angle{2.f * std::numbers::pi_v<float> * static_cast<float>(side) / static_cast<float>(sides)};
glm::vec3 const normal{std::cos(angle), std::sin(angle), 0.f};
model.positions.push_back({radius * normal.x, radius * normal.y, z});
eu07m::attributes attribute;
attribute.qtangent = eu07m::encode_qtangent({normal, {0.f, 0.f, 1.f}, 1.f});
attribute.uv = {static_cast<float>(side) / sides, z};
model.vertex_attributes.push_back(attribute);
eu07m::skin_weights weights;
weights.joints = {0, 1, 0, 0};
weights.weights = {static_cast<std::uint8_t>(255 - tipbyte), tipbyte, 0, 0};
model.weights.push_back(weights);
}
}
for (int ring = 0; ring + 1 < rings; ++ring) {
for (int side = 0; side < sides; ++side) {
auto const a{static_cast<std::uint16_t>(ring * sides + side)};
auto const b{static_cast<std::uint16_t>(ring * sides + (side + 1) % sides)};
auto const c{static_cast<std::uint16_t>(a + sides)};
auto const d{static_cast<std::uint16_t>(b + sides)};
for (auto const index : {a, b, d, a, d, c})
model.indices16.push_back(index);
}
}
eu07m::primitive_record primitive;
primitive.vertex_count = static_cast<std::uint32_t>(model.positions.size());
primitive.index_count = static_cast<std::uint32_t>(model.indices16.size());
primitive.node = tube;
primitive.material = 0;
primitive.skin = 0;
primitive.flags = static_cast<std::uint32_t>(eu07m::topology::triangles) | eu07m::primitive_flags::indexed | eu07m::primitive_flags::opaque;
model.primitives.push_back(primitive);
model.meshes.push_back({0, 1, 0, 0});
model.nodes[tube].mesh = 0;
model.skins.push_back({0, 2, static_cast<std::int32_t>(root), 0});
eu07m::skin_joint_record basejoint;
basejoint.node = base;
basejoint.inverse_bind = {1.f, 0.f, 0.f, 0.f, 1.f, 0.f, 0.f, 0.f, 1.f, 0.f, 0.f, 0.f};
auto tipjoint{basejoint};
tipjoint.node = tip;
tipjoint.inverse_bind[11] = -2.f; // the inverse of the tip at z = 2
model.skin_joints = {basejoint, tipjoint};
eu07m::build_name_index(model);
eu07m::compute_bounds(model);
return model;
}
} // namespace
TEST_CASE("skinning: the weights of the file add up to 1") {
auto const narrow{skinning::unpack(std::array<std::uint16_t, 4>{0, 1, 0, 0}, std::array<std::uint8_t, 4>{128, 127, 0, 0})};
CHECK(narrow.weights[0] + narrow.weights[1] == doctest::Approx(1.f));
CHECK(narrow.weights[0] == doctest::Approx(128.f / 255.f));
CHECK(narrow.joints[1] == 1);
auto const wide{skinning::unpack(std::array<std::uint16_t, 4>{3, 300, 0, 0}, std::array<std::uint16_t, 4>{65535, 0, 0, 0})};
CHECK(wide.weights[0] == doctest::Approx(1.f));
CHECK(wide.joints[1] == 300);
// without weights the vertex follows joint 0
auto const none{skinning::unpack(std::array<std::uint16_t, 4>{5, 6, 0, 0}, std::array<std::uint8_t, 4>{0, 0, 0, 0})};
CHECK(none.joints[0] == 0);
CHECK(none.weights[0] == doctest::Approx(1.f));
}
TEST_CASE("skinning: the bind pose leaves the vertices where they are") {
auto const tipbind{glm::translate(glm::mat4{1.f}, glm::vec3{0.f, 0.f, 2.f})};
std::vector<glm::mat4> const joints{glm::mat4{1.f}, tipbind};
std::vector<glm::mat4> const inversebind{glm::mat4{1.f}, glm::inverse(tipbind)};
auto const palette{skinning::palette(glm::mat4{1.f}, joints, inversebind)};
CHECK(skinning::rest_pose(palette));
// the placement of the model is in both the mesh and the joints, and cancels out
auto const placed{glm::translate(glm::rotate(glm::mat4{1.f}, 0.7f, glm::vec3{0.f, 1.f, 0.f}), glm::vec3{10.f, 5.f, -3.f})};
std::vector<glm::mat4> const placedjoints{placed, placed * tipbind};
CHECK(skinning::rest_pose(skinning::palette(placed, placedjoints, inversebind), 1e-4f));
std::vector<glm::mat4> const moved{glm::translate(glm::mat4{1.f}, glm::vec3{0.f, 0.1f, 0.f})};
CHECK_FALSE(skinning::rest_pose(moved));
}
TEST_CASE("skinning: a joint turned moves its vertices, blended by the weights") {
auto const tipbind{glm::translate(glm::mat4{1.f}, glm::vec3{0.f, 0.f, 2.f})};
// the tip turned by 90 degrees about x at its place: +z goes to -y, +y to +z
auto const tipposed{tipbind * glm::rotate(glm::mat4{1.f}, std::numbers::pi_v<float> / 2.f, glm::vec3{1.f, 0.f, 0.f})};
std::vector<glm::mat4> const joints{glm::mat4{1.f}, tipposed};
std::vector<glm::mat4> const inversebind{glm::mat4{1.f}, glm::inverse(tipbind)};
auto const palette{skinning::palette(glm::mat4{1.f}, joints, inversebind)};
CHECK_FALSE(skinning::rest_pose(palette));
std::vector<gfx::basic_vertex> const rest{vertex_at({0.f, 0.f, 1.f}), vertex_at({0.f, 0.f, 3.f}), vertex_at({0.f, 0.f, 2.f})};
std::vector<skinning::vertex_weights> weights(3);
weights[0] = {{0, 0, 0, 0}, {1.f, 0.f, 0.f, 0.f}};
weights[1] = {{1, 0, 0, 0}, {1.f, 0.f, 0.f, 0.f}};
weights[2] = {{0, 1, 0, 0}, {0.5f, 0.5f, 0.f, 0.f}};
std::vector<gfx::basic_vertex> out(rest.size());
skinning::skin_vertices(rest, weights, palette, out);
// the base doesn't move
CHECK(close_to(out[0].position, {0.f, 0.f, 1.f}));
CHECK(close_to(out[0].normal, {0.f, 1.f, 0.f}));
// 1 m past the tip along z turns to 1 m along -y; the normal +y turns to +z
CHECK(close_to(out[1].position, {0.f, -1.f, 2.f}));
CHECK(close_to(out[1].normal, {0.f, 0.f, 1.f}));
CHECK(out[1].tangent.w == doctest::Approx(1.f));
// the pivot stays put whatever the weights
CHECK(close_to(out[2].position, {0.f, 0.f, 2.f}));
CHECK(glm::length(out[2].normal) == doctest::Approx(1.f));
}
TEST_CASE("skinning: a joint the palette hasn't counts as the identity") {
std::vector<gfx::basic_vertex> const rest{vertex_at({1.f, 2.f, 3.f})};
std::vector<skinning::vertex_weights> const weights{{{7, 0, 0, 0}, {1.f, 0.f, 0.f, 0.f}}};
std::vector<glm::mat4> const palette{glm::translate(glm::mat4{1.f}, glm::vec3{5.f, 0.f, 0.f})};
std::vector<gfx::basic_vertex> out(1);
skinning::skin_vertices(rest, weights, palette, out);
CHECK(close_to(out[0].position, {1.f, 2.f, 3.f}));
}
TEST_CASE("skinning: the skinned tube is a valid EU07M file") {
auto const model{skinned_tube()};
auto const file{eu07m::write(model, {true, 9})};
eu07m::model read;
auto const result{eu07m::read(file, read, {true, true})};
REQUIRE(result.ok);
CHECK(read.skins.size() == 1);
CHECK(read.skin_joints.size() == 2);
CHECK(read.weights.size() == read.positions.size());
CHECK(read.primitives[0].skin == 0);
// kept in the scratch folder for a look in the game
auto const path{std::filesystem::path{EU07_UNIT_TESTS_SCRATCH} / "skinned_tube.eu07m"};
std::ofstream output{path, std::ios::binary};
output.write(reinterpret_cast<char const *>(file.data()), static_cast<std::streamsize>(file.size()));
CHECK(output.good());
}