Trending
Opinion: How will Project 2025 impact game developers?
The Heritage Foundation's manifesto for the possible next administration could do great harm to many, including large portions of the game development community.
Featured Blog | This community-written post highlights the best of what the game industry has to offer. Read more like it on the Game Developer Blogs or learn how to Submit Your Own Blog Post
Changing the layers displayed by a Unity3D camera requires some bitfield maths. This post provides a more explicit and readable way to handle this functionality.
This post was first published on the Concealed Intent blog
Recently I was working on making layers appear and disappear programmatically in Unity3D. The Unity Camera class has a culling mask such that each camera can be set to only display a subset of the available layers. This is handy for placing 3D objects in one layer visible by the main camera, and say the GUI in another layer visible by a different camera. This can be setup using the camera's culling mask dropdown in the editor. However, I wanted to be able to change the visible layers during runtime in response to a player's actions (in Concealed Intent pressing a key will make a grid appear or disappear over the playing area).
To change the culling mask programmatically some bitfield maths is required. Each layer is represented by a bit in an integer. Layer 12 is 000000000000000000100000000000 - the twelfth bit in a 32-bit integer mask. The culling mask itself is just the combination (bitwise OR) of these layers. Thus, if the culling mask is 000000000010000000100000001000 (equivalent to the number 526344), the 4th, 12th and 20th layers would be displayed, as the 4th, 12th and 20th bits are turned on. To do this in code, first find the number of the layer and then shift a bit this number of places, then add or remove this from the mask. For example, to add the "Enemy" layer to the mask would be: cam.cullingMask |= 1 << LayerMask.NameToLayer("Enemy"). Other bitwise operations can remove layers.
I find this messy. I prefer my code to be a little more explicit and readable. So I wrote a set of class extensions for the Camera to add a better way of manipulating the culling mask. Just add the class below into your project and then cam.LayerCullingShow("Enemy") will add the "Enemy" layer to the camera "cam". Similarly, cam.LayerCullingHide("Enemy") will remove the layer; cam.LayerCullingIncludes("Enemy") will tell you whether it is being displayed or not; and cam.LayerCullingToggle("Enemy") will flip the visibility of the "Enemy" layer. If preferred, the layer mask can be passed as a parameter rather than the layer's name. Enjoy.
Contact Details:
Twitter: @JarrahTech
Facebook: JarrahTechnology
Instagram: @JarrahTech
Email: charles at jarrahtechnology dot com
Read more about:
Featured BlogsYou May Also Like