How to save procedurally generated model as asset?

How do you go about saving a procedurally generated model as a project Asset?
I managed to use this code to create the model and save it.

private void CreateModel()
{
    var model = Content.CreateVirtualAsset<Model>();
    model.SetupLODs(new[] { 1 });
    UpdateMesh(model.LODs[0].Meshes[0]);
    Editor.SaveJsonAsset(Globals.ProjectContentFolder + "/model.flax", model);
}

But clicking on the model asset shows an infinite load in the editor.

I see there is Editor.CreateAsset(NewAssetType) but the enum for asset type does not include model.

Any thoughts?

You could use model.Save(true, path) to save the generated model to the file.

1 Like

Thank you very much!

Hi!

I have a related question about saving procedurally generated models.

I’m currently experimenting with Flax for a city-building game where some geometry would be created by the player at runtime. For example, roads would initially be generated as virtual Model assets while the player is designing them, and ideally I would like to persist those models once the player validates the construction.

I noticed that ModelBase::Save() appears to support saving a virtual model when withMeshDataFromGpu is enabled. However, Save() is only available when USE_EDITOR is enabled.

I expect there is a good reason for this restriction, so before designing my system around virtual models I would like to understand it better.

Is saving a generated Model as a regular Flax asset intentionally unsupported at runtime because of the asset/cooking pipeline or platform constraints (for example consoles), or is it mainly an API/build configuration limitation?

More generally, what would be the recommended approach in Flax for a game where players can create persistent procedural geometry at runtime (roads, custom buildings, etc.)?

Should such geometry remain as virtual assets and be reconstructed from game/save data when loading the city, or is there another mechanism intended for this use case?

Thanks!

Is saving a generated Model as a regular Flax asset intentionally unsupported at runtime because of the asset/cooking pipeline or platform constraints (for example consoles), or is it mainly an API/build configuration limitation?

Yes, on most platforms game data is read-only so we didn’t put much editor tooling into the final game build. Of course, you can try to compile custom engine fork with more stuff exposed.

I think you can use custom files (eg. raw memory bytes) saved on some temp folder and build virtual models from it when loading. Does this solution work for you? Or do you need more APIs exposed to game runtime?

Thanks, that clarifies the reason for the limitation. To be honest, we kind of suspected that was the reason, but we weren’t sure. :slight_smile:

For now, our plan would probably be to store the procedural definition of player-created objects (roads, buildings, etc.) in our own save format, and recreate the corresponding virtual models when loading a game. We are also considering storing the generated mesh data as a cache, so that complex geometry would not necessarily have to be regenerated every time a game is loaded.

In that case, the procedural data would remain the authoritative representation, while the cached mesh data would only be an optimization and could be regenerated when necessary. This seems like a good fit for the custom-files/virtual-model approach you suggested. If you see any drawbacks with this approach in Flax, or if there is a more idiomatic way to handle this kind of runtime-generated content, we would be very interested to hear your thoughts. :slight_smile:

Before answering your question about other APIs that could be useful to us at runtime, I would also like to understand one related part of the pipeline.

Does Flax already have an internal API for generating/simplifying LODs from a Model or Mesh, for example the same system used when importing model assets?

Model::SetupLODs() seems to let us define the LOD structure, but as far as I understand it does not generate the lower-detail meshes themselves.

If such a LOD generation API exists, is it currently editor-only as well?

For procedurally generated models at runtime, such as roads or buildings, having access to both model persistence and LOD generation could be very useful!

More generally, I was also wondering whether it could make sense in the future to expose some of the asset creation/saving APIs to runtime builds on platforms where writing game data is possible, rather than making them strictly editor-only. I realize this may be much easier said than done, especially if parts of the asset pipeline depend on editor-only tooling or libraries, but it could be useful for games where players can create persistent content at runtime.

Besides procedural content, I could imagine this being useful for modding as well. For example, a game could potentially import or generate new assets after it has been shipped, whether they come from player-created content, mods, or additional content distributed by the developers themselves. I’m not sure how well this fits with Flax’s asset/content pipeline, though, so I would be interested to know your thoughts on whether exposing some of these APIs selectively on platforms that support writable game data would be practical.

We use mesh optimizer library, which is great one but it’s hidden under ModelTool for editor-only geometry generation. I think you should be able to use it just like we did as it’s simple to integrate into C++ codebase.

I’ll add some todo tasks regarding procedural content generation at runtime to be improved in the future. Thanks for your feedback!

1 Like