Search Unity

Setting individual vertex colours in a line

Discussion in 'Scripting' started by cecarlsen, Nov 27, 2007.

  1. cecarlsen

    cecarlsen

    Joined:
    Jun 30, 2006
    Posts:
    864
  2. NCarter

    NCarter

    Joined:
    Sep 3, 2005
    Posts:
    686
    You can't set the vertex colours of the LineRenderer or TrailRenderer directly. In the case of the LineRenderer, it only has a SetColors method to set the start and end colours of the line, and the TrailRenderer doesn't let you set its fade colours from scripting at all. For particle effects, you can use the ParticleEmitter's Emit method or 'particles' property to manually set the colour of each particle (but you'll need to make sure the ParticleRenderer isn't animating colours or it'll override your settings).

    If you want to have absolute control over where each colour is used, you probably need to use the Mesh API. See how Yoggy uses it in this thread.

    If you have Pro, you could also use the GL class to draw arbitrary triangles with colours of your choice at each vertex. This is often easier to program with, but might not be as fast as the Mesh API in various cases.
     
  3. metervara

    metervara

    Joined:
    Jun 15, 2006
    Posts:
    203
    Hi.

    Interesting, can you elaborate on this? What I would really like to know is in which cases GL would be faster than Mesh API and vice versa?

    thanks,

    Patrik
     
  4. Aras

    Aras

    Unity Technologies

    Joined:
    Nov 7, 2005
    Posts:
    4,770
    Mesh API should be faster in all cases. GL class might be more convenient to do stuff though, or for cases where speed does not really matter.
     
  5. metervara

    metervara

    Joined:
    Jun 15, 2006
    Posts:
    203
    Thanks Aras.

    It's very convenient to draw nice 1-pixel lines in GL... :)

    /P
     
  6. cecarlsen

    cecarlsen

    Joined:
    Jun 30, 2006
    Posts:
    864
    Thanks Carter and Ares, very enlightning.

    I finally made it work by drawing the line using the mesh and adding a shader that supports vertex colors. The example from Yoggi and the procedural tutorial project gave me all the hints I needed.

    Carl Emil
     
  7. NCarter

    NCarter

    Joined:
    Sep 3, 2005
    Posts:
    686
    I should probably have said something like "the GL class is never faster than the Mesh API, but you might not be able to tell the difference in some cases". I haven't done timing tests in Unity, but my experience outside of Unity is that for very small numbers of vertices, there's no meaningful difference between immediate mode (the GL class) and vertex arrays (more or less what the Mesh API does). Once you have a reasonable number of vertices, and especially if you don't change them as often as you draw them, the Mesh API will always be much faster.

    The main argument in favour of the GL class is that it's often possible to structure your drawing code mode conveniently, because you can use the drawing commands anywhere you like between GL.Begin() and GL.End(), including in other functions and classes. The code required to make the Mesh API work can quite complicated and roundabout, especially if you don't know from the outset how many vertices you will have, or if you want to fill in the arrays piecemeal from various different functions instead of all in one block.