From 6cec87568db1111f2fa8b1df00674050a1ff23bb Mon Sep 17 00:00:00 2001 From: Joshua Goins Date: Sat, 25 May 2024 22:59:12 -0400 Subject: [PATCH] Bump libphysis, begin exporting shape data --- extern/libphysis | 2 +- parts/mdl/mdlexport.cpp | 43 +++++++++++++++++++++++++++++++++++++++-- 2 files changed, 42 insertions(+), 3 deletions(-) diff --git a/extern/libphysis b/extern/libphysis index c66307b..5512181 160000 --- a/extern/libphysis +++ b/extern/libphysis @@ -1 +1 @@ -Subproject commit c66307bde1bce11a2102418b8349ed67afb345ab +Subproject commit 551218165d09f756a7b034562ea4967447257cf8 diff --git a/parts/mdl/mdlexport.cpp b/parts/mdl/mdlexport.cpp index 5ce1d3b..5fadeb3 100644 --- a/parts/mdl/mdlexport.cpp +++ b/parts/mdl/mdlexport.cpp @@ -181,8 +181,6 @@ void exportModel(const QString &name, const physis_MDL &model, const physis_Skel gltfPrimitive.attributes["JOINTS_0"] = gltfModel.accessors.size() + 7; } - mesh_offset += part.num_submeshes; - // Vertices { auto &positionAccessor = gltfModel.accessors.emplace_back(); @@ -294,6 +292,47 @@ void exportModel(const QString &name, const physis_MDL &model, const physis_Skel indexBufferView.byteLength = indexBuffer.data.size(); } + + // Shapes + { + for (uint32_t j = 0; j < part.num_submeshes; j++) { + auto &gltfPrimitive = gltfModel.meshes[mesh_offset + j].primitives[0]; + + std::vector morphNames; + + for (int s = 0; s < part.num_shapes; s++) { + auto &shape = part.shapes[s]; + + morphNames.push_back(tinygltf::Value(shape.name)); + + gltfPrimitive.targets.push_back({{"POSITION", gltfModel.accessors.size()}}); + + auto &positionMorphAccessor = gltfModel.accessors.emplace_back(); + positionMorphAccessor.bufferView = gltfModel.bufferViews.size(); + positionMorphAccessor.componentType = TINYGLTF_COMPONENT_TYPE_FLOAT; + positionMorphAccessor.count = lod.parts[i].num_vertices; + positionMorphAccessor.type = TINYGLTF_TYPE_VEC3; + + auto &positionMorphBufferView = gltfModel.bufferViews.emplace_back(); + positionMorphBufferView.name = "Part " + std::to_string(i) + " " + shape.name + " Morph Position Buffer View"; + positionMorphBufferView.buffer = gltfModel.buffers.size(); + positionMorphBufferView.target = TINYGLTF_TARGET_ELEMENT_ARRAY_BUFFER; + + auto &positionMorphBuffer = gltfModel.buffers.emplace_back(); + positionMorphBuffer.name = "Part " + std::to_string(i) + " " + shape.name + " Morph Position Buffer"; + positionMorphBuffer.data.resize(lod.parts[i].num_vertices * sizeof(Vertex)); + memcpy(positionMorphBuffer.data.data(), shape.morphed_vertices, positionMorphBuffer.data.size()); + + positionMorphBufferView.byteLength = positionMorphBuffer.data.size(); + positionMorphBufferView.byteStride = sizeof(Vertex); + } + + gltfModel.meshes[mesh_offset + j].extras = + tinygltf::Value(std::map{{"targetNames", tinygltf::Value(morphNames)}}); + } + } + + mesh_offset += part.num_submeshes; } auto &scene = gltfModel.scenes.emplace_back();