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

I need these two shaders combined

Discussion in 'Shaders' started by cerebrate, Aug 17, 2010.

  1. cerebrate

    cerebrate

    Joined:
    Jan 8, 2010
    Posts:
    261
    I've been trying and trying to get these two shaders to combine, but with no luck. It either is unlit, or the texture masking doesn't work. I require the outline on a masked tint pixel lit diffuse surface.


    If someone can figure this out, awesome. Could you also explain to me how it works.

    Masked Tint:
    Code (csharp):
    1. Shader "Masked Tint" {
    2.  
    3.  
    4. Properties
    5. {
    6.     _Color ("Tint Color", Color) = (1,1,1)
    7.     _MainTex ("Texture  (A = Tint Mask)", 2D) = ""
    8. }
    9.  
    10. SubShader
    11. {
    12.     Pass
    13.     {      
    14.         // tint the texture
    15.         SetTexture [_MainTex]
    16.         {
    17.             ConstantColor [_Color]
    18.             combine texture * constant
    19.         }
    20.                
    21.         // alpha blend between untinted and tinted
    22.         SetTexture [_MainTex]
    23.         {
    24.             combine previous lerp(texture) texture
    25.         }      
    26.     }
    27. }
    28.  
    29.  
    30. }
    Outlined Diffuse:
    Code (csharp):
    1.  
    2. Shader "Outlined Diffuse"
    3. {
    4.    Properties
    5.    {
    6.       _Color ("Main Color", Color) = (.5,.5,.5,1)
    7.       _OutlineColor ("Outline Color", Color) = (0,1,0,1)
    8.       _Outline ("Outline width", Range (0.002, 0.03)) = 0.01
    9.       _MainTex ("Base (RGB)", 2D) = "white" { }
    10.       //Not needed
    11.       //_ToonShade ("ToonShader Cubemap(RGB)", CUBE) = "" { Texgen CubeNormal }
    12.    }
    13.  
    14.    SubShader
    15.    {
    16.       Tags { "RenderType"="Opaque" }
    17.       //Minor switch
    18.       //UsePass "Toon/Basic/BASE"
    19.       UsePass "Diffuse/BASE"
    20.       Pass
    21.       {
    22.          Name "OUTLINE"
    23.          Tags { "LightMode" = "Always" }
    24.          
    25.          CGPROGRAM
    26.          #pragma vertex vert
    27.  
    28.          struct appdata {
    29.              float4 vertex;
    30.              float3 normal;
    31.          };
    32.  
    33.          struct v2f {
    34.             float4 pos : POSITION;
    35.             float4 color : COLOR;
    36.             float fog : FOGC;
    37.          };
    38.          uniform float _Outline;
    39.          uniform float4 _OutlineColor;
    40.  
    41.          v2f vert(appdata v) {
    42.             v2f o;
    43.             o.pos = mul(glstate.matrix.mvp, v.vertex);
    44.             float3 norm = mul ((float3x3)glstate.matrix.modelview[0], v.normal);
    45.             norm.x *= glstate.matrix.projection[0][0];
    46.             norm.y *= glstate.matrix.projection[1][1];
    47.             o.pos.xy += norm.xy * o.pos.z * _Outline;
    48.    
    49.             o.fog = o.pos.z;
    50.             o.color = _OutlineColor;
    51.             return o;
    52.          }
    53.          ENDCG
    54.          
    55.          Cull Front
    56.          ZWrite On
    57.          ColorMask RGB
    58.          Blend SrcAlpha OneMinusSrcAlpha
    59.          //? -Note: I don't remember why I put a "?" here
    60.          SetTexture [_MainTex] { combine primary }
    61.       }
    62.    }
    63.    
    64.    Fallback "Diffuse"
    65. }

    What I thought would work, but doesn't:
    Code (csharp):
    1.  
    2. Shader "Masked Outline Diffuse"
    3. {
    4.    Properties
    5.    {
    6.       _Color ("Main Color", Color) = (.5,.5,.5,1)
    7.       _OutlineColor ("Outline Color", Color) = (0,1,0,1)
    8.       _Outline ("Outline width", Range (0.002, 0.03)) = 0.01
    9.       _MainTex ("Texture  (A = Tint Mask)", 2D) = ""
    10.       //Not needed
    11.       //_ToonShade ("ToonShader Cubemap(RGB)", CUBE) = "" { Texgen CubeNormal }
    12.    }
    13.     SubShader{
    14.         Pass{
    15.             SetTexture [_MainTex]
    16.             {
    17.                 ConstantColor [_Color]
    18.                 combine texture * constant
    19.             }
    20.             SetTexture [_MainTex]
    21.             {
    22.                 combine previous lerp(texture) texture
    23.             }  
    24.         }
    25.     }
    26.     SubShader
    27.     {
    28.         Tags { "RenderType"="Opaque" }
    29.         //Minor switch
    30.         //UsePass "Toon/Basic/BASE"
    31.         UsePass "Diffuse/BASE"       
    32.         Pass
    33.         {
    34.             Name "OUTLINE"
    35.             Tags { "LightMode" = "Always" }
    36.              
    37.             CGPROGRAM
    38.             #pragma vertex vert
    39.  
    40.             struct appdata {
    41.                 float4 vertex;
    42.                 float3 normal;
    43.             };
    44.  
    45.             struct v2f {
    46.                 float4 pos : POSITION;
    47.                 float4 color : COLOR;
    48.                 float fog : FOGC;
    49.             };
    50.             uniform float _Outline;
    51.             uniform float4 _OutlineColor;
    52.  
    53.             v2f vert(appdata v) {
    54.                 v2f o;
    55.                 o.pos = mul(glstate.matrix.mvp, v.vertex);
    56.                 float3 norm = mul ((float3x3)glstate.matrix.modelview[0], v.normal);
    57.                 norm.x *= glstate.matrix.projection[0][0];
    58.                 norm.y *= glstate.matrix.projection[1][1];
    59.                 o.pos.xy += norm.xy * o.pos.z * _Outline;
    60.        
    61.                 o.fog = o.pos.z;
    62.                 o.color = _OutlineColor;
    63.                 return o;
    64.             }
    65.             ENDCG
    66.              
    67.             Cull Front
    68.             ZWrite On
    69.             ColorMask RGB
    70.             Blend SrcAlpha OneMinusSrcAlpha
    71.              //? -Note: I don't remember why I put a "?" here
    72.             SetTexture [_MainTex] { combine primary }
    73.         }
    74.     }
    75.    
    76.    
    77.    
    78.    Fallback "Diffuse"
    79. }
     
  2. cerebrate

    cerebrate

    Joined:
    Jan 8, 2010
    Posts:
    261
    except that wouldn't work. After a bunch of fiddling and reading shader docs cause I've never done them before, I figured that in order to use diffuse lighting, I can't set the tint color as the main material color, because the diffuse pass uses that color. So instead I made a new color _Tint, and use that. Also I changed the order of some stuff.

    Code (csharp):
    1. Shader "Masked Outline Diffuse"
    2. {
    3.    Properties
    4.    {
    5.       _Color ("Main Color", Color) = (.5,.5,.5,1)
    6.       _Tint ("Tint Color", Color) = (.5,.5,.5,1)
    7.       _OutlineColor ("Outline Color", Color) = (0,1,0,1)
    8.       _Outline ("Outline width", Range (0.002, 0.03)) = 0.01
    9.       _MainTex ("Texture  (A = Tint Mask)", 2D) = ""
    10.    }
    11.    
    12.     SubShader
    13.     {
    14.         Tags { "RenderType"="Opaque" }
    15.         UsePass "Diffuse/BASE"
    16.         Pass{
    17.             Tags { "LightMode" = "Always" }
    18.             Cull Back
    19.             ZWrite On
    20.             ColorMask RGB
    21.             Blend SrcAlpha OneMinusSrcAlpha
    22.              Material {
    23.                 Diffuse (1,1,1,1)
    24.                 Ambient (1,1,1,1)
    25.             }
    26.             Lighting On
    27.             SetTexture[_MainTex]
    28.             {
    29.                 combine texture * primary
    30.             }
    31.             SetTexture[_MainTex]
    32.             {
    33.                 ConstantColor[_Tint]
    34.                 combine constant * previous double
    35.             }
    36.         }
    37.         Pass
    38.         {
    39.             Name "OUTLINE"
    40.             Tags { "LightMode" = "Always" }
    41.              
    42.             CGPROGRAM
    43.             #pragma vertex vert
    44.  
    45.             struct appdata {
    46.                 float4 vertex;
    47.                 float3 normal;
    48.             };
    49.  
    50.             struct v2f {
    51.                 float4 pos : POSITION;
    52.                 float4 color : COLOR;
    53.                 float fog : FOGC;
    54.             };
    55.             uniform float _Outline;
    56.             uniform float4 _OutlineColor;
    57.  
    58.             v2f vert(appdata v) {
    59.                 v2f o;
    60.                 o.pos = mul(glstate.matrix.mvp, v.vertex);
    61.                 float3 norm = mul ((float3x3)glstate.matrix.modelview[0], v.normal);
    62.                 norm.x *= glstate.matrix.projection[0][0];
    63.                 norm.y *= glstate.matrix.projection[1][1];
    64.                 o.pos.xy += norm.xy * o.pos.z * _Outline;
    65.        
    66.                 o.fog = o.pos.z;
    67.                 o.color = _OutlineColor;
    68.                 return o;
    69.             }
    70.             ENDCG
    71.              
    72.             Cull Front
    73.             ZWrite On
    74.             ColorMask RGB
    75.             Blend SrcAlpha OneMinusSrcAlpha
    76.         }
    77.     }
    78.    
    79.    
    80.    
    81.    Fallback "Diffuse"
    82. }
    So this works, but it'd be nice if someone could point on how to make it so that the diffuse pass could be give a different color or something, that way I can continue using material.color instead of having to use SetColor and GetColor cause I'm using a non-standard shader variable for the tint.
     
  3. cerebrate

    cerebrate

    Joined:
    Jan 8, 2010
    Posts:
    261
    It doesn't work if I don't use it. I don't know why :?


    Also so that I can have pixel lit diffuse lighting.

    in action:
     
  4. Paradoks

    Paradoks

    Joined:
    Oct 13, 2009
    Posts:
    436
    Hi guys,

    I cant make this shader to work properly, the tinted part works but not the outline.
    Any help ?