- Home /
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
Answer by Seth-Bergman · Nov 14, 2012 at 09:40 PM
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!
Answer by tmdchi · Nov 13, 2012 at 12:34 AM
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.
Of course you can always duplicate the geometry, scale it up a bit, and assign a particles-multiply shader to it with your ambient occlusion as texture. But results may be irregular.
Both of those are possible... What about a custom shader? I know that there is a legacy shader that accepts lightmaps, but I don't want the model to be immune to lighting. Anyone have a custom shader they can pull out of their butt?
A custom shader would still require a texture for the lightmap, so probably you'll be again stuck to one material per object. Unless there's a way to change the reference to the texture, based on the object. $$anonymous$$aybe you're able to configure the material inside the function OnWillRenderObject() ? Just a thought.
All right thank you for your help. It's not that big of a deal I guess. I'm just a fan of making an efficient game. Thanks for your help -YA
Answer by spiceboy9994 · Nov 14, 2012 at 08:56 PM
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.
Thank you, my friend! $$anonymous$$y problem here is, invariably, the Ambient Occlusion maps. They are layered onto whatever Diffuse texture I have and add Ambient Occlusion to the model, giving it a much more realistic look and feel. So although your solution is good for anyone that wants to randomize textures, it doesn't quite fit the glove in this situation. Thank you for your contribution though! This is sure to help out anyone that might stumble upon this thread.