载入中...
搜索中...
未找到
DeformMeshPresentation.cpp
浏览该文件的文档.
1// eve-hot-path
3#include <cmath>
4#include <glm/glm.hpp>
5#include <limits>
6#include <numbers>
7#include "graphics/Graphics.h"
8#include "graphics/Mesh.h"
11namespace eve::physics {
15 float radius;
16 std::vector<DeformPoint> sampled;
17 std::vector<float> positions, normals, uv;
18 std::vector<std::uint32_t> indices;
19 std::vector<glm::dvec3> accumulated;
20};
21namespace {
22Diagnostic invalid(const char* message) {
24}
25} // namespace
26DeformMeshPresentation::DeformMeshPresentation(std::unique_ptr<Impl> impl) : impl_(std::move(impl)) {}
30 if (track.recipe()->coordinates() != BakeCoordinates::MetresRightHandedYUp)
31 return Output::failure(invalid("3D deformation presentation requires metres"));
32 const auto topology = track.topology();
34 if (!triangles && (!std::isfinite(ropeRadius) || ropeRadius <= 0))
35 return Output::failure(invalid("rope radius must be finite and positive"));
36 const auto count = triangles ? topology.vertexCount() * 2 : topology.indices().size() / 2 * 18;
37 const auto indexCount = triangles ? topology.indices().size() * 2 : topology.indices().size() / 2 * 96;
38 if (count > 2000000 || indexCount > 12000000)
39 return Output::failure(invalid("deformation presentation budget exceeded"));
40 try {
41 auto impl = std::make_unique<Impl>(Impl{std::move(track), topology, ropeRadius, {}, {}, {}, {}, {}, {}});
42 impl->sampled.resize(topology.vertexCount());
43 impl->positions.resize(count * 3);
44 impl->normals.resize(count * 3);
45 impl->uv.resize(count * 2);
46 impl->indices.reserve(indexCount);
47 if (triangles) {
48 impl->accumulated.resize(topology.vertexCount());
49 for (auto index : topology.indices()) impl->indices.push_back(index);
50 const auto offset = static_cast<std::uint32_t>(topology.vertexCount());
51 for (std::size_t i = 0; i < topology.indices().size(); i += 3) {
52 impl->indices.push_back(topology.indices()[i] + offset);
53 impl->indices.push_back(topology.indices()[i + 2] + offset);
54 impl->indices.push_back(topology.indices()[i + 1] + offset);
55 }
56 if (!topology.uv().empty())
57 for (std::size_t i = 0; i < count; ++i) {
58 auto value = topology.uv()[i % topology.vertexCount()];
59 impl->uv[i * 2] = value.u;
60 impl->uv[i * 2 + 1] = value.v;
61 }
62 } else {
63 for (std::uint32_t base = 0; base < count; base += 18) {
64 for (std::uint32_t side = 0; side < 8; ++side) {
65 const auto next = (side + 1) % 8;
66 for (auto index :
67 {base + side, base + next, base + 8 + side, base + next, base + 8 + next, base + 8 + side,
68 base + 16, base + next, base + side, base + 17, base + 8 + side, base + 8 + next})
69 impl->indices.push_back(index);
70 }
71 }
72 }
73 return Output::success(std::unique_ptr<DeformMeshPresentation>(new DeformMeshPresentation(std::move(impl))));
74 } catch (const std::bad_alloc&) {
75 return Output::failure(Diagnostic::error(DiagnosticCode::Failed, "deformation mesh allocation failed"));
76 }
77}
78Result<void> DeformMeshPresentation::rebuild(DeformPlayback& player) {
79 if (!impl_->track.isSameAsset(player.track())) return Result<void>::failure(invalid("foreign deformation player"));
80 auto sampled = player.sample(impl_->sampled);
81 if (!sampled) return sampled;
82 auto point = [&](std::uint32_t index) {
83 auto p = impl_->sampled[index];
84 return glm::dvec3(p.x, p.y, p.z);
85 };
86 bool representable = true;
87 auto write = [&](std::size_t index, glm::dvec3 p, glm::dvec3 n) {
88 for (int axis = 0; axis < 3; ++axis) {
89 if (!std::isfinite(p[axis]) || std::abs(p[axis]) > std::numeric_limits<float>::max() ||
90 !std::isfinite(n[axis])) {
91 representable = false;
92 continue;
93 }
94 impl_->positions[index * 3 + axis] = static_cast<float>(p[axis]);
95 impl_->normals[index * 3 + axis] = static_cast<float>(n[axis]);
96 }
97 };
98 if (impl_->topology.primitive() == DeformPrimitive::Triangles) {
99 std::fill(impl_->accumulated.begin(), impl_->accumulated.end(), glm::dvec3(0));
100 const auto indices = impl_->topology.indices();
101 for (std::size_t i = 0; i < indices.size(); i += 3) {
102 const auto normal =
103 glm::cross(point(indices[i + 1]) - point(indices[i]), point(indices[i + 2]) - point(indices[i]));
104 for (int corner = 0; corner < 3; ++corner) impl_->accumulated[indices[i + corner]] += normal;
105 }
106 for (std::size_t i = 0; i < impl_->sampled.size(); ++i) {
107 auto normal = impl_->accumulated[i];
108 const double length = glm::length(normal);
109 normal = length > 0 ? normal / length : glm::dvec3(0, 1, 0);
110 write(i, point(static_cast<std::uint32_t>(i)), normal);
111 write(i + impl_->sampled.size(), point(static_cast<std::uint32_t>(i)), -normal);
112 }
113 } else {
114 const auto indices = impl_->topology.indices();
115 for (std::size_t i = 0; i < indices.size(); i += 2) {
116 auto a = point(indices[i]), b = point(indices[i + 1]);
117 auto axis = b - a;
118 const double length = glm::length(axis);
119 axis = length > 0 ? axis / length : glm::dvec3(0, 1, 0);
120 const auto reference = std::abs(axis.y) < 0.9 ? glm::dvec3(0, 1, 0) : glm::dvec3(1, 0, 0);
121 const auto u = glm::normalize(glm::cross(axis, reference)), v = glm::cross(axis, u);
122 const auto base = i / 2 * 18;
123 for (std::size_t side = 0; side < 8; ++side) {
124 const double angle = double(side) * std::numbers::pi * 0.25;
125 const auto normal = std::cos(angle) * u + std::sin(angle) * v;
126 write(base + side, a + double(impl_->radius) * normal, normal);
127 write(base + 8 + side, b + double(impl_->radius) * normal, normal);
128 }
129 write(base + 16, a, -axis);
130 write(base + 17, b, axis);
131 }
132 }
133 if (!representable) return Result<void>::failure(invalid("deformation mesh position overflow"));
134 for (float value : impl_->positions)
135 if (!std::isfinite(value)) return Result<void>::failure(invalid("deformation mesh position overflow"));
136 for (float value : impl_->normals)
137 if (!std::isfinite(value)) return Result<void>::failure(invalid("deformation mesh normal overflow"));
138 return Result<void>::success();
139}
141 auto ready = rebuild(player);
142 if (!ready) return Result<graphics::Mesh*>::failure(ready.status());
143 auto* mesh = graphics.newMeshFromArrays(impl_->positions.data(), impl_->normals.data(), impl_->uv.data(),
144 static_cast<int>(impl_->positions.size() / 3), impl_->indices.data(),
145 static_cast<int>(impl_->indices.size()));
146 if (!mesh)
148 Diagnostic::error(DiagnosticCode::Failed, "deformation mesh creation failed"));
150}
153 if (!graphics.describeMesh(&mesh)) return Result<void>::failure(invalid("foreign or released deformation mesh"));
154 auto ready = rebuild(player);
155 if (!ready) return ready;
156 if (!graphics.updateMeshVertices(&mesh, impl_->positions.data(), impl_->normals.data(), impl_->uv.data(),
157 static_cast<int>(impl_->positions.size() / 3), impl_->indices.data(),
158 static_cast<int>(impl_->indices.size())))
159 return Result<void>::failure(Diagnostic::error(DiagnosticCode::Failed, "deformation mesh upload failed"));
160 return Result<void>::success();
161}
162} // namespace eve::physics
AuthorityStoreHandleRef reference
Definition Authority.cpp:24
void * impl
glm::vec4 p[6]
std::string message
std::uint32_t indexCount
float u
Definition Grass.cpp:233
glm::vec3 n
Definition Grass.cpp:63
std::vector< std::uint32_t > indices
std::vector< float > normals
std::vector< float > positions
float v
float length
JointPoint2D axis
std::function< void(b3JointId, const Numbers &)> write
size_t offset
MeleePoint3 b
Definition MeleeHit.cpp:41
MeleePoint3 a
Definition MeleeHit.cpp:40
std::vector< TriangleRef > triangles
Texture * normal
OceanMeshSettings mesh
Topology topology
Object::Ptr value
Graphics * graphics
int count
uint32_t index
glm::vec3 point
A structured explanation of a failed, degraded, or noteworthy result.
Definition Diagnostic.h:94
static Diagnostic error(DiagnosticCode code, std::string message, std::string path={})
Construct an error diagnostic with the standard error severity.
Move-only operation result carrying either a value or Status.
Definition Result.h:176
static Result success(T value)
Construct a successful result owning value.
Definition Result.h:185
static Result failure(Status status)
Construct a failed result from a structured status.
Definition Result.h:196
GPU mesh handle (+ optional CPU morph targets).
Definition Mesh.h:26
Prepared dynamic mesh presentation over an owned deformation player. @ownership Owns immutable asset ...
Result< void > upload(DeformPlayback &player, graphics::Graphics &graphics, graphics::Mesh &mesh)
Sample and upload all prepared streams to a live mesh owned by the supplied Graphics....
static Result< std::unique_ptr< DeformMeshPresentation > > prepare(DeformTrack track, float ropeRadius)
Prepare double-sided triangles or capped eight-sided tubes from a metre-space clip.
DeformMeshPresentation(const DeformMeshPresentation &)=delete
Result< graphics::Mesh * > createMesh(DeformPlayback &player, graphics::Graphics &graphics)
Sample the player and create one Graphics-owned mesh with current topology. @ownership Returned point...
Independent clock and placement over a shared immutable deformation clip. @ownership Owns reusable sc...
Result< void > sample(std::span< DeformPoint > output)
Sample and transform into exact-sized output; all failures preserve the caller's entire buffer....
DeformTrack track() const
Return a cheap owning immutable clip copy, independent of player lifetime.
Immutable connectivity and UV ownership for fixed-topology deformation playback. @ownership Copies sh...
DeformPrimitive primitive() const noexcept
Primitive interpretation of the index stream.
Immutable fixed-topology deformation clip, independent of simulation and rendering providers....
Definition DeformTrack.h:25
std::shared_ptr< const BakeRecipe > recipe() const noexcept
Return shared immutable provenance including explicit coordinates.
DeformTopology topology() const
Return a cheap owning copy of the fixed connectivity and UVs.
eve::Result< T > Result
Optional physics backend for vehicle mobility and body attach.
Definition Climbing.h:36