fix(editor): unload ghost preview model when path changes

setGhostPreview reused modelId 59999 for every preview, but loadModel
returns true without doing anything when the ID is already cached. So
selecting a new NPC kept the old ghost model in GPU memory and createInstance
used the stale model. Added M2Renderer::unloadModel public API and call it
from clearGhostPreview.
This commit is contained in:
Kelsi 2026-05-06 00:51:22 -07:00
parent ca630c4e87
commit 1c3307a0b6
3 changed files with 14 additions and 1 deletions

View file

@ -287,6 +287,8 @@ public:
bool hasModel(uint32_t modelId) const;
bool loadModel(const pipeline::M2Model& model, uint32_t modelId);
/** Force-remove a model and all its GPU resources. Caller must ensure no instances reference it. */
void unloadModel(uint32_t modelId);
/** Mark a loaded model as a spell effect (full-brightness particles, no collision). */
void markModelAsSpellEffect(uint32_t modelId);

View file

@ -543,6 +543,15 @@ void M2Renderer::cleanupUnusedModels() {
}
}
void M2Renderer::unloadModel(uint32_t modelId) {
auto it = models.find(modelId);
if (it == models.end()) return;
if (vkCtx_) vkDeviceWaitIdle(vkCtx_->getDevice());
destroyModelGPU(it->second);
models.erase(it);
modelUnusedSince_.erase(modelId);
}
VkTexture* M2Renderer::loadTexture(const std::string& path, uint32_t texFlags) {
constexpr uint64_t kFailedTextureRetryLookups = 512;
auto normalizeKey = [](std::string key) {

View file

@ -623,7 +623,9 @@ void EditorViewport::clearGhostPreview() {
ghostInstanceId_ = 0;
}
if (ghostModelId_ != 0 && m2Renderer_) {
// Don't unload the model — it might be used by placed objects too
// Ghost ID is reserved for previews only — safe to unload so a path
// change can re-load with the new model under the same ID.
m2Renderer_->unloadModel(ghostModelId_);
ghostModelId_ = 0;
ghostModelPath_.clear();
}