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): Shader "/Unlit/Vertex Color + Lightmap Surface" { Properties { _MainTex ("Base (RGB)", 2D) = "white" {} } SubShader { Tags { "RenderType"="Opaque" } LOD 200 CGPROGRAM #pragma surface surf Lambert sampler2D _MainTex; struct Input { float2 uv_MainTex; float4 color : COLOR; }; void surf (Input IN, inout SurfaceOutput o) { fixed4 c = tex2D(_MainTex, IN.uv_MainTex); o.Albedo = IN.color.rgb * c.rgb; } ENDCG } } 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
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.
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
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.
You're in luck because I needed this exact type of shader not too long ago. Code (csharp): Shader "Unlit_VertexColor_Lightmap" { Properties { _MainTex ("Base", 2D) = "white" {} } SubShader { Tags { "RenderType"="Opaque" } Pass { CGPROGRAM #include "UnityCG.cginc" #pragma vertex vert #pragma fragment frag #pragma multi_compile LIGHTMAP_ON LIGHTMAP_OFF struct v2f { half4 color : COLOR; fixed4 pos : SV_POSITION; fixed2 uv[2] : TEXCOORD0; }; sampler2D _MainTex; fixed4 _MainTex_ST; #ifdef LIGHTMAP_ON fixed4 unity_LightmapST; sampler2D unity_Lightmap; #endif v2f vert(appdata_full v) { v2f o; o.pos = mul(UNITY_MATRIX_MVP, v.vertex); o.uv[0] = TRANSFORM_TEX(v.texcoord, _MainTex); #ifdef LIGHTMAP_ON o.uv[1] = v.texcoord1.xy * unity_LightmapST.xy + unity_LightmapST.zw; #endif o.color = v.color; return o; } fixed4 frag(v2f i) : COLOR { fixed4 c = tex2D(_MainTex, i.uv[0]) * i.color; #ifdef LIGHTMAP_ON c.rgb *= DecodeLightmap(tex2D(unity_Lightmap, i.uv[1])); #endif return c; } ENDCG } } } 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.
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): //UNlit //Vertex color //Lightmap support //Texture //Alpha Shader "Unlit/Transparent/Vertex Color + Transparent + Lightmap" { Properties { _MainTex ("Base (RGB) Trans (A)", 2D ) = "white" {} } SubShader { Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" } ZWrite Off Blend SrcAlpha OneMinusSrcAlpha Pass { CGPROGRAM #include "UnityCG.cginc" #pragma vertex vert #pragma fragment frag #pragma multi_compile LIGHTMAP_ON LIGHTMAP_OFF struct v2f { half4 color : COLOR; fixed4 pos : SV_POSITION; fixed2 uv[2] : TEXCOORD0; }; sampler2D _MainTex; fixed4 _MainTex_ST; #ifdef LIGHTMAP_ON fixed4 unity_LightmapST; sampler2D unity_Lightmap; #endif v2f vert(appdata_full v) { v2f o; o.pos = mul(UNITY_MATRIX_MVP, v.vertex); o.uv[0] = TRANSFORM_TEX(v.texcoord, _MainTex); #ifdef LIGHTMAP_ON o.uv[1] = v.texcoord1.xy * unity_LightmapST.xy + unity_LightmapST.zw; #endif o.color = v.color; return o; } fixed4 frag(v2f i) : COLOR { fixed4 c = tex2D(_MainTex, i.uv[0]) * i.color; #ifdef LIGHTMAP_ON c.rgb *= DecodeLightmap(tex2D(unity_Lightmap, i.uv[1])); #endif c.a *= i.color.a; return c; } ENDCG } } }
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.
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.
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): //UNlit //Vertex color //Lightmap support //Texture Shader "/Unlit/Vertex Color + Lightmap" { Properties { _MainTex ("Base", 2D) = "white" {} } SubShader { Tags { "RenderType"="Opaque" } Pass { CGPROGRAM #include "UnityCG.cginc" #pragma vertex vert #pragma fragment frag #pragma multi_compile LIGHTMAP_ON LIGHTMAP_OFF struct v2f { fixed4 color : COLOR; fixed4 pos : SV_POSITION; fixed2 lmap : TEXCOORD1; fixed2 pack0 : TEXCOORD0; }; sampler2D _MainTex; fixed4 _MainTex_ST; #ifdef LIGHTMAP_ON sampler2D unity_Lightmap; fixed4 unity_LightmapST; #endif v2f vert(appdata_full v) { v2f o; o.pos = mul(UNITY_MATRIX_MVP, v.vertex); o.pack0.xy = TRANSFORM_TEX(v.texcoord, _MainTex); #ifdef LIGHTMAP_ON o.lmap.xy = v.texcoord1.xy * unity_LightmapST.xy + unity_LightmapST.zw; #endif o.color = v.color; return o; } fixed4 frag(v2f i) : COLOR { fixed4 c = tex2D(_MainTex, i.pack0)* i.color; #ifdef LIGHTMAP_ON c.rgb *= DecodeLightmap(tex2D(unity_Lightmap, i.lmap)); #endif return c; } ENDCG } } }
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
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 ?
Copy the code above save as .shader . copy the shader into any place of your project folder, after that, Unity should detect it ...Greetings!
hi~ i had used this i used it! run good! but miss shadow use this shader miss shadow.... can you help me?