Threaded Terrain Generation 3d Bug Unity

Viewed 84

So I have this weird bug in my terrain generation project I have 2 methods that were supposed to fix offsets for different noise scales between chunks but for some reason when I implimented threading it stopped working in seemingly random areas. I'm 100% sure this worked before threading.

public struct GetMeshData : IJob
{
    public ChunkData chunkData;
    public NativeArray<float> noiseMap;
    public MeshData meshData;
    public void Execute()
    {
        for (int z = 0; z < chunkData.chunkSize + 1; z++)
        {
            for (int x = 0; x < chunkData.chunkSize + 1; x++)
            {
                noiseMap[z * (chunkData.chunkSize + 1) + x] = Noise.GetNoiseValue(chunkData, x, z);
            }
        }
        MeshData temporaryMeshData = MeshGeneration.GenerateMesh(chunkData, noiseMap, meshData.triangles, meshData.vertices, meshData.uvs);
        meshData = temporaryMeshData;
    }

    public GetMeshData(ChunkData chunkData, NativeArray<Vector3> vertices, NativeArray<Vector2> uvs, NativeArray<int> triangles, NativeArray<float> noiseMap)
    {
        this.chunkData = chunkData;
        this.meshData = new MeshData(vertices, uvs, triangles);
        this.noiseMap = noiseMap;
    }


}

this is the job while this is my code to handle it

for (int i = 0; i < meshJob.Count; i++)
        {
            if (meshJob[i].IsCompleted)
            {
                meshJob[i].Complete();
                TerrainChunk currentChunk = chunks[meshJobData[i].chunkData.chunkPosition.x,
                meshJobData[i].chunkData.chunkPosition.y];
                PostMeshGeneration(currentChunk, meshJobData[i].meshData);
                toBeAdjusted.Add(currentChunk);
                meshJobData[i].noiseMap.Dispose();
                meshJobData[i].meshData.vertices.Dispose();
                meshJobData[i].meshData.triangles.Dispose();
                meshJobData[i].meshData.uvs.Dispose();
                meshJob.RemoveAt(i);
                meshJobData.RemoveAt(i);
            }
        }

this is what I do after the job is complete:


public void PostMeshGeneration(TerrainChunk chunk, MeshData meshData)
    {
        Mesh mesh = new Mesh();
        mesh.vertices = meshData.vertices.ToArray();
        mesh.triangles = meshData.triangles.ToArray();
        mesh.uv = meshData.uvs.ToArray();
        chunk.GetChunkGameObject().GetComponent<MeshFilter>().mesh = mesh;
        ReadjustMeshCollider(chunk);
    }

lastly this is trying to fix the scale offsets

if (toBeAdjusted.Count != 0 && meshJob.Count == 0 && meshJobData.Count == 0)
        {
            while (toBeAdjusted.Count > 0)
            {
                TerrainChunk currentChunk = toBeAdjusted[0];
                Mesh mesh = currentChunk.GetChunkGameObject().GetComponent<MeshFilter>().mesh;
                AdjustNoiseScaling(currentChunk);
                FixCornerVerticesOffset(currentChunk);
                mesh.UploadMeshData(false);
                ApplyColorsToChunk(currentChunk, mesh.vertices);
                ReadjustMeshCollider(currentChunk);

                toBeAdjusted.RemoveAt(0);
            }

        }

if you need to see more code go to the github link https://github.com/htmhell69/TerrainGenerationUnity

0 Answers
Related