- Home /
Drawing onto a render texture in OnGUI
Hi,
I have a camera rendering a view of the scene into a render texture. The texture is then drawn using Graphics.DrawTexture with a material that gives it a nice radar effect. So now I'm trying to draw enemy locations onto this same texture as blips so that they also get rendered using the same material effect.
However, no matter what I try the render texture never shows the blips. The code is as follows:
RenderTexture.active = RadarTexture;
// draw all the blips for enemies and turrets
for (int i = 0; i < AIManager.AIObjects.Count; i++)
{
Vector3 pos = camera.WorldToScreenPoint(AIManager.AIObjects[i].Trans.position);
pos.y = camera.pixelHeight - pos.y;
// scale the position by the radar size
float xScale = size / camera.pixelWidth;
float yScale = size / camera.pixelHeight;
// scale the position by the radar size in relation to the screen
pos.x *= xScale;
pos.y *= yScale;
Vector2 centre = pos;
centre.x -= 6;
centre.y -= 6;
// make sure the centre of the blip is within the radius of the radar image
if (Vector3.Distance(_RadarCentre, centre) < (size / 2.25f))
{
Graphics.DrawTexture(new Rect(centre.x, centre.y, 12, 12), RadarBlip);
}
}
RenderTexture.active = null;
Graphics.DrawTexture(new Rect(_RadarScreenPos.x, _RadarScreenPos.y, size, size), RadarTexture, RadarMaterial);
As you can see I'm setting the active render target to my radar texture (the camera has this assigned so is rendering into it every frame) and then drawing the blips onto the texture before drawing the texture with the appropriate material.
This code seems logical to me and yet I never see any blips - but if I draw them without assigning RenderTexture.active they appear fine (except not drawn on the radar texture of course).
Can anyone see what I'm doing wrong? Intuitively I would have thought this would work.
Thanks.