Vertexlit surface(or CG) shader

Hi, I’m converting built-in Unity’s vertexlit shaders to CG. Is it possible to create a custom vertexlit lighting model with a surface shader? An example would be great.

I know that this can be done with plain CG code, but then I wouldn’t want to consider writing code for all of the passes (forwardbase and forwardadd), different light types and lightmapping.

It would be great to see an example of a vertexlit CG/surf shader, that properly handles all of the passes and light types. The CG vertexlit shader examples found on wikibooks isn’t good enough, because it uses performance-heavy branching.

Thanks!

Just in case anyone else stumbles upon the same issue, here’s a great article describing CG VertexLit shaders: http://unitygems.com/noobs-guide-shaders-5-bumped-diffuse-shader/

Shader "Custom/OutlineToonShader" {
	Properties {
		_MainTex ("Base (RGB)", 2D) = "white" {}
	}
	SubShader {
		Tags { "RenderType"="Opaque" }
		LOD 200

		Pass {

			Cull Back 
			Lighting On

			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag

			#include "UnityCG.cginc"

			sampler2D _MainTex;
			float4 _MainTex_ST;

			struct a2v
			{
				float4 vertex : POSITION;
				float3 normal : NORMAL;
				float4 texcoord : TEXCOORD0;

			}; 

			struct v2f
			{
				float4 pos : POSITION;
				float2 uv ;
				float3 color; 

			};

			v2f vert (a2v v)
			{
				v2f o;
				o.pos = mul( UNITY_MATRIX_MVP, v.vertex);
				o.uv = TRANSFORM_TEX (v.texcoord, _MainTex);  
				o.color = ShadeVertexLights(v.vertex, v.normal);
				return o;
			}

			float4 frag(v2f i) : COLOR
			{
				float4 c = tex2D (_MainTex, i.uv);
				c.rgb = c.rgb  * i.color * 2;
				return c;

			}

			ENDCG
		}
	}
	FallBack "Diffuse"
}

The function that does the magic is: o.color = ShadeVertexLights(v.vertex, v.normal);

3 Likes

In case somebody else needs such a shader, here’s a standard VertexLit shader rewritten in CG: http://wiki.unity3d.com/index.php/CGVertexLit

It looks 100% the same as the original VertexLit, supports lightmaps and specularity.

2 Likes