Exemple #1
0
        public CameraService(Contexts contexts) : base(contexts)
        {
            _context    = contexts as GameContext;
            _cameraData = Data.CameraData;

            CharacterCamera        = _cameraData._cameraSettings.CreateCharacterCamera();
            CameraCinemachineBrain = CharacterCamera.GetComponent <CinemachineBrain>() ?? null;

#if (UNITY_EDITOR)
            EditorApplication.playModeStateChanged += SaveCameraSettings;
#endif
        }
    public bool CenterOnVisibleBounds(float maxDistance, out Vector3 finalPosition, out float finalDistance)
    {
        visibleBounds = GetBoundsWithChildren(visibleObjects);
        float   maxExtent = visibleBounds.extents.magnitude;
        Camera  camera = _camera.GetComponent <Camera>();
        Vector3 dir = camera.transform.forward, c3d = visibleBounds.center;
        Plane   plane = new Plane(transform.up, transform.position);
        float   dist;
        Ray     r;

        if (dir == Vector3.down || dir == Vector3.up)
        {
            maxExtent = Mathf.Max(visibleBounds.extents.x, visibleBounds.extents.z);
            //Debug.Log(maxExtent);
        }
        else
        {
            //         //Debug.Log(dir);
            //if (useSpecialLogic) {
            //             Vector3 ext3d = visibleBounds.extents;
            //             Vector3[] corner = new Vector3[8];
            //             Vector3 Scale(Vector3 a, Vector3 b) { return new Vector3(a.x*b.x, a.y * b.y, a.z * b.z); }
            //             corner[0] = c3d + Scale(ext3d, new Vector3( 1, 1, 1));
            //             corner[1] = c3d + Scale(ext3d, new Vector3( 1, 1,-1));
            //             corner[2] = c3d + Scale(ext3d, new Vector3( 1,-1, 1));
            //             corner[3] = c3d + Scale(ext3d, new Vector3( 1,-1,-1));
            //             corner[4] = c3d + Scale(ext3d, new Vector3(-1, 1, 1));
            //             corner[5] = c3d + Scale(ext3d, new Vector3(-1, 1,-1));
            //             corner[6] = c3d + Scale(ext3d, new Vector3(-1,-1, 1));
            //             corner[7] = c3d + Scale(ext3d, new Vector3(-1,-1,-1));
            //             Vector2 min = camera.WorldToScreenPoint(corner[0]), max = min;
            //             for(int i = 1; i < corner.Length; ++i) {
            //                 Vector2 loc = camera.WorldToScreenPoint(corner[i]);
            //                 min.x = Mathf.Min(min.x, loc.x);
            //                 min.y = Mathf.Min(min.y, loc.y);
            //                 max.x = Mathf.Max(max.x, loc.x);
            //                 max.y = Mathf.Max(max.y, loc.y);
            //             }
            //             Vector2 ext2d = (max - min) / 2;
            //             Vector2 centerScreen = (min + max) / 2;
            //             Plane hypothetical = new Plane(-dir, c3d);
            //             Vector2 c2d = camera.WorldToScreenPoint(c3d);
            //             r = camera.ScreenPointToRay(c2d + new Vector2(ext2d.x, 0));
            //             hypothetical.Raycast(r, out dist);
            //             Vector3 xhatend = r.GetPoint(dist);
            //             float hExtent = Vector3.Distance(xhatend, c3d);
            //             r = camera.ScreenPointToRay(c2d + new Vector2(0, ext2d.y));
            //             hypothetical.Raycast(r, out dist);
            //             Vector3 yhatend = r.GetPoint(dist);
            //             float vExtent = Vector3.Distance(yhatend, c3d);
            //             maxExtent = Mathf.Max(vExtent, hExtent);
            //             r = camera.ScreenPointToRay(c2d);
            //             plane.Raycast(r, out dist);
            //             c3d = r.GetPoint(dist);
            //             Debug.Log(c3d + " vs " + visibleBounds.center);
            //         }
        }
        float   minDistance = (maxExtent) / Mathf.Sin(Mathf.Deg2Rad * camera.fieldOfView / 2f);
        Vector3 p           = c3d - dir * minDistance;

        r = new Ray(p, dir);
        plane.Raycast(r, out dist);
        if (dist <= maxDistance || maxDistance <= 0)
        {
            //transform.position = r.GetPoint(dist);
            //_camera.targetDistance = dist;
            finalPosition = r.GetPoint(dist);
            finalDistance = dist;
            return(true);
        }
        finalPosition = transform.position;
        finalDistance = _camera.targetDistance;
        return(false);
    }