Creating plane of dynamic sizes

So I want to create a plane that will cover the screen and needs to be exact since I am applying textures to it. Is there a way to create a plane with specific width/heights according to the main camera width/heights? I have been trying to create the planes with “primPlane = new GameObject.CreatePrimitive(PrimitiveType.Plane);” but since it’s a GameObject it doesn’t have a way to change width/height only the scale but this doesn’t work well for different resolutions. Is there already someway to do this or do I need to create a mesh to the camera dimensions and then apply the texture to it?

Thanks

You can convert screen space coordinates to camera space coordinates and then either generate a polygon of that size or scale an existing unit polygon to the calculated size. You can scale your PrimtiveType.Plane too but Unity3D makes a plane out of several polygons and therefore will most likely give you texture artifacting if you are seeking pixel perfect representation.

http://www.unifycommunity.com/wiki/index.php?title=CameraFacingBillboard

This one billboards a polygon, I am sure you can figure out how to calculate the size based on camera transform.

http://www.unifycommunity.com/wiki/index.php?title=SplashScreen

Second link just demonstrates showing a GUI texture which may work for your needs. You never specified the desired end result.

Well what I am doing is taking a picture of the scene(using the main camera) and trying to apply it on the backdrop of the scene. I have the whole taking picture and applying it down, but it’s a pain to get this to work with precision. Sometimes its off by a little bit or when I try running different resolutions it is completely off. With the billboard script I need to attach that to an object which isn’t really dynamic atleast I don’t think so, still a noob to unity. I am using an orthographic camera also if that helps.

I am not sure what you mean by “isn’t really dynamic.” I could infer an (in)correct assumption if you like.

Take your screen coords you want to cover, run them through the ScreenToWorld method of your camera, with a distance of how far back from the camera you want the polygon to be. That will give you world space coordinates you can draw you polygon at. You can either dynamically generate a polygon and assign it a material using the GL object (easy, quick, simple, and will do precisely what you need), create a unit polygon in a 3D package and scale it to how big it needs to be (a bit of overkill with the 3D package really) or stick with the PrimitiveType.Plane and scale that to correct size.

I don’t see a problem with attaching the script to a game object, that is the way of Unity unless you are instantiating a class from a MonoBehaviour or declaring a partially static class that self instantiates, e.g. Singleton.

Ok thanks the ScreenToWorld function was what it was. I had something similar to this before but it was gigantic so I thought I was doing it wrong. Now just need to figure out how to get textures on this thing.

    gameObject.AddComponent("MeshFilter");
    gameObject.AddComponent("MeshRenderer");
    gameObject.transform.position = Camera.mainCamera.transform.position;
    gameObject.transform.position.z = 4;
    var mesh : Mesh = GetComponent(MeshFilter).mesh;
	var aa = Camera.mainCamera.ScreenToWorldPoint(Vector3(0,0,0));
	var ab = Camera.mainCamera.ScreenToWorldPoint(Vector3(Camera.mainCamera.pixelWidth,0,0));
	var ac = Camera.mainCamera.ScreenToWorldPoint(Vector3(0,Camera.mainCamera.pixelHeight,0));
	var ad = Camera.mainCamera.ScreenToWorldPoint(Vector3(Camera.mainCamera.pixelWidth,Camera.mainCamera.pixelHeight,0));

    mesh.Clear();
    mesh.vertices = [ab, aa, ac, ad];
    mesh.uv = [Vector2 (0, 0), Vector2 (0, 0), Vector2 (0, 0), Vector2 (0, 0)];
    mesh.triangles = [0, 1, 2,
    	                 0, 2, 3];

You could use a GUITexture instead. If you set all the pixel inset values to 0 and use 1,1 as the scale, it’s automatically as big as the screen, regardless of resolution. The only slightly tricky part is making a GUITexture the background, but that’s accomplished by using a separate camera for the GUITexture, and another camera for rendering everything else on top.

–Eric

Well, I wanted to drag a plane to my scene, scale it as needed and then use my own 2d collision function on it for a 2d game. I did something that could work for your needs. Manually place an empty game object in each of your plane borders. You could even use just 2 of them for the top-left and bottom-right corners. Then, as you scale the plane, this game objects will adjust properly. Then, you can ask for their transform.position to measure the plane dimensions or to know the position of each edge/corner. Remember that the unity editor will display local positions. In code you will get the position in global space (that’s what you need) unless you use transform.localPosition. If you know how to do some editor scripts, you could even print your current plane dimensions as you resize them in the editor, so you don’t need to be playing to have the measures you are looking for. I hope this helps.

Something like this will do:

		//Get camera component
		cam = GetComponent<Camera>();
		//Add a quad as a parent to Camera gameobject
		//Transform
		go = new GameObject("Quad");
		
		go.transform.localPosition = Vector3.zero;
		go.transform.localRotation = Quaternion.identity;
		go.transform.parent = transform;
		//Mesh
		go.AddComponent("MeshFilter");
		go.AddComponent("MeshRenderer");
		go_Renderer = go.GetComponent<Renderer>();
		go_Mesh = go.GetComponent<MeshFilter>().mesh;
		go_Mesh.Clear();
		go_Mesh.vertices = new Vector3[] {
			cam.ScreenToWorldPoint(new Vector3(0, 0, cam.nearClipPlane + 0.1f)),
			cam.ScreenToWorldPoint(new Vector3(0, cam.pixelHeight, cam.nearClipPlane + 0.1f)),
			cam.ScreenToWorldPoint(new Vector3(cam.pixelWidth, cam.pixelHeight, cam.nearClipPlane + 0.1f)),
			cam.ScreenToWorldPoint(new Vector3(cam.pixelWidth, 0, cam.nearClipPlane + 0.1f))
		};
		go_Mesh.uv = new Vector2[] {
			new Vector2(0, 0), new Vector2(0, 1),
			new Vector2(1, 1), new Vector2(1, 0)
		};
		go_Mesh.triangles = new int[] {0, 1, 2, 0, 2, 3};

play with nearclip plane values, or just leave it as it is and do the material on this same as skybox render queue.