- Home /
Material optimization/organization
I have a lot (over 100) of models that the user can add to a scene. Most of these models share generic metal materials to reduce the amount of different materials I'd have to modify to change all of them. However, I want all my models to use different normal maps. The base materials for all of these models will still be the same, steel, brass, gold, iron etc.. One way to do this would be copying the generic materials for each and every model and then assign the desired normal maps, but that'd result in a near endless amount of materials to modify if i want to make a small change in the overall iron shader.
Is there any way to make materials derive from another material, sort of like a hierarchy? I am targeting computers, so draw calls isn't the biggest issue
Answer by KpjComp · Oct 16, 2014 at 01:28 AM
Yes, I've knocked up a script to get you started. This currently allows you to alter the Textures, but you can modify to add other stuff you want to override. Basically set a Source Material that you want to descend from, and a destination material you want to save to, you can then override the textures for Diffuse, Normal and ReflectionCube.
Hopefully this will help, ps it also works in editor mode. Basically attach this script to an EmptyObject and maybe say called it MaterialBuilder or something like.
using UnityEngine;
using System.Collections;
[ExecuteInEditMode]
public class MaterialBuilder : MonoBehaviour {
public Material Source;
public Material Dest;
public Texture DiffuseTexture;
public Texture NormalMap;
public Texture ReflectionCubemap;
void updateMat() {
if (Source == null) return;
if (Dest == null) return;
Dest.CopyPropertiesFromMaterial(Source);
if (DiffuseTexture != null) {
Dest.SetTexture("_MainTex", DiffuseTexture);
}
if (NormalMap != null) {
Dest.SetTexture("_BumpMap", NormalMap);
}
if (ReflectionCubemap != null) {
Dest.SetTexture("_Cube", ReflectionCubemap);
}
}
// Use this for initialization
void Start () {
updateMat();
}
// Update is called once per frame
#if UNITY_EDITOR
void Update () {
if(Application.isEditor) {
updateMat();
}
}
#endif
}
Thanks, definitely a shot in the right direction, however this leaves me with a big list of empty GameObjects. Won't that hurt performance?
I believe Empty games objects should have $$anonymous$$imal effect on performance, the only event here that will get called a lot would be the Update, if you don't $$anonymous$$d not seeing the changes in the Editor you could even maybe leave this out, and then Start is only called once. Or even better wrap the Update inside a #define, I'll actually modify my script to do that, so that BUILD version won't have this unnecessary check.
Your answer
