0

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.adding density to a chunk's points

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();
            }
        }
    }

enter image description here

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();
    }
  • in what way has it not worked? – BugFinder Jan 20 '20 at 13:03
  • no density changed in neighbour chunks. u think the for loops are right? – Gàbor Ràcz Jan 20 '20 at 13:07
  • well looking at the code it seems confusing. if you have 3d space (eg points [x,y,z] type) I would expect only 3 loops. to pick a spot (eg 0-19), and 3 loops to go round the spot. as in x-1 to x+1, y-1 to y+1 and z-1 to z+1 assuming its not a hard edge eg x=0 or 19 etc. The variable naming doesnt entirely help . But on face value it seems you have more loops than you needed, but is making my head hurt a bit... given i dont have names for those methods and you mention circle you possibly then dont even need the x-1 to x+1 type loops to go round so should only need less.. – BugFinder Jan 20 '20 at 13:15
  • i know what u mean but the world cordinates are different than local. with 3 loops i can check only local values i think.i must iterate through edges of both chunk. for example: neighbour(4) whih is the center of 9 chunks have 8 neighbours. if neighbour(4). x,y,z [0,0,0] it means world cordinates are 0,0,0 for the point. but for the right neigbour(5) its: x,y,z = 20,0,0 at the same [0,0,0]. im not perfect in english and i hope u understand what i mean. – Gàbor Ràcz Jan 20 '20 at 13:30
  • i have to iterate through of the edge of 2 chunks at the same time and compare their density. x,y loop goes around first chunk and xx,yy goes around second chunk edge points. – Gàbor Ràcz Jan 20 '20 at 13:30
  • in a more similar way: i want to compare two 3d array edges. and update the first array to same values in the second. – Gàbor Ràcz Jan 20 '20 at 13:36

1 Answers1

0

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.