x


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

more ▼

asked Oct 06 '10 at 11:14 AM

Grimmy gravatar image

Grimmy
533 58 64 69

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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;
more ▼

answered Oct 06 '10 at 02:24 PM

skovacs1 gravatar image

skovacs1
10k 11 25 91

(comments are locked)
10|3000 characters needed characters left

Here is what I do for render 2 layer :

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

more ▼

answered Nov 09 '12 at 04:15 AM

Rithy-Jim gravatar image

Rithy-Jim
1

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3010
x150
x147
x65

asked: Oct 06 '10 at 11:14 AM

Seen: 1874 times

Last Updated: Nov 09 '12 at 04:15 AM