Using Unity LineRenderer for Debug Drawing

I needed to render debug lines for some prototyping that I’m doing for a future project. Unity (5.0.1f1) has functions in the Debug namespace for DrawLine and DrawRay, but unfortunately, these functions only render in the Editor view, and not in the actual game. I’ve read suggestions online for using the LineRenderer for this purpose, but I, among other people on the Internet, have found it to be inadequate as an out-of-the-box solution.

The main problem with the LineRenderer, is that the billboarding method that’s used ends up “twisting” the verts along the width of each line segment that you feed to it. This results in a series of “triangular” shapes being connected together, rather than a continuously flowing line or curve. The LineRenderer class has a function SetWidths(start, end), but this does not work as intended. I was trying to render a circle with a fixed width, and this is what I got:

254_bony_circle

There are, of course, better line rendering systems available on the Unity Asset store, but the good ones are a bit pricey. I’ve seen many suggestions for Vectrosity, which looks quite good and full-featured actually, but is still pretty steep a price for a part of the system that really should just work to begin with.

Determined to stick with the LineRenderer solution, I continued to search for tips on ways to modify or enhance the existing LineRenderer, but most of the solutions either didn’t work, were too old, or only existed as various quick comments in message boards that weren’t fully described for a complete solution.

I settled on the double-back method described here. This solution still suffers from the problem of the “bow-tie” effect due to the collapsing in the center of each line segment, but it’s a pretty cheap and reasonable solution.

254_bowtie_circle

I ran into one more major problem after getting this to work. Notice that the previous images contained black lines. LineRenderer has a function called SetColors() that I couldn’t get working. The above screenshots were using the “Diffuse” shader. The closest that I got it to working was when I assigned the “Particles/Additive” shader (a lot of the solutions I found online use this shader) to the material, but even then, I wasn’t getting the right color, due to the particle’s lack of alpha intensity.

This thread mentions using a GUI shader. That sounded like a great idea, especially if it solves the billboarding issue in a different way, even though I was really just aiming at getting a shader that I can use to adjust the colors properly on the material.

The problem was, I wasn’t sure how to assign a different internal Unity shader. I couldn’t find a list of them. It was important to use an internal shader, because I wouldn’t want my debug line renderer to require an external shader every time I use it.

What I did was:

  1. Create a new dummy Material, or find an existing one in your project.
  2. In the Shader dropdown, find a suitable shader. In this case, we want the “UI/Default” shader.

Using this shader, I was able to simply go:

renderer.material.color = new Color();

254_ui_default

I’m not completely happy with this solution, but it’s good enough to get by until I can find something better, or I get the time to write up my own library of line tools.

This entry was posted in Dev, Programming and tagged . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.