Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
4
Question by AnomalusUndrdog · Jul 09, 2011 at 03:20 AM · shaderselectionoutlinesilhouette

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"
 }
Comment
Add comment · Show 6
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image hrlarsen · Aug 03, 2012 at 05:45 PM 1
Share

Have you solved this yet?

avatar image AlucardJay · Aug 03, 2012 at 06:39 PM 0
Share

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

avatar image ScroodgeM · Aug 05, 2012 at 07:23 PM 0
Share

http://webcache.googleusercontent.com/search?q=cache:OwN7XgF6rdkJ:unifycommunity.com/wiki/index.php%3Ftitle%3DShadowAndOutline+&cd=1&hl=ru&ct=clnk≷=ru

avatar image whydoidoit · Aug 05, 2012 at 07:25 PM 0
Share

Looks like that script is for GUI not for models.

avatar image RodrigoSeVeN · Aug 05, 2012 at 07:45 PM 0
Share

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.

Show more comments

4 Replies

· Add your reply
  • Sort: 
avatar image
2

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

}

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image cgcoder · Nov 11, 2013 at 07:58 AM 0
Share

**hi, as you said, the outline really appear "through " other objects.

Does it possible to aviod the outline to appear "through" other objects????**

avatar image thallippoli · Nov 11, 2013 at 08:18 AM 0
Share

@cgcoder The only solution I can think of with this approach is to make sure you draw your objects in strictly this order

  1. Objects for which it's okay for the outline to "appear through"

  2. The mesh for which you want the outline

  3. The outline-mesh

  4. 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.

avatar image
0

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.

Outline shader package


outline.unitypackage.zip (23.2 kB)
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

12 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Model Outline/Silhouette Shader 1 Answer

Display object with subobjects when it's behind the wall 0 Answers

can you make a surface shader be drawn before or after a certain pass? 1 Answer

Outlined Shader Shadows 1 Answer

Silhouette Outline Shader and SpriteRenderer 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges