How to make a plane fill the field of view?

I’m wondering if it is possible to construct a plane that fills the camera’s fov at an arbitrary distance from the camera?

For instance, I have a plane with a texture on it that I want to fill the screen, but the camera must be in perspective mode (for other reasons that I won’t go into). And yes, it must be on a plane. I can’t use a GUITexture because it is drawn last.

Thanks for any insights.

using UnityEngine;

public class FillScreen:MonoBehaviour
{
    void Update()
    {
        Camera cam = Camera.main;

        float pos = (cam.nearClipPlane + 0.01f);

        transform.position = cam.transform.position + cam.transform.forward * pos;

        float h = Mathf.Tan(cam.fov*Mathf.Deg2Rad*0.5f)*pos*2f;

        transform.localScale = new Vector3(h*cam.aspect,h,0f);
    }
}

Its work for plane with size (1,1)

The correct way to do this is to use GUITextures. Nevermind that it’s drawn last. :slight_smile: I’ll get to this in a bit:

First, make a GUITexture, and set the Texture to whatever you want to cover the whole screen. Now, set all Pixel Inset values to 0, as well as all borders. Now set scale to (1,1,0) and position to (0.5,0.5,0). This causes the texture to take up the entire screen.

As for the drawing last thing:

Define a new layer called “BGLayer” or whatever, and set the GUITexture to belong to this layer. Now, create a new camera, and set its culling mask to draw nothing BUT the layer that contains the background. Set this camera’s depth property to a value which is less than all other cameras in your scene. -1, for example, and then all other cameras to 0 or above. All other cameras must have their Clear Flags set to Depth Only.

This causes the background to always get drawn first, because it’s rendered by a camera that always renders first. All other cameras then only clear the depth buffer, then renders their content right on top of your background, as you wanted.

Check into this (http://docs.unity3d.com/Documentation/ScriptReference/GeometryUtility.CalculateFrustumPlanes.html) it calculates the planes of the camera’s frustum being the limits of the cameras view.

Might also have a look at this, (http://docs.unity3d.com/Documentation/ScriptReference/Camera.ViewportToWorldPoint.html) which could be used to simply find the points at the edge of the screen given a distance from the camera.

I realise this topic is old as the hills, but you don’t need to rotate the plane in Update, just do it once in Start.
To make it always face the camera just parent it to the camera.

The earlier answers are close but not quite right. Here is the correct answer for a plane.
using UnityEngine;

[ExecuteInEditMode]
public class PlaneFillScreen : MonoBehaviour
{
	private void Update()
	{
		Camera cam = Camera.main;

		float pos = (cam.nearClipPlane + 10.0f);

		transform.position = cam.transform.position + cam.transform.forward * pos;
		transform.LookAt(cam.transform);
		float h = (Mathf.Tan(cam.fieldOfView * Mathf.Deg2Rad * 0.5f) * pos * 2f) * cam.aspect / 10.0f;
		float w = h * Screen.height / Screen.width;
		transform.localScale = new Vector3(h, w, 1);
	}
}

Or…

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlaneScale1 : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        float distance = Vector3.Distance(Camera.main.transform.position, gameObject.transform.position);
        float height = 2.0f * Mathf.Tan(0.5f * Camera.main.fieldOfView * Mathf.Deg2Rad) * distance;
        float width = height * Screen.width / Screen.height;
        gameObject.transform.localScale = new Vector3(width / 10f, 1.0f, height / 10f);

    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

what is this plane for?
You could draw a plane with a couple triangles in clipping space and a specific shader that forwards the vertex original values to the pixel shader.
Elegant solution but depends on the purpose of the plane.