예제 #1
0
    IEnumerator CreateTile(Mesh mesh, Vector3 center, Color color)
    {
        yield return(new WaitForSecondsRealtime(0.01f));

        GameObject   go2 = new GameObject();
        MeshFilter   mf2 = go2.AddComponent <MeshFilter>();
        MeshRenderer mr2 = go2.AddComponent <MeshRenderer>();

        mr2.sharedMaterial       = new Material(this.GetComponent <MeshRenderer>().sharedMaterial);// new Material(Shader.Find("Diffuse"));
        mr2.sharedMaterial.color = color;
        // mr2.material = material;

        go2.transform.parent = this.transform;
        //go2.transform.localPosition = Vector3.zero;
        go2.transform.localRotation = Quaternion.identity;
        go2.transform.localScale    = Vector3.one;
        go2.transform.localPosition = center;

        mesh.RecalculateNormals();
        mesh.RecalculateBounds();

        mf2.sharedMesh = mesh;

        SphereMapCell cell = go2.AddComponent <SphereMapCell>();

        cell.PositionSpherical = FromCartesian(center);
    }
예제 #2
0
    IEnumerator CreateTile(int polygonId, Mesh mesh, Vector3 center, Vector3 orientation, Color color, float height, SphereMap map)
    {
        yield return(new WaitForSecondsRealtime(0.0f));

        GameObject   meshGo = new GameObject();
        MeshFilter   mf2    = meshGo.AddComponent <MeshFilter>();
        MeshRenderer mr2    = meshGo.AddComponent <MeshRenderer>();

        mr2.sharedMaterial       = new Material(this.GetComponent <MeshRenderer>().sharedMaterial);// new Material(Shader.Find("Diffuse"));
        mr2.sharedMaterial.color = color;
        // mr2.material = material;

        GameObject cellGo = new GameObject();

        cellGo.transform.parent     = this.transform;
        cellGo.transform.localScale = Vector3.one;
        //cellGo.transform.localRotation = Quaternion.identity;
        cellGo.transform.LookAt(center + orientation);
        cellGo.transform.Rotate(90, 0, 0);
        meshGo.transform.parent = cellGo.transform;
        //go2.transform.localPosition = Vector3.zero;
        //meshGo.transform.localRotation = Quaternion.identity;
        meshGo.transform.localScale    = Vector3.one;
        meshGo.transform.localPosition = Vector3.zero;

        meshGo.hideFlags = HideFlags.HideInHierarchy;

        /*
         * MeshCollider collider = meshGo.AddComponent<MeshCollider>();
         * collider.sharedMesh = mesh;
         * collider.convex = true;
         * collider.inflateMesh = true;
         */
        cellGo.transform.localPosition = center;


        mesh.RecalculateNormals();
        mesh.RecalculateBounds();
        mf2.sharedMesh = mesh;

        bool walkable = height > 0.1f;

        SphereMapCell cell = cellGo.AddComponent <SphereMapCell>();

        cell.PositionSpherical = FromCartesian(center);
        cellGo.name            = "[" + (int)(cell.PositionSpherical.x * Mathf.Rad2Deg + 360) % 360 + "," + (int)(cell.PositionSpherical.y * Mathf.Rad2Deg + 360) % 360 + "]";
        cell.index             = polygonId;
        cell.sphere            = map;
        map.cells[polygonId]   = cell;

        cell.walkable = walkable;

        NavMeshSurface navSurface = cellGo.AddComponent <NavMeshSurface>();

        navSurface.collectObjects = CollectObjects.Children;
        navSurface.defaultArea    = walkable ? 0 : 1;
        navSurface.AddData();
        navSurface.BuildNavMesh();
    }
예제 #3
0
 // Use this for initialization
 void Start()
 {
     cells = new SphereMapCell[this.transform.childCount];
     for (int c = 0; c < this.transform.childCount; ++c)
     {
         SphereMapCell cell = this.transform.GetChild(c).gameObject.GetComponent <SphereMapCell>();
         cells[cell.index] = cell;
     }
 }
예제 #4
0
    void Flip(Mesh mesh)
    {
        Vector3[] vertices  = mesh.vertices;
        Vector3[] normals   = mesh.normals;
        int[]     triangles = mesh.triangles;

        List <Vector3> centers    = new List <Vector3>();
        List <Vector3> newNormals = new List <Vector3>();

        List <int>[] polygons = new List <int> [vertices.Length];

        int inv = 0;

        for (int t = 0; t < triangles.Length; t += 3)
        {
            Vector3 center = (vertices[triangles[t]] + vertices[triangles[t + 1]] + vertices[triangles[t + 2]]) / 3;
            centers.Add(center);
            Vector3 normal = (normals[triangles[t]] + normals[triangles[t + 1]] + normals[triangles[t + 2]]).normalized;
            newNormals.Add(normal);

            for (int d = 0; d < 3; ++d)
            {
                if (polygons[triangles[t + d]] == null)
                {
                    polygons[triangles[t + d]] = new List <int>();
                }
                polygons[triangles[t + d]].Add(inv);
            }
            ++inv;
        }

        for (int pol = 0; pol < polygons.Length; ++pol)
        {
            // SORT THE POLYGON CONTOUR
            List <int> shape        = new List <int>();
            List <int> unusedPoints = new List <int>(polygons[pol]);
            shape.Add(unusedPoints[0]);
            unusedPoints.RemoveAt(0);
            {
                int p = 0;
                while (unusedPoints.Count > 0)
                {
                    int   min = 0;
                    float d   = (centers[shape[p]] - centers[unusedPoints[0]]).sqrMagnitude;

                    for (int p2 = 1; p2 < unusedPoints.Count; ++p2)
                    {
                        float d2 = (centers[shape[p]] - centers[unusedPoints[p2]]).sqrMagnitude;
                        if (d2 < d)
                        {
                            d   = d2;
                            min = p2;
                        }
                    }
                    shape.Add(unusedPoints[min]);
                    unusedPoints.RemoveAt(min);
                    ++p;
                }
                polygons[pol] = shape;
            }
            //Make it clockwise
            {
                Vector3 center = Vector3.zero;
                foreach (int p in polygons[pol])
                {
                    center += centers[p];
                }
                center /= polygons[pol].Count;

                Vector3 a     = center;
                Vector3 b     = centers[polygons[pol][0]];
                Vector3 c     = centers[polygons[pol][1]];
                Vector3 side1 = b - a;
                Vector3 side2 = c - a;
                Vector3 perp  = Vector3.Cross(side1, side2);
                if ((a + perp).sqrMagnitude <= (a).sqrMagnitude)
                {
                    polygons[pol].Reverse();
                }
            }
        }
        // Compute neighbours
        List <int>[] sharedVertices = new List <int> [centers.Count];
        for (int pol = 0; pol < polygons.Length; ++pol)
        {
            foreach (int v in polygons[pol])
            {
                if (v >= sharedVertices.Length)
                {
                    Debug.LogWarning("problem in size: " + v + " >= " + sharedVertices.Length);
                }
                if (sharedVertices[v] == null)
                {
                    sharedVertices[v] = new List <int>();
                }
                sharedVertices[v].Add(pol);
            }
        }
        List <int>[]     neighbours       = new List <int> [polygons.Length];
        List <Vector3>[] neighboursJoints = new List <Vector3> [polygons.Length];
        for (int v = 0; v < sharedVertices.Length; ++v)
        {
            for (int pol = 0; pol < sharedVertices[v].Count; ++pol)
            {
                if (neighbours[sharedVertices[v][pol]] == null)
                {
                    neighbours[sharedVertices[v][pol]]       = new List <int>();
                    neighboursJoints[sharedVertices[v][pol]] = new List <Vector3>();
                }
                for (int pol2 = 0; pol2 < sharedVertices[v].Count; ++pol2)
                {
                    if (pol == pol2)
                    {
                        continue;
                    }
                    neighbours[sharedVertices[v][pol]].Add(sharedVertices[v][pol2]);
                    neighboursJoints[sharedVertices[v][pol]].Add(centers[v]);
                }
            }
        }

        #region basemesh

        /*
         * //THIS IS THE SPHERE WITHOUT DISPLACEMENT
         * {
         *  List<int> newTriangles = new List<int>();
         *  foreach (List<int> pol in polygons)
         *  {
         *      Vector3 center = Vector3.zero;
         *      Vector3 normal = Vector3.zero;
         *      foreach (int p in pol)
         *      {
         *          center += centers[p];
         *          normal += newNormals[p];
         *      }
         *      center /= pol.Count;
         *      centers.Add(center);
         *
         *      normal.Normalize();
         *      newNormals.Add(normal);
         *
         *      int centerIdx = centers.Count - 1;
         *      {
         *
         *          for (int idxp = 0; idxp < pol.Count - 1; ++idxp)
         *          {
         *              newTriangles.Add(centerIdx);
         *              newTriangles.Add(pol[idxp]);
         *              newTriangles.Add(pol[idxp + 1]);
         *          }
         *          newTriangles.Add(centerIdx);
         *          newTriangles.Add(pol[pol.Count - 1]);
         *          newTriangles.Add(pol[0]);
         *
         *
         *      }
         *  }
         *  mesh.vertices = centers.ToArray();
         *  mesh.normals = newNormals.ToArray();
         *  mesh.triangles = newTriangles.ToArray();
         *  mesh.RecalculateNormals();
         * }
         */
        #endregion

        #region Tiles
        float[]         offset = new float[polygons.Length];
        SphereMapCell[] cells  = new SphereMapCell[polygons.Length];
        SphereMap       map    = GetComponent <SphereMap>();
        map.cells = cells;
        {
            Clear();
            for (int pol = 0; pol < polygons.Length; ++pol)
            {
                List <int> polygon   = polygons[pol];
                Mesh       mesh2     = new Mesh();
                Vector3[]  vertices2 = new Vector3[polygon.Count + 1];
                Vector3[]  normals2  = new Vector3[polygon.Count + 1];

                Vector3 center = Vector3.zero;
                Vector3 normal = Vector3.zero;

                int idx = 0;
                foreach (int p in polygon)
                {
                    center += centers[p];
                    normal += newNormals[p];

                    vertices2[idx] = centers[p];
                    normals2[idx]  = newNormals[p];

                    ++idx;
                }
                center /= polygon.Count;
                int centerIdx = vertices2.Length - 1;
                vertices2[centerIdx] = center;

                normal.Normalize();
                normals2[centerIdx] = normal;



                List <int> triangles2 = new List <int>();

                {
                    for (int idxp = 0; idxp < polygon.Count - 1; ++idxp)
                    {
                        triangles2.Add(centerIdx);
                        triangles2.Add(idxp);
                        triangles2.Add(idxp + 1);
                    }
                    triangles2.Add(centerIdx);
                    triangles2.Add(polygon.Count - 1);
                    triangles2.Add(0);
                }



                for (int v = 0; v < vertices2.Length; ++v)
                {
                    vertices2[v] -= center;
                }
                offset[pol] = Mathf.Max(0, (Perlin.Fbm(center * noiseScale + offsetPerlin, bumpiness))) * steepness;

                /*for (int v = 0; v < vertices2.Length; ++v)
                 * {
                 *
                 *  vertices2[v] += normal* 0.0001f;
                 *
                 * }*/
                center += normal * (offset[pol] + 0.01f);
                Color color = colors.Evaluate(offset[pol] / steepness);

                mesh2.vertices  = vertices2;
                mesh2.normals   = normals2;
                mesh2.triangles = triangles2.ToArray();

                StartCoroutine(CreateTile(pol, mesh2, center, normal, color, offset[pol], map));
                //CreateTile(pol, mesh2, center, normal, color);
            }
            StartCoroutine(CreateJumpLinks(map, neighbours, neighboursJoints));
        }
        #endregion
        #region TilesWithBase
        if (CreateTiles)
        {
            Material material = this.GetComponent <MeshRenderer>().material;// new Material(Shader.Find("Diffuse"));
            for (int c = transform.childCount - 1; c >= 0; --c)
            {
                Destroy(transform.GetChild(c).gameObject);
            }
            for (int pol = 0; pol < polygons.Length; ++pol)
            {
                List <int> polygon = polygons[pol];

                GameObject   go2 = new GameObject();
                MeshFilter   mf2 = go2.AddComponent <MeshFilter>();
                MeshRenderer mr2 = go2.AddComponent <MeshRenderer>();
                mr2.material = material;

                Mesh      mesh2     = new Mesh();
                Vector3[] vertices2 = new Vector3[2 * polygon.Count + 1];
                Vector3[] normals2  = new Vector3[2 * polygon.Count + 1];

                Vector3 center = Vector3.zero;
                Vector3 normal = Vector3.zero;

                go2.transform.parent        = this.transform;
                go2.transform.localPosition = Vector3.zero;
                go2.transform.localScale    = Vector3.one;


                int idx = 0;
                foreach (int p in polygon)
                {
                    center += centers[p];
                    normal += newNormals[p];

                    vertices2[idx] = centers[p];
                    normals2[idx]  = newNormals[p];

                    vertices2[idx + polygon.Count] = centers[p];
                    normals2[idx + polygon.Count]  = newNormals[p];

                    ++idx;
                }
                center /= polygon.Count;
                int centerIdx = vertices2.Length - 1;
                vertices2[centerIdx] = center;

                normal.Normalize();
                normals2[centerIdx] = normal;



                List <int> triangles2 = new List <int>();

                {
                    for (int idxp = 0; idxp < polygon.Count - 1; ++idxp)
                    {
                        triangles2.Add(centerIdx);
                        triangles2.Add(idxp);
                        triangles2.Add(idxp + 1);
                    }
                    triangles2.Add(centerIdx);
                    triangles2.Add(polygon.Count - 1);
                    triangles2.Add(0);

                    {
                        for (int idxp = 0; idxp < polygon.Count - 1; idxp++)
                        {
                            triangles2.Add(idxp);
                            triangles2.Add(idxp + polygon.Count);
                            triangles2.Add(idxp + 1);



                            triangles2.Add(idxp + polygon.Count);
                            triangles2.Add(idxp + polygon.Count + 1);
                            triangles2.Add(idxp + 1);
                        }
                        triangles2.Add(polygon.Count - 1);
                        triangles2.Add(polygon.Count + polygon.Count - 1);
                        triangles2.Add(0);

                        triangles2.Add(polygon.Count + polygon.Count - 1);
                        triangles2.Add(polygon.Count);
                        triangles2.Add(0);
                    }
                }
                go2.transform.localPosition = center;
                // go2.transform.LookAt(normal);


                for (int v = 0; v < vertices2.Length; ++v)
                {
                    vertices2[v] -= center;
                }
                float height = offset[pol];
                for (int v = 0; v < polygon.Count; ++v)
                {
                    vertices2[v] += normal * height;
                }
                vertices2[centerIdx] += normal * height;
                mr2.material.color    = Color.Lerp(Random.ColorHSV(0.3f, 0.35f, 0.5f, 1, 0.8f, 0.9f), Random.ColorHSV(0.1f, 0.15f, 0.5f, 1, 0.8f, 0.9f), height * 5);
                mesh2.vertices        = vertices2;
                mesh2.normals         = normals2;
                mesh2.triangles       = triangles2.ToArray();
                mf2.sharedMesh        = mesh2;
                mf2.mesh.RecalculateNormals();
                mf2.mesh.RecalculateBounds();
            }
        }
        #endregion
        #region DisplacedMesh
        else
        {
            List <Vector3> vertices3  = new List <Vector3>();
            List <Vector3> normals3   = new List <Vector3>();
            List <int>     triangles3 = new List <int>();
            for (int pol = 0; pol < polygons.Length; ++pol)
            {
                List <int> polygon   = polygons[pol];
                Vector3[]  vertices2 = new Vector3[2 * polygon.Count + 1];
                Vector3[]  normals2  = new Vector3[2 * polygon.Count + 1];

                Vector3 center = Vector3.zero;
                Vector3 normal = Vector3.zero;


                int idx = 0;
                foreach (int p in polygon)
                {
                    center += centers[p];
                    normal += newNormals[p];

                    vertices2[idx] = centers[p];
                    normals2[idx]  = newNormals[p];

                    vertices2[idx + polygon.Count] = centers[p];
                    normals2[idx + polygon.Count]  = newNormals[p];

                    ++idx;
                }
                center /= polygon.Count;
                int centerIdx = vertices2.Length - 1;
                vertices2[centerIdx] = center;

                normal.Normalize();
                normals2[centerIdx] = normal;



                List <int> triangles2 = new List <int>();

                {
                    for (int idxp = 0; idxp < polygon.Count - 1; ++idxp)
                    {
                        triangles2.Add(centerIdx);
                        triangles2.Add(idxp);
                        triangles2.Add(idxp + 1);
                    }
                    triangles2.Add(centerIdx);
                    triangles2.Add(polygon.Count - 1);
                    triangles2.Add(0);

                    {
                        for (int idxp = 0; idxp < polygon.Count - 1; idxp++)
                        {
                            triangles2.Add(idxp);
                            triangles2.Add(idxp + polygon.Count);
                            triangles2.Add(idxp + 1);



                            triangles2.Add(idxp + polygon.Count);
                            triangles2.Add(idxp + polygon.Count + 1);
                            triangles2.Add(idxp + 1);
                        }
                        triangles2.Add(polygon.Count - 1);
                        triangles2.Add(polygon.Count + polygon.Count - 1);
                        triangles2.Add(0);

                        triangles2.Add(polygon.Count + polygon.Count - 1);
                        triangles2.Add(polygon.Count);
                        triangles2.Add(0);
                    }
                }
                float height = offset[pol];
                for (int v = 0; v < polygon.Count; ++v)
                {
                    vertices2[v] += normal * height;
                }
                vertices2[centerIdx] += normal * height;

                int prevCount = vertices3.Count;
                foreach (Vector3 v in vertices2)
                {
                    vertices3.Add(v);
                }
                foreach (Vector3 n in normals2)
                {
                    normals3.Add(n);
                }
                foreach (int t in triangles2)
                {
                    triangles3.Add(t + prevCount);
                }
            }

            mesh.vertices  = vertices3.ToArray();
            mesh.normals   = normals3.ToArray();
            mesh.triangles = triangles3.ToArray();
            mesh.RecalculateNormals();
        }
        #endregion
    }