ViewportToScreenPoint() public method

Transforms position from viewport space into screen space.

public ViewportToScreenPoint ( Vector3 position ) : Vector3
position Vector3
return Vector3
Exemplo n.º 1
0
    // Use this for initialization
    void Start()
    {
        // Try default camera
        cameraObject = GameObject.Find("Camera");
        myCamera = (Camera) cameraObject.GetComponent("Camera");

        planeSize = myCamera.ViewportToScreenPoint(this.transform.position);
        planeSize.x -= Screen.width;
        planeSize.y -= Screen.height;
    }
Exemplo n.º 2
0
 static public int ViewportToScreenPoint(IntPtr l)
 {
     try{
         UnityEngine.Camera  self = (UnityEngine.Camera)checkSelf(l);
         UnityEngine.Vector3 a1;
         checkType(l, 2, out a1);
         UnityEngine.Vector3 ret = self.ViewportToScreenPoint(a1);
         pushValue(l, ret);
         return(1);
     }
     catch (Exception e) {
         LuaDLL.luaL_error(l, e.ToString());
         return(0);
     }
 }
 static public int ViewportToScreenPoint(IntPtr l)
 {
     try {
         UnityEngine.Camera  self = (UnityEngine.Camera)checkSelf(l);
         UnityEngine.Vector3 a1;
         checkType(l, 2, out a1);
         var ret = self.ViewportToScreenPoint(a1);
         pushValue(l, true);
         pushValue(l, ret);
         return(2);
     }
     catch (Exception e) {
         return(error(l, e));
     }
 }
Exemplo n.º 4
0
    /// <summary>
    /// Calculates the box surrounding the of the unit.
    /// </summary>
    /// <returns>The box</returns>
    /// <param name="cntr">The center of the unit</param>
    public static Rect CalculateBoxFromCntr(Vector3 cntr, Camera cam, int pix)
    {
        Rect box = new Rect();

        Vector3 actor_cntr = cam.WorldToViewportPoint(cntr);
        actor_cntr = cam.ViewportToScreenPoint(actor_cntr);
        Debug.Log(actor_cntr.x + " : " + actor_cntr.y + " : " + actor_cntr.z);

        box.xMin = actor_cntr.x - pix;
        box.xMax = actor_cntr.x + pix;
        box.yMin = Screen.height - actor_cntr.y - pix;
        box.yMax = Screen.height - actor_cntr.y + pix;

        return box;

    }
Exemplo n.º 5
0
 public float ViewportToScreenY(Camera camera, float viewportY)
 {
     return camera.ViewportToScreenPoint(new Vector2(0, viewportY)).y;
 }
Exemplo n.º 6
0
 public float ViewportToScreenX(Camera camera, float viewportX)
 {
     return camera.ViewportToScreenPoint(new Vector2(viewportX, 0)).x;
 }
Exemplo n.º 7
0
 public static Vector2 ViewportToCameraPoint(Camera cam, Vector2 viewportPoint)
 {
     return (Vector2)cam.ViewportToScreenPoint(viewportPoint) - new Vector2(cam.rect.x * Screen.width, cam.rect.y * Screen.height);
 }
    public static float GetPerspectiveWidth(Collider objectCollider, Camera camera, out Vector3 rightestViewPoint)
    {
        //First get the center middle points
        Bounds objBounds = objectCollider.bounds;
        float adjustValue = 1f;
        float xNeg = objBounds.center.x - (objBounds.extents.x * adjustValue);
        float xPos = objBounds.center.x + (objBounds.extents.x * adjustValue);

        float zNeg = objBounds.center.z - (objBounds.extents.z * adjustValue);
        float zPos = objBounds.center.z + (objBounds.extents.z * adjustValue);

        List<Vector3> cornerCoordinates = new List<Vector3>();
        for (int x = 0; x < 2; x++)
        {
                for (int z = 0; z < 2; z++)
                {
                    Vector3 coor = new Vector3();
                    coor.x = (x == 0) ? xNeg : xPos;
                    coor.y = objBounds.center.y;
                    coor.z = (z == 0) ? zNeg : zPos;
                    cornerCoordinates.Add(coor);
                }
        }

        Vector3 mostLeft = camera.WorldToViewportPoint(cornerCoordinates[0]);
        Vector3 mostRight = camera.WorldToViewportPoint(cornerCoordinates[0]);

        Vector3 originalLeft = cornerCoordinates[0];
        Vector3 originalRight = cornerCoordinates[0];

        //Debug.Log("Center: " + mostLeft);

        foreach (Vector3 coor in cornerCoordinates)
        {

            Vector3 screenCoor = camera.WorldToViewportPoint(coor);
            if (screenCoor.x <= mostLeft.x)
            {
                mostLeft = camera.WorldToViewportPoint(coor);
                originalLeft = coor;
            }
            else if (screenCoor.x >= mostRight.x)
            {
                mostRight = camera.WorldToViewportPoint(coor);
                originalRight = coor;
            }
            //Debug.Log(screenCoor);
        }

        //Fix for weird thing with only seeing one plane (ask me, or not. Rather dont actually)
        Vector3 firstClosest = new Vector3(float.MaxValue, float.MaxValue, float.MaxValue);
        Vector3 secondClosest = new Vector3(float.MaxValue, float.MaxValue, float.MaxValue);
        Vector3 thirdClosest = new Vector3(float.MaxValue, float.MaxValue, float.MaxValue);
        if (originalLeft.x == originalRight.x || originalRight.z == originalLeft.z)
        {

            foreach (Vector3 coor in cornerCoordinates)
            {
                if (Vector3.Distance(camera.transform.position, thirdClosest) >= Vector3.Distance(camera.transform.position, coor))
                {
                    thirdClosest = coor;
                    if (Vector3.Distance(camera.transform.position, secondClosest) >= Vector3.Distance(camera.transform.position, thirdClosest))
                    {
                        Vector3 placeholder = secondClosest;
                        secondClosest = thirdClosest;
                        thirdClosest = placeholder;
                        if (Vector3.Distance(camera.transform.position, firstClosest) >= Vector3.Distance(camera.transform.position, secondClosest))
                        {
                            Vector3 placeholder2 = firstClosest;
                            firstClosest = secondClosest;
                            secondClosest = placeholder2;
                        }
                    }
                }
            }

            //Vector3

            if (Mathf.Abs(objBounds.center.x - camera.transform.position.x) <= Mathf.Abs(objBounds.center.x - firstClosest.x))
            {
                if (firstClosest.z == thirdClosest.z)
                {
                    secondClosest = thirdClosest;
                }
            }
            else
            {
                //dan is ie in de z categorie
                if (firstClosest.x == thirdClosest.x)
                {
                    secondClosest = thirdClosest;
                }
            }

            if (camera.WorldToViewportPoint(firstClosest).x >= camera.WorldToViewportPoint(secondClosest).x)
            {
                originalLeft = secondClosest;
                originalRight = firstClosest;
            }
            else
            {
                originalLeft = firstClosest;
                originalRight = secondClosest;
            }

            mostLeft = camera.WorldToViewportPoint(originalLeft);
            mostRight = camera.WorldToViewportPoint(originalRight);

            //Debug.Log("weird");
        }
        //TODO fix bug with the viewpoint switching and the code below is not correct anymore somehow

        //Debug.Log(camera.ViewportToScreenPoint(mostRight));
        //Debug.Log("left" + camera.ViewportToScreenPoint(mostLeft));

        if (mostLeft.x <= 0)
        {
            mostLeft.x = 0;
        }
        else if (mostLeft.x >= 1)
        {
            mostLeft.x = 1;
        }

        if (mostRight.x <= 0)
        {
            mostRight.x = 0;
        }
        else if (mostRight.x >= 1)
        {
            mostRight.x = 1;
        }

        Debug.DrawLine(originalLeft, objBounds.center, Color.blue, 0.5f);
        Debug.DrawLine(originalRight, objBounds.center, Color.red, 0.5f);

        //Debug.Log(camera.pixelRect);

        float width = camera.ViewportToScreenPoint(mostRight).x - camera.ViewportToScreenPoint(mostLeft).x;
        //Debug.Log(width);
        rightestViewPoint = camera.ViewportToScreenPoint(mostRight);
        return width;  //Total width can be gotten with pixelwidth of camera.

        // also return x and y from center point of visible points!
        //maybe also inform of pixel rect
    }
    public static float GetPerspectiveHeigth(Collider objectCollider, Camera camera, out Vector3 highestViewPoint)
    {
        Bounds objBounds = objectCollider.bounds;

        float adjustValue = 1;
        float xNeg = objBounds.center.x - (objBounds.extents.x * adjustValue);
        float xPos = objBounds.center.x + (objBounds.extents.x * adjustValue);
        float yNeg = objBounds.center.y - (objBounds.extents.y * adjustValue);
        float yPos = objBounds.center.y + (objBounds.extents.y * adjustValue);
        float zNeg = objBounds.center.z - (objBounds.extents.z * adjustValue);
        float zPos = objBounds.center.z + (objBounds.extents.z * adjustValue);

        //List all different coordinates of the cube vertexes
        List<Vector3> cornerCoordinates = new List<Vector3>();
        List<Vector3> viewPortCornerCoordinates = new List<Vector3>();
        for (int x = 0; x < 2; x++)
        {
            for (int y = 0; y < 2; y++)
            {
                for (int z = 0; z < 2; z++)
                {
                    Vector3 coor = new Vector3();
                    coor.x = (x == 0) ? xNeg : xPos;
                    coor.y = (y == 0) ? yNeg : yPos;
                    coor.z = (z == 0) ? zNeg : zPos;
                    cornerCoordinates.Add(coor);
                    viewPortCornerCoordinates.Add(camera.WorldToViewportPoint(coor));
                }
            }
        }

        Vector3 highestPoint = new Vector3(float.MinValue, float.MinValue, float.MinValue);
        Vector3 lowestPoint = new Vector3(float.MaxValue, float.MaxValue, float.MaxValue);

        foreach (Vector3 vpCoor in viewPortCornerCoordinates)
        {
            if (vpCoor.y >= highestPoint.y)
            {
                highestPoint = vpCoor;
            }
            else if(vpCoor.y <= lowestPoint.y)
            {
                lowestPoint = vpCoor;
            }
        }

        if (highestPoint.y >= 1)
        {
            highestPoint.y = 1;
        }

        if (lowestPoint.y <= 0)
        {
            lowestPoint.y = 0;
        }
        //Debug.Log("lowest" + lowestPoint);
        float height = camera.ViewportToScreenPoint(highestPoint).y - camera.ViewportToScreenPoint(lowestPoint).y;
        //Debug.Log("heigth" + height);

        highestViewPoint = camera.ViewportToScreenPoint(highestPoint);
        return height;
    }