Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
This question was closed Jan 04, 2019 at 08:35 AM by Bunny83 for the following reason:

The question is answered, right answer was accepted

avatar image
9
Question by AlBench · Sep 11, 2012 at 01:12 AM · render

Can I obscure an object using an invisible object?

I'd like to have an object closer to the camera "cut-out" the object behind it. The closer object needs to be invisible though. Is this possible?

Comment
Comments Locked
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

  • Sort: 
avatar image
40
Best Answer

Answer by aldonaletto · Sep 11, 2012 at 02:44 AM

You can use a special shader that only writes to the depth buffer. That's not so difficult, but you may end with a black area in the screen! The problem is: since this shader doesn't actually draw anything, the area behind the object will remain with the original image, which is defined by the camera's Clear Flags. In order to avoid this, you must handle the rendering order so that the background items are rendered first, then the invisible object, and finally the objects that may be obscured. You can do that by setting the property material.renderQueue of each object, assigning greater values to the ones that should be rendered last (read more about renderQueue values in the shader docs).

The shader itself could be as simple as this one:

Shader "Custom/InvisibleMask" {
  SubShader {
    // draw after all opaque objects (queue = 2001):
    Tags { "Queue"="Geometry+1" }
    Pass {
      Blend Zero One // keep the image behind it
    }
  } 
}
Save this shader as "InvisibleMask.shader" (or other suitable name) somewhere in the Assets folder, then create a new material (InvisibleMat, for instance), select this shader and assign the material to the invisible mask object.

Now you must set the rendering order for the "obscurable" objects: they must have a renderQueue value >= 2002, since the shader above has a render queue value = Geometry+1 (2001). You can do this easily by attaching the script below to any "obscurable" object:

function Start(){
  // get all renderers in this object and its children:
  var renders = GetComponentsInChildren(Renderer);
  for (var rendr: Renderer in renders){
    rendr.material.renderQueue = 2002; // set their renderQueue
  }
}
Comment
Comments Locked · Show 16 · 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 AlBench · Sep 11, 2012 at 02:51 PM 0
Share

Thanks for your prompt reply. I understand the first bit regarding the "Invisible$$anonymous$$ask" shader; however, as I am new to writing, I don't know what to do with the function part. Where does this fall in the script string? $$anonymous$$any thanks.

avatar image invadererik · Jan 22, 2013 at 03:48 AM 1
Share

is there any way to do this same idea with a gradient ?

avatar image aldonaletto · Jan 22, 2013 at 10:26 PM 1
Share

If you mean something like having soft borders in the mask, the answer is no: the mask just blocks rendering any pixel behind it due to the depth buffer (A$$anonymous$$A Z-buffer). Despite the mask itself doesn't render anything to the screen, the depth of each of its pixels is recorded in the Z-buffer. Once the mask has been "rendered", any further rendering behind it is blocked.

avatar image cruisereric · Feb 03, 2018 at 01:13 PM 7
Share

hey, I have changed the above code a little bit to avoid the black area in the screen.

I changed Tags { "Queue"="Geometry+1" } to Tags { "Queue"="Transparent+1" } and also rendr.material.renderQueue = 2002; to rendr.material.renderQueue = 3002;

this way my material is transparent and no black area is seen.

avatar image tourajv cruisereric · Aug 07, 2018 at 07:09 AM 0
Share

Thank you so much

avatar image GameDev_Chuck · Jan 04, 2019 at 02:27 AM 3
Share

I don't care if this answer was made in 2012. You were awesome then and you are awesome now! THAN$$anonymous$$ YOU!

Show more comments

Follow this Question

Answers Answers and Comments

23 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

Related Questions

using openGL to render simple line 1 Answer

Another Java to C# Conversion Question!! 1 Answer

how to render bothsides? 2 Answers

How to make a mini map? 1 Answer

hi all, Low-level Native Plugin Interface(for unity 3.5) 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