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

Vertex Shader with Lighting intensity

Discussion in 'Shaders' started by lancer, Sep 13, 2016.

  1. lancer

    lancer

    Joined:
    Aug 1, 2013
    Posts:
    231
    I'm back on the forums after not being active for several years, now I'm fluent in C# and Unity so I won't make posts asking stupid questions again ;)

    For a project I'm working on I wanted a shader that didn't react to or cast shadows, and used the mesh's vertex colors. After working on it for a couple hours I was able to piece together a working shader (with no prior experience to working with shaders..) :)

    Code (CSharp):
    1. Shader "Game/VertexColorLight" {
    2.     SubShader{
    3.         Tags{ "RenderType" = "Opaque" }
    4.         Pass{
    5.             CGPROGRAM
    6.                 #pragma vertex vert
    7.                 #pragma fragment frag
    8.  
    9.                 #include "UnityCG.cginc"
    10.                 #include "Lighting.cginc"
    11.  
    12.                 struct VertOut {
    13.                     float4 position : POSITION;
    14.                     float4 color : COLOR;
    15.                 };
    16.  
    17.                 struct VertIn {
    18.                     float4 vertex : POSITION;
    19.                     float4 color : COLOR;
    20.                 };
    21.  
    22.                 VertOut vert(VertIn input)
    23.                 {
    24.                     VertOut output;
    25.                     output.position = mul(UNITY_MATRIX_MVP, input.vertex);
    26.                     output.color = input.color;
    27.                     return output;
    28.                 }
    29.  
    30.                 float4 frag(VertOut input) : SV_Target
    31.                 {
    32.                     return input.color * _LightColor0;
    33.                 }
    34.             ENDCG
    35.         }  
    36.     }
    37. }
    This shader uses the mesh's vertex colors for coloring, and the multiplies it by _LightColor0 (the color and intensity of the light) .

    The result is that the mesh is colored via it's vertex colors, and the scene's light "illuminates" the object without casting shadows.


    I hope someone else besides me finds this useful! I got the hint about _LightColor0 from here, and the base of the vertex shader from here.

    I'll probably be tweaking it a bit more as I use it over the next few days. I'll be sure to post any changes I make!
     
    Kin0min and UnityLighting like this.
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,329
    Minor note. The vertex output : POSITION should be SV_POSITION otherwise it might have problems with dx11 or consoles. Also you should be using UnityObjectToClipPos(v.vertex) in Unity 5.4 instead of mul(UNITY_MATRIX_MVP, v.vertex) mainly for VR support; it'll work with out it, but going forward that should be what you use.
     
    Kin0min and lancer like this.