ADD: object adding system ((RT shadow error)
This commit is contained in:
@@ -34,6 +34,9 @@ void SceneManager::update_scene()
|
||||
{
|
||||
auto start = std::chrono::system_clock::now();
|
||||
|
||||
// Release any GLTF assets that were scheduled for safe destruction after GPU idle.
|
||||
pendingGLTFRelease.clear();
|
||||
|
||||
mainDrawContext.OpaqueSurfaces.clear();
|
||||
mainDrawContext.TransparentSurfaces.clear();
|
||||
mainDrawContext.nextID = 1;
|
||||
@@ -78,12 +81,30 @@ void SceneManager::update_scene()
|
||||
}
|
||||
}
|
||||
|
||||
auto tagOwner = [&](RenderObject::OwnerType type, const std::string &name,
|
||||
size_t opaqueBegin, size_t transpBegin)
|
||||
{
|
||||
for (size_t i = opaqueBegin; i < mainDrawContext.OpaqueSurfaces.size(); ++i)
|
||||
{
|
||||
mainDrawContext.OpaqueSurfaces[i].ownerType = type;
|
||||
mainDrawContext.OpaqueSurfaces[i].ownerName = name;
|
||||
}
|
||||
for (size_t i = transpBegin; i < mainDrawContext.TransparentSurfaces.size(); ++i)
|
||||
{
|
||||
mainDrawContext.TransparentSurfaces[i].ownerType = type;
|
||||
mainDrawContext.TransparentSurfaces[i].ownerName = name;
|
||||
}
|
||||
};
|
||||
|
||||
// Draw all loaded GLTF scenes (static world)
|
||||
for (auto &[name, scene] : loadedScenes)
|
||||
{
|
||||
if (scene)
|
||||
{
|
||||
const size_t opaqueStart = mainDrawContext.OpaqueSurfaces.size();
|
||||
const size_t transpStart = mainDrawContext.TransparentSurfaces.size();
|
||||
scene->Draw(glm::mat4{1.f}, mainDrawContext);
|
||||
tagOwner(RenderObject::OwnerType::StaticGLTF, name, opaqueStart, transpStart);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -93,7 +114,10 @@ void SceneManager::update_scene()
|
||||
const GLTFInstance &inst = kv.second;
|
||||
if (inst.scene)
|
||||
{
|
||||
const size_t opaqueStart = mainDrawContext.OpaqueSurfaces.size();
|
||||
const size_t transpStart = mainDrawContext.TransparentSurfaces.size();
|
||||
inst.scene->Draw(inst.transform, mainDrawContext);
|
||||
tagOwner(RenderObject::OwnerType::GLTFInstance, kv.first, opaqueStart, transpStart);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -123,6 +147,8 @@ void SceneManager::update_scene()
|
||||
obj.sourceMesh = inst.mesh.get();
|
||||
obj.surfaceIndex = surfaceIndex++;
|
||||
obj.objectID = mainDrawContext.nextID++;
|
||||
obj.ownerType = RenderObject::OwnerType::MeshInstance;
|
||||
obj.ownerName = kv.first;
|
||||
if (obj.material->passType == MaterialPass::Transparent)
|
||||
{
|
||||
mainDrawContext.TransparentSurfaces.push_back(obj);
|
||||
@@ -299,7 +325,17 @@ void SceneManager::addGLTFInstance(const std::string &name, std::shared_ptr<Load
|
||||
|
||||
bool SceneManager::removeGLTFInstance(const std::string &name)
|
||||
{
|
||||
return dynamicGLTFInstances.erase(name) > 0;
|
||||
auto it = dynamicGLTFInstances.find(name);
|
||||
if (it == dynamicGLTFInstances.end()) return false;
|
||||
|
||||
// Defer destruction until after the next frame fence (update_scene).
|
||||
if (it->second.scene)
|
||||
{
|
||||
pendingGLTFRelease.push_back(it->second.scene);
|
||||
}
|
||||
|
||||
dynamicGLTFInstances.erase(it);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool SceneManager::setGLTFInstanceTransform(const std::string &name, const glm::mat4 &transform)
|
||||
@@ -312,6 +348,13 @@ bool SceneManager::setGLTFInstanceTransform(const std::string &name, const glm::
|
||||
|
||||
void SceneManager::clearGLTFInstances()
|
||||
{
|
||||
for (auto &kv : dynamicGLTFInstances)
|
||||
{
|
||||
if (kv.second.scene)
|
||||
{
|
||||
pendingGLTFRelease.push_back(kv.second.scene);
|
||||
}
|
||||
}
|
||||
dynamicGLTFInstances.clear();
|
||||
}
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <glm/vec2.hpp>
|
||||
#include <string>
|
||||
|
||||
#include "scene/vk_loader.h"
|
||||
class EngineContext;
|
||||
@@ -28,8 +29,19 @@ struct RenderObject
|
||||
uint32_t surfaceIndex = 0;
|
||||
// Unique per-draw identifier for ID-buffer picking (0 = none).
|
||||
uint32_t objectID = 0;
|
||||
// Optional owning glTF scene for this draw (null for procedural/dynamic meshes).
|
||||
// Optional logical owner for editor/picking (instance name etc.).
|
||||
enum class OwnerType : uint8_t
|
||||
{
|
||||
None = 0,
|
||||
StaticGLTF, // loaded scene
|
||||
GLTFInstance, // runtime glTF instance with transform
|
||||
MeshInstance // dynamic primitive/mesh instance
|
||||
};
|
||||
OwnerType ownerType = OwnerType::None;
|
||||
std::string ownerName;
|
||||
// Optional owning glTF scene and node for this draw (null for procedural/dynamic meshes).
|
||||
LoadedGLTF *sourceScene = nullptr;
|
||||
Node *sourceNode = nullptr;
|
||||
};
|
||||
|
||||
struct DrawContext
|
||||
@@ -137,6 +149,9 @@ private:
|
||||
std::unordered_map<std::string, std::shared_ptr<Node> > loadedNodes;
|
||||
std::unordered_map<std::string, MeshInstance> dynamicMeshInstances;
|
||||
std::unordered_map<std::string, GLTFInstance> dynamicGLTFInstances;
|
||||
// Keep GLTF assets alive until after the next frame fence to avoid destroying
|
||||
// GPU resources that might still be in-flight.
|
||||
std::vector<std::shared_ptr<LoadedGLTF>> pendingGLTFRelease;
|
||||
|
||||
PickingDebug pickingDebug{};
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user