{"id":1446,"date":"2020-03-23T23:11:51","date_gmt":"2020-03-23T15:11:51","guid":{"rendered":"http:\/\/blog.coolcoding.cn\/?p=1446"},"modified":"2020-09-27T11:23:09","modified_gmt":"2020-09-27T03:23:09","slug":"camrabox","status":"publish","type":"post","link":"https:\/\/blog.coolcoding.cn\/?p=1446","title":{"rendered":"[Unity]\u6444\u50cf\u673a\u76d2\u5b50\uff1aCamraBox"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>using System.Collections;\nusing System.Collections.Generic;\nusing UnityEngine;\n\npublic class CameraCalc : MonoBehaviour\n{\n    static private Plane XZPlane = new Plane( Vector3.up, Vector3.zero );\n\n    public Transform TargetObject;\n\n    void OnDrawGizmos()\n    {\n\n        var camera = GetComponent&lt;Camera>();\n\n        var viewRect = new Rect(0.0f, 0.0f, 1.0f, 1.0f);\n\n        Vector3[] frustumCorners = new Vector3[4];\n        camera.CalculateFrustumCorners(viewRect, camera.farClipPlane, Camera.MonoOrStereoscopicEye.Mono, frustumCorners);\n        for (int i = 0; i &lt; 4; i++)\n        {\n            var p = camera.transform.TransformVector(frustumCorners[i]);            \n            Ray ray = new Ray( camera.transform.position, (p- camera.transform.position).normalized );\n            float f;\n            if (XZPlane.Raycast(ray, out f))\n            {\n                frustumCorners[i] = ray.GetPoint(f);\n            }\n        }\n\n        if (TargetObject == null)\n        {\n            return;\n        }\n\n        Vector2[] p2d = new Vector2[4];\n        for(int i=0; i&lt;4; ++i)\n        {\n            p2d[i] = new Vector2( frustumCorners[i].x, frustumCorners[i].z);\n        }\n\n        var checkPoint3 = TargetObject.transform.position;\n        Vector2 checkPoint = new Vector2(checkPoint3.x, checkPoint3.z);\n\n        Vector2[] c2cd= new Vector2[4];\n\n        bool cross0 = getCross(p2d[0], p2d[1], checkPoint, checkPoint + new Vector2(1024, checkPoint.y),  out c2cd[0]);\n        if (cross0)\n        {\n            Gizmos.DrawSphere( new Vector3(c2cd[0].x, 0, c2cd[0].y), 1);\n        }\n\n        bool cross1 = getCross(p2d[2], p2d[3], checkPoint, checkPoint - new Vector2(1024, checkPoint.y),  out c2cd[2]);\n        if (cross1)\n        {\n            Gizmos.DrawSphere( new Vector3(c2cd[2].x, 0, c2cd[2].y), 1);\n        }\n\n        bool cross2 = getCross(p2d[1], p2d[2], checkPoint, checkPoint - new Vector2(checkPoint.x, 1024),  out c2cd[1]);\n        if (cross2)\n        {\n            Gizmos.DrawSphere( new Vector3(c2cd[1].x, 0, c2cd[1].y), 1);\n        }\n\n        bool cross3 = getCross(p2d[3], p2d[0], checkPoint, checkPoint + new Vector2(checkPoint.x, 1024),  out c2cd[3]);\n        if (cross3)\n        {\n            Gizmos.DrawSphere( new Vector3(c2cd[3].x, 0, c2cd[3].y), 1);\n        }\n\n        Gizmos.color = Color.yellow;\n        Gizmos.DrawLine(frustumCorners[0], frustumCorners[1]);\n        Gizmos.DrawLine(frustumCorners[1], frustumCorners[2]);\n        Gizmos.DrawLine(frustumCorners[2], frustumCorners[3]);\n        Gizmos.DrawLine(frustumCorners[3], frustumCorners[0]);\n    }\n\n    public static bool linecut(Vector2 a, Vector2 b, Vector2 c, Vector2 d, out float output) \n    {\n        Vector2 cd = d - c;\n        Vector2 n = new Vector2(cd.y, -cd.x);\n        float div = Vector2.Dot(b - a, n);\n        if (Mathf.Approximately(div, 0.0f))\n        {\n            output = 0;\n            return false;\n        }\n        output = Vector2.Dot(c - a, n) \/ div;\n        return true;\n    }\n\n    public static bool getCross(Vector2 a, Vector2 b, Vector2 c, Vector2 d, out Vector2 cross) \n    {\n        float t1, t2;\n        if (linecut(a, b, c, d, out t1) &amp;&amp; linecut(c, d, a, b, out t2))\n        {\n            cross = (b - a) * t1 + a;\n            return t1 >= 0f &amp;&amp; t1 &lt;= 1f &amp;&amp; t2 >= 0f &amp;&amp; t2 &lt;= 1f;\n        }\n        cross = Vector2.zero;\n        return false;\n    }\n\n\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[],"_links":{"self":[{"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/posts\/1446"}],"collection":[{"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1446"}],"version-history":[{"count":3,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/posts\/1446\/revisions"}],"predecessor-version":[{"id":1843,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/posts\/1446\/revisions\/1843"}],"wp:attachment":[{"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1446"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1446"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1446"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}