예제 #1
0
        public bool CorrectPoints()
        {
            var ch = ConvexHull.CH2(new List <Point> {
                TopLeft, TopRight, BottomLeft, BottomRight
            }, true);
            var emptyPoint = new Point();

            if (ch.Count != 4 || ch.FindAll(i => i == emptyPoint).Count > 1)
            {
                SetMaximum();

                return(false);
            }

            var pointList = new List <Point> {
                TopLeft, TopRight, BottomLeft, BottomRight
            }.OrderBy(i => i.Y);
            var pointTopList = new List <Point> {
                pointList.ElementAt(0), pointList.ElementAt(1)
            }.OrderBy(i => i.X);

            var topLeftPoint = pointTopList.First();

            var current = ch.IndexOf(topLeftPoint);

            TopLeft     = ch.ElementAt(current);
            TopRight    = ch.ElementAt((current + 1) % 4);
            BottomLeft  = ch.ElementAt((current + 3) % 4);
            BottomRight = ch.ElementAt((current + 2) % 4);

            return(true);
        }
    private void generateFloor()
    {
        Vector3[] node_points = nodes.Keys.ToArray();
        if (point_pairs_array.Length > 2)
        {
            List <Point> p = new List <Point> ();
            for (int i = 0; i < node_points.Length; i++)
            {
                Point s = new Point();
                s.X = node_points [i].x;
                s.Y = node_points [i].z;
                p.Add(s);
            }

            Point[]   ch             = ConvexHull.CH2(p).ToArray();
            Vector2[] floor_vertices = new Vector2[ch.Length - 1];

            for (int i = 0; i < ch.Length - 1; i++)
            {
                floor_vertices [i] = new Vector2(ch [i].X, ch [i].Y);
                Debug.Log(floor_vertices [i]);
            }

            GameObject floor = new GameObject();
            floor.name             = "Floor";
            floor.transform.parent = _3DContainer.transform;
            floor.AddComponent <MeshFilter> ();
            floor.AddComponent <MeshRenderer> ();

            Mesh floor_m = floor.GetComponent <MeshFilter> ().mesh;

            Polygon floor_poly = createPoly(floor_vertices);
            P2T.Triangulate(floor_poly);

            for (int i = 0; i < floor_poly.Triangles.Count; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    TriangulationPoint tpt = floor_poly.Triangles [i].Points [j];
                    Vector3            pt  = new Vector3((float)tpt.X, 0, (float)tpt.Y);
                    new_tris.Add(vertexIndices [pt]);
                }
            }

            floor_m.vertices = new_verts.ToArray();
            int[] tris = new_tris.ToArray();
            for (int i = 0; i < tris.Length; i += 3)
            {
                int temp = tris [i + 1];
                tris [i + 1] = tris [i + 2];
                tris [i + 2] = temp;
            }
            floor_m.triangles = tris;
            floor_m.RecalculateNormals();

            //assign material

            /*Material newMat = new Material(Shader.Find("Standard"));
             * //Resources.Load("meshgen material", typeof(Material)) as Material;
             * MeshRenderer renderer = floor.GetComponent<MeshRenderer>();
             * renderer.sharedMaterial = newMat;*/
            Material newMat = new Material(Shader.Find("Standard"));            //GetDefaultMaterial();
            newMat.color = Color.gray;
            //newMat.mainTexture.wrapMode = TextureWrapMode.Repeat;
            floor.GetComponent <MeshRenderer>().material = newMat;

            //PUNEET -> Added Mesh Collider to floor
            floor.AddComponent <MeshCollider> ();
            floor.GetComponent <MeshCollider> ().sharedMesh = floor_m;
            floor.layer = 10;
        }
    }