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

Unity example vert-frag shader without lighting ?

Discussion in 'Shaders' started by daprato, Oct 2, 2019.

  1. daprato

    daprato

    Joined:
    Sep 25, 2013
    Posts:
    31
    Hi fabulous Unity people. Tired of tweaking some custom lighting from a surf that is at the end a forward shader that use other macros... I'd like to learn (and be free afterward creating) the basis of Vert and Frag, doing a shader that is lit, shadowed and get gi-ambient, etc...

    Once I'm started, I think I could use the many examples of CG and HLSL found on the net regardless the engine rendering... But I just need a starting structure.

    Just sad the official "lit" examples from here are not lit at all for me, something is going wrong (yes I read carefully all the page header).
    Only the ambient is passing
    ShadeSH9(half4(worldNormal,1))

    btw I'm in simple 3D-WithExtra mode, 2019.2.
    All my tier renderpath are in Forward.


    Is there some CGInclude missing ? Or missing v2f special CAPS things that import or get light color/pos ?
    I'm trying to put as debug output of o.diff, the LightColor, LightColor.rgbb or the _WorldSpaceLightPos0.xyz. Nothing showed up.
    But the rest seams to work tho. I can render as color the
    o.vertex
    or the
    worldNormal


    the Unity proposed example:
    Code (CSharp):
    1. Shader "Lit/Diffuse With Shadows"
    2. {
    3.     Properties
    4.     {
    5.         [NoScaleOffset] _MainTex ("Texture", 2D) = "white" {}
    6.     }
    7.     SubShader
    8.     {
    9.         Pass
    10.         {
    11.             Tags {"LightMode"="ForwardBase"}
    12.             CGPROGRAM
    13.             #pragma vertex vert
    14.             #pragma fragment frag
    15.             #include "UnityCG.cginc"
    16.             #include "Lighting.cginc"
    17.  
    18.             // compile shader into multiple variants, with and without shadows
    19.             // (we don't care about any lightmaps yet, so skip these variants)
    20.             #pragma multi_compile_fwdbase nolightmap nodirlightmap nodynlightmap novertexlight
    21.             // shadow helper functions and macros
    22.             #include "AutoLight.cginc"
    23.  
    24.             struct v2f
    25.             {
    26.                 float2 uv : TEXCOORD0;
    27.                 SHADOW_COORDS(1) // put shadows data into TEXCOORD1
    28.                 fixed3 diff : COLOR0;
    29.                 fixed3 ambient : COLOR1;
    30.                 float4 pos : SV_POSITION;
    31.             };
    32.             v2f vert (appdata_base v)
    33.             {
    34.                 v2f o;
    35.                 o.pos = UnityObjectToClipPos(v.vertex);
    36.                 o.uv = v.texcoord;
    37.                 half3 worldNormal = UnityObjectToWorldNormal(v.normal);
    38.                 half nl = max(0, dot(worldNormal, _WorldSpaceLightPos0.xyz));
    39.                 o.diff = nl * _LightColor0.rgb;
    40.                 o.ambient = ShadeSH9(half4(worldNormal,1));
    41.                 // compute shadows data
    42.                 TRANSFER_SHADOW(o)
    43.                 return o;
    44.             }
    45.  
    46.             sampler2D _MainTex;
    47.  
    48.             fixed4 frag (v2f i) : SV_Target
    49.             {
    50.                 fixed4 col = tex2D(_MainTex, i.uv);
    51.                 // compute shadow attenuation (1.0 = fully lit, 0.0 = fully shadowed)
    52.                 fixed shadow = SHADOW_ATTENUATION(i);
    53.                 // darken light's illumination with shadow, keep ambient intact
    54.                 fixed3 lighting = i.diff * shadow + i.ambient;
    55.                 col.rgb *= lighting;
    56.                 return col;
    57.             }
    58.             ENDCG
    59.         }
    60.  
    61.         // shadow casting support
    62.         UsePass "Legacy Shaders/VertexLit/SHADOWCASTER"
    63.     }
    64. }
    Thx for your precious Help.
    Once I know the base, I'll share it for sure. All the "blog" that explained the basic Vert Frag of Unity are alreay deprecated...
     
  2. mouurusai

    mouurusai

    Joined:
    Dec 2, 2011
    Posts:
    350
  3. daprato

    daprato

    Joined:
    Sep 25, 2013
    Posts:
    31
    Thx, yes at the same time I was browsing the other post here about frag shader and found this guy too. This guy seams to do a really nice job, I'll simply do all the tuto related to scriptable rendering and shaders.
    Thx @mouurusai

    Is it me, or the proposed lit "legacy" shader of Unity, even if the doc says 2019.2, are deprecated ? It's maybe just an include missing or importing stuff...