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

Replacing Colour with Texture

Discussion in 'Shaders' started by SMD-Indomitable, Mar 27, 2018.

  1. SMD-Indomitable

    SMD-Indomitable

    Joined:
    Mar 21, 2016
    Posts:
    9
    Hi, I am someone that hasn't touch shader programming yet, but I am willing to learn. I need a solution where I can replace colours in a texture with another texture. So for example, I have blue, green, red in a texture, I want to be able to replace the blue in that texture with a sea/water texture. Is there anyway I could do it?

    I have found some resources that I think is what I want, but I am unsure. I would really appreciate if someone could guide me. Sorry if this has been posted somewhere before, but I cant seems to find it.

    Source 1:

    From what I learnt about this video is that one texture is using the white portion of the splash texture and the other is using the black portion.

    Source 2:

    Since, I needed more than 2 texture, the same guy who made the video, made another video for 4 texture. From what I understand is that now instead of black and white, it uses the red, green, blue, alpha portion of the splash texture. So, the red in the splash texture will represent 1 texture, the green will represent another and so on provided that the splash texture contains red, green, blue and alpha colour. Am I right to say that?

    If so, is there a way where I can replace any colour I want with another texture without limiting to red, green, blue and alpha?
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,256
    Sure, but the benefit of using RGBA for masks is in handling transitions. It's easy to know how much red, green, blue, or alpha an image has since that's how the image data is stored. And as red fades out you can easily tell if you want to fade to green or blue or alpha, or maybe all three. Once you go to, say, red, yellow, green, teal, blue, and purple, you don't know if orange is red blending to green, or red blending to yellow.

    If you don't care about transitions you can use a single grey scale image to hold 256 masks. Just select the appropriate grey value and replace anything in the image that color with the texture of your choice.

    Otherwise if you really want to replace a specific color that isn't just the main RGBA, look into chroma key. That works by choosing a specific color and calculating how "far away" each pixel's color is from that specific color and replacing it if it's within some range and threshold of the specified color. You could do this for multiple arbitrary colors if you wish, but I suspect this won't work as well as you expect.

    Really if you want more than the 5 masks you get from RBGA (black can also be a mask if you set things up properly), I would suggest just using more than one mask texture. On desktop you can have two RGB (DXT1) textures for the same memory cost as a single RGBA (DXT5) texture.
     
  3. SMD-Indomitable

    SMD-Indomitable

    Joined:
    Mar 21, 2016
    Posts:
    9
    I see, thanks for the reply. One last doubt I want to clarify. If I were to use the tutorial, I can mask up to 4 textures right? Right now, I have a png which is a map. Lets say my water is blue, road is Red and ground is green. The shader in the tutorial should change my blue to water texture, red to road texture and green to ground texture, right?