• 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
0
Question by Inx51 · Feb 13, 2017 at 08:20 AM · shaderscaleoutlinenormalwidth

Normal length? Outline shader

Hi Im trying to create myself an outline shader based on the one provided in the SteamVR - plugin. (Even if I kind of modified the entire shader by now :)).. Anyhow.. Im having some issue with retaining the "scale"/width of the outline between different meshes/objects.. and I cant really get why this is happening..

alt text

As you can see.. both objects/meshes uses the very same material with the very same shader.. however.. they get different "width" of the outline..

Any help would be very much appreciated!

Oh.. guess I should mention that the shader is currently doing some stencil-magic to be able to use "layered"-materials (multiple materials on one object, and yes.. Im aware of the performance hit this might end up with :)).

Shader:

 // Upgrade NOTE: replaced '_Object2World' with 'unity_ObjectToWorld'
 
 Shader "Unlit/Outline"
 {
     Properties
     {
         _OutlineColor("Color", Color) = (0,0,0,0)
         _OutlineWidth("Width", Range(.0001, 0.1)) = .0005
     }
 
     SubShader
     {
         Tags
         { 
             "RenderType" = "Transparent"
             "Queue" = "Transparent" 
         }
 
         LOD 100
         
         ////////////////////
         // Base
         ////////////////////
         Pass
         {
             Tags
             { 
                 "LightMode" = "Always"
                 "Queue" = "Transparent" 
             }
 
             ColorMask A
             Cull Off
             ZWrite On
 
             Stencil
             {
                 Ref 1
                 Comp LEqual
                 Pass replace
                 Fail replace
             }
 
             CGPROGRAM
 
                 #pragma vertex vert
                 #pragma fragment frag
 
                 #include "UnityCG.cginc"
 
                 struct appdata
                 {
                     float4 vertex : POSITION;
                     float2 uv : TEXCOORD0;
                     float3 normal : NORMAL;
                 };
 
                 struct v2f
                 {
                     float2 uv : TEXCOORD0;
                     UNITY_FOG_COORDS(1)
                     float4 vertex : SV_POSITION;
                 };
 
                 sampler2D _MainTex;
                 float4 _MainTex_ST;
                 float _OutlineWidth;
                 float4 _OutlineColor;
 
                 v2f vert(appdata_full v)
                 {
                     v2f o;
                     o.vertex = UnityObjectToClipPos(v.vertex);
                     return o;
                 }
 
                 fixed4 frag(v2f i) : SV_Target
                 {
                     fixed4 col = tex2D(_MainTex, i.uv);
                     UNITY_APPLY_FOG(i.fogCoord, col);
                     return _OutlineColor;
                 }
 
             ENDCG
         }
         
         ////////////////////
         // Outline
         ////////////////////
         Pass
         {
             Tags
             { 
                 "LightMode" = "Always"
                 "Queue" = "Transparent" 
             }
 
             Cull Back
             Blend SrcAlpha OneMinusSrcAlpha
             ZWrite On
 
             Stencil
             {
                 Ref 1
                 Comp notequal
                 Pass replace
                 Fail replace
             }
 
             CGPROGRAM
 
                 #pragma vertex vert
                 #pragma fragment frag
 
                 #include "UnityCG.cginc"
 
                 struct v2f
                 {
                     float2 uv : TEXCOORD0;
                     UNITY_FOG_COORDS(1)
                     float4 vertex : SV_POSITION;
                 };
 
                 sampler2D _MainTex;
                 float4 _MainTex_ST;
                 float _OutlineWidth;
                 float4 _OutlineColor;
 
                 v2f vert(appdata_full v)
                 {
                     v2f o;
                     float3 n = v.normal;
                     v.vertex.xyz += (n.xyz * _OutlineWidth);
                     o.vertex = UnityObjectToClipPos(v.vertex);
                     return o;
                 }
 
                 fixed4 frag(v2f i) : SV_Target
                 {
                     fixed4 col = tex2D(_MainTex, i.uv);
                     UNITY_APPLY_FOG(i.fogCoord, col);
                     return _OutlineColor;
                 }
 
             ENDCG
         }
     }
 }

Br, Inx

capture.png (201.9 kB)
Comment
Add comment
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

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Shader Outline scale problem since U5 1 Answer

Shader: actual view direction/normal? 2 Answers

Combine Shaders 0 Answers

Procedural heightmap shader - Normals and tangents 0 Answers

bumped self illumination shader question 3 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