25 bool write_via_registry(
26 const std::string& filepath,
27 const std::vector<Kakshya::MeshData>& meshes,
33 "save_mesh: no writer registered for '{}'", filepath);
37 const bool ok = writer->write(filepath, meshes, options);
40 "save_mesh: writer failed for '{}': {}", filepath, writer->get_last_error());
43 "save_mesh: wrote '{}'", filepath);
60 std::string pack_diffuse_texture(
61 const std::shared_ptr<Core::VKImage>& texture,
62 const std::string& model_filepath,
63 const std::string& suffix)
69 const std::filesystem::path model_path(model_filepath);
70 std::string name = model_path.stem().string() +
"_diffuse";
71 if (!suffix.empty()) {
76 const auto image_path = (model_path.parent_path() / name).string();
79 "save_mesh: failed to write diffuse texture to '{}'", image_path);
89 void ensure_submesh_region(Kakshya::MeshData& mesh_data,
const std::string& name)
91 if (mesh_data.submeshes && !mesh_data.submeshes->regions.empty()) {
95 Kakshya::MeshSubrange sub;
97 sub.index_count = mesh_data.face_count() * 3;
100 Kakshya::RegionGroup rg(
"submeshes");
101 rg.add_region(sub.to_region());
102 mesh_data.submeshes = std::move(rg);
110 std::optional<Kakshya::MeshData> pack_node(
111 const std::shared_ptr<Nodes::GpuSync::MeshWriterNode>& node,
112 const glm::mat4& world,
113 const std::string& name,
114 const std::string& diffuse_path = {})
116 auto mesh_data = node->get_mesh_data();
121 const glm::mat3 normal_matrix(world);
123 auto& vertex_bytes = std::get<std::vector<uint8_t>>(mesh_data->vertex_variant);
124 std::vector<Kakshya::MeshVertex> verts(vertex_bytes.size() /
sizeof(Kakshya::MeshVertex));
125 std::memcpy(verts.data(), vertex_bytes.data(), vertex_bytes.size());
126 for (
auto& v : verts) {
127 v.position = glm::vec3(world * glm::vec4(v.position, 1.0F));
128 v.normal = glm::normalize(normal_matrix * v.normal);
129 v.tangent = glm::vec4(glm::normalize(normal_matrix * glm::vec3(v.tangent)), v.tangent.w);
131 std::memcpy(vertex_bytes.data(), verts.data(), vertex_bytes.size());
133 const auto index_count = std::get<std::vector<uint32_t>>(mesh_data->index_variant).size();
135 Kakshya::MeshSubrange sub;
137 sub.index_count =
static_cast<uint32_t
>(index_count);
139 sub.diffuse_path = diffuse_path;
140 Kakshya::RegionGroup rg(
"submeshes");
141 rg.add_region(sub.to_region());
142 mesh_data->submeshes = std::move(rg);
147 std::string splice_timestamp(
const std::string& pattern)
149 const auto now_ms = std::chrono::duration_cast<std::chrono::milliseconds>(
150 std::chrono::system_clock::now().time_since_epoch())
158 const std::shared_ptr<Buffers::MeshBuffer>& buffer,
159 const std::string& filepath,
167 auto mesh_data = buffer->get_mesh_data();
168 if (buffer->has_diffuse_texture()) {
169 const auto image_path = pack_diffuse_texture(buffer->get_diffuse_texture(), filepath,
"");
170 if (!image_path.empty()) {
171 ensure_submesh_region(mesh_data,
"mesh");
172 auto& region = mesh_data.submeshes->regions.front();
178 region.set(std::move(sub));
182 return write_via_registry(filepath, { mesh_data }, options);
186 const std::shared_ptr<Nodes::Network::MeshNetwork>& network,
187 const std::string& filepath,
195 std::vector<Kakshya::MeshData> meshes;
196 meshes.reserve(network->slots().size());
197 for (
const auto& slot : network->slots()) {
201 std::string diffuse_path;
202 if (slot.diffuse_texture) {
203 const auto suffix = !slot.name.empty() ? slot.name : std::to_string(slot.index);
204 diffuse_path = pack_diffuse_texture(slot.diffuse_texture, filepath, suffix);
206 if (
auto packed = pack_node(slot.node, slot.world_transform, slot.name, diffuse_path)) {
207 meshes.push_back(std::move(*packed));
211 if (meshes.empty()) {
213 "save_mesh: no exportable slots in network");
217 return write_via_registry(filepath, meshes, options);
221 const std::shared_ptr<Buffers::MeshNetworkBuffer>& network_buffer,
222 const std::string& filepath,
225 if (!network_buffer) {
230 auto net = network_buffer->get_network();
233 "save_mesh: MeshNetworkBuffer has no network");
237 std::vector<Kakshya::MeshData> meshes;
238 meshes.reserve(net->slots().size());
239 const std::string shared_diffuse_path = network_buffer->has_diffuse_texture()
240 ? pack_diffuse_texture(network_buffer->get_diffuse_texture(), filepath,
"")
242 for (
const auto& slot : net->slots()) {
246 std::string diffuse_path = shared_diffuse_path;
247 if (slot.diffuse_texture) {
248 const auto suffix = !slot.name.empty() ? slot.name : std::to_string(slot.index);
249 diffuse_path = pack_diffuse_texture(slot.diffuse_texture, filepath, suffix);
251 if (
auto packed = pack_node(slot.node, slot.world_transform, slot.name, diffuse_path)) {
252 meshes.push_back(std::move(*packed));
256 if (meshes.empty()) {
258 "save_mesh: no exportable slots in network");
262 return write_via_registry(filepath, meshes, options);
266 const std::shared_ptr<Nodes::GpuSync::MeshWriterNode>& node,
267 const std::string& filepath,
275 auto packed = pack_node(node, glm::mat4(1.0F),
"mesh");
278 "save_mesh: node has no vertices/indices yet");
282 return write_via_registry(filepath, { *packed }, options);
286 const std::shared_ptr<Buffers::ComputeMeshBuffer>& buffer)
292 const std::shared_ptr<Buffers::ComputeMeshBuffer>& buffer,
293 const std::string& filepath,
301 if (buffer->has_diffuse_texture()) {
302 const auto image_path = pack_diffuse_texture(buffer->get_diffuse_texture(), filepath,
"");
303 if (!image_path.empty()) {
304 ensure_submesh_region(*mesh_data,
"mesh");
305 auto& region = mesh_data->submeshes->regions.front();
311 region.set(std::move(sub));
315 return write_via_registry(filepath, { *mesh_data }, options);
319 const std::shared_ptr<Buffers::ComputeMeshBuffer>& buffer,
320 const std::string& path_pattern,
323 return save_mesh(buffer, splice_timestamp(path_pattern), options);
327 const std::shared_ptr<Buffers::MeshBuffer>& buffer,
328 const std::string& path_pattern,
331 return save_mesh(buffer, splice_timestamp(path_pattern), options);
335 const std::shared_ptr<Buffers::MeshNetworkBuffer>& network_buffer,
336 const std::string& path_pattern,
339 return save_mesh(network_buffer, splice_timestamp(path_pattern), options);
343 const std::shared_ptr<Nodes::Network::MeshNetwork>& network,
344 const std::string& path_pattern,
347 return save_mesh(network, splice_timestamp(path_pattern), options);
351 const std::shared_ptr<Nodes::GpuSync::MeshWriterNode>& node,
352 const std::string& path_pattern,
355 return save_mesh(node, splice_timestamp(path_pattern), options);
#define MF_INFO(comp, ctx,...)
#define MF_ERROR(comp, ctx,...)
Cycle Behavior: The for_cycles(N) configuration controls how many times the capture operation execute...
static WriterRegistry & instance()
std::unique_ptr< Writer > create_writer(const std::string &filepath) const
std::optional< Kakshya::MeshData > download_compute_mesh(const std::shared_ptr< ComputeMeshBuffer > &buffer)
Download a ComputeMeshBuffer's current live geometry to CPU.
bool save_image(const std::shared_ptr< Core::VKImage > &image, const std::string &filepath, const ImageWriteOptions &options)
Save a VKImage directly to disk via the ImageWriter registry.
std::optional< Kakshya::MeshData > download_compute_mesh(const std::shared_ptr< Buffers::ComputeMeshBuffer > &buffer)
Download a ComputeMeshBuffer's current live geometry to CPU.
std::string resolve_sequence_path(std::string_view pattern, uint64_t frame)
Substitute a frame index into a numbered output pattern.
bool save_mesh(const std::shared_ptr< Buffers::MeshBuffer > &buffer, const std::string &filepath, const ModelWriteOptions &options)
Save a MeshBuffer's current mesh data to disk via ModelWriterRegistry.
bool save_mesh_snapshot(const std::shared_ptr< Buffers::ComputeMeshBuffer > &buffer, const std::string &path_pattern, const ModelWriteOptions &options)
Save a ComputeMeshBuffer with a millisecond epoch timestamp spliced into the path.
@ FileIO
Filesystem I/O operations.
@ IO
Networking, file handling, streaming.
Configuration for model writing.
Byte-range descriptor for one submesh within the shared index buffer.