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

Shader with 4 textures, 4 colors and diffuse output?

Discussion in 'Shaders' started by xagustudios, Mar 14, 2022.

  1. xagustudios

    xagustudios

    Joined:
    Feb 6, 2017
    Posts:
    8
    Hi.

    I'm totally noob about shaders and trying to combine in a material 4 textures each one with it's own color. The goal for this is making a customizable outfit for the players so they can choose their own cloth design pattern in any color. Achieved to combine the 4 textures ( 2 for upper body 2 for lower body: base coloured texture and coloured texture pattern) and it works well but looks so plain, unlit...

    It's the first time I'm messing around with shaders and I can´t make it react to light. The simplest diffuse would be enough, no need for complex output. Any clue in how to achieve this? Code below.

    Thanks a lot in advance.

    Have a nice day!
    Code (CSharp):
    1.     Shader "Combined + Diffuse" {
    2.         Properties {
    3.        
    4.             _MainTex ("Base (RGB)", 2D) = "white" {}
    5.             _BlendTex ("Alpha Blended (RGBA)", 2D) = "white" {}
    6.             _BlendTex2 ("Alpha Blended (RGBA) 2", 2D) = "white" {}
    7.             _BlendTex3 ("Alpha Blended (RGBA) 3", 2D) = "white" {}
    8.  
    9.  
    10.             _Color ("M1", Color) = (1,1,1)
    11.             _Color1 ("M2", Color) = (1,1,1)
    12.             _Color2 ("C1", Color) = (1,1,1)
    13.             _Color3 ("C2", Color) = (1,1,1)
    14.  
    15.         }
    16.         SubShader {
    17.  
    18.        
    19.             Pass {
    20.            
    21.                 SetTexture [_MainTex] {
    22.                     constantColor [_Color]
    23.                         combine constant lerp(texture) previous
    24.                    }
    25.  
    26.                 SetTexture [_MainTex] {
    27.                     combine previous * texture
    28.                 }
    29.                  SetTexture [_BlendTex] {
    30.                     constantColor [_Color1]
    31.                         combine constant lerp(texture) previous
    32.                    }
    33.                 SetTexture[_BlendTex]{
    34.                     combine previous * texture
    35.                 }
    36.                  SetTexture [_BlendTex2] {
    37.                     constantColor [_Color2]
    38.                         combine constant lerp(texture) previous
    39.                    }
    40.                     SetTexture[_BlendTex2]{
    41.                     combine previous * texture
    42.                 }
    43.                  SetTexture [_BlendTex3] {
    44.                     constantColor [_Color3]
    45.                         combine constant lerp(texture) previous
    46.                    }
    47.                     SetTexture[_BlendTex3]{
    48.                     combine previous * texture
    49.                 }
    50.             }
    51.         }
    52.         FallBack "Diffuse"
    53.  
    54.     }
    Edited: A screenshot because I can feel I'm not explaining this correctly. Yellow is the main color of the upper outfit and blue is the pattern color. Orange is the main color of the lower outfit and green is the pattern color.
    Shader.PNG
     
    Last edited: Mar 14, 2022
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,236
    You're using an old, and deprecated, fixed function shader syntax. This style of shader code was used to write shaders for devices made 20+ years ago, and which functionally no longer exist. The last desktop GPU made that used these stopped being made sometime in the early 00's, the last consumer mobile devices outside of extreme budget ones by the end of the 00's.

    For backwards compatibility, Unity will automatically convert shaders written in this style to a more modern shader behind the scenes so they continue to work. But you're limiting yourself significantly for any kind of customization you might be wanting to do.

    You might try going through these tutorials:
    https://www.alanzucconi.com/2015/06/10/a-gentle-introduction-to-shaders-in-unity3d/

    And then you'll likely want to use a Surface Shader, which lets you write more modern shader code while still letting Unity handle most of the complicated parts behind the scenes. Or perhaps look at getting Amplify Shader Editor or Unity's Shader Graph to write your shaders using a node based shader system.
     
    xagustudios likes this.
  3. xagustudios

    xagustudios

    Joined:
    Feb 6, 2017
    Posts:
    8
    Thanks for the reply

    Didn´t realize my tiny knowledge about shaders was so out of date. Did a bit research starting from the tutorials you sent me and made a surface shader from scratch. The result is far better than the original one in terms of visual quality.
    Shader2.PNG

    Still not 100% convinced with the result, don't know exactly why... It maybe receives more light than expected, is too bright. Will fool around a bit more, any new advice or link is very welcome.

    Thanks a lot for your help, you saved my day.

    Have a nice day.
     
    Last edited: Mar 16, 2022
  4. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,236
  5. xagustudios

    xagustudios

    Joined:
    Feb 6, 2017
    Posts:
    8