Hi!
I’m trying to show my camera in Game Mode - with its Field of View wireframe bounding box rendered in the game.
How would you go about it??
Plugin? extracting the wirefrane and line-rendering?
Please help if you have an idea
Thanks
You would need to draw the lines yourself, but it’s trivial to get the positions to draw to. There are several ways to draw lines depending on what you want it to look like.
The simplest method I can think of is get the 4 corners of the far projection with Camera.ViewportToWorldPoint(). Pass in Vector3(0.0, 0.0, dist), Vector3(1.0, 0.0, dist), Vector3(1.0, 1.0, dist), and Vector3(0.0, 1.0, dist) where dist is how far away in world units you want those points. That will give you 4 world space corners that you then need to draw 4 lines between to make the rectangle, and 4 more from the camera’s position to the points.
How you draw the lines is up to you. Search for “drawing lines in Unity3d” and you’ll come across a ton of info for various options.
Thanks bgolus!
That really helps.
What do you mean by pass in those 4 vectors.
I don’t need the camera.nearClipPlane definition for it to instantiate a point in the corners of the frame.
can this work?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine;
using System.Collections;
public class viewPort : MonoBehaviour
{
public Transform prefab;
private void Start()
{
Camera camera = GetComponent<Camera>();
Instantiate(prefab, new Vector3(1, 1, camera.nearClipPlane, Quaternion.identity));
}
}
You could also use the intersection of three of the frustum planes to determine the corner vertices. I made some code for that before:
/// <summary>
/// Calculates the 8 intersection points of the 6 frustum planes.
/// Requires that plane[0] opposes plane[1], plane[2] opposes plane[3] and plane[4] opposes plane[5]
/// This is in line with the result from GeometryUtility.CalculateFrustumPlanes, where:
/// [0] = Left, [1] = Right, [2] = Down, [3] = Up, [4] = Near, [5] = Far
/// </summary>
/// <param name="planes"></param>
public static Vector3[] CalculateFrustumCorners(Plane[] planes)
{
if (planes.Length == 6)
{
Vector3[] corners = new Vector3[8];
int index = 0;
for (int i = 0;i < 2;i++)
{
for (int j = 2;j < 4;j++)
{
for (int k = 4;k < 6;k++)
{
corners[index++] = CalculateFrustumCorner(planes[i], planes[j], planes[k]);
}
}
}
return corners;
}
return null;
}
/// <summary>
/// Intersection point of three planes that are not parallel and where intersection lines are also not parallel.
/// http://geomalgorithms.com/a05-_intersect-1.html
/// </summary>
private static Vector3 CalculateFrustumCorner(Plane plane1, Plane plane2, Plane plane3)
{
float denominator = Vector3.Dot(plane1.normal, Vector3.Cross(plane2.normal, plane3.normal));
if (Mathf.Abs(denominator) > Mathf.Epsilon)
{
Vector3 nominator = -plane1.distance * Vector3.Cross(plane2.normal, plane3.normal);
nominator -= plane2.distance * Vector3.Cross(plane3.normal, plane1.normal);
nominator -= plane3.distance * Vector3.Cross(plane1.normal, plane2.normal);
nominator *= 1.0f / denominator;
return nominator;
}
else
{
Debug.LogWarning("Corner error");
}
return Vector3.zero;
}