Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Draw two materials on an object.

Discussion in 'Scripting' started by Samazure, Jun 10, 2020.

  1. Samazure

    Samazure

    Joined:
    Mar 31, 2013
    Posts:
    7
    Hello, I came with a question about materials. Specifically, how to I display two materials on the same object, one on top of the other?

    What I want to have a "normal" material of whatever the object is made. And then another material like "dirt". The second material of dirt could have holes in it and from there you could see the original material.

    I know I could externally create a texture to simulate this, but then it would be fixed. I would like the holes to be random in size or even shape. Maybe even "remove" part of the dirt during runtime.

    A workaround I've thought about is doing it the other way, instead of having a material and removing parts of it to show the material below, I could have only the base material and then "draw" the second material on top of the first one... but I don't know how to do this either.

    Any help would be appreciated.
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    You could accomplish this with something like a dissolve shader as per this tutorial:


    You could either try to get the dissolve shader to hide one texture and show another instead of alpha clipping, or you could just dissolve and have a slightly smaller scaled model underneath that will become visible.
     
  3. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    The problem you are having is this: a "material" in Unity is actually two things: a "shader" (which is a small, very fast program that calculates the color of a pixel based on it's position and some other data like textures) and a bunch of pre-configured data (the settings or configuration) that tells the shader the details of how to work.
    You can place multiple materials on a single mesh (all you need to do is add another material slot), but that will usually only have two effects:
    - take twice as long to render
    - unless the two materials are somehow (and this is difficult) co-ordinated, the shader that executes last will draw over whatever was rendered before.

    So your best bet is create the shader yourself. The bad part about that is that shader programming can be really difficult and much more involved than other Unity scripting.

    Unless you feel ready to create your own shader(s), your best bet is some more advanced shaders in the Asset Store, or perhaps look at substances.
     
  4. tawdry

    tawdry

    Joined:
    Sep 3, 2014
    Posts:
    1,356
    The standard shader can do this use the mask slot to define the holes and the second texture slot for the dirt texture.
     
  5. Samazure

    Samazure

    Joined:
    Mar 31, 2013
    Posts:
    7
    Thank you very much everyone. Guess I'll start to learn shader stuff.

    As a workaround in the mean time I used a copy of the object scaled slightly bigger with the "dirt" texture, then used both a raycast and texture set pixels to make transparent some areas in the one with the dirt. It might be slow and not the most recommended solution, but it is the workaround I found while I learn shaders. The advantages are I can change it while the game is runnings and if I don't use a backup for the texture, the changes are permanent (which sometimes you might want them to be).

    And in case someone else in the future finds this through google, I started learning shaders with this as a starting point.