Persistent Dynamic Materials (Programming)

 

What

Recently I had to create a system to make materials remember several input parameters that are changed at runtime.
For infrastructure reasons, the project needed this to be implemented without involving any “Save Game” systems, to capture all parameters from existing dynamic material instances on scenes and restore them exactly like they were left when the application quit.
It had to serialize Scalar parameters, Vector parameter, and Texture2D reference parameters without care about saving or loading back any level or info about the actors using those material instances.

So I put together a plugin that captures and restores all dynamic materials of a scene that can later be restored, for persistent game worlds and interactive ArchViz projects.
The idea was to make it feel like a “save game” system, but without using any actual save game system with dependencies to the “Game World”.
There’s a “Capture Snapshots” and a “Restore from Snapshots” node that can be used to save and restore all runtime material states from a scene.

The materials must be “Dynamic Material Instances” to work. No workflow changes or changes to existing materials are required, the target meshes just have to properly use a Dynamic Material Instance to make it work.
Scalar Parameters, Vector Parameters, Texture Param2Ds are supported by the system. I decided to make it public and publish on Marketplace, Epic Games should have it available online in their store soon.


 

How To Use

This is extremely simple to use even for non programmers.

  • Step #1, your materials assigned to the meshes must be dynamic material instances, it’s an internal shader requirement of Unreal Engine due to how materials work.
    From within your Blueprint’s Constructor Script you can do something like this to create and apply a dynamic material instance to your mesh:
  • Step #2, the base material, the one you edit in Material Editor, it should contain dynamic parameters you’re willing to be changed at runtime and that the plugin will later record and restore recorded values from.
    A very simple base material as example. “Color” vector, “Diffuse” texture, “NormalMap” texture, “Metallic” param, and “Roughness” param, are all parameters from the material below that can be changed at runtime and restored back to their changed values by the plugin even after the application was closed and then later executed again:
  • Step #3, to capture or restore material data. You just need to execute a variation of these nodes to do so.
    Capture Material Snapshots” will record every dynamic material instances and their parameters in scene.
    Restore Materials from Snapshots” will revert them instantly back to the state they were captured:

 

Note

Keep in mind that this system doesn’t make distinction of “levels” or “maps”, this is by design. A material named “MyCoolestMatInstance01” in multiple levels will be treated as one same material to be captured and restored.


 

This is now published to Unreal’s Marketplace:
https://www.unrealengine.com/marketplace/en-US/slug/persistent-dynamic-materials