mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-12-01 19:57:06 +01:00
c8068d8f3d
Stage 8 of the EU07M model format. A model can carry animation clips; each track of a clip sets the translation, the rotation or the scale of a node at a time of the clip, and the parts of a node without a track keep its TRS. - Format, five optional sections: ACLP (clips: name, duration, flags 0x1 loop and 0x2 autoplay, range of tracks), ATRK (tracks: node, part, interpolation step / linear / cubic, ranges of keys), ATIM (key times), AVEC (vectors; with cubic the in-tangent, value and out-tangent of glTF) and AROT (rotations in 8 bytes, "smallest three": the three smaller components as snorm16 scaled by sqrt(2) and the index of the largest; eu07m::encode_rotation / decode_rotation). The validator refuses a node out of range, keys out of their arrays, times which aren't numbers from 0 up, a cubic rotation and tracks out of the range of the clip. -eu07m dump lists the clips and their tracks. The current release doesn't read these sections; a file without them loads as before. - Sampling (model/clip.cpp): step, lerp / slerp the shorter way, the Hermite spline of glTF with tangents per second; the first and the last key held outside of them; a looped clip goes around its duration, another stops at its end. - In the game: the clips play by the simulation time, from TAnimModel::play_clip(name, speed) or, flagged autoplay, from the first time the instance is drawn. RaPrepare() samples them for the instance drawn and sets the transforms of the nodes (TSubModel::SetClipTransform(), the new animation type at_Clip, gated by the instance like the others), so a clip moving the joints of a skin bends the skinned parts. The nodes a clip moves are flagged animated at load, which keeps the model out of the instancing. stop_clips() stops them. - The editor self-test gets "clip <node> <name> [speed]". Not yet: a scenery event starting a clip, and the CHNL channels bound to the time of a clip. Checked: a skinned tube with an autoplay clip (the tip to 60 degrees and back in 2 s, looped) beside a still one, six screenshots in Vulkan and in GL 4.6 go from straight to fully bent while the other stays (time let run in the editor for the test only). Unit tests 1200/1200, 6 new ones for the packing, the interpolations, the time and the file. Not built with GCC here.
138 lines
7.0 KiB
C++
138 lines
7.0 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/eu07m/eu07m_dump.h"
|
|
|
|
#include <array>
|
|
#include <format>
|
|
#include <string_view>
|
|
|
|
namespace eu07m {
|
|
|
|
std::string fourcc_name(std::uint32_t const Id) {
|
|
std::string text(4, ' ');
|
|
for (std::size_t idx = 0; idx < 4; ++idx) {
|
|
auto const character{static_cast<char>((Id >> (idx * 8)) & 0xFF)};
|
|
text[idx] = (character >= 32 && character < 127) ? character : '?';
|
|
}
|
|
return text;
|
|
}
|
|
|
|
namespace {
|
|
|
|
std::string_view topology_name(std::uint32_t const Flags) {
|
|
switch (static_cast<topology>(Flags & primitive_flags::topology_mask)) {
|
|
case topology::points: return "points";
|
|
case topology::lines: return "lines";
|
|
case topology::line_loop: return "line loop";
|
|
case topology::line_strip: return "line strip";
|
|
case topology::triangles: return "triangles";
|
|
case topology::triangle_strip: return "triangle strip";
|
|
case topology::triangle_fan: return "triangle fan";
|
|
default: return "?";
|
|
}
|
|
}
|
|
|
|
std::string quoted(model const &Model, std::uint32_t const Id) {
|
|
return Id == no_index ? std::string{"-"} : std::format("\"{}\"", Model.string(Id));
|
|
}
|
|
|
|
void dump_counts(model const &Model, std::string &Output) {
|
|
Output += std::format("flags: {:#x}, source hash: {:#018x}\n", Model.flags, Model.source_hash);
|
|
Output += std::format("strings: {}, nodes: {} (matrices {}), meshes: {}, primitives: {}, materials: {}\n", Model.strings.size(), Model.nodes.size(), Model.transforms.size(),
|
|
Model.meshes.size(), Model.primitives.size(), Model.materials.size());
|
|
Output += std::format("vertices: {}, indices: {} x u16 + {} x u32, weights: {} + {} wide, userdata: {}, bindings: {}\n", Model.vertex_count(), Model.indices16.size(),
|
|
Model.indices32.size(), Model.weights.size(), Model.weights_wide.size(), Model.vertex_userdata.size(), Model.bindings.size());
|
|
Output += std::format("skins: {} ({} joints), vertex groups: {}, components: {} ({} bytes), bounds: {}, name index: {}\n", Model.skins.size(), Model.skin_joints.size(),
|
|
Model.vertex_groups.size(), Model.components.size(), Model.component_data.size(), Model.bounds.size(), Model.name_index.size());
|
|
for (auto const §ion : Model.unknown)
|
|
Output += std::format("unknown section {}: {} bytes\n", fourcc_name(section.id), section.data.size());
|
|
}
|
|
|
|
void dump_primitives(model const &Model, mesh_record const &Mesh, std::string_view const Indent, std::string &Output) {
|
|
for (auto idx = Mesh.primitive_first; idx < Mesh.primitive_first + Mesh.primitive_count && idx < Model.primitives.size(); ++idx) {
|
|
auto const &primitive{Model.primitives[idx]};
|
|
auto const material{primitive.material < Model.materials.size() ? Model.materials[primitive.material] : material_record{}};
|
|
auto const materialname{material.replaceable != 0 ? std::format("skin {}", material.replaceable) : quoted(Model, material.name)};
|
|
Output += std::format("{} {} {}{}, vertices {}+{}, indices {}+{}, material {}, flags {:#x}\n", Indent, topology_name(primitive.flags),
|
|
(primitive.flags & primitive_flags::indexed) != 0 ? "indexed" : "plain", (primitive.flags & primitive_flags::index32) != 0 ? " u32" : "", primitive.base_vertex,
|
|
primitive.vertex_count, primitive.first_index, primitive.index_count, materialname, primitive.flags);
|
|
}
|
|
}
|
|
|
|
void dump_components(model const &Model, std::uint32_t const First, std::uint32_t const Count, std::string_view const Indent, std::string &Output) {
|
|
for (auto idx = First; idx < First + Count && idx < Model.components.size(); ++idx) {
|
|
auto const &component{Model.components[idx]};
|
|
Output += std::format("{} [{}] {} v{}, {} bytes\n", Indent, idx, fourcc_name(component.type), component.version, component.data_size);
|
|
}
|
|
}
|
|
|
|
void dump_nodes(model const &Model, std::string &Output) {
|
|
std::vector<int> depth(Model.nodes.size(), 0);
|
|
for (std::uint32_t idx = 0; idx < Model.nodes.size(); ++idx) {
|
|
auto const &node{Model.nodes[idx]};
|
|
if (node.parent >= 0 && node.parent < static_cast<std::int64_t>(idx))
|
|
depth[idx] = depth[node.parent] + 1;
|
|
std::string const indent(static_cast<std::size_t>(depth[idx]) * 2, ' ');
|
|
Output += std::format("{}{} {} t({:.4f} {:.4f} {:.4f}) r({:.4f} {:.4f} {:.4f} {:.4f}) s({:.4f} {:.4f} {:.4f}){}{} range {:.1f}-{:.1f} legacy {:#x}\n", indent, idx,
|
|
quoted(Model, node.name), node.translation[0], node.translation[1], node.translation[2], node.rotation[0], node.rotation[1], node.rotation[2], node.rotation[3],
|
|
node.scale[0], node.scale[1], node.scale[2], (node.flags & node_flags::matrix) != 0 ? " matrix" : "", (node.flags & node_flags::animated) != 0 ? " animated" : "",
|
|
std::sqrt(node.range_min_sq), std::sqrt(node.range_max_sq), node.legacy_flags);
|
|
if (node.mesh >= 0 && node.mesh < static_cast<std::int64_t>(Model.meshes.size()))
|
|
dump_primitives(Model, Model.meshes[node.mesh], indent, Output);
|
|
dump_components(Model, node.component_first, node.component_count, indent, Output);
|
|
}
|
|
}
|
|
|
|
void dump_model_components(model const &Model, std::string &Output) {
|
|
for (std::uint32_t idx = 0; idx < Model.components.size(); ++idx) {
|
|
auto const &component{Model.components[idx]};
|
|
if (component.kind == target_kind::model)
|
|
Output += std::format("model component [{}] {} v{}, {} bytes\n", idx, fourcc_name(component.type), component.version, component.data_size);
|
|
}
|
|
}
|
|
|
|
void dump_clips(model const &Model, std::string &Output) {
|
|
constexpr std::array<std::string_view, 3> paths{"translation", "rotation", "scale"};
|
|
constexpr std::array<std::string_view, 3> interpolations{"step", "linear", "cubic"};
|
|
for (std::uint32_t idx = 0; idx < Model.clips.size(); ++idx) {
|
|
auto const &clip{Model.clips[idx]};
|
|
Output += std::format("clip {} {} {:.3f} s{}{}\n", idx, quoted(Model, clip.name), clip.duration, (clip.flags & clip_flags::loop) != 0 ? " loop" : "",
|
|
(clip.flags & clip_flags::autoplay) != 0 ? " autoplay" : "");
|
|
for (auto track = clip.track_first; track < clip.track_first + clip.track_count && track < Model.clip_tracks.size(); ++track) {
|
|
auto const &record{Model.clip_tracks[track]};
|
|
auto const path{static_cast<std::size_t>(record.path)};
|
|
auto const interpolation{static_cast<std::size_t>(record.interpolation)};
|
|
auto const node{record.node < Model.nodes.size() ? quoted(Model, Model.nodes[record.node].name) : std::string{"?"}};
|
|
Output += std::format(" node {} {} {} {}, {} keys\n", record.node, node, path < paths.size() ? paths[path] : "?", interpolation < interpolations.size() ? interpolations[interpolation] : "?",
|
|
record.key_count);
|
|
}
|
|
}
|
|
}
|
|
|
|
} // namespace
|
|
|
|
std::string dump(model const &Model) {
|
|
std::string output;
|
|
dump_counts(Model, output);
|
|
dump_nodes(Model, output);
|
|
dump_model_components(Model, output);
|
|
dump_clips(Model, output);
|
|
if (false == Model.bounds.empty()) {
|
|
auto const &whole{Model.bounds.back()};
|
|
output += std::format("bounds: ({:.3f} {:.3f} {:.3f}) - ({:.3f} {:.3f} {:.3f}), radius {:.3f}\n", whole.min[0], whole.min[1], whole.min[2], whole.max[0], whole.max[1], whole.max[2],
|
|
whole.sphere[3]);
|
|
}
|
|
return output;
|
|
}
|
|
|
|
} // namespace eu07m
|