Add a texture onto a material

What I’m doing here is spawning asteroids.

A script will pick a random model, and a random material (diffuse texture and it’s respective bump map), and assign it to the asteroid.
This gives me a lot of variety!

My only question is this:

Each model has its own respective Ambient Occlusion map. (It’s just a shadow map). I’d like to layer this on top of the diffuse material I am using through scripting, because just a random rock texture looks kind of sad haha

If I could multiply it, that would be ideal, but I don’t mind doing an alphaOver (I can convert the shadow map to a transparent png).

Thanks!- YA

You’re in luck!

http://answers.unity3d.com/questions/248758/custom-shader-to-add-bruises-to-character.html

This is what you want, it’s a shader someone very kindly helped me out with, to overlay a “bruises” diffuse w/alpha over a character’s base diffuse map…

Be sure to thumbs-up the answer in the link!

To make it happen I think you should have a unique material for each combination of texture+lightmap, which would be not much efficient if you have a lot of them.

The only way I know of having one material in many objects, having each object its own lightmap, is baking the lightmap from inside Unity. But I guess it’s not a viable option for you.

How about having a class with the list of possible textures and bumpmaps and then randomize from them?. Not sure about how the oclusion maps work, but here’s a small example. You just have to be sure that Main Textures at position [n], has its matching Bump Map at position [n]:

using UnityEngine;

public class MaterialManager: MonoBehaviour
{
	public Texture[] PossibleTextures;
	public Texture[] PossibleBumpMaps;

	void SpawnAsteroid()
	{
		int RandomIndex = Random.Range(0, PossibleTextures.Length);
		renderer.material.SetTexture("_MainTex") = PossibleTextures[RandomIndex];  
		renderer.material.SetTexture("_BumpMap") = PossibleBumpMaps[RandomIndex];
	}
}

Hope this helps.