My code is for a borderlands shader. Works great, until I move the sphere away…
Shader "SFX/Toon Outline" {
Properties {
_Color ("Main Color", Color) = (1, 1, 1, 1)
_Shadow ("Shadow Strength", Range (0, 1)) = 0.5
_Threshold ("Shadow Threshold", Range (0, 1)) = 0.5
_Diffuse ("Diffuse Amount", Range (0, 1)) = 0.5
_Color3 ("Outline Color", Color) = (0, 0, 0, 1)
_Outline ("Outline width", Range (.002, 0.03)) = .005
_MainTex ("Base (RGB)", 2D) = "white" { }
}
CGINCLUDE
#include "UnityCG.cginc"
float _Outline;
float4 _Color;
float4 _Color2;
float4 _Color3;
ENDCG
SubShader
{
Tags
{
"RenderType" = "Opaque"
"LightMode" = "ForwardBase"
}
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
float _Shadow;
float _Threshold;
float _Diffuse;
sampler2D _MainTex;
float4 _MainTex_ST;
float4 _LightColor0;
struct vertexInput {
float4 pos : POSITION;
float3 norm : NORMAL;
float4 tex : TEXCOORD0;
};
struct vertexOutput {
float4 pos : POSITION;
float4 col : COLOR;
float4 tex : TEXCOORD0;
};
vertexOutput vert (vertexInput input) {
float3 normalDirection = normalize (mul (float4 (input.norm, 0), _World2Object)).xyz;
float3 lightDirection = normalize (_WorldSpaceLightPos0).xyz;
float3 diffuseReflection = dot (normalDirection, lightDirection);
float3 finalColor = diffuseReflection * _Color.rgb + UNITY_LIGHTMODEL_AMBIENT;
vertexOutput output;
output.pos = mul (UNITY_MATRIX_MVP, input.pos);
output.col = float4 (finalColor, 1);
output.tex = input.tex;
output.tex.xy = TRANSFORM_TEX (input.tex, _MainTex);
return output;
}
float4 frag (vertexOutput output) : COLOR {
float4 col = output.col;
if ((col.r + col.g + col.b) / 3 <= _Threshold)
{
col = float4 (saturate (_Color.x - _Shadow), saturate (_Color.y - _Shadow), saturate (_Color.z - _Shadow), 1);
}
else
{
col = _Color;
}
float4 finalCol = lerp (col, output.col, _Diffuse) * tex2D (_MainTex, output.tex);
return finalCol;
}
ENDCG
}
Pass
{
Cull Front
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
struct vertexInput {
float4 pos : POSITION;
float3 norm : NORMAL;
};
struct vertexOutput {
float4 pos : POSITION;
fixed4 color : COLOR;
};
vertexOutput vert (vertexInput input) {
vertexOutput output;
output.pos = mul (UNITY_MATRIX_MVP, input.pos);
float3 norm = normalize (mul ((float3x3) UNITY_MATRIX_MV, input.norm));
float2 offset = TransformViewToProjection (norm.xy);
output.pos.xy += offset * _Outline;
output.color = _Color3;
return output;
}
float4 frag (vertexOutput output) : COLOR {
return output.color;
}
ENDCG
}
}
}