public void Respawn()
    {
        StopCoroutine(AnnualReproduction());

        if (ThreadedScaleUpdater.Instance)
        {
            ThreadedScaleUpdater.Instance.RemoveAll();
        }

        if (forestContainer)
        {
            MiscUtils.DeleteChildren(forestContainer);
        }

        for (int index = 0; index < foliageTypes.Length; index++)
        {
            foliageTypes[index].cummulativeTotalOfThisType = 0;
        }

        int seed = CustomMathf.GetRandomSeed();

        if (LevelController.Instance)
        {
            seed = LevelController.Instance.seed;
        }

        InitalSpawn(seed);
    }
示例#2
0
    public void GenerateMesh()
    {
        MiscUtils.DeleteChildren(transform);

        terrainObject = new GameObject("Terrain");
        terrainObject.transform.parent = transform;
        terrainObject.tag   = "Terrain";
        terrainObject.layer = terrainLayer;

        mapSize    = GetComponent <ForestController>().mapSize;
        mapSize.x *= 1f + borderPadding;
        mapSize.z *= 1f + borderPadding;

        proceduralTerrainSettings.GetNoise(mapSize);
        maxHeight = 0f;


        Vector3 vertexOffset = new Vector3(-mapSize.x / 2f, 0f, -mapSize.z / 2f);

        gridSize = new Vector2Int((int)(mapSize.x * verticesPerMeter) - 1,
                                  (int)(mapSize.z * verticesPerMeter) - 1);

        //ensure the mesh isn't too high poly
        int totalVertCount = gridSize.x * gridSize.y;

        if (totalVertCount > 65535)
        {
            float resizePercent = 65530f / (float)totalVertCount;
            gridSize.x = Mathf.FloorToInt(resizePercent * gridSize.x);
            gridSize.y = Mathf.FloorToInt(resizePercent * gridSize.y);
        }

        Mesh mesh = new Mesh();

        mesh.name = "Terrain";

        Vector3[] vertices = new Vector3[(gridSize.x + 1) * (gridSize.y + 1)];
        Vector2[] uv       = new Vector2[vertices.Length];
        Vector4[] tangents = new Vector4[vertices.Length];
        Vector4   tangent  = new Vector4(1f, 0f, 0f, -1f);

        for (int i = 0, y = 0; y <= gridSize.y; y++)
        {
            for (int x = 0; x <= gridSize.x; x++, i++)
            {
                uv[i]       = new Vector2((float)x / (float)gridSize.x, (float)y / (float)gridSize.y);
                vertices[i] = SetVertex(x, y, vertexOffset, uv[i]);

                tangents[i] = tangent;
            }
        }

        mesh.vertices = vertices;
        mesh.uv       = uv;
        mesh.tangents = tangents;

        int[] triangles = new int[gridSize.x * gridSize.y * 6];
        for (int ti = 0, vi = 0, y = 0; y < gridSize.y; y++, vi++)
        {
            for (int x = 0; x < gridSize.x; x++, ti += 6, vi++)
            {
                triangles[ti]     = vi;
                triangles[ti + 3] = triangles[ti + 2] = vi + 1;
                triangles[ti + 4] = triangles[ti + 1] = vi + gridSize.x + 1;
                triangles[ti + 5] = vi + gridSize.x + 2;
            }
        }
        mesh.triangles = triangles;

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

        terrainObject.AddComponent <MeshFilter>();
        terrainObject.GetComponent <MeshFilter>().sharedMesh = mesh;

        terrainObject.AddComponent <MeshCollider>();
        terrainObject.GetComponent <MeshCollider>().sharedMesh = mesh;

        terrainObject.AddComponent <MeshRenderer>();
        terrainObject.GetComponent <MeshRenderer>().sharedMaterial = terrainMaterial;

        GetComponent <ForestController>().maxTerrainHeight = maxHeight;

        wallSettings.GenerateInitialWalls(terrainObject.transform, mesh.vertices, gridSize, mapSize);

        UpdateMaterial();
        Invoke("GenerateFlowMap", .01f);
    }