- Home /
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?
Answer by maccabbe · Aug 03, 2015 at 05:24 PM
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"
http://docs.unity3d.com/ScriptReference/MonoBehaviour.OnRenderObject.html
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.
http://docs.unity3d.com/ScriptReference/Mesh.SetIndices.html
Very thaks! Work! I use the tag for main camera.
public void OnRenderObject ()
{
if (Camera.current.tag == "$$anonymous$$ainCamera"){
......
$$anonymous$$esh.SetIndicies it seems complicated for me. I just want a line (with no depth buffer) which starts from an object to his forwards vector.
Answer by samochreno · Mar 08 at 10:49 PM
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...
}
Your answer
Follow this Question
Related Questions
Fade out a line renderer? 1 Answer
Bouncing Laser Beam 1 Answer
How do I draw simple shapes? 1 Answer
Help: Create line renderer rope with hinge joint 2D make FPS go down badly 0 Answers
Line renderer spring shape 1 Answer