vkmEngine 1.0.0
A C++ game engine · vkmengine.com
Loading...
Searching...
No Matches
Vkm::Engine::AssetSerializer Namespace Reference

Serialize / deserialize the asset graph referenced by a Scene. More...

Classes

struct  EntityAssetRefs
 Every asset reference a set of entities holds, as an assets block would name it. More...

Enumerations

enum class  LoadMode { Create , Reload }
 What loadAssets does about a name resources already holds. More...

Functions

void collectAssetRefs (const Scene &scene, EntityId id, const ResourceManager &resources, EntityAssetRefs &refs)
 Append every asset reference id holds to refs.
nlohmann::json saveAssetsForEntities (const Scene &scene, const std::vector< EntityId > &entities, const ResourceManager &resources)
 The assets block for a chosen set of entities.
nlohmann::json saveAssetsForScene (const Scene &scene, const ResourceManager &resources)
 The assets block for every entity in scene.
nlohmann::json saveAllAssets (const ResourceManager &resources)
 The assets block for everything resources holds, referenced or not.
bool loadAssets (const nlohmann::json &assetsJson, ResourceManager &resources, LoadMode mode=LoadMode::Create)
 Recreate the assets a document names into resources.
size_t dropAssetsNotIn (const nlohmann::json &assetsJson, ResourceManager &resources)
 Drop every asset in resources that assetsJson does not name.
void applyInline (const nlohmann::json &source, MaterialAsset &target, const ResourceManager &resources)
 Apply an "inline" material descriptor (kind=="inline") to a freshly constructed MaterialAsset.
nlohmann::json materialToInline (const MaterialAsset &material, const ResourceManager &resources)
 Build a material's canonical "inline" source descriptor: PBR scalars plus texture refs by name.

Detailed Description

Serialize / deserialize the asset graph referenced by a Scene.

Saves name every asset the scene references, one section per kind keyed like the library's directories; an unnamed asset has no serializable identity and is skipped. Loads resolve names through the asset library - the cooked file, else the recipe through the AssetFactory seam - skipping names ResourceManager already holds; a name neither serves stays unresolved. Sections load in ASSET_DEPENDENCY_ORDER, so textures land before the materials that resolve them.

Enumeration Type Documentation

◆ LoadMode

What loadAssets does about a name resources already holds.

Enumerator
Create 

Leave it alone.

What a load wants: a name not held is built; a held one already is what the document describes.

Reload 

Rebuild a material's contents in place, keeping its handle and name; leave every other kind alone, as Create does.

For restoring a snapshot (see PlaySnapshot). In place, so handles already held still name the material (see ResourceManager::swapValue). Materials alone: their rebuild reads a small recipe, where other kinds would re-read cooked data, so those keep edits made during the session.

Function Documentation

◆ collectAssetRefs()

void Vkm::Engine::AssetSerializer::collectAssetRefs ( const Scene & scene,
EntityId id,
const ResourceManager & resources,
EntityAssetRefs & refs )

Append every asset reference id holds to refs.

The one walk over an entity's references: a field that names an asset is seen here or nowhere.

Parameters
sceneScene holding the entity.
idEntity to walk; alive.
resourcesThe asset graph the entity's handles index into.
refsWhere the references are appended; duplicates are kept.

◆ saveAssetsForEntities()

nlohmann::json Vkm::Engine::AssetSerializer::saveAssetsForEntities ( const Scene & scene,
const std::vector< EntityId > & entities,
const ResourceManager & resources )

The assets block for a chosen set of entities.

What a prefab needs: its file lists the assets its subtree names, not the whole scene's.

Parameters
sceneScene holding the entities.
entitiesThe entities to walk; must be alive.
resourcesResolves each handle to the asset it names.
Returns
One array per asset kind, keyed by its VKM_ASSET_KINDS directory name.

◆ saveAssetsForScene()

nlohmann::json Vkm::Engine::AssetSerializer::saveAssetsForScene ( const Scene & scene,
const ResourceManager & resources )

The assets block for every entity in scene.

Includes entities inside prefab instances, though the prefab file has its own block: an instance may override a Mesh or a Decal with an asset the prefab never names.

Parameters
sceneScene to walk.
resourcesResolves each handle to the asset it names.
Returns
An object with the same section keys loadAssets reads.

◆ saveAllAssets()

nlohmann::json Vkm::Engine::AssetSerializer::saveAllAssets ( const ResourceManager & resources)

The assets block for everything resources holds, referenced or not.

Unlike saveAssetsForScene, also lists assets not yet assigned to anything, which restoring a whole graph needs (see PlaySnapshot). Hidden and unnamed assets are skipped: a private preview asset is not the author's, and loadAssets finds by name.

Parameters
resourcesThe asset graph to enumerate.
Returns
An object with the same section keys loadAssets reads.

◆ loadAssets()

bool Vkm::Engine::AssetSerializer::loadAssets ( const nlohmann::json & assetsJson,
ResourceManager & resources,
LoadMode mode = LoadMode::Create )

Recreate the assets a document names into resources.

Parameters
assetsJsonAn assets block, from any of the save functions above.
resourcesThe asset graph to build into.
modeWhat to do about a name the graph already holds; see LoadMode.
Returns
False if the block was not an object; per-asset failures are logged and skipped.

◆ dropAssetsNotIn()

size_t Vkm::Engine::AssetSerializer::dropAssetsNotIn ( const nlohmann::json & assetsJson,
ResourceManager & resources )

Drop every asset in resources that assetsJson does not name.

The other half of a restore: loadAssets leaves alone what the document does not name, and a session that generates its world creates assets the document has never heard of. Only the kinds saveAllAssets writes are considered, so never a font. Hidden assets are kept: save filters them, so their absence says nothing.

Parameters
assetsJsonA document from saveAllAssets().
resourcesGraph to prune.
Returns
How many assets were removed.

◆ applyInline()

void Vkm::Engine::AssetSerializer::applyInline ( const nlohmann::json & source,
MaterialAsset & target,
const ResourceManager & resources )

Apply an "inline" material descriptor (kind=="inline") to a freshly constructed MaterialAsset.

A missing key keeps the field's value; a wrong-typed value throws, reported against the material.

Parameters
sourceThe descriptor, as materialToInline writes it.
targetThe material to fill.
resourcesResolves texture names; one it does not hold leaves its slot as it was.

◆ materialToInline()

nlohmann::json Vkm::Engine::AssetSerializer::materialToInline ( const MaterialAsset & material,
const ResourceManager & resources )

Build a material's canonical "inline" source descriptor: PBR scalars plus texture refs by name.

The material's editable source of truth, its runtime form, and what its recipe file holds.

Parameters
materialThe material to describe.
resourcesResolves its texture handles to the names written.
Returns
The descriptor.