Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
5
Question by jacksmash2012 · Sep 06, 2012 at 12:21 PM · planes

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

7 Replies

· Add your reply
  • Sort: 
avatar image
6
Best Answer

Answer by mouurusai · Sep 06, 2012 at 08:56 PM

 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)

Comment
Add comment · Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image jacksmash2012 · Oct 12, 2012 at 05:21 PM 0
Share

Thanks. This works well.

avatar image Nullspace · Feb 18, 2013 at 02:57 PM 0
Share

The third component of transform.localScale is 0. It results in a 0 in scale z and makes the plane invisible. How to solve this?

avatar image jacksmash2012 · Feb 19, 2013 at 02:19 PM 0
Share

Simple - just hardcode the z-value to something you need.

avatar image Elsammak · Nov 21, 2013 at 07:58 PM 0
Share

I have a plane of size (1,1.5) & tried this but it doesn't work, am I missing something?

avatar image MrMatthias · May 20, 2014 at 09:10 PM 1
Share

This will work for a quad but not for the unity plane. The unity plane is 10 times bigger and rotated 90 degrees around the x axis. The rotation should also be changed to face the camera:

     using UnityEngine;
      
     public class FillScreen:$$anonymous$$onoBehaviour
     {
     void Update()
     {
     Camera cam = Camera.main;
      
     float pos = (cam.nearClipPlane + 0.01f);
      
     transform.position = cam.transform.position + cam.transform.forward * pos;
     transform.LookAt (cam.transform);
     transform.Rotate (90.0f, 0.0f, 0.0f);
      
     float h = ($$anonymous$$athf.Tan(cam.fov*$$anonymous$$athf.Deg2Rad*0.5f)*pos*2f) /10.0f;
      
     transform.localScale = new Vector3(h*cam.aspect,1.0f, h);
     }
     }
avatar image udubaso MrMatthias · Oct 27, 2017 at 01:14 PM 0
Share

Thank you!!! I have tried a lot of codes, but finally this works perfectly!

Show more comments
avatar image
2

Answer by CHPedersen · Sep 06, 2012 at 02:25 PM

The correct way to do this is to use GUITextures. Nevermind that it's drawn last. :) 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.

Comment
Add comment · Show 9 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image jacksmash2012 · Sep 06, 2012 at 02:28 PM 0
Share

I think this is precisely what I was looking for. I'll try it right now...

avatar image CHPedersen · Sep 06, 2012 at 02:40 PM 0
Share

I forgot to mention that the culling mask of all other cameras must of course EXCLUDE the layer you're sticking the background in. :) (Or remove their GUILayer component, so they can't render GUITextures)

$$anonymous$$eep in $$anonymous$$d that this works at runtime and in the game view, but not in the scene view, because you don't have access to modify the layer properties of the scene view camera.

avatar image jacksmash2012 · Sep 06, 2012 at 02:46 PM 0
Share

I was using a plane because I thought I couldn't get a GUITexture to render the way I wanted it to... but I was wrong.

However, I still have a problem. Yes, I can get the GUITexture to render the way I want. But when the user presses a button, he is given an alternative view. The first view he is presented with is a "closeup" view (where the GUITexture gives the desired background). But the secondary view us a more "distant" view where the GUITexture should still provide a background, but only of a small section of the view (that is still proportional). With a plane, this would be easy because you just engage a 2nd camera and the plane still has it's same position... but I'm not sure what to do with the GUITexture approach.

I hope this makes some sense.

avatar image jacksmash2012 · Sep 06, 2012 at 02:47 PM 0
Share

In other words... the GUITexture should not take up the entire screen in the 2nd view... only in the first "closeup" view.

avatar image CHPedersen · Sep 06, 2012 at 02:48 PM 0
Share

He wants to have a plane fill the entire field of view. He does not want a skybox. A skybox has seams where the sides meet which require you to design textures that repeat neatly so the seams are invisible. This is unnecessary given the simple requirement that he just wants a texture to fill the entire screen.

Putting an actual geometrical plane in 3D and carefully scaling and positioning it so it fills up the far plane is an awkward solution because it forces polygons through the pipeline. There is no reason to multiply 3D vertices with an $$anonymous$$VP matrix, no matter how simple an object, if what you want is just an orthographic texture covering the entire screen.

Show more comments
avatar image
1

Answer by lodendsg · Sep 06, 2012 at 01:29 PM

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.

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image lodendsg · Sep 06, 2012 at 01:32 PM 0
Share

Once you have the planes there you just need to pick the one you want being the "back" one.

avatar image jacksmash2012 · Sep 06, 2012 at 02:12 PM 0
Share

Ok, so by using ViewportToWorldPoint, I can definitely get the bounds of the plane at some value for Z, but that doesn't help me scale the Plane object in my scene so that it fits the viewport perfectly... unless I'm missing something.

avatar image lodendsg · Sep 06, 2012 at 02:29 PM 0
Share

I'm not where I can code and test but but psudo

(assu$$anonymous$$g your plane is $$anonymous$$yPlane)

//Get a world space vector to the upper right corner of the screen

Vector3 UpRight = camera.ViewportToWorldPoint (new Vector3 (1,1, $$anonymous$$yPlane.transform.z));

//Get a would space vector to the lower left corner of the screen

Vector3 DownLeft = camera.ViewportToWorldPoint (new Vector3 (-1,-1, $$anonymous$$yPlane.transform.z));

//Set our width scale to be right - left

$$anonymous$$yPlane.scale.x = UpRight.x - DownLeft.x;

//Set our height scale to be up - down

$$anonymous$$yPlane.scale.y = UpRight.y - DownLeft.y;

avatar image jacksmash2012 · Sep 06, 2012 at 03:12 PM 0
Share

Yes, so that scales the plane to be the correct aspect ratio as far as I can tell... but it doesn't fit it inside the viewport (it's much too large).

avatar image sfilo · Mar 12, 2014 at 11:04 AM 0
Share

The bottom-left of the camera is (0,0) so if you change DownLeft to
Vector3 DownLeft = camera.ViewportToWorldPoint (new Vector3 (0,0, $$anonymous$$yPlane.transform.z)); it will work fine.

avatar image
0

Answer by hawken · Sep 15, 2019 at 01:05 PM

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by RakNet · Nov 11, 2019 at 02:45 PM

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);
     }
 }
 
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
  • 1
  • 2
  • ›

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Understanding Texture Atlasing 0 Answers

Error CS8025: Parsing error. HELP! 1 Answer

GUI using planes - pixel perfect on iPad 2, not on iPad 1 0 Answers

Double-sided cross section ("slice") 0 Answers

Which plane is which with CalculateFrustumPlanes 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges