• 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 thelonelypanda · Dec 10, 2020 at 12:37 AM · shadershader programming

How to allow an overlapping object's edge to show in front of another object with the same shader.

This is what it looks like currently, but I want the black outline to outline the whole sphere when in front of the other object in the scene.

alt text


Here is my shader coder:

 Shader "test/shader"
 {
     Properties
     {
         _Color("Color", Color) = (1,1,1,1)
         _LightPosition("Light Position", Vector) = (0,0,0,0)
         _OutLineColor("Outline Color", Color) = (0,0,0,1)
         _OutlineThickness("Outline Thickness", Range(1,1.5)) = 1.1
     }
 
     SubShader
     {
         Pass //Outline
         {
             ZWrite Off
 
             CGPROGRAM
             #pragma vertex vert
             #pragma fragment frag
 
             #include "UnityCG.cginc"
 
             struct appdata
             {
                 float4 vertex : POSITION;
                 float2 uv : TEXCOORD0;
                 float3 normal : NORMAL;
             };
             struct v2f
             {
                 float4 position : SV_POSITION;
                 float2 uv : TEXCOORD0;
                 float3 normal : NORMAL;
             };
 
             fixed4 _OutLineColor;
             fixed4 _OutlineThickness;
 
             v2f vert(appdata IN)
             {
                 v2f OUT;
                 OUT.normal = IN.normal;
                 OUT.position = UnityObjectToClipPos(IN.vertex * 1.1);
                 OUT.uv = IN.uv;
 
                 return OUT;
             }
 
             fixed4 frag(v2f IN) : SV_Target
             {
                 float3 outlineColor = _OutLineColor.rgb;
                 return float4(outlineColor, 1);
             }
 
             ENDCG
         }
 
         Pass //Main
         {
             CGPROGRAM
 
             #pragma vertex vert
             #pragma fragment frag
 
             #include "UnityCG.cginc"
 
             struct appdata
             {
                 float4 vertex : POSITION;
                 float2 uv : TEXCOORD0;
                 float3 normal : NORMAL;
             };
 
             struct v2f 
             {
                 float4 position : SV_POSITION;
                 float2 uv : TEXCOORD0;
                 float3 normal : NORMAL;
             };
 
             fixed4 _Color;
             float4 _LightPosition;
             
             v2f vert(appdata IN)
             {
                 v2f OUT;
                 OUT.normal = IN.normal;
                 OUT.position = UnityObjectToClipPos(IN.vertex);
                 OUT.uv = IN.uv;
 
                 return OUT;
             }
 
             fixed4 frag(v2f IN) : SV_Target
             {
                 //Direct Light
                 float3 lightDir = _WorldSpaceLightPos0.xyz;
                 float3 color = _Color.rgb;
                 fixed3 lightFalloff = saturate(dot(lightDir, IN.normal));
                 lightFalloff = step(0.1, lightFalloff);
                 float3 directDiffuseLight = color * lightFalloff;
 
                 //Ambient light
                 float3 ambientLight = float3(0.1,0.1,0.1);
 
                 //Composite
                 float3 diffuseLight = ambientLight + directDiffuseLight;
                 float3 finalSurfaceColor = diffuseLight * color;
                 float1 zero = 0;
                 return float4(finalSurfaceColor, zero);
             }
 
             ENDCG
         }
     }
 }
 

I believe the problem lies at the beginning of the first pass, the "ZWrite Off" specifically. But the thing is that I believe that is necessary in order for the upscaled mesh to appear behind the main mesh, and maybe that is why the upscaled/outline mesh appears behind other objects with the same shader.

Is there a simple fix to this?

unity-answers-pic.jpg (9.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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by thelonelypanda · Dec 10, 2020 at 09:56 PM

I figured it out for anyone having the same problem. I flipped the passes around so that the second pass was the outline. Then I replaced "Zwrite Off" with "Cull Front".


Even though that solved that specific problem, don't use my shader script, as the way it is written requires objects that have equal height, width, and depth. If the dimensions are different then the outline thickness will also be different.

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.

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

214 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

Find position of pixel that's currently being calculated in shadergraph custom function 0 Answers

Only render stencil mask when behind maskable object 0 Answers

Silhouette overlay shader 0 Answers

Shader forge & Spine 1 Answer

Shader was doing good suddenly shows this error pls help 1 Answer


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