Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
0
Question by zambino · Mar 30, 2018 at 09:53 PM · shaders

Geometry Intersection Shader

I have seen innumerable examples on how to perform this, yet none of them work anymore, or they only allow one instance of this intersection to appear.

I would like to recreate the intersection shader from The Divison, as seen here

Grenade Highlights

Characters and world geometry alike are highlighted inside a sphere. As well, the intensity of the highlight is a gradient, where in the center it is less intense, and on the edges, it is most intense.

How can I start this? Will I need to render a sphere several times and use the stencil buffer?

maxresdefault.jpg (104.8 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

2 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by Glurth · Mar 31, 2018 at 09:20 PM

This shader "colors" an area inside a "rendered" mesh: for you case, simply create a material with it, and apply it to a sphere (or whatever) placed appropriately in worldspace. It DOES lack the gradient you mentioned, but it's a start. (the sample picture uses 2 concentric spheres with different materials, both using this shader)

 Shader "Unlit/highlightVolume"
 {
     Properties
     {
         _Color("Color", Color) = (1,1,1,.2) 
     }
     SubShader
     {
         Tags{ "Queue" = "Transparent" "RenderType" = "Transparent" }
         Pass
         {
             Stencil
             {
                 Ref 172
                 Comp Always
                 Pass Replace
                 ZFail Zero
             }
             Blend Zero One
             Cull Front
             ZTest  GEqual
             ZWrite Off
 
         }// end stencil pass
         Pass
         {
             Blend SrcAlpha OneMinusSrcAlpha
             Stencil
             {
                 Ref 172
                 Comp Equal
             }
             CGPROGRAM
             #pragma vertex vert
             #pragma fragment frag
             float4 _Color;
             struct appdata
             {
                 float4 vertex : POSITION;
             };
             struct v2f
             {
                 float4 vertex : SV_POSITION;
             };
             v2f vert(appdata v)
             {
                 v2f o;
                 o.vertex = UnityObjectToClipPos(v.vertex);
                 return o;
             }
             fixed4 frag(v2f i) : SV_Target
             {
                 return _Color;
             }
             ENDCG
         }//end color pass
     }
 }

alt text


volume-shader.png (37.4 kB)
Comment
Add comment · Show 6 · 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 zambino · Mar 31, 2018 at 09:41 PM 0
Share

This is useful, however if the camera is inside the object it is applied to, the highlight disappears.

avatar image Radivarig · Aug 09, 2021 at 10:46 PM 0
Share

This works great in builtin, but doesn't in URP. Any chance you would know why?

avatar image Glurth Radivarig · Aug 09, 2021 at 11:03 PM 0
Share

This shader uses the stencil flags, which, as far as I've been able to tell, are not available in neither URP nor HDRP.

avatar image Radivarig Glurth · Aug 09, 2021 at 11:28 PM 0
Share

From the docs it seems that it should be supported for Unity 2020.3 https://docs.unity3d.com/Manual/SL-Stencil.html

A user suggested that multiple passes might be a problem https://forum.unity.com/threads/intersection-shader-cull-front-$$anonymous$$us-cull-back.536812/

Not sure why that would be as the docs also mention multiple passes

Show more comments
avatar image
2

Answer by Bunny83 · Mar 31, 2018 at 01:32 AM

Well, since you want the sphere highlighting to not be visible behind the car in the front you have to use the stencil buffer. A common implementation could be done by useng a two pass stencil shader. The first pass would render just the front faces of the sphere (cull back) and set the stencil value on pass. The second pass would only render the backfaces of the sphere (cull front) and do a stencil compare to only pass if the stencil value is "1". The important part of the second pass is that you now only draw when the ztest fails (ztest greater). That means when the backfaces are actually behind something else. The second pass would use alpha blending to render those backfaces. Note that you would need to do the fading based on the distance of each fragment from the center of the sphere.


Note that this technique may give you problems when your camera would "enter" the sphere area. If you want / need to support this case you basically have to implement the usual depth fail stencil shadows. It works similar to this one but instead of actual rendering the sphere we only increment / decrement the stencil value. You would need a thrid pass to actually rendering the sphere visually.

Comment
Add comment · Show 4 · 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 zambino · Mar 31, 2018 at 03:06 AM 0
Share

Ok, I've done that by having three individual spheres with the stencils. Shader

I'm somewhat stuck on how to get the depth of the pixel which occluded the comparison, in order to fade out the center

avatar image zambino zambino · Mar 31, 2018 at 07:51 PM 0
Share

Well I just realized even what I have is still broken. In the editor, it shows just fine, but my main camera shows parts of the sphere which shouldn't be drawn? alt text

avatar image Bunny83 zambino · Mar 31, 2018 at 08:41 PM 1
Share

Uhm we don't know what your shader does since you haven't shown your code. However i suspect you have the same problem as in this question?

Show more comments

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

87 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

Related Questions

"Sand/grain" URP Shader Artifact On Borders After % Operation 1 Answer

how do you make a texture only show near edges at certain angles 0 Answers

Shader Graph transparency issue 0 Answers

Shader white on Quest 2 when reflection intensity > 0 1 Answer

How do I keep an object lit when a light moves past it? 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