mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-12-02 05:02:45 +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.
73 lines
2.3 KiB
C++
73 lines
2.3 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/.
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#include "model/skinning.h"
|
|
#include "rendering/geometrybank.h"
|
|
|
|
#include <cstdint>
|
|
#include <memory>
|
|
#include <mutex>
|
|
#include <unordered_map>
|
|
#include <vector>
|
|
|
|
class TModel3d;
|
|
class TSubModel;
|
|
|
|
namespace gfx
|
|
{
|
|
|
|
// the skinned submodels of the instances drawn (EU07M skins), moved by the pose of the joints of each instance. the walks of the passes
|
|
// work out the vertices once a frame for each instance and submodel, on the CPU, when the pose changed; the geometry is made and
|
|
// replaced by flush(), before the walks of the next frame, as nothing the walks draw may change while they draw (gfx::walk_sync). so
|
|
// the vertices drawn follow the joints a frame late, and the first frame an instance is seen it's drawn in the bind pose
|
|
class skin_cache
|
|
{
|
|
public:
|
|
static skin_cache &instance();
|
|
// in a walk: the geometry to draw the skinned Submodel of Model with, for the instance drawn (TSubModel::iInstance). the bind
|
|
// pose of the submodel while its joints stay in it, or till its own geometry is made
|
|
geometry_handle geometry(TSubModel &Submodel, TModel3d const &Model, unsigned int Frame);
|
|
// on the main thread, before the walks of a frame: the vertices worked out go to the renderer, the instances not drawn for a while
|
|
// let go of theirs
|
|
void flush(unsigned int Frame);
|
|
// the entries kept and the geometry made, for the statistics
|
|
std::size_t size() const;
|
|
|
|
private:
|
|
struct key
|
|
{
|
|
std::uintptr_t owner{0};
|
|
TSubModel const *submodel{nullptr};
|
|
bool operator==(key const &) const = default;
|
|
};
|
|
struct key_hash
|
|
{
|
|
std::size_t operator()(key const &Key) const noexcept;
|
|
};
|
|
struct entry
|
|
{
|
|
std::shared_ptr<skinning::skinned_geometry const> source;
|
|
int type{0};
|
|
unsigned int frame{0}; // the frame the pose was worked out in
|
|
std::vector<glm::mat4> palette;
|
|
gfx::vertex_array vertices;
|
|
bool posed{false}; // the joints aren't in the bind pose
|
|
bool pending{false}; // the vertices are to go to the renderer
|
|
geometry_handle handle{};
|
|
};
|
|
|
|
mutable std::mutex m_mutex;
|
|
std::unordered_map<key, entry, key_hash> m_entries;
|
|
geometrybank_handle m_bank{};
|
|
};
|
|
|
|
} // namespace gfx
|