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

How to mask a material/texture?

Discussion in 'Shaders' started by AlienBoyGames, Feb 12, 2020.

  1. AlienBoyGames

    AlienBoyGames

    Joined:
    Feb 12, 2020
    Posts:
    17
    Hi-

    Does anyone know how to mask a material and replace the masked part with a color.

    Let's say I have a material on a 3D object, and I paint on it so now there are multiple colors. What I want to do is apply a white and black mask and remove parts of the color applied earlier.

    Here is an example. This is the normal texture/material.
    2.png

    This is the mask.
    1.png

    After applying the mask I want to remove the color and still have the default color of the material where the mask was applied.
    3.png


    I found a shader online but it made the masked parts transparent and removed the lighting.

    Thank you!
     
  2. tmcthee

    tmcthee

    Joined:
    Mar 8, 2013
    Posts:
    119
    You could use a lerp to do that effect.
    A lerp does a linear interpolation between two values using a third value as the interpolation weight.
    https://developer.download.nvidia.com/cg/lerp.html

    So you'd use your colour texture as one value. Then a colour property, in this case white, but it could just as easily be another texture or something. Then use the mask texture as the interpolation weight.

    Something like
    Code (CSharp):
    1. col = lerp( tex, white, mask);
    2. return col;
     
  3. jeshjesh

    jeshjesh

    Joined:
    Jul 7, 2019
    Posts:
    14
  4. IlisanVlad

    IlisanVlad

    Joined:
    Dec 3, 2017
    Posts:
    30
    Yep, this did it. Thank you :)
     
  5. IlisanVlad

    IlisanVlad

    Joined:
    Dec 3, 2017
    Posts:
    30
    I tried experimenting with the project and I got pretty close but I found the lerp method which did the trick.
     
  6. AlanMattano

    AlanMattano

    Joined:
    Aug 22, 2013
    Posts:
    1,500
    You can do that using the Unity shader graph
     
  7. FalseFeint

    FalseFeint

    Joined:
    Jul 21, 2022
    Posts:
    7
    Hi, I'm a complete beginner with unity and would like to do something similar to the OP. Could you explain with a little more detail where I would use the lerp function? I'm not sure if I have to write a custom shader or something else? For context, I'm trying to generate cucumbers that have been randomly peeled and therefore have their original skin texture replaced with a flesh color. I currently have a prefab of a cucumber with the default texture, a dream of what I want, and no idea what I'm doing :)