x


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.

alt text

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"
}
more ▼

asked Jul 09 '11 at 03:20 AM

AnomalusUndrdog gravatar image

AnomalusUndrdog
86 2 2 4

Have you solved this yet?

Aug 03 '12 at 05:45 PM hrlarsen

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

Aug 03 '12 at 06:39 PM alucardj

Looks like that script is for GUI not for models.

Aug 05 '12 at 07:25 PM whydoidoit

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.

Aug 05 '12 at 07:45 PM RodrigoSeVeN
(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

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.

more ▼

answered Aug 06 '12 at 01:35 AM

RodrigoSeVeN gravatar image

RodrigoSeVeN
91 1 3

(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Nov 07 '12 at 02:46 PM

thallippoli gravatar image

thallippoli
1 1

(comments are locked)
10|3000 characters needed characters left

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
		}
	}
}
more ▼

answered Aug 05 '12 at 08:30 PM

ScroodgeM gravatar image

ScroodgeM
7.6k 2 6

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1745
x133
x45
x6

asked: Jul 09 '11 at 03:20 AM

Seen: 7168 times

Last Updated: Jan 24 at 10:36 PM