I have never messed with the lighting in Unity or Shaders before. I want to make a Toon or Cel-Shaded look but what i am finding online says one of two options: use the built in Toon Shader or Make my own and apply it to each model individually. The question is, how do i change over to the Toon Shader? And is there a way to apply a custom shader to everything at once?
You can play around with this shader I am working on. It might be working out for you:
Shader "Toon/Lithe"
{
Properties
{
_Color ("Main Colour", Color) = (1,1,1,1)
_RimColor ("Rimlight Colour", Color) = (0,0,0,0.0)
_RimPower ("Rimlight Power", Range(0,10)) = 3.0
_MainTex ("Main Texture (RGB)", 2D) = "gray" {}
_Ramp ("Toon Ramp (RGB)", 2D) = "gray" {}
_MinDistance ("Minimum Distance", Float) = 0
_MaxDistance ("Maximum Distance", Float) = 100
_Treshold ("Cel treshold", Range(1., 20.)) = 5.
_Ambient ("Ambient intensity", Range(0., 0.5)) = 0.1
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 200
Cull Back
CGPROGRAM
// custom lighting function that uses a texture ramp based
// on angle between light direction and normal
// THIS ONLY SEEMS TO WORK FOR SKINNED MESH RENDERERS IN FORWARD LIGHTING
// DEFERRED LIGHTING DOES NOT WORK
#pragma surface surf ToonRamp
#pragma lighting ToonRamp exclude_path:prepass
#pragma multi_compile_instancing
sampler2D _Ramp;
sampler2D _MainTex;
UNITY_INSTANCING_BUFFER_START(Props)
UNITY_DEFINE_INSTANCED_PROP(fixed4, _Color)
UNITY_DEFINE_INSTANCED_PROP(fixed4, _RimColor)
UNITY_DEFINE_INSTANCED_PROP(fixed, _RimPower)
UNITY_DEFINE_INSTANCED_PROP(fixed, _MinDistance)
UNITY_DEFINE_INSTANCED_PROP(fixed, _MaxDistance)
UNITY_DEFINE_INSTANCED_PROP(fixed, _Treshold)
UNITY_DEFINE_INSTANCED_PROP(fixed, _Ambient)
UNITY_INSTANCING_BUFFER_END(Props)
struct Input
{
fixed2 uv_MainTex : TEXCOORD0;
float3 viewDir;
float3 worldPos;
UNITY_VERTEX_INPUT_INSTANCE_ID
};
inline fixed4 LightingToonRamp (SurfaceOutput s, half3 lightDir, half atten)
{
#ifndef USING_DIRECTIONAL_LIGHT
lightDir = normalize(lightDir);
#endif
fixed d = (dot (s.Normal, lightDir) * 0.5 + 0.5) * atten;
fixed3 ramp = lerp(tex2D (_Ramp, fixed2(d,d)).rgb, fixed3(1.0, 1.0, 1.0), 1.0 - s.Alpha);
//ramp *= ramp;
fixed4 c;
// Use s.Alpha here to get some sort of night-torch effect:
c.rgb = min(ramp, s.Albedo) * _LightColor0.rgb; // s.Albedo * (_LightColor0.rgb * _LightColor0.rgb) * ramp; // * s.Alpha; // * (atten * 2);
c.a = _LightColor0.a;
return c;
}
float LightToonShading(float3 normal, float3 lightDir)
{
fixed treshold = UNITY_ACCESS_INSTANCED_PROP(Props, _Treshold);
fixed NdotL = max(0.0, dot(normalize(normal), normalize(lightDir)));
return floor(NdotL * treshold) / (treshold - 0.5);
}
void surf (Input IN, inout SurfaceOutput o)
{
fixed4 colour = UNITY_ACCESS_INSTANCED_PROP(Props, _Color);
fixed startFade = UNITY_ACCESS_INSTANCED_PROP(Props, _MinDistance);
fixed endFade = UNITY_ACCESS_INSTANCED_PROP(Props, _MaxDistance);
fixed dist = distance(_WorldSpaceCameraPos, IN.worldPos);
fixed weight = saturate( (dist - startFade) / (endFade - startFade));
fixed4 rimColour = UNITY_ACCESS_INSTANCED_PROP(Props, _RimColor);
fixed rimPower = UNITY_ACCESS_INSTANCED_PROP(Props, _RimPower);
fixed rim = 1.0 - saturate(dot (normalize(IN.viewDir), normalize(o.Normal)));
fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * colour;
//fixed4 c = tex2D(_MainTex, IN.uv_MainTex);
//c.rgb *= saturate(LightToonShading(o.Normal, _WorldSpaceLightPos0.xyz) + UNITY_ACCESS_INSTANCED_PROP(Props, _Ambient)) * _LightColor0.rgb;
o.Albedo = c.rgb + (rimColour.rgb * pow(rim, rimPower));
o.Alpha = c.a * ((weight * weight));
//o.Emission = o.Normal * c.rgb * IN.viewDir;
//o.Emission = c.rgb * c.rgb; // * _LightColor0.rgb; // rimColour.rgb * pow(rim, rimPower) * c.rgb;
}
ENDCG
}
Fallback "Diffuse"
}
This is the effect it gets:
Um…once i applied this to a model to see how it looked it just turned them white.
Did you apply a texture and a toonramp? The toonramp is usually a horizontal gradient from left to right being dark to light. It is based on ToonLitOutline which has been a part of the Unity files you can add to a project.
Lol when i said i was new at this, i meant i knew nothing about shading. Thank you
