Search Unity

Billboard facing all cameras

Discussion in 'Scripting' started by Dark-Protocol, Feb 6, 2016.

  1. Dark-Protocol

    Dark-Protocol

    Joined:
    Nov 19, 2011
    Posts:
    279
    I'm writing a simple custom particle system and I'm having trouble making the particles face the cameras. I can make the particles face a certain camera (for example Camera.main) without a problem but what about if I have several cameras in the scene ? How can I set the particles to look at each camera before they start rendering ? MonoBehavior.OnPreRender is a good method which kind of does the thing I'm looking for but it only works on objects with cameras and it's called only for the object's camera.

    I'd like to achieve the same effect as the Particles, Terrain grass, Line renderers which are built-in in Unity.
     
  2. Dark-Protocol

    Dark-Protocol

    Joined:
    Nov 19, 2011
    Posts:
    279
    So far I found one solution to the problem and that is:
    Create a MonoBehavior with an OnPreRender method, attach it to each camera (manually or via code in runtime) and have it rebuild the mesh for each billboard entity for each camera so that the billboards face the camera. This is a clunky solution and I would like to know if there is another approach.
     
  3. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    That is the actual solution, and exactly how all those other things work.
     
  4. Dark-Protocol

    Dark-Protocol

    Joined:
    Nov 19, 2011
    Posts:
    279
    Thanks, StarManta. I really don't like the idea of having to attach an extra component to all cameras because that would mean that I'll have to constantly check for new cameras using FindObjectsOfType and this is really slow (since I want this process to be fully automatic). Also, I'm not sure how to deal with the editor camera either. I can't attach a component to it but I'd like it to render the billboards correctly so that is another problem.

    Particle systems, line renderers etc. don't need an extra component for them to work but I guessing cameras do that internally ?
     
  5. popupAsylum

    popupAsylum

    Joined:
    Apr 7, 2014
    Posts:
    119
    Use OnWillRenderObject on any script with a renderer attached, and use Camera.current in that function to get the camera rendering
     
    Yiming075 and Alic like this.
  6. Dark-Protocol

    Dark-Protocol

    Joined:
    Nov 19, 2011
    Posts:
    279
    Oh, That's actually a nice solution but the problem in my case is that I don't have a renderer and a mesh filter attached to the billboard object. I'm actually using Graphics.DrawMesh in order to visualize the particles but if there's no other workaround, I'll have to scrap DrawMesh and use a good old mesh renderer instead. Thanks for that tip!
     
    Last edited: Feb 7, 2016
    popupAsylum likes this.