Ejemplo n.º 1
0
        public FrustumSphereMeshBuilder(Vector3 center, float radius, Camera cam, int triangleRatio)
        {
            this.center = center;
            this.radius = radius;
            triangles   = new List <TriangleData>();
            camPos      = cam.transform;

            Vector3[] corners = new Vector3[5];
            cam.CalculateFrustumCorners(cam.rect,
                                        cam.farClipPlane,
                                        Camera.MonoOrStereoscopicEye.Mono,
                                        corners);

            Transform camTransform = cam.transform;
            Vector3   camPosition  = cam.transform.position;

            //centerInter = new RaySphereIntersection(camPosition, center-camPosition, center, radius);
            // compute world space coordinates for frustum
            for (var index = 0; index < 4; index++)
            {
                corners[index] = camTransform.TransformPoint(corners[index]);
            }
            corners[4] = corners[0];

            Vector3 p0 = camPosition;
            Vector3 p1, p2;

            plane = new Plane[5];

            float distanceSqr = (p0 - center).sqrMagnitude;

            if (distanceSqr > radius * radius)
            {
                plane[4] = Plane.SphereBackPlaneSeenFromPosition(p0, center, radius);
                psi      = new PlaneSphereIntersection(plane[4], center, radius);
                seed     = psi.ProjectOnCircle(camPosition);
                for (int i = 0; i < 4; ++i)
                {
                    p1 = corners[i];
                    p2 = corners[i + 1];

                    Plane p = new Plane(p0, p2, p1);
                    p.SetName(Vector3.Lerp(p1, p2, 0.5f), "SidePl" + i);
                    plane[i] = p;
                    MoveToPlaneIfOnNegativeSide(ref plane[i], ref seed);
                }
            }
            else
            {
                return;
            }

            Vector3    axis      = plane[4].normal;
            Quaternion q         = Quaternion.AngleAxis(120, axis);
            Vector3    direction = Vector3.Cross(axis, camTransform.up).normalized;

            direction *= radius / triangleRatio;

            p0 = direction;

            p1    = q * p0;
            p2    = q * p1;
            p0   += seed;
            p1   += seed;
            p2   += seed;
            first = new TriangleData(new List <VerticeData>(), p0, p1, p2, "0", psi);
            first[0].isVisible = first.IsVisible(first[0].pos, plane);
            first[1].isVisible = first.IsVisible(first[1].pos, plane);
            first[2].isVisible = first.IsVisible(first[2].pos, plane);
        }