- Home /
Question by
Brandondpbell · Mar 15, 2014 at 03:04 PM ·
shadershader writingoutline
Shader edit help/request: outline only and color feature
Hi everyone, I'm asking for help on shaders I found online which I can't figure out.
A) The first adds an Outline to 2D Sprites. B) A Shiny Toon Shader. C) Non-Toon Oultine Shader
For [A] I'd like to be able to change Outline Color via Inspector. And a variant which draws only the outline of the sprite.
For [B] Same: Changing Outline Color and a variant which draws only the outline of the object.
For [C] Same: Drawing only the outline, not the actual Object.
I've tried hard but any help would be greatly appreciated. Thanks in advance!
A: Outline_2DSprite
Shader "Custom/Outline_2DSprite" {
Properties
{
_MainTex ("Base (RGB)", 2D) = "white" {}
_OutLineSpread ("Outline Spread", Range(0,0.012)) = 0.007
}
SubShader
{
Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
ZWrite On Blend One OneMinusSrcAlpha Cull Off
LOD 110
CGPROGRAM
#pragma surface surf Lambert alpha
struct Input
{
float2 uv_MainTex;
fixed4 color : COLOR;
};
sampler2D _MainTex;
float _OutLineSpread;
void surf(Input IN, inout SurfaceOutput o)
{
fixed4 mainColor = (tex2D(_MainTex, IN.uv_MainTex+float2(_OutLineSpread,_OutLineSpread)) + tex2D(_MainTex, IN.uv_MainTex-float2(_OutLineSpread,_OutLineSpread))) * fixed4(0,0,0,1);
fixed4 addcolor = tex2D(_MainTex, IN.uv_MainTex) * IN.color;
if(addcolor.a > 0.95){
mainColor = addcolor;}
o.Albedo = mainColor.rgb;
o.Alpha = mainColor.a;
}
ENDCG
}
SubShader
{
Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
ZWrite Off Blend One OneMinusSrcAlpha Cull Off Fog { Mode Off }
LOD 100
Pass {
Tags {"LightMode" = "Vertex"}
ColorMaterial AmbientAndDiffuse
Lighting On
SetTexture [_MainTex]
{
Combine texture * primary double, texture * primary
}
}
}
Fallback "Diffuse", 1
}
B) Shiny Toon Shader
Shader "Custom/Toon_Shiny" {
Properties {
_MainTex ("Base (RGB)", 2D) = "white" {}
_Bump ("Bump", 2D) = "bump" {}
_Tooniness ("Tooniness", Range(0.1,20)) = 4
_ColorMerge ("Color Merge", Range(0.1,20000)) = 8
_Ramp ("Ramp Texture", 2D) = "white" {}
_Outline ("Outline", Range(0,1)) = 0.4
}
SubShader {
Tags { "RenderType"="Opaque" }
LOD 200
CGPROGRAM
// Upgrade NOTE: excluded shader from Xbox360 because it uses wrong array syntax (type[size] name)
#pragma exclude_renderers xbox360
#pragma surface surf Toon
sampler2D _MainTex;
sampler2D _Bump;
sampler2D _Ramp;
float _Tooniness;
float _Outline;
float _ColorMerge;
struct Input {
float2 uv_MainTex;
float2 uv_Bump;
float3 viewDir;
};
void surf (Input IN, inout SurfaceOutput o) {
half4 c = tex2D (_MainTex, IN.uv_MainTex);
o.Normal = UnpackNormal( tex2D(_Bump, IN.uv_Bump));
half edge = saturate(dot (o.Normal, normalize(IN.viewDir)));
edge = edge < _Outline ? edge/4 : 1;
o.Albedo = (floor(c.rgb*_ColorMerge)/_ColorMerge) * edge;
o.Alpha = c.a;
}
half4 LightingToon(SurfaceOutput s, half3 lightDir, half atten )
{
half4 c;
half NdotL = dot(s.Normal, lightDir);
NdotL = tex2D(_Ramp, float2(NdotL, 0.5));
c.rgb = s.Albedo * _LightColor0.rgb * NdotL * atten * 2;
c.a = s.Alpha;
return c;
}
ENDCG
}
FallBack "Diffuse"
}
C) 3D Outline Basic Shader
Shader "Custom/Outline_ObjectBasic" {
Properties {
_Color ("Main Color", Color) = (.5,.5,.5,1)
_OutlineColor ("Outline Color", Color) = (0,0,0,1)
_Outline ("Outline width", Range (0.0, 0.03)) = .01
_MainTex ("Base (RGB)", 2D) = "white" { }
}
CGINCLUDE
#include "UnityCG.cginc"
struct appdata {
float4 vertex : POSITION;
float3 normal : NORMAL;
};
struct v2f {
float4 pos : POSITION;
float4 color : COLOR;
};
uniform float _Outline;
uniform float4 _OutlineColor;
v2f vert(appdata v) {
// just make a copy of incoming vertex data but scaled according to normal direction
v2f o;
o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
float3 norm = mul ((float3x3)UNITY_MATRIX_IT_MV, v.normal);
float2 offset = TransformViewToProjection(norm.xy);
o.pos.xy += offset * o.pos.z * _Outline;
o.color = _OutlineColor;
return o;
}
ENDCG
SubShader {
Tags { "Queue" = "Transparent" }
// note that a vertex shader is specified here but its using the one above
Pass {
Name "OUTLINE"
Tags { "LightMode" = "Always" }
Cull Off
ZWrite Off
ColorMask RGB // alpha not used
// you can choose what kind of blending mode you want for the outline
Blend SrcAlpha OneMinusSrcAlpha // Normal
//Blend One One // Additive
//Blend One OneMinusDstColor // Soft Additive
//Blend DstColor Zero // Multiplicative
//Blend DstColor SrcColor // 2x Multiplicative
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
half4 frag(v2f i) :COLOR {
return i.color;
}
ENDCG
}
Pass {
Name "BASE"
ZWrite On
ZTest LEqual
Blend SrcAlpha OneMinusSrcAlpha
Material {
Diffuse [_Color]
Ambient [_Color]
}
Lighting On
SetTexture [_MainTex] {
ConstantColor [_Color]
Combine texture * constant
}
SetTexture [_MainTex] {
Combine previous * primary DOUBLE
}
}
}
SubShader {
Tags { "Queue" = "Transparent" }
Pass {
Name "OUTLINE"
Tags { "LightMode" = "Always" }
Cull Front
ZWrite Off
ColorMask RGB
// you can choose what kind of blending mode you want for the outline
Blend SrcAlpha OneMinusSrcAlpha // Normal
//Blend One One // Additive
//Blend One OneMinusDstColor // Soft Additive
//Blend DstColor Zero // Multiplicative
//Blend DstColor SrcColor // 2x Multiplicative
//CGPROGRAM
//#pragma vertex vert
//#pragma exclude_renderers gles xbox360 ps3 flash
//ENDCG
SetTexture [_MainTex] { combine primary }
}
Pass {
Name "BASE"
ZWrite On
ZTest LEqual
Blend SrcAlpha OneMinusSrcAlpha
Material {
Diffuse [_Color]
Ambient [_Color]
}
Lighting On
SetTexture [_MainTex] {
ConstantColor [_Color]
Combine texture * constant
}
SetTexture [_MainTex] {
Combine previous * primary DOUBLE
}
}
}
Fallback "Diffuse"
}
Comment