简体   繁体   中英

c# unity 3d coordinates map sync

I'm working on a procedurally generated map/game. I have 3D chunks with 20x20x20 points in a 3D array. I'm using marching cubes algorithm to generate the chunks. I try to manipulate the density with adding and subtracting the values but in the same time trying to sync the neighbour chunks edge together the with chunk im modifying. 将密度添加到块的点

Here is the code i tough it will work, but its not working:

`for (int i = 0; i < 9; i++)
    {
        Chunk nChunk = neighbours[i].GetComponent<Chunk>();
        if (i == 4) continue; //skip self
        for (int z = 0; z < 20; z += 19)
        {
            for (int x = 0; x < 20; x += 19)
            {
                for (int zz = 0; zz < 20; zz += 19)
                {
                    for (int xx = 0; xx < 20; xx += 19)
                    {
                        for (int y = 0; y < 20; y++)
                        {
                            if (Points[xx, y, zz].wPosition == nChunk.Points[x, y, z].wPosition)
                                nChunk.Points[x, y, z].density = Points[xx, y, zz].density;
                        }
                    }
                }
            }
        }
    }`

I want to check the edges and not all Points to save performance. the y coords the height.

Here is how i call this:

        //mouse click
    if (Input.GetMouseButtonDown(0))
    {
        Ray raym = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hitm;
        if (Physics.Raycast(raym, out hitm))
        {
            if (hitm.transform.GetComponent<Chunk>())
            {
                Chunk cScript = hitm.transform.GetComponent<Chunk>();
                for (int z = 0; z < 20; z++)
                {
                    for (int y = 1; y < 19; y++)
                    {
                        for (int x = 0; x < 20; x++)
                        {
                            if (PointInsideSphere(cScript.Points[x, y, z].wPosition, hitm.point, 1f))
                                cScript.Points[x, y, z].density -= 0.1f;
                        }
                    }
                }
                cScript.SyncNeighbours();
            }
        }
    }

在此处输入图片说明

In this way its working good but i want to check only the edges.

        for (int i = 0; i < 9; i++)
    {
        Chunk nChunk = neighbours[i].GetComponent<Chunk>();

        if (i == 4) continue; //skip self
        for (int z = 0; z < 20; z ++)
        {
            for (int x = 0; x < 20; x ++)
            {
                for (int zz = 0; zz < 20; zz ++)
                {
                    for (int xx = 0; xx < 20; xx ++)
                    {
                        for (int y = 0; y < 20; y++)
                        {
                            if (Points[xx, y, zz].wPosition.x == nChunk.Points[x, y, z].wPosition.x && Points[xx, y, zz].wPosition.y == nChunk.Points[x, y, z].wPosition.y && Points[xx, y, zz].wPosition.z == nChunk.Points[x, y, z].wPosition.z)
                                nChunk.Points[x, y, z].density = Points[xx, y, zz].density;
                        }
                    }
                }
            }
        }
    }

    for (int j = 0; j < 9; j++)
    {
        neighbours[j].GetComponent<Chunk>().UpdateChunk();
    }

It's solved in different way: When the game creates a chunk it checks points with 0 and 19 positions in 3d array then add them to a list. from that list i can check the points on neighbouring faces and make the densitys equal.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM