Javier-Garzo / Marching-cubes-on-Unity-3D

Terrain voxel engine with the use of Marching Cubes implemented in Unity 2020.3.17f1 (LTS).
MIT License
332 stars 43 forks source link

Getting deeper #3

Closed KarolDebiec closed 3 years ago

KarolDebiec commented 3 years ago

Hi, thanks for your project! Ater changing to HDRP and using the lit texture on terrain I have this weird problem where I can only light up in one direction. Something like this. helpas Do you have any clue what happened there?

Javier-Garzo commented 3 years ago

i'm glad you used my project. The problem with the height of the surface can be solved with the change of variable inthe "NoiseManager" gameobject, it have a component "NoiseManager" with a variable called "Surface Level". That varible indicate the desire height of the surface for all biomes. You can increase to the max height - 45 for adapt it to the actual biomes. You will need delete the actual data of the world for see the changes in the edited terrain (saved in the Application.persistentDataPath).

Img surfaceLevel

For the second problem of the lighting, I try to update the project to the HDRP but i archive no illumination on the terrain, so I can't investigate the problem of your light. However I recommend you the use of the LWRP, because the terrain is generated in real-time you can't bake lightmaps so it's a better option for a voxel-game with realtime-lighting.

KarolDebiec commented 3 years ago

Thanks for the reply. So I found the problem with the lights. You forgot to include :

        meshGenerated.RecalculateNormals();
        meshGenerated.RecalculateTangents();

in MeshBuilder.cs it should be something like this I think.

public Mesh BuildChunk(byte[] b)
    {
        BuildChunkJob buildChunkJob = new BuildChunkJob
        {
            chunkData = new NativeArray<byte>(b, Allocator.TempJob),
            isoLevel = this.isoLevel,
            interpolate = this.interpolate,
            vertex = new NativeList<float3>(500, Allocator.TempJob),
            uv = new NativeList<float2>(100, Allocator.TempJob),
        };
        JobHandle jobHandle = buildChunkJob.Schedule();
        jobHandle.Complete();

        //Get all the data from the jobs and use to generate a Mesh
        Mesh meshGenerated = new Mesh();
        Vector3[] meshVert = new Vector3[buildChunkJob.vertex.Length];
        int[] meshTriangles = new int[buildChunkJob.vertex.Length];
        for (int i = 0; i < buildChunkJob.vertex.Length; i++)
        {
            meshVert[i] = buildChunkJob.vertex[i];
            meshTriangles[i] = i;
        }
        meshGenerated.vertices = meshVert;

        Vector2[] meshUV = new Vector2[buildChunkJob.vertex.Length];

        for (int i = 0; i < buildChunkJob.vertex.Length; i++)
        {
            meshUV[i] = buildChunkJob.uv[i];
        }
        meshGenerated.uv = meshUV;
        meshGenerated.triangles = meshTriangles;

        //Dispose (Clear the jobs NativeLists)
        meshGenerated.RecalculateNormals();
        meshGenerated.RecalculateTangents();
        buildChunkJob.vertex.Dispose();
        buildChunkJob.uv.Dispose();
        buildChunkJob.chunkData.Dispose();

        return meshGenerated;
    }
Javier-Garzo commented 3 years ago

Thanks for the fix, I will add to the actual repository. Good luck with your project.