Question by
venenomz97 · Feb 04 at 10:04 AM ·
rendertexturegl
Minimap Cam Rotation Using UnityEngine.GL
In my project I have a minimap that renders the viewport of the camera onto a render texture. It works great, however my camera needs to rotate and having rotation messes with the GL pretty badly. Below is the code for rendering the camera viewport.
private Vector3 GetCameraFrustum(Vector3 pos)
{
Ray posRay = Game.MainCamera.Cam.ScreenPointToRay(pos);
Vector3 res = Game.MapCollider.Raycast(posRay, out RaycastHit hit, 200) ? hit.point : new Vector3();
return res;
}
public void OnPostRender()
{
if (_canRender)
{
Vector3 minViewPortPoint = m_minimapCam.WorldToViewportPoint(GetCameraFrustum(Vector3.zero));
Vector3 maxViewPortPoint = m_minimapCam.WorldToViewportPoint(GetCameraFrustum(new Vector3(Screen.width, Screen.height, Game.MainCamera.Cam.nearClipPlane)));
float minx = minViewPortPoint.x;
float miny = minViewPortPoint.y;
float maxX = maxViewPortPoint.x;
float maxY = maxViewPortPoint.y;
GL.PushMatrix();
{
m_camBoxMaterial.SetPass(0);
GL.LoadOrtho();
GL.Begin(GL.QUADS);
GL.Color(Color.black);
{
GL.Vertex(new Vector3(minx, miny + m_linewidth, 0));
GL.Vertex(new Vector3(minx, miny - m_linewidth, 0));
GL.Vertex(new Vector3(maxX, miny - m_linewidth, 0));
GL.Vertex(new Vector3(maxX, miny + m_linewidth, 0));
GL.Vertex(new Vector3(minx + m_linewidth, miny, 0));
GL.Vertex(new Vector3(minx - m_linewidth, miny, 0));
GL.Vertex(new Vector3(minx - m_linewidth, maxY, 0));
GL.Vertex(new Vector3(minx + m_linewidth, maxY, 0));
GL.Vertex(new Vector3(minx, maxY + m_linewidth, 0));
GL.Vertex(new Vector3(minx, maxY - m_linewidth, 0));
GL.Vertex(new Vector3(maxX, maxY - m_linewidth, 0));
GL.Vertex(new Vector3(maxX, maxY + m_linewidth, 0));
GL.Vertex(new Vector3(maxX + m_linewidth, miny, 0));
GL.Vertex(new Vector3(maxX - m_linewidth, miny, 0));
GL.Vertex(new Vector3(maxX - m_linewidth, maxY, 0));
GL.Vertex(new Vector3(maxX + m_linewidth, maxY, 0));
}
GL.End();
}
GL.PopMatrix();
}
}
My camera rotates on the Z axis, but when it does some weird behaviour occures, which you can find in the attached gif. https://gyazo.com/f129336f85e3ae7c09fce150e787354f Is there a way to have it nicely rotate with the camera?
Comment
Your answer
Follow this Question
Related Questions
Cannot get RenderTexture to clear garbage. 2 Answers
CEFGlue with Unity 1 Answer
Mesh Texture Not Rendered On Edges on iOS 0 Answers
Selectively apply material via RaycastHit 0 Answers
Capturing high resolution screenshot with Temporal AA 1 Answer