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. Dismiss Notice

How to change the swap the color of a shape within a texture?

Discussion in 'Shaders' started by Notserp56, Oct 24, 2016.

  1. Notserp56

    Notserp56

    Joined:
    Jun 6, 2013
    Posts:
    10
    I know next to nothing about shaders, so I'm not clear on how to do this. There is a single black shape on a white background. I want to change the color of the black shape and the color of the white background separately.

    e.g. a black square on a white background could become a blue square on a red background

    Is there an efficient way to do this?

    to be clear I am talking about changing the colors of individual areas of a material
     
  2. Namey5

    Namey5

    Joined:
    Jul 5, 2013
    Posts:
    188
    Is there a particular shader as a basis you were planning on using this with, i.e. Standard, Unlit, etc.
     
  3. Notserp56

    Notserp56

    Joined:
    Jun 6, 2013
    Posts:
    10
    My knowledge of shaders is very limited, but unlit should be sufficient.
     
  4. eXonius

    eXonius

    Joined:
    Feb 2, 2016
    Posts:
    207
    It should be as simple as modifying whatever shader you're using now to offset the resulting color.

    For example if you're using the shader in this example named "Unlit/NewUnlitShader" https://docs.unity3d.com/Manual/SL-VertexFragmentShaderExamples.html

    Then before "return col;" in the fragment shader you do something like:

    col = fixed4(col.r + offset, col.g + offset, col.b + offset, col.a);

    You probably want to do something different with the color channels than just add an offset but it's just an example.

    If your shader does something more with the pixels than just read the color from a texture then you probably want to add your offset right after reading the color from the texture so that light calculations and other stuff happens after.

    In the example above the line "fixed4 col = tex2D(_MainTex, i.uv);" is the line that reads the color from a texture.
     
    Last edited: Oct 25, 2016