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 /
avatar image
1
Question by Grimmy · Oct 06, 2010 at 11:14 AM · cameralayersmaskculling

How do I get my camera to render 2 culling masks in scripts?

I can get a camera to render just 1 culling mask with.. myCamera.cullingMask = 1;

or everything up to layer 8 with..

camera.cullingMask = 1 << 8; (I think)

..but how do I get my camera to render only layers 1 and 12 say?

Cheers

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

Answer by skovacs1 · Oct 06, 2010 at 02:24 PM

Layer masks are bit masks.

Consider that data in binary computers are represented by 1's and 0's.

The left-bit shift operator << takes as arguments the value to shift and the number of positions. (...00100000000 is ...00000000001 shifted 8 positions to the left). You seem to have gotten your mask example from the docs on layers, but you didn't read the comments about casting only against layer 8, not everything up to layer 8.

If you want a mask that masks a bunch of layers you could do something like:

1 + 1<<1 + 1<<2 + ...

To simplify this method for some range, you could use:

var mask : int = 0;
for(var i : int = start; i < end+1; i++) mask += 1<<i;

As described in the docs on layers, Bitwise negation turns all 1's to 0's and all 0's to 1's, thus creating the inverted mask.

var mask : int = ~(1<<8); //everything but 8

If we assume the binary representation of numbers in use, you can generate custom masks by understanding that everything is in base 2.

...0000 is 0, ...0001 is 1, ...0010 is 2, ...0011 is 3, ...0100 is 4, ...0101 is 5, etc.

A mask that is layers 0, 1, and 2 is 7, and a mask that is layers 0 and 2 is 5. The bit representing the layer is equivalent to 2^layerNumber. To get all layers from 0-12, would be 2^13 - 1 = 8191. To get all layers from 1-12, you would subtract layer 0 (which is 1<<0 or simply 1), so your mask is 2"13 - 2 = 8190.

camera.cullingMask = 8190;
//should be equivalent to
//camera.cullingMask = 0;
//for(var i : int = 1; i < 13; i++) camera.cullingMask += 1<<i;
Comment
Add comment · 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
0

Answer by Rithy-Jim · Nov 09, 2012 at 04:15 AM

Here is what I do for render 2 layer :

camera.cullingMask = 1 << 8 | 1 << 12;

Comment
Add comment · 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

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

2 People are following this question.

avatar image avatar image

Related Questions

Changing camera with different culling mask 1 Answer

My HUD disappears when switching cameras. 0 Answers

Apply Camera.layerCullDistances to certain/specific layers? 2 Answers

Dynamically render/hide object individually for each camera in scene 0 Answers

Help with layers? 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