GL lines layer mask?

Hi,
I have multiple cameras on my scene with different culling mask and different render depth.
Now, I want to use GL lines but I want render the lines only on a specific camera with a specific layer mask.
My render GL scripts are on objects not on camera.
Is possible to exlude GL renders from some cameras?

It is possible to exclude GL renders from some cameras. Camera.current returns the camera you are currently rendering with so you can use it to check the layer, name, or tag of the camera. For instance

void OnRenderObject() {
    if(Camera.current.name=="Camera1") {
         //rest of GL functions
    }
}

will only draw for cameras with the name “Camera1”

I would also note that Unity3D adds efficiency onto GL so you might be better off just using a mesh. You can draw lines using Mesh.SetIndicies with a topology of MeshTopology.Lines or MeshTopology.LinesStrip.

Camera.current sometimes returns null in current version of URP, for anyone wondering, the solution I figured out was to subscribe to the RenderPipelineManager.beginCameraRendering and RenderPipelineManager.endCameraRendering events and cache if main camera is rendering or not.

private void OnEnable()
{
    RenderPipelineManager.beginCameraRendering += OnCameraBeginRendering;
    RenderPipelineManager.endCameraRendering += OnCameraEndRendering;
}

private void OnDisable()
{
    RenderPipelineManager.beginCameraRendering -= OnCameraBeginRendering;
    RenderPipelineManager.endCameraRendering -= OnCameraEndRendering;
}

private void OnCameraBeginRendering(ScriptableRenderContext t_context, Camera t_cam)
{
    if (t_cam != MainCamera)
        return;

    IsMainCameraRendering = true;
}   

private void OnCameraEndRendering(ScriptableRenderContext t_context, Camera t_cam)
{
    if (t_cam != MainCamera)
        return;

    IsMainCameraRendering = false; 
}

private void OnRenderObject()
{
    if (!IsMainCameraRendering)
        return;
    
    // GL Code here...
}
1 Like