- Home /
Camera Culling Mask specific object
Hi guys,
is it possible at all to apply a culling mask to a camera for a specific object rather than a layer? It's important to my game that there are lots of cameras and each can only render one object. I know I can do this by adding a layer for each object and then doing something like:
cam.cullingMask = 1 << LayerMask.NameToLayer("Object1");
But it doesn't seem very efficient to make a layer for each object. It's important that a lot of the objects which need to be rendered to separate cameras will be close together so I do need some sort of masking.
Any tips would be greatly appreciated.
Answer by Glurth · Jun 29, 2016 at 03:48 PM
One, not ideal, way to do this is by using a custom shader on each object. If you write a shader with a particular, unique, "tag" for each object, you can then use Camera.RenderWithShader, rather than Camera.Render, to specify the particular tag value of the object you want drawn. Only objects that use a shader containing the specified tag will be drawn. All other shaders are ignored.
Unfortunately, I do not know of anyway to do this dynamically (since the Shader constructors that allows one to to provide a string compiled at runtime, are now depreciated). I think you would need to copy the original shader code into a new shader file, and change the tag manually, for EVERY object you wish rendered separately. (I use this for previews, so I only have one object at a time that need such a shader. Your case sounds quite different, so this may not be the best solution.)
Thank you for your reply, I'll definitely consider it but I would presume that if the shader is checking for objects with a specific tag that would probably be a lot more costly to performance and in my situation it would probably be more efficient (time and resource wise) to just suck it up and add a new layer for each object even though it's not what I'd prefer. Don't get me wrong I appreciate your help a lot and I'll definitely consider it. Thank you.