Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Blending multiple textures to create uniform patterns

Discussion in 'Shaders' started by pedrohba, Aug 27, 2013.

  1. pedrohba

    pedrohba

    Joined:
    Aug 27, 2013
    Posts:
    13
    Hello,

    I'm creating a sports application in which the user can customize players uniforms colors (3 different colors). I'm relatively new to shader development, so I tought that the best way to go was to write a custom shader that receive 4 textures as inputs:

    • one containing details that doesn't change (skin, eyes, etc.)
    • three containing a pattern for each color

    I was able to apply the patterns, but I'm unable to apply lightning to the uniforms. Here is the result I achieved so far:

    $soccer_model.jpg

    Here's the shader source code:

    Code (csharp):
    1. Shader "TacticalPad/PlayerJersey_Triple_Full" {
    2.     Properties {
    3.         _MainTex ("Main Tex", 2D) = "white" {}
    4.  
    5.         _BGColor ("BG Color", Color) = (1,1,1,1)
    6.  
    7.         _Tex1Color ("Cor 1", Color) = (1,1,1,1)
    8.         _Tex1 ("Textura Cor 1", 2D) = "white" {}
    9.  
    10.         _Tex2Color ("Cor 2", Color) = (1,1,1,1)
    11.         _Tex2 ("Textura Cor 2", 2D) = "white" {}
    12.        
    13.         _Tex3Color ("Cor 3", Color) = (1,1,1,1)
    14.         _Tex3 ("Textura Cor 3", 2D) = "white" {}
    15.     }
    16.  
    17.     SubShader {
    18.         Tags { "RenderType"="Opaque" }
    19.  
    20.         // Non-lightmapped
    21.         Pass {
    22.  
    23.             Material {
    24.                 Diffuse [_BGColor]
    25.                 Ambient [_BGColor]
    26.             }
    27.  
    28.             Lighting On
    29.  
    30.             SetTexture [_Tex1] {
    31.                 constantColor [_Tex1Color]
    32.                 Combine constant lerp(texture) primary
    33.             }
    34.  
    35.             SetTexture [_Tex2] {
    36.                 constantColor [_Tex2Color]
    37.                 Combine constant lerp(texture) previous
    38.             }
    39.  
    40.             SetTexture[_Tex3] {
    41.                 ConstantColor[_Tex3Color]
    42.                 Combine constant Lerp(texture) previous
    43.             }
    44.  
    45.             SetTexture [_MainTex] {
    46.                 Combine texture * previous double
    47.             }
    48.         }
    49.        
    50.         Pass {
    51.  
    52.                 Tags { "LightMode"="ShadowCaster" }
    53.  
    54.                 Material {
    55.                     Diffuse [_BGColor]
    56.                     Ambient [_BGColor]
    57.                 }
    58.  
    59.                 Lighting On
    60.         }
    61.     }
    62. }
    I've tried to add a fifth combiner that applies lightning to the result, but then I lost the others colors.

    Can someone please help me with the shader or even if this is indeed the best way to achieve the effect I'm trying to create?

    Best regards,

    Pedro Almeida
     
  2. pedrohba

    pedrohba

    Joined:
    Aug 27, 2013
    Posts:
    13
    If anyone wishes to do something similar I've solved the problem. By far, I'm not a shader specialist, but here is the code that applies the textures in the pattern that I wanted and also lights it correctly. I'd to use three passes, maybe there is a way to use less.

    $soccer_model_fixed.jpg

    Code (csharp):
    1.  
    2. Shader "TacticalPad/PlayerJersey_Triple_Full" {
    3.     Properties {
    4.         _MainTex ("Main Tex", 2D) = "white" {}
    5.  
    6.         _BGColor ("BG Color", Color) = (1,1,1,1)
    7.  
    8.         _Tex1Color ("Cor 1", Color) = (1,1,1,1)
    9.         _Tex1 ("Textura Cor 1", 2D) = "white" {}
    10.  
    11.         _Tex2Color ("Cor 2", Color) = (1,1,1,1)
    12.         _Tex2 ("Textura Cor 2", 2D) = "white" {}
    13.        
    14.         _Tex3Color ("Cor 3", Color) = (1,1,1,1)
    15.         _Tex3 ("Textura Cor 3", 2D) = "white" {}
    16.     }
    17.  
    18.     SubShader {
    19.  
    20.         Pass {
    21.  
    22.             Material {
    23.                 Diffuse [_BGColor]
    24.                 Ambient [_BGColor]
    25.             }
    26.  
    27.             Lighting On
    28.         }
    29.  
    30.         Pass {
    31.  
    32.             Blend DstColor SrcColor
    33.  
    34.             SetTexture [_Tex1] {
    35.                 constantColor [_Tex1Color]
    36.                 Combine constant lerp (texture) previous
    37.             }
    38.  
    39.             SetTexture [_Tex2] {
    40.                 constantColor [_Tex2Color]
    41.                 Combine constant lerp (texture) previous
    42.             }
    43.  
    44.             SetTexture[_Tex3] {
    45.                 ConstantColor[_Tex3Color]
    46.                 Combine constant lerp (texture) previous
    47.             }
    48.  
    49.             SetTexture [_MainTex] {
    50.                 Combine texture lerp (texture) previous
    51.             }
    52.         }
    53.  
    54.         Pass {
    55.  
    56.                 Tags { "LightMode"="ShadowCaster" }
    57.         }
    58.     }
    59. }
    60.  
    Best regards,

    Pedro Almeida