Draw trapezoid on minimap with support for any camera angle

Hi guys. I guess it’s more of a math problem but I can’t really figure it out on my own.

Say in Starcraft II your minimap box that represents your view area is a trapezoid. Now if you would tilt camera and change its angle so that you can see far away up to the skybox, trapezoid would gain a lot of height and it would be stretched on the minimap to cover almost all of it. This is done in for example Heroes of Newerth practice mode.

For me to build such trapezoid I guess I would need to get simply four points representing my view (translate bottom points and top points from screen to game world?). But I’m not sure if that’s the way to do it. Any tips are welcome.

Ok, it’s been a while since I wrote this and I just ripped the following portion from my larger minimap script, so it might not work in this form but it should be enough to get you the idea.

This is for drawing a basci rectangular box to represent your screen portion on the minimap, I tried to draw a accurate trapezoid representation by using some user-created GUI drawline script ut couldn’t figure it out. For this reason, there are only two points need for the following example (top left and bottom right corner).

Basically, you cast rays at the corners of your main screen and store the hit points as WorldToViewportPoint coordinates of your minimap camera, then use those for drawing your box or lines or whatever in OnGUI.

// +++ MiniMap Stuff
var MiniMapLastPos : Vector2 = Vector2.zero;
var MiniMapAbsoluteWidth = 248.0; //in pixels
var MiniMapAbsoluteHeight = 124.0;
var MiniMapOffsetX : int = 20;
var MiniMapOffsetY : int = 64;

var ScreenCorner1 : Vector3;
var ScreenCorner2 : Vector3;

var MiniMapCorner1 : Vector3;
var MiniMapCorner2 : Vector3;

var MiniMapScreenOffsetX1 : int;
var MiniMapScreenOffsetY1 : int;

var MiniMapScreenOffsetX2 : int;
var MiniMapScreenOffsetY2 : int;

var MiniMapCamera : Camera;
MiniMapCamera = GameObject.Find("MiniMapCamera").camera;

var BorderTexture : Texture2d; //just a small transparent image with a white border

var MapBackgroundMask : LayerMask;

function Update()
{
     //MiniMap Stuff
     var MiniMapCurrentPos = Vector2(Screen.width, Screen.height);
     if ((MiniMapLastPos - MiniMapCurrentPos).magnitude != 2)
     {
         MiniMapLastPos = MiniMapCurrentPos;
	 MiniMapCamera.pixelRect = new Rect(MiniMapOffsetX, Screen.height - MiniMapAbsoluteHeight - MiniMapOffsetY, MiniMapAbsoluteWidth, MiniMapAbsoluteHeight);
     }
    
     var ray1 : Ray = MainCamera.camera.ViewportPointToRay (Vector3(0,1,0));
     var hit1 : RaycastHit;
     if (Physics.Raycast (ray1, hit1, Mathf.Infinity, MapBackgroundMask))
     {
     ScreenCorner1 = Vector3(hit1.point.x, hit1.point.y, hit1.point.z);
     //print (ScreenCorner1);
     }
     var ray2 : Ray = MainCamera.camera.ViewportPointToRay (Vector3(1,0,0));
     var hit2 : RaycastHit;
     if (Physics.Raycast (ray2, hit2, Mathf.Infinity, MapBackgroundMask))
     {
          ScreenCorner2 = Vector3(hit2.point.x, hit2.point.y, hit2.point.z);
	  //print (ScreenCorner2);
     }
    
     MiniMapCorner1 = MiniMapCamera.camera.WorldToViewportPoint(ScreenCorner1);
     MiniMapCorner2 = MiniMapCamera.camera.WorldToViewportPoint(ScreenCorner2);

     MiniMapScreenOffsetX1 = MiniMapAbsoluteWidth * MiniMapCorner1.x;
     MiniMapScreenOffsetY1 = MiniMapAbsoluteHeight * MiniMapCorner1.y;
    
     MiniMapScreenOffsetX2 = MiniMapAbsoluteWidth * MiniMapCorner2.x;
     MiniMapScreenOffsetY2 = MiniMapAbsoluteHeight * MiniMapCorner2.y
}

function OnGUI()
{
     var MiniMapWindow = new Rect(MiniMapScreenOffsetX1 + MiniMapWindowOffsetX, MiniMapWindowOffsetY - MiniMapScreenOffsetY1, MiniMapScreenOffsetX2 - MiniMapScreenOffsetX1, MiniMapScreenOffsetY1 - MiniMapScreenOffsetY2);
	GUI.Box(MiniMapWindow,"MiniMap Box", BorderTexture);
}

using UnityEngine;
using System.Collections;

public class sampleInCSharp : MonoBehaviour 
{
	public Vector2 MiniMapLastPos = Vector2.zero;
	public float MiniMapAbsoluteWidth = 248.0f; //in pixels
	public float MiniMapAbsoluteHeight = 124.0f;
	public int MiniMapOffsetX = 20;
	public int MiniMapOffsetY = 64;
	
	public Vector3 ScreenCorner1;
	public Vector3 ScreenCorner2;
	
	public Vector3 MiniMapCorner1;
	public Vector3 MiniMapCorner2;
	
	public float MiniMapScreenOffsetX1;
	public float MiniMapScreenOffsetY1;
	
	public float MiniMapScreenOffsetX2;
	public float MiniMapScreenOffsetY2;
	
	Camera MiniMapCamera;
	void Start ()
	{
		MiniMapCamera = GameObject.Find("CameraMiniMap").GetComponent<Camera>();
	}
	
	Texture2D BorderTexture; //just a small transparent image with a white border

	LayerMask MapBackgroundMask;

	void OnGUI()
	{
		//MiniMap Stuff
		Vector2 MiniMapCurrentPos = new Vector2(Screen.width, Screen.height);
		if ((MiniMapLastPos - MiniMapCurrentPos).magnitude != 2)
		{
			MiniMapLastPos = MiniMapCurrentPos;
			MiniMapCamera.pixelRect = new Rect(MiniMapOffsetX, Screen.height - MiniMapAbsoluteHeight - MiniMapOffsetY, MiniMapAbsoluteWidth, MiniMapAbsoluteHeight);
		}
		
		Ray ray1 = Camera.main.ViewportPointToRay (new Vector3(0,1,0));
		RaycastHit hit1;
		if (Physics.Raycast (ray1, out hit1, Mathf.Infinity, MapBackgroundMask))
		{
			ScreenCorner1 = new Vector3(hit1.point.x, hit1.point.y, hit1.point.z);
			Debug.Log (ScreenCorner1);
		}
		Ray ray2 = Camera.main.ViewportPointToRay (new Vector3(1,0,0));
		RaycastHit hit2;
		if (Physics.Raycast (ray2, out hit2, Mathf.Infinity, MapBackgroundMask))
		{
			ScreenCorner2 = new Vector3(hit2.point.x, hit2.point.y, hit2.point.z);
			Debug.Log (ScreenCorner2);
		}
		
		MiniMapCorner1 = MiniMapCamera.camera.WorldToViewportPoint(ScreenCorner1);
		MiniMapCorner2 = MiniMapCamera.camera.WorldToViewportPoint(ScreenCorner2);
		
		MiniMapScreenOffsetX1 = MiniMapAbsoluteWidth * MiniMapCorner1.x;
		MiniMapScreenOffsetY1 = MiniMapAbsoluteHeight * MiniMapCorner1.y;
		
		MiniMapScreenOffsetX2 = MiniMapAbsoluteWidth * MiniMapCorner2.x;
		MiniMapScreenOffsetY2 = MiniMapAbsoluteHeight * MiniMapCorner2.y;


		Rect MiniMapWindow = new Rect(MiniMapScreenOffsetX1 + MiniMapScreenOffsetX1, MiniMapScreenOffsetY1 - MiniMapScreenOffsetY1, MiniMapScreenOffsetX2 - MiniMapScreenOffsetX1, MiniMapScreenOffsetY1 - MiniMapScreenOffsetY2);
		GUI.Box(MiniMapWindow, BorderTexture);
	}
}

its working, but i can see affects, i nned an rect as main Camera position in Minimap.
Anyone can help me?