How do I only get bounds of the visible parts of an object?
I need to track what percent of the screen the enemy is taking up. The solution I think will work is to get the renderer bounds of the enemy and make a rect around it in screen space, then compare the sizes. I made the rect as a GUI box to test. My problem right now is that the bounds of the object are displaying the entire object even if only part is visible.
This gets the dimensions correctly
But this should have the gui rect smaller than it is displaying because it is obstructed by another object
If there is a better solution to what I am trying to do then I'd also be open to that.
Answer by AlexHogan · Mar 11, 2021 at 08:24 AM
This is a fun one, because the object, when it is rendered, is technically visible. Later on, another object is rendered on top of it. Your GUI box is giving you a pretty good idea of whether it's 'on screen' or not.
To get more accurate that that, one way is to analyze the rendered image after it's been created. Here is my suggestion on steps to do that:
Use the Scriptable Render Pipeline so that you can easily script your render passes
Create a special render pass that runs before your main render, rendering at a much lower resolution. Like 160x100. Maybe smaller than that. Tiny.
In this pass, you render your object in question with an UnLit white material, and everything else with an Unlit Black material.
You should have a reference to the rendered image now.
Use more standard pixel processing techniques to get the bounding box of the white pixels in that image.
You can continue on to render your scene as normal now.
I can acknowledge this is a pretty high level explanation - and that there are certainly other ways to do this. Has this been sufficiently outlined for you?
Helpful References:
Scriptable Render Pipeline: What You Need To Know
Scriptable Render Pipeline Overview
In addendum -- once it's working, you can tweak the image size and your pixel search algorithm to get faster and better results. Even better, once you have this working you can use it for all sorts of other effects and features.
Your explanation definitely sounds like what I'm looking for. Though this is a lot of stuff that is going way over my head and I'll need to read all this, but thank you for pointing me in the right direction. I think this will be better choice in the long run.
I'm reading through this to figure out what to do. Is this a good source to go with?
If I have more questions should I make new posts or DM you directly since you know this subject well? This is my first time posting here since I'm mostly past the youtube tutorials phase of learning.
I'm glad it was helpful.
I'm happy to provide help here in the thread -just add a reply or comment. I like to keep it in here - it invites other people to share potentially better answers, and it provides whatever answers we uncover back to the answers.unity community.
You are always welcome to DM me too, of course. My reply time varies depending on my schedule. ;)
~$$anonymous$$
So I've started to work on learning the SRP. I don't see anyway to just add things on top of the HDRP or URP. I only see ways to build the SRP from the ground up entirely. Is there anyway to just add the one function without having to recreate everything from scratch?
I found this tutorial to be the most understandable
Your answer
Follow this Question
Related Questions
Occlusion culling rendersing through walls and objects (everything is static) 1 Answer
Trying to get a completely transparent mesh to hide parts of another visible mesh it overlaps. 0 Answers
Why do render bounds go wonky at runtime? 0 Answers
Custom Obj appears behind other GameObjects when viewpoint is changed 0 Answers
Sprite not rendering well on android 0 Answers