Search Unity

Selective Camera Culling Mask

Discussion in 'Scripting' started by olliecard, Aug 18, 2009.

  1. olliecard

    olliecard

    Joined:
    Nov 6, 2007
    Posts:
    39
    Hey everyone,
    I am having a bit of trouble working out how to turn on/off specific layers for a dynamic changing camera (ActiveCamera) so that I can toggle certain elements of my enviroment. Eg. Turn on/off buildings (1 << 12), newly created objects (1 << 14), etc.

    I am using a toggle GUI, with the actual function of checking existing in an Update.

    I want to leave the cameras to continually look at certain layers (GUI, etc) but to just add or remove the object layers without culling everything.

    I have had quite a few goes and read all the layers docs in all the references, but I cannot seem to specify a single layer to turn on and off without doing the whole thing. Any help would be really appreciated.
     
  2. Jim Offerman

    Jim Offerman

    Joined:
    Jul 17, 2009
    Posts:
    177
    What are you doing exactly? My guess would be that you're simply setting the wrong bitmasks.

    Here's a few code snippets that should work:

    Code (csharp):
    1.  
    2. // Render everything *except* layer 14
    3. camera.cullingMask = ~(1 << 14);
    4.  
    5. // Switch off layer 14, leave others as-is
    6. camera.cullingMask = ~(1 << 14);
    7.  
    8. // Switch on layer 14, leave others as-is
    9. camera.cullingMask |= (1 << 14);
    10.  
    (you might want to read up on how the bitwise operators work too)
     
    MrWy07, rocket5tim, nk1406 and 4 others like this.
  3. olliecard

    olliecard

    Joined:
    Nov 6, 2007
    Posts:
    39
    That works great! All I needed to know was the correct syntax for picking out 1 singular layer. I am creating a architectual modelling program where you have a 3d map of a city, which you can view from street level, or a bird's eye perspective. You choose to place your proposed building to view it from various angles/see how it sits in the city, etc.
    This is a snippet of what I finished with... thanks again.

    Code (csharp):
    1.  
    2. if(ToggleBuildings){
    3.         GUISwitchcamera.currentActiveCamera.cullingMask |= (LayerBuildings);
    4. }
    5. else {
    6. GUISwitchcamera.currentActiveCamera.cullingMask = ~(LayerBuildings);
    7. }
    8.  
    9. if(ToggleNewBuildings){
    10.         GUISwitchcamera.currentActiveCamera.cullingMask |= (LayerNewBuildings);
    11. }
    12. else {
    13. GUISwitchcamera.currentActiveCamera.cullingMask = ~(LayerNewBuildings);
    14. }
    15.  
     
  4. Jim Offerman

    Jim Offerman

    Joined:
    Jul 17, 2009
    Posts:
    177
    Glad I could help! :)
     
  5. LostBalloon

    LostBalloon

    Joined:
    Feb 12, 2011
    Posts:
    25
    Thanks guys, helpful thread
     
  6. alok.kr.029.hotmail

    alok.kr.029.hotmail

    Joined:
    Feb 17, 2014
    Posts:
    5
    perfect code thanxxxx
     
  7. larku

    larku

    Joined:
    Mar 14, 2013
    Posts:
    1,422
    Sorry for necro-ing this thread but a correction in case anyone tries to use this ( the & was missing from the second example)

    Code (csharp):
    1.  
    2. // Render everything *except* layer 14
    3. camera.cullingMask = ~(1 << 14);
    4.  
    5. // Switch off layer 14, leave others as-is
    6. camera.cullingMask &= ~(1 << 14);
    7.  
    8. // Switch on layer 14, leave others as-is
    9. camera.cullingMask |= (1 << 14);
    10.  
     
    Last edited: Mar 8, 2017