- Home /
Making a silhouette outline shader
I'm trying to make an outline shader that only shows outline from the model's silhouette, to be used as indication that the character has been selected.
the current problem with this one is the outline doesn't show in the feet, because of the offset, it gets hidden by the ground.
I want the outline to be above the terrain, but below the character.
Here is the code:
Shader "Outlined/Diffuse" {
Properties {
_Color ("Main Color", Color) = (.5,.5,.5,1)
_OutlineColor ("Outline Color", Color) = (0,0,0,1)
_Outline ("Outline width", Range (.002, 0.03)) = .005
_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" = "Overlay" }
CGPROGRAM
#pragma surface surf Lambert
sampler2D _MainTex;
fixed4 _Color;
struct Input {
float2 uv_MainTex;
};
void surf (Input IN, inout SurfaceOutput o) {
fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
o.Albedo = c.rgb;
o.Alpha = c.a;
}
ENDCG
// note that a vertex shader is specified here but its using the one above
Pass {
Name "OUTLINE"
Tags { "LightMode" = "Always" "Queue" = "Overlay" }
Cull Front
ZWrite On
ZTest LEqual
ColorMask RGB
Blend SrcAlpha OneMinusSrcAlpha
Offset 15,15
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
half4 frag(v2f i) :COLOR { return i.color; }
ENDCG
}
}
SubShader {
Tags {"Queue" = "Overlay" }
CGPROGRAM
#pragma surface surf Lambert
sampler2D _MainTex;
fixed4 _Color;
struct Input {
float2 uv_MainTex;
};
void surf (Input IN, inout SurfaceOutput o) {
fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
o.Albedo = c.rgb;
o.Alpha = c.a;
}
ENDCG
Pass {
Name "OUTLINE"
Tags { "LightMode" = "Always" }
Cull Front
ZWrite On
ColorMask RGB
Blend SrcAlpha OneMinusSrcAlpha
CGPROGRAM
#pragma vertex vert
#pragma exclude_renderers gles xbox360 ps3
ENDCG
SetTexture [_MainTex] { combine primary }
}
}
Fallback "Diffuse"
}
There was one from memory on the UnifyCommunity (wiki) 'site, but of course that is out of action right now. This was the link : http://unifycommunity.com/wiki/index.php?title=ShadowAndOutline
Does anyone have this script ?
Edit : whydoidoit is correct, I was thinking of : http://wiki.unity3d.com/index.php/Silhouette-Outlined_Diffuse
Just to "outline" the issue, the main problem is how the shader uses a mesh copy of your model to outline it, so it is limited in the same way a normal model would be. Considering this, right now I can only think of using multiple cameras, rendering only specific layers, in this order for example: Ground - Shader - Character. I'm sure this can work with some testing put into it. Good luck for now, I'll try to test this so I can post an answer.
Answer by ScroodgeM · Aug 05, 2012 at 08:30 PM
this is not exactly what asked for, but methinks this is a good alternative to do outline with low CPU/GPU load. real outline will take much more improvements to achieve.
Shader "Unity Answers/Diffuse Rim"
{
Properties
{
_MainTex ("Base (RGB)", 2D) = "white" { }
_Color ("Main Color", Color) = (.5,.5,.5,1)
_OutlineColor ("Outline Color", Color) = (0,0,0,1)
_OutlineOffset ("Outline offset", Range (0, 1)) = 0.1
_OutlineSize ("Outline size", Range (0, 1)) = 0.1
}
SubShader
{
Tags { "RenderType"="Opaque" }
CGPROGRAM
#pragma surface surf Lambert
sampler2D _MainTex;
fixed4 _Color;
struct Input
{
float2 uv_MainTex;
};
void surf (Input IN, inout SurfaceOutput o)
{
fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
o.Albedo = c.rgb;
o.Alpha = c.a;
}
ENDCG
//ouline pass
Pass
{
Blend SrcAlpha OneMinusSrcAlpha
Cull Back
Offset -1, -1
Lighting Off
ZWrite Off
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
float _OutlineOffset;
float _OutlineSize;
fixed4 _OutlineColor;
uniform float4x4 _Object2World, _World2Object;
uniform float3 _WorldSpaceCameraPos;
uniform float4 _Time;
struct appdata
{
float4 vertex : POSITION;
float3 normal : NORMAL;
};
struct v2f
{
float4 pos : SV_POSITION; // project view position
float4 wPos : TEXCOORD0; // world-based position
float4 wNor : TEXCOORD1; // world-based normal
};
v2f vert(appdata v)
{
v2f o;
v.vertex.xyz += _OutlineOffset * v.normal;
o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
o.wPos = mul(_Object2World, v.vertex);
o.wNor = mul(_Object2World, float4(v.normal.xyz, 0));
return o;
}
half4 frag(v2f i) : COLOR
{
float3 worldNormal = normalize(i.wNor.xyz);
float3 worldToCameraDirection = normalize(_WorldSpaceCameraPos - i.wPos.xyz);
half4 answer = _OutlineColor;
answer.a *= saturate((_OutlineSize - dot(worldNormal, worldToCameraDirection)) * 100f);
return answer;
}
ENDCG
}
}
}
Answer by RodrigoSeVeN · Aug 06, 2012 at 01:35 AM
I'll post this as an answer because it's another possibility, not sure if it's costly when scaled.
It's simple but may prove to be unpractical to manage at some point(in case you modify the camera a lot).
Using the first shader posted, you duplicate your camera and create one new layer, let's call it 'Outlined'.
You select your object that has the mesh with that shader, and change the layer to 'Outlined'.
On one camera, you only change the culling mask value, removing the 'Outlined' layer from it. On the other, you have to change three values:
On "Clear Flags" change to "Depth only";
On "Culling mask", select Nothing, then select only the 'Outlined' layer;
Lastly, set the "Depth" value to 0(while the other camera will be set to -1);
This works properly and may be a valid option.
Answer by thallippoli · Nov 07, 2012 at 02:46 PM
Won't disabling the depth test with ZTest Always for the outline-mesh do the trick? Also, this will cause the outline to appear "through" other objects - but since you are doing this to indicate selection, this is probably acceptable.
**hi, as you said, the outline really appear "through " other objects.
Does it possible to aviod the outline to appear "through" other objects????**
@cgcoder The only solution I can think of with this approach is to make sure you draw your objects in strictly this order
Objects for which it's okay for the outline to "appear through"
The mesh for which you want the outline
The outline-mesh
Objects that you don't want the outline to appear through
you should be able to achieve this if you put these objects into render queues that follow the above order.
Answer by MrVerdoux · Jan 09, 2014 at 09:45 AM
Old question, but anyway it seems important enough to share this answer: I found a really good source here although I modified the final result (very slightly). It´s not perfect, but if your camera is not too close to the object and the outline is thin enough, it works well. As it´s a long shader I included it as a unity package with shader, material and a test scene.
Your answer
![](https://koobas.hobune.stream/wayback/20220612135647im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Silhouette Outline Shader and SpriteRenderer 0 Answers
Silhouetted-Outlined Shader not working with baked lights. 1 Answer
Silhouette Toon Shader 0 Answers
Object outline component 0 Answers
Model Outline/Silhouette Shader 1 Answer