Hi All, I'm wondering if someone could help me... I'm currently using one the brilliant Toony Gooch shaders from Jean Moreno as a basis, but wanted to customise it slightly to have a consistent outline. Currently I am achieving this with the following code, but I don't know much about shaders and I am pretty sure this isn't very optimised. Could anyone tell me if I can do this more efficiently? Code (csharp): Shader "Toony Gooch/Toony Gooch Outline" { Properties { _Color ("Main Color", Color) = (1,1,1,1) _MainTex ("Base (RGB)", 2D) = "white" {} _Ramp ("Toon Ramp (RGB)", 2D) = "gray" {} _SColor ("Shadow Color", Color) = (0.0,0.0,0.0,1) _LColor ("Highlight Color", Color) = (0.5,0.5,0.5,1) _OutlineColor ("Outline Color", Color) = (0,0,0,1) _Outline ("Outline width", Range (.002, 0.03)) = .005 } SubShader { Tags { "RenderType"="Opaque" } LOD 200 CGPROGRAM #pragma surface surf ToonRamp sampler2D _MainTex; sampler2D _Ramp; float4 _LColor; float4 _SColor; float4 _Color; // custom lighting function that uses a texture ramp based // on angle between light direction and normal #pragma lighting ToonRamp exclude_path:prepass inline half4 LightingToonRamp (SurfaceOutput s, half3 lightDir, half atten) { #ifndef USING_DIRECTIONAL_LIGHT lightDir = normalize(lightDir); #endif //Calculate N . L half d = dot (s.Normal, lightDir)*0.5 + 0.5; //Basic toon shading half3 ramp = tex2D(_Ramp, float2(d,d)).rgb; //Gooch shading ramp = lerp(_SColor,_LColor,ramp); half4 c; c.rgb = s.Albedo * _LightColor0.rgb * ramp * (atten * 2); c.a = 0; return c; } struct Input { float2 uv_MainTex : TEXCOORD0; }; void surf (Input IN, inout SurfaceOutput o) { half4 c = tex2D(_MainTex, IN.uv_MainTex); o.Albedo = c.rgb * _Color.rgb; o.Alpha = c.a; } ENDCG Pass { Cull Front Lighting Off CGPROGRAM #pragma vertex vert #pragma fragment frag #include "UnityCG.cginc" struct a2v { float4 vertex : POSITION; float3 normal : NORMAL; }; struct v2f { float4 pos : POSITION; }; float _Outline; v2f vert (a2v v) { v2f o; o.pos = mul( UNITY_MATRIX_MVP, v.vertex + (float4(v.normal,0) * _Outline)); return o; } float4 frag (v2f IN) : COLOR { return float(0); } ENDCG } } Fallback "Toon/Lighted" } Any help would be much appreciated. Thanks Rich
I cant see any thing to help make this more effecient, but i think you could do more to make the outline looks better
Ok, first of all, thanks for the responses... Lulucifer - Thanks, but I'm pretty happy with the effect at the moment, just want to speed it up as its for a mobile game. Dolkar - It's just the standard ramp texture that comes with the asset... Have you got any pointers for where I could find out how I could compute it procedurally? Hippocoder - You are probably correct, but it achieves the effect I want... Do you have any suggestions how I could create this same effect more efficiently? To be honest, I was thinking it probably doesn't need to calculate the light either as the light doesn't move in my scene and its a 2d based game, I tried to implement this but couldn't get the right look using a constant value for the lightDir. I calculated a vector 3 that took the light's position, took that away from the game objects position and divided it by the distance, but this didn't produce the same effect. Like I said, I've got little / no knowledge of shader programming so any help would be great. Thanks once again...