- Home /
Billboarding flat plane with multiple cameras?
Hello. I understand how to billboard a sprite-plane object to a single camera, but I need to billboard these sprites (in my case, these are characters, a la Doom) to multiple cameras simultaneously (e.g. split screen?).
Since an object can only take up one rotation at a time, I would need access to some "pre-render object/scene" event for each camera, and rotate the player's sprite plane to face the current camera. Trouble is, I need the billboard rotation part be executed player-side, not camera-side (because in my case, I do dynamic scaling and other tasks that are tied into current character state not accessible to a camera).
Is there any event/message that could be of use? Thanks in advance.
Answer by s-m-k · Apr 10, 2014 at 10:42 PM
I know it might be a little bit too late, but it might be useful for other people as well (including me). I came up to solution that just works on plain Quads (tested) and feels fairly clean.
Actually, the script didn't work with OnPreRender well, so I used OnPreCull.
If we want to keep the code clean, we should limit the amount of information cameras and billboards share between each other. This is the situation when statics and delegates make sense. Here's my solution:
First, attach the following script to each camera:
using UnityEngine;
using System.Collections;
public class CameraPreRender : MonoBehaviour {
public delegate void PreCullEvent();
public static PreCullEvent onPreCull;
void OnPreCull() {
if (onPreCull != null) {
onPreCull();
}
}
}
Then attach the following script to each billboard:
using UnityEngine;
using System.Collections;
public class Billboard : MonoBehaviour {
void OnEnable() {
CameraPreRender.onPreCull += MyPreCull;
}
void OnDisable() {
CameraPreRender.onPreCull -= MyPreCull;
}
void MyPreCull() {
//we want to look back
Vector3 difference = Camera.current.transform.position - transform.position;
transform.LookAt(transform.position - difference, Camera.current.transform.up);
}
}
That's it! The code is simple and flexible, you can enable/disable/create/destroy your billboards/cameras on the fly and the code should work (it works for me at least).
That's ingenius! Thanks for your answer; much appreciated!
Answer by Benproductions1 · Jul 18, 2013 at 01:40 AM
http://lmgtfy.com?q=Unity+prerender
Was that so hard?
OnPreRender: "This function is called only if the script is attached to the camera and is enabled."
Arookas: "Trouble is, I need the billboard rotation part be executed player-side, not camera-side"
Now it's not that simple, isn't it? He obviously knows OnPreRender, but wants to keep his code as clean as possible and asks for the most clean and simple solution.
Your answer is totally useless.
There is no event that gets called on all objects for every render. $$anonymous$$y answer isn't useless because it's the only answer there is. This is the most clean and simple solution
It's not a solution, it's not the answer to the problem. Also, you seem to be shortsighted clai$$anonymous$$g that it's the only possible answer to that problem, I'll tell you why later.
He asked how to execute the OnPreRender listener code in the object itself, which is not possible directly.
If we want to achieve it in the clean way, the best I can think of for now is an implementation of Pub/Sub pattern. It supports loosy object coupling, so both camera and billboard don't need to know too much information about each other (both are independent from each other).
This is one of several possible solutions to that problem. You can, for example, just inform your cameras about all billboards and tell them to call appropriate functions by themselves (loop through all billboards and call PreRender or sth). But that's an ugly solution. It needs more work, it makes the camera's code less flexible and dependent on the Billboard class.
Your answer is simply a clue. It's useless, because the author is aware of it, at least his post indicates so. Also, my solution works on OnPreCull, not OnPreRender (I've tested it). OnPreRender "shifts" the billboard's transform to the next camera, also it makes more sense to use OnPreCull here, because our transform can deter$$anonymous$$e the visibility of our billboard.
I see no indication that the OP knew anything about OnPreRender.
I would need access to some "pre-render object/scene" event for each camera
$$anonymous$$akes it quite obvious. None the less, yes, your solution is better, but that doesn't excuse the lack of research from the OP.
Probably the best solution is to do this with shaders.
Also I don't understand why you would revive this question, it was obviously dead a long time ago...
You're right, the question is dead, but I ran into the same issue, googled it just in case Unity has a native easy-to-use support and thought that the answer would be just useful to others.
I assumed OP knew about OnPreRender event from here:
Trouble is, I need the billboard rotation part be executed player-side, not camera-side
He knew that the event is executed per-camera and asked for "pre-render object/scene" event.
You're right about the shaders, it might be even simpler, because it would probably eli$$anonymous$$ate the need of listening to the events at all. I'll do something about it later.
Your answer