7/11/2012

Material Replacer

During application development with Unity we often get intermediate 3d models or dummy models which are evolving over time with the rest of the application. This basically means that for every 3d model someone usually ends up importing it several times until the final version is done.
Looking at asset pipelines a lot recently (see previous posts) I found one general issue with this task.

Whenever a 3d model is updated in the Unity project updating materials references can become a hassle. If you are smart the correct Unity materials have been defined once and forever with the first import of the model. But if the material setup changed in the source file the material assignments have to be updated as well after import. Unity is flexible enough to search for materials with the same name on import before it decides to create a new one. But if the names are not identically Unity cannot do anything about it.

To aovid manually replacing materials one after another in arbitrary 3d models.I felt that a more general approach would help in many cases to avoid such work here and streamline the import. To achieve that it is necessary (and actually highly recommended anyway) to define a material name mapping which can be automatically applied to a 3d model in Unity. E.g. your designer creates a house which uses grey_concrete and a red_roof material. He probably also creates the corresponding materials in Unity as well which are then using special shaders, bump maps etc. These materials probably have a more general name since there might be a lot other materials with similar properties in your project. E.g. the corresponding materials would be fine_concrete_bright and roof_clapboard_red.

To make the mapping from source to target materials as easy as possible (without changing names) I have created an editor script which receives a prefab as input and then shows a list of all materials used. The list has an object field per material where the replacement material can be assigned. As soon as all material transitions have been defined pressing the "Apply" finally replaces the materials in the prefab itself. To automate this process its possible to save the defined transitions to a simple xml file once which can be loaded next time. Having these transitions set carefully correct material replacement is only one click away. :-)

Although merging individual transition files is not supported by the script it can be done easily manually if necessary. This would e.g. allow to store "all" material transitions from the modeling world to the Unity world.

To make this point clear: I think the smoothest strategy here is to use same material names in your modeling tools as well as Unity. But where this is not possible (e.g. external designers, different naming policys) this script approach comes in handy.

I´ve placed the script at UnifyCommunity with detailed instructions how to get started. I hope it saves others some time to focus on making better applications ;-)

PS: I´m wondering about adding an xml file location to the Model Import Preset script to automatically replace materials on import. Only issue here is that it would involve the automatic creation of a prefab here which might be unwanted in some cases...


EDIT: On request I have added the feature to select scene objects (instances of prefabs or not) as input to perform the replacement only on these objects "locally". The script at UnifyCommunity has been updated.

1 comment:

  1. Thnx for the script had real use for it, in order for it to work in Unity 4, I had to change line 294 to: PrefabUtility.ReplacePrefab( instance, mObject, ReplacePrefabOptions.ConnectToPrefab );



    ReplyDelete