载入中...
搜索中...
未找到
SceneLoader.h
浏览该文件的文档.
1#pragma once
2#include "common/Export.h"
4
5
6#include "common/Module.h"
7#include "common/Result.h"
8
9#include <functional>
10#include <memory>
11#include <mutex>
12#include <string>
13#include <unordered_map>
14#include <unordered_set>
15#include <vector>
16
17struct aiScene;
18struct aiNode;
19struct aiMesh;
20
21namespace eve {
22namespace animation {
23class AnimSkeleton;
24class AnimClip;
25} // namespace animation
26namespace graphics {
27class Graphics;
28class Renderable3D;
29class Light3D;
30class Camera3D;
31class Texture;
32class Mesh;
33} // namespace graphics
34namespace model3d {
35class ModelData;
36} // namespace model3d
37namespace scene {
38struct NodeDesc;
39class SceneHost;
40} // namespace scene
41namespace thread {
42class ThreadPool;
43}
44
45namespace sceneloader {
46
63 enum class Action { Add, Remove, Modify, Move };
64
66 std::string id; // GameObject id (scene-object id).
67 std::string parent; // new parent id (Add / Move only).
68};
69
71struct SceneDiff {
72 std::vector<SceneDiffEntry> entries;
73 int added = 0;
74 int removed = 0;
75 int modified = 0;
76 int moved = 0;
77
79 bool empty() const { return entries.empty(); }
80};
81
85 int nodeCount = 0;
87 std::vector<std::string> warnings;
88 std::vector<std::string> sockets;
89 std::vector<std::string> collisions;
90};
91
99struct MeshSlot {
100 const aiScene *scene = nullptr;
101 const aiMesh *mesh = nullptr;
102 unsigned materialIndex = 0;
103};
104using MeshSlotMap = std::unordered_map<std::string, std::vector<MeshSlot>>;
105
117 bool triangulate = true;
120 bool flipUVs = true;
123 bool sharedMeshes = true;
125 bool mipmaps = true;
127 bool importLights = true;
129 bool importCameras = false;
131 bool importAnimations = true;
132};
133
155public:
157
159 SceneLoader();
161 ~SceneLoader() override;
162
164 scene::SceneHost *load(const std::string &path, bool linkRenderables = true,
165 const LoadOptions &options = {});
167 scene::SceneHost *load(const std::string &path, const LoadOptions &options);
169 scene::SceneHost *loadPreset(const std::string &path, const std::string &preset);
170
179 [[nodiscard("scene reload outcome must be checked")]] eve::Result<bool> reload(const std::string &path,
180 SceneDiff *out = nullptr,
181 const LoadOptions &options = {});
182
184 SceneDiff diff(const std::string &path);
186 [[nodiscard]] eve::Result<SceneInspection> inspect(const std::string &path,
187 const LoadOptions &options = {});
188
190 scene::SceneHost *host(const std::string &path);
191
193 void unload(const std::string &path);
194
196 int nodeCount(const std::string &path);
198 bool loaded(const std::string &path);
199
200 // ---- async loading (decode off-thread, mount on the main thread) ----
201
208 bool loadAsync(const std::string &path, const LoadOptions &options = {},
210 std::function<void(scene::SceneHost *)> done = nullptr);
211
213 bool loadAsyncDefault(const std::string &path) { return loadAsync(path); }
215 bool loadAsyncPreset(const std::string &path, const std::string &preset);
216
221 int pollAsync();
222
224 bool prewarmAsync(const std::string &path, const LoadOptions &options = {});
226 bool prewarm(const std::string &path) { return prewarmAsync(path); }
228 bool prewarmed(const std::string &path) const;
230 void clearPrewarm(const std::string &path);
231
233 int pendingAsyncCount() const;
234
236 int warningCount(const std::string &path) const;
238 std::string warning(const std::string &path, int index) const;
240 bool setLod(const std::string &path, int level);
242 int socketCount(const std::string &path) const;
244 std::string socketName(const std::string &path, int index) const;
246 int collisionCount(const std::string &path) const;
248 std::string collisionName(const std::string &path, int index) const;
249
250 // ---- imported scene extras ----
251
253 int lightCount(const std::string &path);
255 graphics::Light3D *light(const std::string &path, int index);
256
258 int cameraCount(const std::string &path);
260 graphics::Camera3D *camera(const std::string &path, int index);
261
263 int animationCount(const std::string &path);
265 animation::AnimSkeleton *skeleton(const std::string &path);
267 animation::AnimClip *clip(const std::string &path, int index);
268
269 // ---- pure tree helpers (no graphics required; unit-testable) ----
270
272 using TextureCache = std::unordered_map<std::string, graphics::Texture *>;
273 using MeshCache = std::unordered_map<const aiMesh *, graphics::Mesh *>;
275 struct CpuImage {
276 int w = 0;
277 int h = 0;
278 std::vector<uint8_t> rgba;
279 };
280 using CpuImageMap = std::unordered_map<std::string, CpuImage>;
281
283 static scene::NodeDesc buildNodeDesc(const aiScene *scene, MeshSlotMap *slotsOut = nullptr);
284
286 static SceneDiff diffTree(scene::SceneHost *host, const scene::NodeDesc &newRoot);
287
293 static bool applyTreeDiff(scene::SceneHost *host, const scene::NodeDesc &newRoot,
294 const SceneDiff &diff, graphics::Graphics *gfx,
295 const MeshSlotMap *slots);
296
302 static void fillSceneBounds(scene::SceneHost *host, const MeshSlotMap &slots);
303
304private:
305 struct Loaded {
306 std::string path;
307 scene::SceneHost *host = nullptr;
308 graphics::Graphics *gfx = nullptr;
310 std::vector<graphics::Light3D *> lights;
311 std::vector<graphics::Camera3D *> cameras;
312 animation::AnimSkeleton *skeleton = nullptr;
313 std::vector<animation::AnimClip *> clips;
314 };
315
316 struct DecodedScene {
318 DecodedScene();
320 ~DecodedScene();
322 DecodedScene(DecodedScene &&) noexcept;
324 DecodedScene &operator=(DecodedScene &&) noexcept;
325 DecodedScene(const DecodedScene &) = delete;
326 DecodedScene &operator=(const DecodedScene &) = delete;
327
328 std::string path;
329 model3d::ModelData *md = nullptr;
330 std::unique_ptr<scene::NodeDesc> root;
333 CpuImageMap cpuImages; // external textures pre-decoded off-thread
334 std::vector<graphics::Light3D *> lights;
335 std::vector<graphics::Camera3D *> cameras;
336 animation::AnimSkeleton *skeleton = nullptr;
337 std::vector<animation::AnimClip *> clips;
338 bool prewarmOnly = false;
340 std::function<void(scene::SceneHost *)> done;
341 };
342
343 bool decode(const std::string &path, const LoadOptions &options, DecodedScene *out);
344 scene::SceneHost *mount(DecodedScene &d);
346 void linkMeshNodes(scene::SceneHost *host, const MeshSlotMap &slots,
347 graphics::Graphics *gfx, const LoadOptions &options,
348 TextureCache &textures, MeshCache &shared,
349 const CpuImageMap *predecoded = nullptr);
351 void clearTextures();
353 std::string decodeErrorFor(const std::string &path) const;
354
355 std::unordered_map<std::string, Loaded> scenes_;
356 std::unordered_map<std::string, DecodedScene> prewarmed_;
357 TextureCache textures_;
358 std::shared_ptr<thread::ThreadPool> pool_;
359 std::vector<DecodedScene> pending_;
360 std::unordered_set<std::string> inFlight_;
361 // Internal async/decode diagnostics; public operations return structured Results.
362 std::unordered_map<std::string, std::string> lastErrors_;
363 std::unordered_map<std::string, std::vector<std::string>> warnings_;
364 mutable std::mutex pendingMu_;
365};
366
367} // namespace sceneloader
368} // namespace eve
float w
Definition AnimClip.cpp:738
int root
Definition AnimSmr.cpp:119
std::mutex mutex
#define EVENGINE_API_ORCHESTRATION
Definition Export.h:111
std::vector< eve::ProcgenProbeDesc > lights
float camera[2]
glm::vec4 clip
int h
bool done
int level
std::string path
Definition PlayHost.cpp:111
std::unordered_set< std::string > loaded
Definition Plugins.cpp:24
std::weak_ptr< PrimitiveScene > scene
float d
Move-only, checked operation results for the common layer.
Graphics * graphics
MigrationFunction function
ScriptStructure out
Light3D::Data * light
Definition Shadow.cpp:147
LocalPageCacheEntry slots[ShadowConfig::kLocalSlots]
Definition Shadow.cpp:159
const SquirrelValueOptions & options
Share string attribute map operations through the Windows Foundation DLL.
UIHostHandle host
uint32_t index
EVENGINE_API_FOUNDATION public API.
Definition Module.h:48
Move-only operation result carrying either a value or Status.
Definition Result.h:176
Keyframed skeletal animation clip (local TRS tracks per bone). Script type: AnimClip.
Definition AnimClip.h:145
3D bone hierarchy + bind-pose local TRS for skeletal animation. Independent of ik::Skeleton3D (FABRIK...
EVENGINE_API_BACKENDS public API.
Declarative 3D light. Collected by RenderSystem3D (max 8 per frame). type: "point" | "dir" | "spot" (...
Definition Light.h:190
ECS mount point for one scene graph (full scene or nested subtree root). Isomorphic to eve::ui::UIHos...
Definition SceneHost.h:89
Loads a 3D scene (glTF / OBJ / FBX / GLB ... via Assimp/medialoader) into the ECS scene tree.
std::unordered_map< const aiMesh *, graphics::Mesh * > MeshCache
bool prewarm(const std::string &path)
Script-friendly CPU prewarm using the default import preset.
std::unordered_map< std::string, CpuImage > CpuImageMap
bool loadAsyncDefault(const std::string &path)
Script-friendly async load using the default import preset.
std::unordered_map< std::string, graphics::Texture * > TextureCache
Caches shared across the loader: textures by source key, GPU meshes per aiMesh.
std::unordered_map< std::string, std::vector< MeshSlot > > MeshSlotMap
Build metadata (engine git commit, build time, third-party version).
Definition Build.cpp:16
Declarative scene-node description (build once / on dirty → flatten into SceneHost::Tree)....
Definition NodeDesc.h:19
Options controlling how a 3D scene file is decoded and mounted.
bool importLights
Import aiLight entries as graphics::Light3D entities.
bool mipmaps
Generate mipmaps + anisotropic filtering for imported textures.
bool sharedMeshes
Reuse one GPU mesh when several nodes reference the same aiMesh.
bool importAnimations
Import aiAnimation clips (skeleton + clips exposed via accessors).
bool importCameras
Import aiCamera entries as inactive graphics::Camera3D entities.
A GPU mesh reference for one Assimp mesh referenced by an aiNode. The loader uploads one Renderable3D...
Definition SceneLoader.h:99
SceneDiffEntry / SceneDiff
Definition SceneLoader.h:61
SceneDiff public API.
Definition SceneLoader.h:71
bool empty() const
Empty.
Definition SceneLoader.h:79
std::vector< SceneDiffEntry > entries
Definition SceneLoader.h:72
CPU-only import inspection result used by editor preflight.
Definition SceneLoader.h:83
std::vector< std::string > warnings
Definition SceneLoader.h:87
std::vector< std::string > collisions
Definition SceneLoader.h:89
std::vector< std::string > sockets
Definition SceneLoader.h:88
CPU-decoded texture (RGBA8) keyed by the same source+suffix key as TextureCache.