Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 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
1
Question by AlexForTheWin · Aug 04, 2019 at 03:57 AM · cameraraycastingselecting objects

How do you determine if an object is visible to the camera?

I am making a run-time level editor.
One of the features is selecting objects in a region by clicking and dragging.
There are several tutorials on this, but none of them take into account if the object is visible or not.

I want it to work like the selection does in Blender. All of the visible objects are selected, but not ones that are occluded:
  alt text
alt text


My idea was to iterate through all of the objects in the selection and only select the ones visible to the camera. But I don't know how to do this.

I can't use ray-casting because I would need a ray for every pixel on the screen, and that would cause some lag.
And I can't use Renderer.isVisible or OnBecameVisible() because they don't check for occlusion.

Is there some way to check if an object is being rendered onscreen?

selection.png (324.6 kB)
outcome.png (132.0 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
1
Best Answer

Answer by Pakillottk · Aug 05, 2019 at 08:44 PM

Hi. A simple, and very common, way to solve this is by implementing Color Selection (http://www.opengl-tutorial.org/miscellaneous/clicking-on-objects/picking-with-an-opengl-hack/). Instead of programmatically calculate ray-intersections, distances and so on... you simply render the scene twice: one to the screen, the other to a texture where each selectable object it's rendered with an unique color. So then you can make a lookup of that texture and recover the ID/s of the visible object/s that match your selection boundaries.

Comment
Add comment · Show 2 · 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 AlexForTheWin · Aug 06, 2019 at 12:51 AM 0
Share

That's a cool solution!

 

But I'm a bit worried since the site you linked advises against this method due to the performance hit.

Rendering the frame takes time, and the ReadPixels function used to get the color information is slow too.

 

In the example, the object-identification frame is rendered every time the mouse is clicked.

So maybe I can optimize this by rendering the frame and reading the pixels only once (after each time the user finishes moving the camera or an object).

This would also allow the process to be run asynchronously and hide any delay.

 

Since it doesn't look like its possible to see the z-buffer of the camera, I'll try this out and report back.

 

 

P.S. would native plugins be useful for this at all?

avatar image Pakillottk AlexForTheWin · Aug 06, 2019 at 08:56 PM 1
Share

Sure rendering the scene twice hurt performance but as you say, you can only render that way whenever you need to make a selection and only for a single frame. Unless you are handling really complex scenes (in which case the performance would be bad anyways) it shouldn't be noticeable. You can find some frame breakdowns of popular modern games (i.e. Resident Evil 2 Remake) and you'll see how many times the scene is renderer into different framebuffers behind the scenes. In unity, I suggest that you make a second camera, with a RenderTexture as output, and attach a script that only enables it when other script order it (something like: ColorSelectCamera.RenderInSelection$$anonymous$$ode()) and then once the frame is fully rendered the camera turns off again (therefore, the performance hit will be $$anonymous$$imal). After that, you can read from the RenderTexture to make the real selection. The way to render the objects could be using ShaderReplacement when rendering with the second camera. The shader would simply outputs the object's color id.

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

176 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

Related Questions

Raycast object flies towards mouse on screen, i need it on floor 1 Answer

How to get the pixel depth values of the camera view? 0 Answers

How to make a ray always shoot forward from Camera viewpoint. 1 Answer

cast a ray to hit an object in the center of the screen 1 Answer

transform.forward doesn't correspond to rotation 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