From c556795ecfb59fb98a8f47f4614123dc27ac5f19 Mon Sep 17 00:00:00 2001 From: shenlei Date: Mon, 12 Oct 2026 18:18:14 +0900 Subject: [PATCH] Keep SetWeapon/AttachPart helper attachments across rebinds Helper attachments made outside the part slots were attached to PART_MAIN only and dropped whenever the parts were rebound (LOD level change, shape change, slot update). They are now recorded on the actor, attached to the drawn instance and re-attached after the slots. A slot taking the same bone drops the helper (last writer wins, as before); SetRace/SetModel clear them. LODs missing a helper's bone stay on PART_MAIN. Co-Authored-By: Claude Opus 5.5 --- src/eter_render/ActorInstance.cpp | 27 ++++++++++++++++++++++++--- src/eter_render/ActorInstance.h | 8 ++++++++ tests/ava2_render_3d_test.cpp | 16 ++++++++++++++++ 3 files changed, 48 insertions(+), 3 deletions(-) diff --git a/src/eter_render/ActorInstance.cpp b/src/eter_render/ActorInstance.cpp index bb1b8b40..74f91102 100644 --- a/src/eter_render/ActorInstance.cpp +++ b/src/eter_render/ActorInstance.cpp @@ -82,6 +82,17 @@ void ActorInstance::RebindParts() { if (m_hasSashScale) target->SetAttachmentScale(slot.bone, m_sashScale[0], m_sashScale[1], m_sashScale[2]); } } + for (const auto& extra : m_extraAttachments) { + if (extra.bone.empty()) target->AttachPart(extra.model); + else target->AttachModel(extra.bone, extra.model); + } +} + +void ActorInstance::DropExtraAttachment(const std::string& bone) { + if (bone.empty()) return; + m_extraAttachments.erase(std::remove_if(m_extraAttachments.begin(), m_extraAttachments.end(), + [&](const SExtraAttachment& e) { return e.bone == bone; }), + m_extraAttachments.end()); } Gr2ModelInstance* ActorInstance::DrawModel() const { @@ -105,6 +116,8 @@ bool ActorInstance::LodHasAttachBones(const Gr2ModelInstance& lod) const { const auto& slot = m_parts[part]; if (slot.model && !slot.bone.empty() && !HasBone(lod, slot.bone)) return false; } + for (const auto& extra : m_extraAttachments) + if (!extra.bone.empty() && !HasBone(lod, extra.bone)) return false; static const std::string kSaddle = "saddle"; return !HasBone(*m_model, kSaddle) || HasBone(lod, kSaddle); } @@ -161,6 +174,7 @@ bool ActorInstance::SetRace(uint32_t race) { // memset(m_adwPartItemID) + CGraphicThingInstance::Clear(): every part model is dropped. std::fill(std::begin(m_partItemID), std::end(m_partItemID), 0u); for (auto& slot : m_parts) slot = SPartSlot{}; + m_extraAttachments.clear(); m_model.reset(); m_modelPath.clear(); m_shapeFile.clear(); @@ -197,7 +211,6 @@ void ActorInstance::SetShape(uint32_t shape, float specular) { const RaceData::Shape* pkShape = raceData->FindShape(shape); if (pkShape) { const std::string& file = pkShape->model.empty() ? raceData->GetBaseModelFileName() : pkShape->model; - // The _lod_01.._03 LOD things are not ported: LOD 0 always draws. auto model = LoadPartModel(file); m_mainSkins.clear(); for (const auto& skin : pkShape->skins) m_mainSkins.emplace_back(skin.srcFileName, skin.dstFileName); @@ -242,6 +255,7 @@ void ActorInstance::AttachWeaponPart(uint32_t part, const SPartItem& item) { m_parts[part].model = LoadPartModel(item.model); m_parts[part].file = item.model; m_parts[part].bone = bone; + DropExtraAttachment(bone); // USE_WEAPON_SPECULAR sphere map and CWeaponTrace are not ported. } @@ -271,6 +285,7 @@ void ActorInstance::AttachSash(const SPartItem* item, float specular) { m_parts[RaceData::PART_SASH].model = LoadPartModel(item->model); m_parts[RaceData::PART_SASH].file = item->model; m_parts[RaceData::PART_SASH].bone = "Bip01 Spine2"; + DropExtraAttachment(m_parts[RaceData::PART_SASH].bone); } RebindParts(); } @@ -321,6 +336,7 @@ bool ActorInstance::SetModel(const std::string& modelPath) { if (!m_model) return false; m_modelPath = modelPath; m_mainSkins.clear(); + m_extraAttachments.clear(); // a fresh instance carries none of the old attachments ResetLodModel(); return true; } @@ -337,7 +353,10 @@ bool ActorInstance::SetWeapon(const std::string& weaponPath, const std::string& } } if (!weaponModel) return false; - m_model->AttachModel(boneName, std::move(weaponModel)); + std::shared_ptr weapon(std::move(weaponModel)); + DropExtraAttachment(boneName); + m_extraAttachments.push_back({boneName, weapon}); + DrawModel()->AttachModel(boneName, std::move(weapon)); return true; } @@ -347,7 +366,9 @@ bool ActorInstance::AttachPart(const std::string& partPath) { if (!std::filesystem::exists(resolved)) return false; auto partModel = Gr2ModelInstance::Load(resolved); if (!partModel) return false; - m_model->AttachPart(std::move(partModel)); + std::shared_ptr part(std::move(partModel)); + m_extraAttachments.push_back({std::string(), part}); + DrawModel()->AttachPart(std::move(part)); return true; } diff --git a/src/eter_render/ActorInstance.h b/src/eter_render/ActorInstance.h index b1d81ece..cf2dccbc 100644 --- a/src/eter_render/ActorInstance.h +++ b/src/eter_render/ActorInstance.h @@ -246,6 +246,14 @@ private: uint32_t m_hair = 0; RaceData* m_raceData = nullptr; SPartSlot m_parts[kPartCount]; + // SetWeapon/AttachPart helpers outside the part slots, re-attached on every rebind after the + // slots (empty bone: skinned part). A slot taking the same bone drops the helper. + struct SExtraAttachment { + std::string bone; + std::shared_ptr model; + }; + std::vector m_extraAttachments; + void DropExtraAttachment(const std::string& bone); uint32_t m_partItemID[kPartCount] = {}; bool m_hasSashScale = false; float m_sashScale[3] = {1.0f, 1.0f, 1.0f}; diff --git a/tests/ava2_render_3d_test.cpp b/tests/ava2_render_3d_test.cpp index 5cc7641c..2ba3af6f 100644 --- a/tests/ava2_render_3d_test.cpp +++ b/tests/ava2_render_3d_test.cpp @@ -844,6 +844,22 @@ int main(int argc, char** argv) { assert(actor.GetLODAniFPS()==30 && actor.GetLODLevel()==0); assert(actor.GetDrawModel()->GetFilePath().find("_lod_03.gr2")!=std::string::npos); assert(!actor.BuildDrawCommands().empty()); + // The SetWeapon helper attachment (outside the part slots) follows the LOD instance. + { + auto weaponOnly = ava2::Gr2ModelInstance::Load(weapon_path); + assert(weaponOnly); + std::set weaponTextures; + for (const auto& d : weaponOnly->BuildDrawCommands()) weaponTextures.insert(d.texture0); + auto drawsWeapon = [&]() { + for (const auto& d : actor.BuildDrawCommands()) if (weaponTextures.count(d.texture0)) return true; + return false; + }; + assert(!weaponTextures.empty() && drawsWeapon() && "weapon must stay attached on the LOD instance"); + actor.UpdateLODLevel(0,200,-300,0,0); + assert(actor.GetDrawModel()==actor.GetModel() && drawsWeapon()); + actor.UpdateLODLevel(0,200,-4900,2000,0); + assert(actor.GetLODLevel()==0 && drawsWeapon()); + } float lodBone[16]; assert(actor.GetBoneWorldTransform("Bip01 Spine",lodBone)); const auto poseBeforeLod=actor.GetDrawModel()->GetImpl()->GetPoseEvaluationCount();