Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 ROMgame · Nov 25, 2015 at 08:57 PM · camerarenderinggraphicsrendererrendertexture

Render a camera multiple times in a single frame?

I have several groups of 3D objects, I want to render them as if they were in "layers" - no matter what their Z depth is.

So I render "Group 1" in front of "Group 2", I render "Group 2" in front of "Group 3" and so on. Because they aren't single meshes I can't unfortunately do this by turning culling off because the objects within each group need to cull with each other, just not with other groups! (e.g. Group 1 objects cull with each other but not with Group 2).

The way I thought it doing this is by rendering a single camera multiple times whilst switching the layers of the objects. So I have a script attached to each of the groups which does the following:

 public void Update()
     {
         SetLayerRecursively( mRenderBatch.gameObject, 8);
         
         mCamera.Render();
         SetLayerRecursively( mRenderBatch.gameObject, 0);
     }
 
 void SetLayerRecursively(GameObject obj, int newLayer)
     {
         
         obj.layer = newLayer;
         
         foreach (Transform child in obj.transform)
         {
             if (null == child)
             {
                 continue;
             }
             SetLayerRecursively(child.gameObject, newLayer);
         }
     }

This switches the object onto a renderable layer (8), renders it and then switches it back onto a non renderable layer. The camera is then set to depth only so I hoped that this would render the layers nicely on top of each other and it does - in Free Aspect mode. The second I switch the mode to anything else it just renders a blank screen.

What am I doing wrong? I feel like I'm incredibly close - given it works in free aspect! - but just a little bit away.

Note: I have considered using multiple cameras but I wanted to use one camera for cleanliness and because I read that it's a more optimal solution (and appears to be in my limited testing!).

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

Answer by callen · Nov 25, 2015 at 09:26 PM

I'm not sure why you say multiple cameras are not 'optimal' but that's the approach I'd suggest. I don't see any reason in your description for calling Camera.Render() rather than letting Unity handle it, but according to this page is your camera.enabled=false as the docs say it must be?

But if you used multiple cameras you could make each camera render just the subset of objects (or specific layer) you need, and you can control their order by using the Camera.depth property. No scripting required.

I think possibly in your testing, you were trying to call Camera.Render on multiple cameras, in one frame? This sounds like it would be very inefficient.

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 ROMgame · Nov 25, 2015 at 10:48 PM 0
Share

Hey! Thanks for the reply! As far as I understand it multiple cameras do additional processing - alongside the actual render cycle - making their usage more expensive. In my own tests I got around a 30% performance increase by swapping to the method I've detailed in the above post.

When using the multiple cameras I was letting them render "automatically" - it's only in the above code that I wanted to render them in a custom way given that I needed a single camera to do multiple 'passes'.

Cheers!

avatar image Bunny83 ROMgame · Nov 26, 2015 at 04:46 AM 0
Share

What "additional processing" are you talking about? A camera is just a virtual object all it defines is a viewpoint / direction and the projection parameters. So it specifies the View- and Projectionmatrix, that's all. When you call Render() on the camera it does exactly the same thing as when it's rendered automatically.

On the other hand your SetLayerRecursively method can be quite costy if you have many objects.

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Masking elements with non-rendered layers? Culling masks, camera, render textures 0 Answers

Graphics.DrawTexture vs GUI.DrawTexture vs Graphics.Blit 0 Answers

Overlapping particle systems 0 Answers

Multiple cameras, depths, and image effects! 2 Answers

Is it possible to override Unity camera rendering? 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