Search Unity

Unlit + Vertex Color + Lightmap

Discussion in 'Shaders' started by Eyeofgod, Jan 27, 2012.

  1. Eyeofgod

    Eyeofgod

    Joined:
    Jun 25, 2010
    Posts:
    126
    Hi there,
    Im trying to do a shader that supports Unlit + vertex color + lightmap (Beast). Im starting with surface shaders and I thought that this was a good first try.

    Right know I have this:

    Code (csharp):
    1.  
    2. Shader "/Unlit/Vertex Color + Lightmap Surface" {
    3.     Properties {
    4.         _MainTex ("Base (RGB)", 2D) = "white" {}
    5.     }
    6.     SubShader {
    7.         Tags { "RenderType"="Opaque" }
    8.         LOD 200
    9.    
    10.     CGPROGRAM
    11.     #pragma surface surf Lambert
    12.    
    13.     sampler2D _MainTex;
    14.    
    15.     struct Input {
    16.         float2 uv_MainTex;
    17.         float4 color : COLOR;
    18.     };
    19.    
    20.     void surf (Input IN, inout SurfaceOutput o) {
    21.         fixed4 c = tex2D(_MainTex, IN.uv_MainTex);
    22.         o.Albedo = IN.color.rgb * c.rgb;
    23.     }
    24.     ENDCG
    25.     }
    26. }
    27.  
    As you sill see the lighting model used is Lambert, that of course is taking lights into account. I don't know how to tell the shader to not to use lights. Thats where surface shaders goes "ninja". Any help?

    Many thanks
     
  2. Farfarer

    Farfarer

    Joined:
    Aug 17, 2010
    Posts:
    2,249
    Under the "SubShader {" line put Lighting Off, should work.
     
  3. Daniel_Brauer

    Daniel_Brauer

    Unity Technologies

    Joined:
    Aug 11, 2006
    Posts:
    3,355
    I don't think that will work, as it is a fixed function setting. Surface shaders have their own vertex programs, and will calculate all lighting on their own.

    If you want a mostly unlit shader, I would recommend starting with this template and then adding vertex colour and light map support.
     
  4. Eyeofgod

    Eyeofgod

    Joined:
    Jun 25, 2010
    Posts:
    126
    I have tried the "lighting off" but it doesn't work.
    I'm learning how surface shaders works, that's why I like to follow that path instead of writing the normal cg shader.
    Despite that, thanks for your suggestions
     
  5. Daniel_Brauer

    Daniel_Brauer

    Unity Technologies

    Joined:
    Aug 11, 2006
    Posts:
    3,355
    Surface shaders are designed specifically to make it easy to integrate your shader properly with Unity's ever-more-complex lighting system. If the only lighting you want is from a light map, a surface shader is a waste.
     
  6. chronos78

    chronos78

    Joined:
    Dec 6, 2009
    Posts:
    154
    You're in luck because I needed this exact type of shader not too long ago.

    Code (csharp):
    1.  
    2. Shader "Unlit_VertexColor_Lightmap"
    3. {
    4.     Properties
    5.     {
    6.         _MainTex ("Base", 2D) = "white" {}
    7.     }
    8.  
    9.     SubShader
    10.     {
    11.         Tags { "RenderType"="Opaque" }
    12.  
    13.         Pass
    14.         {
    15.             CGPROGRAM
    16.                 #include "UnityCG.cginc"
    17.                 #pragma vertex vert
    18.                 #pragma fragment frag
    19.                 #pragma multi_compile LIGHTMAP_ON LIGHTMAP_OFF
    20.  
    21.                 struct v2f
    22.                 {
    23.                     half4 color : COLOR;
    24.                     fixed4 pos : SV_POSITION;
    25.                     fixed2 uv[2] : TEXCOORD0;
    26.                 };
    27.            
    28.                 sampler2D _MainTex;
    29.                 fixed4 _MainTex_ST;
    30.  
    31.                 #ifdef LIGHTMAP_ON
    32.                 fixed4 unity_LightmapST;
    33.                 sampler2D unity_Lightmap;
    34.                 #endif
    35.  
    36.                 v2f vert(appdata_full v)
    37.                 {
    38.                     v2f o;
    39.                     o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
    40.                     o.uv[0] = TRANSFORM_TEX(v.texcoord, _MainTex);
    41.                    
    42.                     #ifdef LIGHTMAP_ON
    43.                     o.uv[1] = v.texcoord1.xy * unity_LightmapST.xy + unity_LightmapST.zw;
    44.                     #endif
    45.  
    46.                     o.color = v.color;
    47.  
    48.                     return o;
    49.                 }
    50.        
    51.                 fixed4 frag(v2f i) : COLOR
    52.                 {
    53.                     fixed4 c = tex2D(_MainTex, i.uv[0]) * i.color;
    54.  
    55.                     #ifdef LIGHTMAP_ON
    56.                     c.rgb *= DecodeLightmap(tex2D(unity_Lightmap, i.uv[1]));
    57.                     #endif
    58.  
    59.                     return c;
    60.                 }
    61.             ENDCG
    62.         }
    63.     }
    64. }
    65.  
    It isn't a surface shader like you were trying to get working but this does the trick and it runs fast even on IOS. Also this exact code hasn't been tested as I just stripped out some of it that was specific to my project so there may be something that was missed.
     
  7. Eyeofgod

    Eyeofgod

    Joined:
    Jun 25, 2010
    Posts:
    126
    Thanks all. I will give that shader a try to that shader. Also I will continue investigating surface shaders, and let you know if I get something.

    EDIT: It worked perfectly!. Based in your shader here is a new one with transparency support (vertex alpha and texture alpha mixed by a dot operation)

    Code (csharp):
    1. //UNlit
    2. //Vertex color
    3. //Lightmap support
    4. //Texture
    5. //Alpha
    6.  
    7.  
    8.  
    9. Shader "Unlit/Transparent/Vertex Color + Transparent + Lightmap" {
    10.  
    11.     Properties
    12.  
    13.     {
    14.  
    15.         _MainTex ("Base (RGB) Trans (A)", 2D ) = "white" {}
    16.  
    17.     }
    18.  
    19.  
    20.  
    21.     SubShader
    22.  
    23.     {
    24.  
    25.         Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" }
    26.    
    27.         ZWrite Off
    28.         Blend SrcAlpha OneMinusSrcAlpha
    29.  
    30.  
    31.         Pass
    32.  
    33.         {
    34.  
    35.             CGPROGRAM
    36.  
    37.                 #include "UnityCG.cginc"
    38.                 #pragma vertex vert
    39.                 #pragma fragment frag
    40.                 #pragma multi_compile LIGHTMAP_ON LIGHTMAP_OFF
    41.  
    42.                 struct v2f
    43.  
    44.                 {
    45.  
    46.                     half4 color : COLOR;
    47.                     fixed4 pos : SV_POSITION;
    48.                     fixed2 uv[2] : TEXCOORD0;
    49.  
    50.                 };
    51.  
    52.                 sampler2D _MainTex;
    53.                 fixed4 _MainTex_ST;
    54.  
    55.                 #ifdef LIGHTMAP_ON
    56.                 fixed4 unity_LightmapST;
    57.                 sampler2D unity_Lightmap;
    58.                 #endif
    59.  
    60.                 v2f vert(appdata_full v)
    61.  
    62.                 {
    63.  
    64.                     v2f o;
    65.                     o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
    66.                     o.uv[0] = TRANSFORM_TEX(v.texcoord, _MainTex);
    67.                    
    68.                     #ifdef LIGHTMAP_ON
    69.                     o.uv[1] = v.texcoord1.xy * unity_LightmapST.xy + unity_LightmapST.zw;
    70.                     #endif
    71.  
    72.                     o.color = v.color;             
    73.                     return o;
    74.  
    75.                 }
    76.                 fixed4 frag(v2f i) : COLOR
    77.  
    78.                 {
    79.                     fixed4 c = tex2D(_MainTex, i.uv[0]) * i.color;
    80.                     #ifdef LIGHTMAP_ON
    81.                     c.rgb *= DecodeLightmap(tex2D(unity_Lightmap, i.uv[1]));
    82.                     #endif
    83.             c.a *= i.color.a;
    84.                     return c;
    85.  
    86.                 }
    87.  
    88.             ENDCG
    89.  
    90.         }
    91.  
    92.     }
    93.  
    94. }
    95.  
    96.  
     
    Last edited: Jan 30, 2012
  8. Eyeofgod

    Eyeofgod

    Joined:
    Jun 25, 2010
    Posts:
    126
    I just found that this has shader works perfect on editor but it doesn't work well on iOS. The textures appear like they where made by quads, and objects shows they faces without any smooth. I will try to fix it and post a new version. Also, the performance of this shader on iPhone 4 or iPad 1 is not good enough, even thought what it gets is really simple.
     
    Last edited: Feb 1, 2012
  9. chronos78

    chronos78

    Joined:
    Dec 6, 2009
    Posts:
    154
    Not sure about the faceting that you are experiencing. This shader doesn't calculate lighting therefore has no control over how smooth or hard edges appear. It may be an issue with the lightmaps or the normals on the original meshes. Another area to check is compression on the lightmaps or the padding setting for meshes that are generating lightmap uvs.

    As to performance on IOS my guess is that the problem is fill rate do to the fact that you have added transparency to the shader and older devices have problems drawing lots of transparent objects. Try the non transparent version and see if frame rate improves, if it does then you are almost certainly fill rate bound. In my project with the original shader both the iphone 4 and 1st gen ipad peg at 60fps but every project is different so your result are bound to be different.
     
  10. Eyeofgod

    Eyeofgod

    Joined:
    Jun 25, 2010
    Posts:
    126
    I have fixed the two problems. The performance one was because of the color precision. Changed it to fixed4 did the job.
    Im truly not sure what was causing the faceting thing, but has something to be with how iOS reads the lightmap coords. With the following changes I managed to solve it:

    Code (csharp):
    1. //UNlit
    2. //Vertex color
    3. //Lightmap support
    4. //Texture
    5.  
    6.  
    7.  
    8. Shader "/Unlit/Vertex Color + Lightmap" {
    9.  
    10.     Properties
    11.  
    12.     {
    13.         _MainTex ("Base", 2D) = "white" {}
    14.     }
    15.     SubShader
    16.  
    17.     {
    18.         Tags { "RenderType"="Opaque" }
    19.         Pass
    20.  
    21.         {
    22.  
    23.             CGPROGRAM
    24.                 #include "UnityCG.cginc"
    25.                 #pragma vertex vert
    26.                 #pragma fragment frag
    27.                 #pragma multi_compile LIGHTMAP_ON LIGHTMAP_OFF
    28.  
    29.                 struct v2f
    30.  
    31.                 {
    32.                     fixed4 color : COLOR;
    33.                     fixed4 pos : SV_POSITION;
    34.                     fixed2 lmap : TEXCOORD1;
    35.                     fixed2 pack0 : TEXCOORD0;
    36.                    
    37.  
    38.                 };
    39.                 sampler2D _MainTex;
    40.                 fixed4 _MainTex_ST;
    41.                
    42.                  #ifdef LIGHTMAP_ON
    43.                     sampler2D unity_Lightmap;
    44.                     fixed4 unity_LightmapST;
    45.                 #endif
    46.                
    47.                 v2f vert(appdata_full v)
    48.  
    49.                 {
    50.                     v2f o;
    51.                     o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
    52.                     o.pack0.xy = TRANSFORM_TEX(v.texcoord, _MainTex);
    53.                     #ifdef LIGHTMAP_ON
    54.                         o.lmap.xy = v.texcoord1.xy * unity_LightmapST.xy + unity_LightmapST.zw;
    55.                     #endif
    56.                     o.color = v.color;
    57.                    
    58.                     return o;
    59.  
    60.                 }
    61.                 fixed4 frag(v2f i) : COLOR
    62.                 {
    63.  
    64.                     fixed4 c = tex2D(_MainTex, i.pack0)* i.color;
    65.                     #ifdef LIGHTMAP_ON
    66.                         c.rgb *= DecodeLightmap(tex2D(unity_Lightmap, i.lmap));
    67.                     #endif
    68.                     return c;
    69.  
    70.                 }
    71.  
    72.             ENDCG
    73.  
    74.         }
    75.  
    76.     }
    77.  
    78. }
    79.  
     
    Last edited: Feb 2, 2012
  11. petey

    petey

    Joined:
    May 20, 2009
    Posts:
    1,824
    Hi there,

    I'm struggling to make a shader like yours and I just stumbled across this thread, but I cant seem to get it to work :(

    This is what the shader looks like before I bake the lightmap -


    Then after I bake it, the whole thing just turns invisible :(


    Any ideas why it's doing this?

    Thanks
    Pete
     

    Attached Files:

  12. LordKnyv

    LordKnyv

    Joined:
    Dec 12, 2012
    Posts:
    36
    Hi, I am looking fo something similiar but it's the first time I dwell into the custom shader thing.
    Could you explain me how to apply this to my scene ?
     
  13. U7Games

    U7Games

    Joined:
    May 21, 2011
    Posts:
    943
    Copy the code above save as .shader .
    copy the shader into any place of your project folder, after that, Unity should detect it
    ...Greetings!
     
  14. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    I think he probably found the answer last year - no need to necro :)
     
  15. play_with_wolf

    play_with_wolf

    Joined:
    Aug 26, 2014
    Posts:
    36
    hi~ i had used this
    i used it! run good! but miss shadow
    diffuse2.jpg
    use this shader miss shadow....
    v2.png

    can you help me?