Exemple #1
0
 void ClearMesh()
 {
     chunkMesh.Clear();
     vertices.Clear();
     uvs.Clear();
     triangles.Clear();
     MeshColliderController.RemoveMeshCollider(this);
 }
Exemple #2
0
    public void ClearChunks()
    {
        for (int i = transform.childCount - 1; i >= 0; i--)
        {
            DestroyImmediate(transform.GetChild(i).gameObject);
        }

        MeshColliderController.ClearColliders();
    }
Exemple #3
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!");
    }
Exemple #4
0
    public void GenerateMesh()
    {
        int originalSize       = 16;
        int blockTextureLength = originalSize * 6;
        int textureSize        = masterMaterial.mainTexture.height;

        int vertexIndex   = 0;
        int triangleIndex = 0;

        for (int x = 0; x < faceMap.GetLength(0); x++)
        {
            for (int y = 0; y < faceMap.GetLength(1); y++)
            {
                for (int h = 0; h < faceMap.GetLength(2); h++)
                {
                    int id = blockMap[x, y, h];

                    if (id != -1 && faceMap[x, y, h] != null)
                    {
                        for (int f = 0; f < faceMap[x, y, h].Length; f++)
                        {
                            if (faceMap[x, y, h][f] == -1)
                            {
                                Vector3 position = new Vector3(chunkOffset.x + x, h, chunkOffset.y + y);
                                Vector3 offset   = new Vector3(.5f, -.5f, .5f) + position;

                                Vector3 localUp = Block.directions[f];
                                Vector3 xAxis   = new Vector3(localUp.y, localUp.z, localUp.x);
                                Vector3 zAxis   = Vector3.Cross(localUp, xAxis);

                                vertices.Add(localUp / 2 + (0f - .5f) * xAxis + (0f - .5f) * zAxis + offset);
                                vertices.Add(localUp / 2 + (1f - .5f) * xAxis + (0f - .5f) * zAxis + offset);
                                vertices.Add(localUp / 2 + (1f - .5f) * xAxis + (1f - .5f) * zAxis + offset);
                                vertices.Add(localUp / 2 + (0f - .5f) * xAxis + (1f - .5f) * zAxis + offset);

                                int     localX      = (blockTextureLength * id) % masterMaterial.mainTexture.width;
                                int     localY      = ((blockTextureLength * id) / masterMaterial.mainTexture.width) * originalSize;
                                Vector2 uvOffset    = new Vector2(localX + (f * 16), localY);
                                Vector2 adjustedUv0 = ((Block.faceUvMaps[f][0] * originalSize) + uvOffset) / new Vector2(textureSize * 6, textureSize);
                                Vector2 adjustedUv1 = ((Block.faceUvMaps[f][1] * originalSize) + uvOffset) / new Vector2(textureSize * 6, textureSize);
                                Vector2 adjustedUv2 = ((Block.faceUvMaps[f][2] * originalSize) + uvOffset) / new Vector2(textureSize * 6, textureSize);
                                Vector2 adjustedUv3 = ((Block.faceUvMaps[f][3] * originalSize) + uvOffset) / new Vector2(textureSize * 6, textureSize);
                                uvs.Add(adjustedUv0);
                                uvs.Add(adjustedUv1);
                                uvs.Add(adjustedUv2);
                                uvs.Add(adjustedUv3);

                                triangles.Add(vertexIndex);
                                triangles.Add(vertexIndex + 2);
                                triangles.Add(vertexIndex + 3);
                                triangles.Add(vertexIndex);
                                triangles.Add(vertexIndex + 1);
                                triangles.Add(vertexIndex + 2);

                                vertexIndex   += 4;
                                triangleIndex += 6;
                            }
                        }
                    }
                }
            }
        }

        // Debug.Log("Vertices: " + vertices.ToArray().Length + " | Triangles: " + triangles.ToArray().Length);

        // chunkMesh.Clear();
        chunkMesh.vertices  = vertices.ToArray();
        chunkMesh.triangles = triangles.ToArray();
        chunkMesh.uv        = uvs.ToArray();
        chunkMesh.RecalculateNormals();

        meshFilter.sharedMesh = chunkMesh;
        meshRenderer.material = masterMaterial;

        MeshColliderController.AddMeshCollider(this, chunkMesh);

        // Resources.UnloadUnusedAssets();
    }