Area() private method

private Area ( ) : float
return float
Esempio n. 1
0
 /**
  *	\brief Returns the area of the currently drawn polygon.
  */
 public static float GetArea(List <Vector2> points)
 {
     if (points != null && points.Count > 2)
     {
         return(Mathf.Abs(Triangulator.Area(points)));
     }
     else
     {
         return(0f);
     }
 }
Esempio n. 2
0
    Polygon GenerateMesh(Polygon poly) // Use the MeshGenerator Class in order to calculate the mesh given an array of vertices
    {
        // MeshGenerator meshGenerator = new MeshGenerator(form);
        // meshGenerator.GenerateMesh();
        // quadMesh.mesh = meshGenerator.GetMesh();

        Vector3[] vertex     = poly.GetVertices();
        Vector2[] vertices2D = new Vector2[vertex.Length];
        for (int i = 0; i < vertex.Length; i++)
        {
            vertices2D[i] = new Vector2(vertex[i].x, vertex[i].y);

            //Debug.Log(vertices2D[i]+" "+i);
        }
        Triangulator tr = new Triangulator(vertices2D);

        int[] indices = tr.Triangulate();

        // Create the Vector3 vertices
        Vector3[] vertices = new Vector3[vertices2D.Length];
        for (int i = 0; i < vertices.Length; i++)
        {
            vertices[i] = new Vector3(vertices2D[i].x, vertices2D[i].y, 0);
            //Debug.Log(vertices[i]+" "+i);
        }



        poly.SetIndices(indices);
        poly.SetVertices(vertices);


        // Create the mesh
        Mesh msh = new Mesh();

        msh.vertices  = vertices;
        msh.triangles = indices;
        msh.RecalculateNormals();
        msh.RecalculateBounds();
        poly.SetMesh(msh);
        poly.area = tr.Area();

        return(poly);
    }
Esempio n. 3
0
    public void BuildMesh()
    {
        List <Vector3> _vertices = new List <Vector3>();

        for (int i = 0; i < Corners.Count; i++)
        {
            int nextIndex, lastIndex;
            if (i > 0)
            {
                lastIndex = i - 1;
            }
            else
            {
                lastIndex = Corners.Count - 1;
            }
            if (i < Corners.Count - 1)
            {
                nextIndex = i + 1;
            }
            else
            {
                nextIndex = 0;
            }

            /*int segCount = Mathf.CeilToInt((
             *  Vector2.Distance((Corners[i].position + Corners[i].controlPoint1)/2, (Corners[i].controlPoint1 + Corners[nextIndex].controlPoint0) / 2) +
             *  Vector2.Distance((Corners[nextIndex].position + Corners[nextIndex].controlPoint0) / 2, (Corners[i].controlPoint1 + Corners[nextIndex].controlPoint0) / 2)
             *  ) * 50 * smootFactor);*/

            int segCount = Mathf.CeilToInt(Mathf.Sqrt(
                                               Vector2.Distance(Corners[i].position, Corners[i].controlPoint1) +
                                               Vector2.Distance(Corners[nextIndex].controlPoint0, Corners[i].controlPoint1) +
                                               Vector2.Distance(Corners[nextIndex].controlPoint0, Corners[nextIndex].position)
                                               ) * smootFactor);

            if (Corners[i].isBezier && Corners[nextIndex].isBezier)
            {
                for (int j = 0; j < segCount; j++)
                {
                    float t = (float)j / segCount;
                    _vertices.Add(GetPointBezier(Corners[i].position, Corners[nextIndex].position, Corners[i].controlPoint1, Corners[nextIndex].controlPoint0, t));
                }
            }
            else
            {
                _vertices.Add(Corners[i].position);
            }
        }

        Vector2[] vertices2D = new Vector2[_vertices.Count];
        Vector3[] vertices   = new Vector3[_vertices.Count];

        for (int i = 0; i < _vertices.Count; i++)
        {
            vertices2D[i] = _vertices[i];
            vertices[i]   = _vertices[i];
        }


        Triangulator tr = new Triangulator(vertices2D);

        int[] indices = tr.Triangulate();


        Mesh msh = new Mesh();

        msh.vertices  = vertices;
        msh.triangles = indices;
        msh.uv        = vertices2D;
        msh.RecalculateNormals();
        msh.RecalculateBounds();

        GetComponent <MeshFilter>().mesh = msh;

        area = Mathf.Abs(tr.Area());

        if (GetComponent <PolygonCollider2D>())
        {
            GetComponent <PolygonCollider2D>().points = vertices2D;
        }
    }
Esempio n. 4
0
        /**
         *	\brief Creates a mesh and applies all collisions and physics components.
         *	This method accepts user points and creates a new gameObject with a mesh composed from point data.  As opposed to DrawPreviewMesh(), this function will check against all user specified rules.  If conditions are met, CheckMaxMeshes() is called, and the new gameObject is added to the internal cache of all PolyDraw created objects.
         *	@param _points List of user points.  Assumed to be in world space (use ScreenToWorldPoint(screenPoint) where screenPoint is typically Input.mousePosition ).
         */
        public void DrawFinalMesh(List <Vector2> _points)
        {
            DestroyPreviewMesh();

            if (_points.Count < 3)
            {
                CleanUp();
                        #if DEBUG
                Debug.LogWarning("Polydraw: Mesh generation failed on points < 3");
                        #endif
                OnCanceledObjectCreation();
                return;
            }

            if ((_points[0] - _points[_points.Count - 1]).sqrMagnitude > maxDistance && useDistanceCheck)
            {
                        #if DEBUG
                Debug.LogWarning("Mesh generation failed on distance check");
                        #endif
                CleanUp();
                OnCanceledObjectCreation();
                return;
            }

            // Calculate this here because the collision code needs it too
            float obj_area = Mathf.Abs(Triangulator.Area(_points));

            if (drawSettings.requireMinimumArea && obj_area < drawSettings.minimumAreaToDraw)
            {
                        #if DEBUG
                Debug.Log("Polydraw: Mesh generation failed on requireMinimumArea check.");
                        #endif
                CleanUp();
                return;
            }

            // graphics = any mesh that you can see, collision = the side mesh
            Mesh             graphics, collision;
            Draw.PolygonType convexity;

            // Since Draw doesn't expose special collision settings, just the side settings.
            drawSettings.colAnchor = drawSettings.anchor;
            drawSettings.colDepth  = drawSettings.sideLength > .01 ? drawSettings.sideLength : .01f;

            if (!DrawUtility.MeshWithPoints(_points, drawSettings, out graphics, out collision, out convexity))
            {
                if (graphics != null)
                {
                    DestroyImmediate(graphics);
                }
                if (collision != null)
                {
                    DestroyImmediate(collision);
                }

                CleanUp();

                return;
            }

            // If we're over max, delete the earliest drawn mesh
            CheckMaxMeshes();

            GameObject finalMeshGameObject = new GameObject();
            finalMeshGameObject.name = drawSettings.meshName;

            if (drawSettings.useTag)
            {
                finalMeshGameObject.tag = drawSettings.tagVal;
            }

            finalMeshGameObject.AddComponent <MeshFilter>();
            finalMeshGameObject.GetComponent <MeshFilter>().sharedMesh = graphics;
            finalMeshGameObject.AddComponent <MeshRenderer>();


            Material[] mats = (drawSettings.generateSide) ?
                              new Material[2] {
                drawSettings.frontMaterial, drawSettings.sideMaterial
            } :
            new Material[1] {
                drawSettings.frontMaterial
            };

            finalMeshGameObject.GetComponent <MeshRenderer>().sharedMaterials = mats;

            switch (drawSettings.colliderType)
            {
            case ColliderType.MeshCollider:
                finalMeshGameObject.AddComponent <MeshCollider>();

                finalMeshGameObject.GetComponent <MeshCollider>().sharedMesh = collision;

                if (drawSettings.applyRigidbody)
                {
                    Rigidbody rigidbody = finalMeshGameObject.AddComponent <Rigidbody>();

                    if ((convexity == PolygonType.ConcaveCounterClockwise || convexity == PolygonType.ConcaveClockwise) && drawSettings.forceConvex == false)
                    {
                        finalMeshGameObject.GetComponent <MeshCollider>().convex = false;
                    }
                    else
                    {
                        finalMeshGameObject.GetComponent <MeshCollider>().convex = true;
                    }

                    if (drawSettings.areaRelativeMass)
                    {
                        rigidbody.mass = obj_area * drawSettings.massModifier;
                    }
                    else
                    {
                        rigidbody.mass = drawSettings.mass;
                    }

                    rigidbody.constraints = RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationY | RigidbodyConstraints.FreezePositionZ;

                    if (drawSettings.useGravity)
                    {
                        rigidbody.useGravity = true;
                    }
                    else
                    {
                        rigidbody.useGravity = false;
                    }

                    if (drawSettings.isKinematic)
                    {
                        rigidbody.isKinematic = true;
                    }
                    else
                    {
                        rigidbody.isKinematic = false;
                    }
                }
                break;

            case ColliderType.BoxCollider:
                if (drawSettings.applyRigidbody)
                {
                    BoxCollider parent_collider = finalMeshGameObject.AddComponent <BoxCollider>();

                    // the parent collider - don't allow it to be seen, just use it for
                    // mass and other settings
                    parent_collider.size = new Vector3(.01f, .01f, .01f);

                    Rigidbody rigidbody = finalMeshGameObject.AddComponent <Rigidbody>();

                    if (drawSettings.areaRelativeMass)
                    {
                        rigidbody.mass = obj_area * drawSettings.massModifier;
                    }
                    else
                    {
                        rigidbody.mass = drawSettings.mass;
                    }

                    rigidbody.constraints = RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationY | RigidbodyConstraints.FreezePositionZ;

                    if (drawSettings.useGravity)
                    {
                        rigidbody.useGravity = true;
                    }
                    else
                    {
                        rigidbody.useGravity = false;
                    }

                    if (drawSettings.isKinematic)
                    {
                        rigidbody.isKinematic = true;
                    }
                    else
                    {
                        rigidbody.isKinematic = false;
                    }
                }

                if (!drawSettings.manualColliderDepth)
                {
                    drawSettings.colDepth = drawSettings.sideLength;
                }

                float zPos_collider = drawSettings.zPosition + drawSettings.faceOffset;

                switch (drawSettings.anchor)
                {
                case Anchor.Front:
                    zPos_collider += drawSettings.sideLength / 2f;
                    break;

                case Anchor.Back:
                    zPos_collider -= drawSettings.sideLength / 2f;
                    break;

                default:
                    break;
                }

                for (int i = 0; i < _points.Count; i++)
                {
                    float x1, x2, y1, y2;
                    x1 = _points[i].x;
                    y1 = _points[i].y;

                    if (i > _points.Count - 2)
                    {
                        x2 = _points[0].x;
                        y2 = _points[0].y;
                    }
                    else
                    {
                        x2 = _points[i + 1].x;
                        y2 = _points[i + 1].y;
                    }

                    GameObject boxColliderObj = new GameObject();
                    boxColliderObj.name = "BoxCollider" + i;
                    boxColliderObj.AddComponent <BoxCollider>();

                    boxColliderObj.transform.position = new Vector3(((x1 + x2) / 2f), ((y1 + y2) / 2f), zPos_collider);

                    Vector2 vectorLength = new Vector2(Mathf.Abs(x1 - x2), Mathf.Abs(y1 - y2));

                    float length = Mathf.Sqrt((Mathf.Pow((float)vectorLength.x, 2f) + Mathf.Pow(vectorLength.y, 2f)));
                    float angle  = Mathf.Atan2(y2 - y1, x2 - x1) * Mathf.Rad2Deg;

                    boxColliderObj.transform.localScale = new Vector3(length, boxColliderSize, drawSettings.colDepth);
                    boxColliderObj.transform.rotation   = Quaternion.Euler(new Vector3(0f, 0f, angle));

                    boxColliderObj.transform.parent = finalMeshGameObject.transform;
                }
                break;

            case ColliderType.PolygonCollider2d:
                PolygonCollider2D poly = finalMeshGameObject.AddComponent <PolygonCollider2D>();
                finalMeshGameObject.AddComponent <Rigidbody2D>();
                poly.points = _points.ToArray();
                break;

            default:
                break;
            }

            generatedMeshes.Add(finalMeshGameObject);

            if (drawSettings.drawEdgePlanes)
            {
                DrawEdgePlanes(_points, convexity, new Vector2(.4f, 1.2f), obj_area);
            }

            CleanUp();

            OnCreatedNewObject();
        }