Files
ReSkate/Engine/Resource/Test/skinned_mesh_tests.cpp
Krischan-KlugandClaude Opus 5.5 c7d2ca3601 Add Hall of Meat: the bones a bail hurt and its Meat score
As in skate. 3: when the local skater bails, the bones it hurt show over
the world through the body, yellow where a hit bruised one and red where
one broke it, with the bail's Meat card in the bottom left corner in the
place of skate.'s own HUD there: its time, hits (head hits double,
vehicles half again), broken bones, road rash, airtime, fall, top speed
and rotations, each scoring, and the map's best. A bone breaking pulses
the screen's edges red and, in single player, slows the game down a
moment. Off until switched on, as in skate.: in skate.'s menu (Custom
Stuff > Player) or with `hallofmeat 0|1`, saved with the profile.

Everything it draws is skate.'s own, read from the installed game: its
skeleton mesh (the Dem Bones costume) posed with the renderer's own
skinning matrices, its scoring icons, the THRASHER wordmark and its UI
brush strokes. Nothing is shipped.

How it works: No Bail already hooks the skeleton response, which runs
once a physics step right after the body contacts; it now hands that step
on, with the local skater it follows, but only while Hall of Meat is on.
Each step reads what every ragdoll body hit and how hard, and whether the
skater is a ragdoll; the bail runs on the game's own clock so slow motion
measures what the body did. The feature's own hooks, on the renderer's
skinned draw packets and main view and on the UI model's write, are
prepared at startup and switched on only with the feature, and off again
once the HUD corner and the game's speed are given back: switched off, the
game never calls them and the physics step pays nothing. The skeleton mesh
is read the first time it is on; only the images are read at startup,
since the overlay builds its atlas once. The renderer's picture of the
skater is read only while a bail shows. The menu's switch reads the
overlay model like the others. Every address is in the build table with a
fingerprint checked before use. Extension/HallOfMeat/README.md describes
the parts; its tests build with DINGOSDK_BUILD_HALL_OF_MEAT_TESTS.

The slow motion changes SimulationTime's rate, frame cap and time scale as
a session override and steps the local skater along, since skate. sets a
skater's step only when it builds its core. It runs the same at every
break, but never faster than the game ran: with the trainer's game speed
below 1 it only takes the part that is slower still. Afterwards it writes
back what it found, each setting only while it still holds what the slow
motion wrote, so the trainer's speed comes back as it was; a setting
changed from elsewhere meanwhile is left as it is and ends that slow
motion. named_setting_value reads a setting's value for it.

Tuned in play: a bruise from 7 m/s and a break from 12 m/s along the
contact normal (raised twice: a hard fall on flat ground broke bones), a
break worth ten of the lightest hits (a heavy bail's hits and breaks
outweighed the rest of the card) and a fall half of skate. 3's rate.

Fix: the overlay now draws in the back buffer's pixels, not the window's,
so it lines up on displays Windows scales (1920x1080 borderless on 4K).

The skater's render hooks, its skeleton and its body maps were first
found by gBGYo's ReSkate fork (github.com/gBGYo/ReSkate).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-09 15:29:38 +02:00

135 lines
6.0 KiB
C++

// The skinned MeshSet and SkeletonAsset readers, on made-up resources.
#include "Engine/Resource/mesh_set.h"
#include "Engine/Resource/skeleton_asset.h"
#include <cstring>
#include <iostream>
#include <memory>
#include <stdexcept>
using namespace dingosdk::frostbite;
namespace {
void check(bool value, const char* message) { if (!value) throw std::runtime_error(message); }
template <class T> void put(std::vector<std::byte>& data, std::size_t offset, T value) {
if (data.size() < offset + sizeof(T)) data.resize(offset + sizeof(T));
std::memcpy(data.data() + offset, &value, sizeof(T));
}
void pointer(std::vector<std::byte>& data, std::size_t offset, std::size_t target) {
put<std::uint64_t>(data, offset, target - 16);
}
// One level of detail, one section of three vertices on palette {5, 7}, one triangle.
struct Made {
std::vector<std::byte> resource, geometry;
};
Made made_mesh(std::uint8_t second_weight = 55) {
Made m;
auto& r = m.resource;
r.resize(1100);
const std::array<std::uint32_t, 3> header{208, 188, 384};
for (std::size_t i = 0; i < header.size(); ++i) put<std::uint32_t>(r, i * 4, header[i]);
put<std::uint8_t>(r, 124, 1);
put<std::uint16_t>(r, 180, 1);
pointer(r, 48, 256);
put<std::uint32_t>(r, 256, 1); // kind
put<std::uint32_t>(r, 256 + 8, 1); // one section
pointer(r, 256 + 12, 512);
put<std::uint32_t>(r, 256 + 84, 33); // 16-bit indices
put<std::uint32_t>(r, 256 + 88, 6); // index bytes
put<std::uint32_t>(r, 256 + 92, 108); // vertex bytes: 3 x 36
put<std::uint8_t>(r, 256 + 116, 0x42); // the chunk's guid
pointer(r, 512 + 16, 1024); // palette
put<std::uint16_t>(r, 512 + 24, 2);
put<std::uint8_t>(r, 512 + 31, 3); // triangle list
put<std::uint32_t>(r, 512 + 32, 1);
put<std::uint32_t>(r, 512 + 44, 3);
const std::size_t d = 512 + 112;
const std::array<std::array<std::uint8_t, 4>, 5> elements{{{1, 3, 0, 0}, {2, 23, 12, 0}, {3, 23, 20, 0}, {4, 13, 28, 0}, {5, 13, 32, 0}}};
for (std::size_t i = 0; i < elements.size(); ++i)
for (std::size_t j = 0; j < 4; ++j) put<std::uint8_t>(r, d + i * 4 + j, elements[i][j]);
put<std::uint8_t>(r, d + 64, 36);
put<std::uint8_t>(r, d + 96, 5);
put<std::uint8_t>(r, d + 97, 1);
put<std::uint16_t>(r, 1024, 5);
put<std::uint16_t>(r, 1026, 7);
auto& g = m.geometry;
for (std::size_t v = 0; v < 3; ++v) {
const auto at = v * 36;
put<std::array<float, 3>>(g, at, {static_cast<float>(v), 0.0f, 0.0f});
put<std::array<std::uint16_t, 4>>(g, at + 12, {0, 1, 0, 0});
put<std::array<std::uint16_t, 4>>(g, at + 20, {0, 0, 0, 0});
put<std::array<std::uint8_t, 4>>(g, at + 28, {200, second_weight, 0, 0});
put<std::array<std::uint8_t, 4>>(g, at + 32, {0, 0, 0, 0});
}
put<std::array<std::uint16_t, 3>>(g, 108, {0, 1, 2});
return m;
}
void a_skinned_lod_reads() {
const auto m = made_mesh();
const auto lod = read_mesh_lod(m.resource, 0);
check(lod.vertex_bytes == 108 && lod.index_bytes == 6 && lod.chunk != Guid{}, "the level's buffers and chunk");
const auto mesh = read_skinned_mesh(m.resource, 0, m.geometry);
check(mesh.vertices.size() == 3 && mesh.triangles == std::vector<std::uint32_t>{0, 1, 2}, "three vertices, one triangle");
const auto& v = mesh.vertices[2];
check(v.position[0] == 2.0f && v.bones[0] == 5 && v.bones[1] == 7 && v.weights[0] == 200 && v.weights[1] == 55,
"positions, and bones through the palette");
}
void four_influences_are_enough() {
auto m = made_mesh();
m.resource[512 + 112 + 96] = std::byte{3}; // only position, the first bones and weights:
const std::array<std::array<std::uint8_t, 4>, 3> elements{{{1, 3, 0, 0}, {2, 23, 12, 0}, {4, 13, 28, 0}}};
for (std::size_t i = 0; i < elements.size(); ++i)
for (std::size_t j = 0; j < 4; ++j) put<std::uint8_t>(m.resource, 512 + 112 + i * 4 + j, elements[i][j]);
const auto mesh = read_skinned_mesh(m.resource, 0, m.geometry);
check(mesh.vertices[1].bones[1] == 7 && mesh.vertices[1].weights[1] == 55 && mesh.vertices[1].weights[4] == 0,
"a coarse level of detail weighs four bones");
}
void a_bad_mesh_is_refused() {
const auto broken = made_mesh(54);
bool refused{};
try { (void)read_skinned_mesh(broken.resource, 0, broken.geometry); } catch (const std::runtime_error&) { refused = true; }
check(refused, "weights that do not add up");
refused = false;
try { (void)read_mesh_lod(broken.resource, 1); } catch (const std::runtime_error&) { refused = true; }
check(refused, "a level of detail it does not have");
}
ebx::Value object(std::vector<std::pair<std::string, ebx::Value>> fields) {
auto made = std::make_shared<ebx::Object>();
for (auto& [name, value] : fields) made->fields.push_back({0, std::move(name), std::move(value)});
return {made};
}
void a_skeleton_reads() {
const auto asset = object({
{"BoneNames", {ebx::Value::Array{{std::string("Root")}, {std::string("Hand")}}}},
{"Hierarchy", {ebx::Value::Array{{std::int64_t{-1}}, {std::int64_t{0}}}}},
});
const auto skeleton = read_skeleton(*std::get<std::shared_ptr<ebx::Object>>(asset.data));
check(skeleton.names == std::vector<std::string>{"Root", "Hand"} && skeleton.parents == std::vector<int>{-1, 0}, "names and parents");
const auto backwards = object({
{"BoneNames", {ebx::Value::Array{{std::string("A")}, {std::string("B")}}}},
{"Hierarchy", {ebx::Value::Array{{std::int64_t{1}}, {std::int64_t{-1}}}}},
});
bool refused{};
try { (void)read_skeleton(*std::get<std::shared_ptr<ebx::Object>>(backwards.data)); } catch (const std::runtime_error&) { refused = true; }
check(refused, "a parent after its child");
}
}
int main() {
try {
a_skinned_lod_reads();
four_influences_are_enough();
a_bad_mesh_is_refused();
a_skeleton_reads();
} catch (const std::exception& error) {
std::cerr << "FAILED: " << error.what() << '\n';
return 1;
}
std::cout << "Skinned mesh tests passed.\n";
return 0;
}