Exemple #1
0
 void ClearMesh()
 {
     chunkMesh.Clear();
     vertices.Clear();
     uvs.Clear();
     triangles.Clear();
     MeshColliderController.RemoveMeshCollider(this);
 }
Exemple #2
0
    public void Generate()
    {
        // if (player == null) player = GameObject.Find("Player").GetComponent<Player>();
        // if (noise == null) noise = new SimplexNoiseGenerator(seed);
        masterMaterial             = new Material(Shader.Find("Standard"));
        masterMaterial.mainTexture = masterTexture;
        masterMaterial.SetFloat("_Smoothness", 0);
        // masterMaterial.SetFloat("_Mode", 3);

        MeshColliderController.Initialize();

        if (chunks != null && chunks.Length > 0)
        {
            for (int y = 0; y < chunks.GetLength(1); y++)
            {
                for (int x = 0; x < chunks.GetLength(0); x++)
                {
                    // Debug.Log(chunks[x, y].GetMesh());
                    // MeshCollider collider = gameObject.AddComponent<MeshCollider>();
                    // collider.sharedMesh = chunks[x, y].GetMesh();
                    // colliders[chunks[x, y]] = collider;
                    MeshColliderController.RemoveMeshCollider(chunks[x, y]);
                }
            }
        }

        Debug.Log("Starting Map Generation...");

        if (transform.childCount > 0)
        {
            ClearChunks();
        }

        // Debug.Log("Generating new chunks...");
        chunks = new Chunk[worldSize, worldSize];

        for (int y = 0; y < worldSize; y++)
        {
            for (int x = 0; x < worldSize; x++)
            {
                // Debug.Log("Chunk " + (y * worldSize + x) + "/" + (worldSize * worldSize) + " loaded...");
                chunks[x, y] = new Chunk(chunkSize, chunkHeight, new Vector2(x, y), transform, GetDeepNoise(x, y), GetSurfaceNoise(x, y), masterMaterial);
            }
        }

        GenerateMeshes();

        // for (int y = 0; y < worldSize; y++)
        // {
        //   for (int x = 0; x < worldSize; x++)
        //   {
        //     if (chunks[x, y].GetMesh() != null)
        //     {
        //       // Debug.Log(chunks[x, y].GetMesh());
        //       // MeshCollider collider = gameObject.AddComponent<MeshCollider>();
        //       // collider.sharedMesh = chunks[x, y].GetMesh();
        //       // colliders[chunks[x, y]] = collider;
        //       MeshColliderController.AddMeshCollider(chunks[x, y], chunks[x, y].GetMesh());
        //     }
        //     else
        //     {
        //       Debug.Log("X: " + x + " Y: " + y);
        //     }
        //   }
        // }

        // Debug.Log("Moving player...");

        int highest = 0;

        for (int h = 0; h < chunks[0, 0].blockMap.GetLength(2); h++)
        {
            if (chunks[0, 0].blockMap[0, 0, h] != -1)
            {
                highest = h;
            }
        }

        player.transform.position = new Vector3(.5f, highest + 1.5f, .5f);

        Debug.Log("Map Generated!");
    }