Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 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
0
Question by patben8 · Jan 10 at 02:14 AM · renderinglayerspost processingeffectspost-process-effect

How can I make a camera render only one layer WITH occlusion?

Hi everyone. I've been racking my head trying to figure this out. Basically what I'm looking for is to have a camera render only one layer of objects, just like changing the culling mask on a camera, however I also need the object to be occluded by objects in other layers.

Here's an example of what I mean: I've got a scene with a Sphere and a cube. The sphere is in front of the cube, partially occluding it. Like so: alt text


And what I'm looking to achieve is a camera output that looks like this: alt text


I managed to achieve that result by using a shader that uses a render texture of only the sphere as a mask for a second render texture of only the cube. However the issue is that if the occluding object (in this case the sphere) moves behind the object, you get the following: alt text Which is the same result you would get if the sphere were in front of the cube. When instead I'd want it to look like this: alt text Since the cube is covering the sphere, the cube is "in front" and is therefore "complete", as opposed to having the sphere in front of the cube in which case the cube is missing a "sphere sized chunk".


I know this is probably an obscure question but if anyone has any ideas on how I could achieve this, I would love to hear it! Thanks in advance for any help you can provide.


PS: I'm using the URP. The ultimate goal is to pixelate the "special" masked layer while leaving all other objects rendering at full resolution.

e1.png (59.6 kB)
e2.png (10.1 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

Answer by MaxKramarov · Jan 12 at 11:32 AM

Do next:

  1. Draw mask geometry before opaque geometry by the special shader (depthWrite=true, stenscil=1, colorWrite=false)

  2. Then draw opaque geometry that has to be masked. Draw it by your normal shaders. The mask from the previous step will be "applied" because the mask is in the depth buffer. Also, override the stencil for maskable geometry. Set it in 2.

  3. You have to remove mask geometry from the depth buffer to skybox and transparent geometry display properly. For that clear depth buffer where stencil equals 2. You can clear the depth buffer by drawing a plane over the screen with a shader that has depthTest=always and sets vertex position in max possible value.

So you URP renderer should seem: alt text

Shader for mask geometry:

 Shader "Unlit/MaskShader"
 {
     Properties
     {
     }
     SubShader
     {
         Tags { "RenderType"="Opaque" }
         LOD 100
 
         Pass
         {
             ColorMask 0
         }
     }
 }
 

Shader for the plane that clears depth:

 Shader "Unlit/RestoreDepthBuffer"
 {
     Properties
     {
     }
     SubShader
     {
         Tags { "RenderType"="Opaque" }
         LOD 100
 
         Pass
         {
             ColorMask 0
             ZTest Always
             
             CGPROGRAM
             #pragma vertex vert
             #pragma fragment frag
 
             #include "UnityCG.cginc"
 
             struct appdata
             {
                 float4 vertex : POSITION;
             };
 
             struct v2f
             {
                 float4 vertex : SV_POSITION;
             };
 
             v2f vert (appdata v)
             {
                 v2f o;
                 o.vertex = UnityObjectToClipPos(v.vertex);
 #if UNITY_REVERSED_Z
                 o.vertex.z = 0;
 #else
                 o.vertex.z = _ProjectionParams.z;
 #endif
                 o.vertex.w = _ProjectionParams.z;
                 return o;
             }
 
             void frag (v2f i)
             {
             }
             ENDCG
         }
     }
 }
 



screenshot-30.png (111.0 kB)
Comment
Add comment · Show 1 · 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 MaxKramarov · Jan 12 at 11:39 AM 0
Share

About the plane... Just put it inside the camera to draw it over whole screen

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

151 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

Related Questions

URP: How can I view ScriptableRendererFeature in scene view? 1 Answer

Post-Processing effects not working,Cant get post-processing effects to work 2 Answers

Modify values on VolumeProfile URP. 1 Answer

How to properly use RTHandles in HDRP for post effects 0 Answers

Custom URP Postprocessing 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