Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Multiple textures per material

Discussion in 'Editor & General Support' started by baylor, Jan 6, 2010.

  1. baylor

    baylor

    Joined:
    Oct 29, 2009
    Posts:
    25
    Hi, i'm new to Unity and fairly new to 3D. We're porting a 2D game and i have a noob question. Hopefully this is the correct forum (note: i also posted on the beta answers Web site but i wasn't sure how active or permanent that is)

    How does one make a material that has multiple textures? How does one change those textures at run time?

    Our game is a dress up/fashion game. There are ~35 layers/types of clothing (including tattoos and makeup). Many things are mounted (belts, hair, mustaches, glasses, etc.) but quite a few are not. The doll has 4 skinned meshes (body (divided into regions to allow different tattoos), upper (shirts, vests, bikini tops, etc.), lower (pants, shorts, socks, etc.) and over (dress, coat, cape, etc.)). Each mesh has a material. Different layers of clothing go in different parts of the mesh - bra is upper[1], undershirt is upper[2], necklace is upper[3], overshirt is upper[4], jacket is over[1], etc. We could, of course, use multiple skinned meshes but those get to be a real pain once you have several

    In T3D (where we prototyped the game), a material has a texture array and setting/changing one was done with the code
    myMaterial.diffuseMap[1] = "tshirt.png";
    myMaterial.diffuseMap[3] = "vest.png";
    i'm trying to find the Unity equivalent. i'm fairly sure Unity supports multiple textures per material (the skybox seems to use 6), i just haven't figured out the keywords to make it work
     
  2. antenna-tree

    antenna-tree

    Joined:
    Oct 30, 2005
    Posts:
    5,324
  3. baylor

    baylor

    Joined:
    Oct 29, 2009
    Posts:
    25
    Maybe i don't understand but the link talks about how to set the bump map, normal, etc. texture with what i assume is just one texture for each kind. In my case, there are multiple diffuse textures. More concretely, we've been trying to use the shader Transparent/Cutout/Diffuse (since Transparent/Diffuse caused the layers to be drawn incorrectly, with the shirt above the jacket). How would i find the correct phrase to use, for example, to set the third diffuse texture to draw for that material? i assume the following is incorrect:

    var vestImage : Texture;
    renderer.material.SetTexture("_MainTex_3", vestImage);

    Is there such a texture as MainTex_3? Is it something i'm supposed to add to the texture? Is it something that's created when i set it (and if so, how would i tell Unity to draw it after MainText_2)? Is this not possible with the Unity shaders and i need to write my own?

    Sorry, i'm new to this. i've read the Material API in the help files but still don't see how to do what we need
     
  4. Sycle

    Sycle

    Joined:
    Nov 24, 2009
    Posts:
    446
    The number (and type) of textures a material uses is defined by it's shader. Most (all?) of the default shaders only have one diffuse texture - you can tell because there's only one diffuse texture slot when you're setting up the material.

    If I understand you correctly, you're using materials that have several textures layered over each other (presumably with alpha maps to let the lower layers show through) but you can't do that with the built-in shaders. What you'd probably need is to write/edit a shader to support multiple textures in that way, then you'd be able to change them in script with something like renderer.material.SetTexture("_MainTex_3", vestImage); , as you wrote.

    Basically before you can set a MainTex_3 you need a shader that has a MainTex_3. Shaders kind of do my head in though, so I'm no help in that regard but hopefully it'll give you a place to start investigating.
     
  5. baylor

    baylor

    Joined:
    Oct 29, 2009
    Posts:
    25
    OK, so it's different from T3D that way. Rather than use the normal shaders, write my own, which Unity presumably allows. Cool. i don't know how to do that but now i know which help files to go read, so i'm making progress
     
  6. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Renderer.sharedMaterials / Renderer.materials (note the "s").

    --Eric
     
  7. The-Scripter

    The-Scripter

    Joined:
    Nov 10, 2009
    Posts:
    30
    If you only need the objects to display 1 texture at a time a standard shader will do that. Your list of textures has to go in the script, not the material. All you need to do is change the main (diffuse) texture of the material during the game, like this,

    Code (csharp):
    1. var yourTexture: Texture;
    2. //...
    3. yourObject.renderer.material.mainTexture = yourTexture;
    this is the same as

    Code (csharp):
    1. yourObject.renderer.material.SetTexture("_MainTex", yourTexture);
    You should probably use the second way so that you can use it to change the alpha texture as well. Using any variable that starts with "shared" should usually be avoided, as it will modify the imported file, not just the instance of it in the object, which I think will permantly change the material until you change it back.