- Home /
Vectrosity: SetCamera(), aspect, resolution and rendertextures
Hi all! (and Eric, perhaps, particularly? ;) )
I'm trying to draw some basic lines and points with Vectrosity. Its default settings work fine. One issue is that I need them drawn on top of GUI controls drawn using UnityGUI. These are always drawn last, with no way to change it, so to get around that, I'd like to render the Vectrosity lines into a RenderTexture, and then just display that in a call to GUI.DrawTexture.
My problem with this approach is that the rendertexture has a resolution and aspect ratio which are radically different from the screen's. I need the rendertexture's resolution to be 600x40, resulting in an aspect ratio of 600/40 = 15. The methods which set the camera with which Vectrosity renders its linemeshes do not pick up neither the aspect ratio nor the rendertarget of the camera I'm passing to it. I'm doing this:
RenderTexture PowerLinesRT = new RenderTexture(600, 40, 0, RenderTextureFormat.ARGB32); // No depth buffer needed. Will only contain 2D lines
PowerLinesRT.Create();
camera.aspect = 600 / 40; // = 15
camera.targetTexture = PowerLinesRT;
VectorLine.SetCamera(camera); // Creates a camera for the vectorlines, but does not pick up the custom aspect ratio or the rendertarget
// I expect these endpoints to define a horizontal line from one end of the rendertexture to the other, 10 pixels from the top
Vector2 feedthroughStart = new Vector2(0, PowerLinesRT.height - 10);
Vector2 feedthroughStop = new Vector2(PowerLinesRT.width-1, PowerLinesRT.height - 10);
// PowerLineOffColor and PowerLineOff are colors and materials set in the editor, they work as expected
FeedthroughLine = new VectorLine("Feedthrough", new Vector2[] { feedthroughStart, feedthroughStop }, PowerLineOffColor, PowerLineOff, 20);
FeedthroughLine.Draw();
I tried to further customize the camera by picking up the reference returned by SetCamera, i.e. I can do this:
Camera vectrosityCam = VectorLine.SetCamera(camera); // Creates a camera for the vectorlines, but does not pick up the custom aspect ratio or the rendertarget
vectrosityCam.aspect = 600 / 40; // = 15
vectrosityCam.targetTexture = PowerLinesRT; // Manually set the vectrosity camera's rendertarget instead
This gives the vectrosity camera the desired aspect ratio and rendertarget. But it does not pick up the rendertexture's resolution. The coordinates given to VectorLine's constructor afterwards still look like they generate a mesh relative to the Screen's resolution, and not the rendertexture's. This is a problem. I'd really like to be able to give it coordinates relative to the rendertexture's resolution. Is there a way to make it understand that? Or am I doing something really silly here?
Answer by Eric5h5 · Sep 18, 2012 at 10:04 PM
The bad news is, it's not quite that simple. The good news is, it so happens that Vectrosity 2.1 has a SetCameraRendertexture function, which makes the vector camera use a rendertexture, like this:
VectorLine.SetCameraRendertexture (myRenderTexture);
Then you can use
VectorLine.SetCameraRendertexture (null);
to make it stop rendering to a rendertexture. The other bad news is, Vectrosity 2.1 isn't out yet. The other good news is, it will be out Real Soon Now (after I update the docs).
Hey Eric,
Thanks for getting back to me. In a way I'm relieved it wasn't quite that simple. I was worried I was doing something silly and not understanding how to use it correctly. :)
The feature you describe for 2.1 looks really neat! I'm looking forward to the update. I spent some time yesterday messing around with 2.0 and eventually settled on a hacky solution I'll use temporarily until you release Vectrosity 2.1. I'll simply transform the coordinates given to the VectorLine constructor from the screen's coordinate system to the rendertexture's by adding or subtracting some multiple of the ratio between their respective aspect ratios. (The "aspect ratio-ratio", as it were. Confusing, much?) Then pass those coordinates to VectorLine ins$$anonymous$$d. That is, to put a point at x = 0 in the RT, I'm actually giving it a negative x coordinate that makes it move the point way off-screen, to a place where the wide rendertexture's left side ends up. It works ok, it just makes the code look a little unintuitive.