Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 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
0
Question by _watcher_ · May 20, 2016 at 02:10 PM · cullingworldtoscreenpointfrustrumonbecamevisibleisvisible

How to properly use Renderer.OnBecameInvisible for Culling objects off screen?

Hi,

Found an excellent post about checking "Is target in view frustrum". If i understand it correctly, we would stop rendering of an object by disabling its Renderer when off screen. But as soon as i do this ..

 using UnityEngine;
 using System.Collections;
 
 public class ExampleClass : MonoBehaviour {
     void OnBecameInvisible() {
         GetComponent<MeshRenderer>().enabled = false;
     }
     void OnBecameVisible() {
         GetComponent<MeshRenderer>().enabled = true;
     }
 }

.. and the object leaves camera frustrum, Its Renderer does not dispatch OnBecameVisible again, once it later again enters the camera frustrum - well, thats obvious, since we disabled it in the first place - so a disabled Renderer would not fire OnBecameVisible.

What gives? Using this culling technique i cant do the culling.

Further more i wanted to actually disable the whole GameObject (not only its Renderer) when it leaves frustrum. Any thoughts on the best-practise implementation-vise here? I guess you cant use Renderer's isVisible/OnBecameVisible/OnBecameInvisible? Is using WorldToScreenPoint favored approach in this case? Is there better approach than disabling whole GameObject performance-vise?

Brainstorm please, tested methods preferred!

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
0
Best Answer

Answer by HarshadK · May 20, 2016 at 02:24 PM

You don't have to disable the Renderer when gameobject is not shown on the screen as it will not be rendered anyway because of frustrum culling.

We can provide you with method to use based on what exactly you want to achieve here.

Comment
Add comment · Show 3 · 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 _watcher_ · May 20, 2016 at 02:43 PM 0
Share

@Harshadk thanks for the feedback, i did suspect his, so that is awesome. I could disable everything else in the GameObject ins$$anonymous$$d (like scripts colliders etc). Im reading that GameObject.SetActive is extremely expensive operation anyways (1, 2, etc) - as i was considering using WorldToScreenPoint manual culling with complete gameObject.SetActive(true/false), but alas i might wanna disable stuff more granularly inside the GO.

$$anonymous$$y case is a 2.5D platformer (1 camera, 1 plane - simple), i have lots of primitive objects on screen at once (up to 1k objects constantly), with a lot of stuff offscreen. Im using StaticBatchingUtility for batching everything thats batchable, that helps, but every object has some scripts and collider(s) attached to it, and in my testing, this eats up CPU even when the objects are offscreen.

So next im gonna test disabling these on offscreen objects: 1) colliders 2) scripts 3) anything else except Renderer

And ill get back here with results. Let me know if you think there is better approach so i can try it as well.

Thanks!

avatar image HarshadK _watcher_ · May 20, 2016 at 02:55 PM 0
Share

As @Baste said it would not be wise to just perform optimizations like disable/enable all components blindly as there is a cost associated with enabling/disabling components which cumulatively might become more costly if you are doing it for thousands of objects than actually not doing it.

So first check for pain points and then try to optimize them. You can also use optimization techniques like collision layers to reduce unwanted collision checks which is better way than enabling/disabling colliders as collision layer optimization will work even if the objects are on screen where optimization are actually important.

avatar image _watcher_ · May 20, 2016 at 03:05 PM 0
Share

Ill accept your answer as it was my misunderstanding that Frustrum culling would render offscreen object IF its Renderer is active, which isnt the case (Renderer script can be kept active for OnBecameVisible trigger and Frustrum culling will ensure the object wont render offscreen). So the question was answered by you correcting my wrong assumption, thanks.

avatar image
1

Answer by Baste · May 20, 2016 at 02:44 PM

As @HarshadK said, the object's renderer is automatically culled as long as no part of it is visible on any camera.

It makes sense to "cull" (turn off) expensive components on your GameObject when they leave the view. If you have a NPC with a patrolling script and a navmesh agent, you can turn off the script and agent when they leave view. Projectors also make sense to turn off - as long as they're projecting onto a mesh that's visible (like a big ground mesh), they will cause extra draw calls even though the projector itself isn't visible.

As with everything that's performance related, optimizing before you know what's causing bad performance is the stupidest thing you can do. If your game's running slowly, open the profiler and check what's slow. Making a blind guess and doing a bunch of manual culling of objects is probably not going to help, and cost a lot of time and effort.

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 _watcher_ · May 20, 2016 at 03:00 PM 0
Share

@Baste "As with everything that's performance related, optimizing before you know what's causing bad performance is the stupidest thing you can do" Well said x)

$$anonymous$$y case is primitive objects with colliders and scripts on them, its that simple. Ive noticed full screen of these runs well. Other 2 full screens of them off camera and it runs pretty bad. $$anonymous$$ust be the active Scripts like $$anonymous$$onobehaviours waiting for Collider triggers etc eating empty CPU cycles on every frame. Thanks for the examples with projector and navmesh, might be useful later.

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

43 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

Related Questions

Is there a way to find out whether an object is being occlusion culled? 0 Answers

Is there a way that I can control the maximum number of Visable Skinned Mesh? 0 Answers

Visualize frustrum culling in Editor 1 Answer

Detect if bounds are "partially" inside camera frustum 2 Answers

Low fps with many objects even when not looking at them 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