Search Unity

Transparency / Diffuse with Vertex Color + Lighting

Discussion in 'Shaders' started by Jesse_Pixelsmith, Dec 14, 2013.

  1. Jesse_Pixelsmith

    Jesse_Pixelsmith

    Joined:
    Nov 22, 2009
    Posts:
    296
    So I'm trying to get a shader to work that will act like the default Transparent/Diffuse shader, which is able to be lit by point lights - but also support accepting a vertex color to change the tint.

    This is so I can have multiple objects with the same material but vary the tints slightly, and still have them be affected by lighting.

    Here is a shader that is transparent and has the Vertex color, but isn't lit. Is it possible to modify this to be lit, or modify the Transparent/Diffuse shader to also accept vertex colors?

    I'm very much a n00b with shader code.

    Thanks!

    Code (csharp):
    1. Shader "Ferr/Unlit Transparent Textured Vertex Color" {
    2.     Properties {
    3.         _MainTex("Texture (RGBA)", 2D) = "white" {}
    4.     }
    5.     SubShader {
    6.         Tags { "Queue"="Transparent" "RenderType"="Transparent"  }
    7.         Blend SrcAlpha OneMinusSrcAlpha
    8.  
    9.         LOD 200
    10.        
    11.         Pass {
    12.             CGPROGRAM
    13.             #pragma vertex   vert
    14.             #pragma fragment frag
    15.             #include "UnityCG.cginc"
    16.  
    17.             sampler2D _MainTex;
    18.             float4    _MainTex_ST;
    19.  
    20.             struct VS_OUT {
    21.                 float4 position : SV_POSITION;
    22.                 float4 color    : COLOR;
    23.                 float2 uv       : TEXCOORD0;
    24.             };
    25.  
    26.             VS_OUT vert (appdata_full input) {
    27.                 VS_OUT result;
    28.                 result.position = mul (UNITY_MATRIX_MVP, input.vertex);
    29.                 result.uv       = TRANSFORM_TEX (input.texcoord, _MainTex);
    30.                 result.color    = input.color;
    31.  
    32.                 return result;
    33.             }
    34.  
    35.             half4 frag (VS_OUT input) : COLOR {
    36.                 half4 color = tex2D(_MainTex, input.uv);
    37.                 return color * input.color;
    38.             }
    39.             ENDCG
    40.         }
    41.     }
    42. }
     
  2. Jesse_Pixelsmith

    Jesse_Pixelsmith

    Joined:
    Nov 22, 2009
    Posts:
    296
    I'm not even sure if this is possible or practical, I'd like to be able to lighten/darken my 2d terrain layers, but still accept normal lighting. I suppose I can create an instance of each material at runtime and modify the colors, or just create separate materials for each shade, but then I won't be able to batch them.

    Would a custom shader solve any of this, or is a vertex colored shader that accepts normal lighting simply not possible?

    Thanks,
    Jesse
     
  3. cjddmut

    cjddmut

    Joined:
    Nov 19, 2012
    Posts:
    179
    Take a look at this page which implements a shader that handles multiple light sources:

    http://en.wikibooks.org/wiki/Cg_Programming/Unity/Multiple_Lights

    It should have the information you need. Also you can change the brightness of a texture by modifying the final calculated color for each pixel (like double it for brightness or half it to darken it).