Ejemplo n.º 1
0
    void GenerateVoxelDensity()
    {
        if (!generator)
        {
            return;
        }

        Vector3 chunkWorldPosition = generator.ChunkToWorldNotScaled(chunkPosition);

        if (generator.EnableJob)
        {
            NativeArray <Voxel> nativeVoxels = new NativeArray <Voxel>(gridSize.x * gridSize.y * gridSize.z, Allocator.TempJob);

            VoxelNoiseJob noiseJob = new VoxelNoiseJob
            {
                chunkWorldPosition = chunkWorldPosition,
                cellSize           = generator.CellSize,
                gridSize           = gridSize,
                voxels             = nativeVoxels,
                frequency          = generator.Frequency
            };
            JobHandle noiseJobHandle = noiseJob.Schedule(gridSize.x * gridSize.y * gridSize.z, 32);
            noiseJobHandle.Complete();

            unsafe
            {
                fixed(void *voxelPointer = voxels)
                {
                    UnsafeUtility.MemCpy(voxelPointer, NativeArrayUnsafeUtility.GetUnsafeBufferPointerWithoutChecks(nativeVoxels), voxels.Length * (long)UnsafeUtility.SizeOf <Voxel>());
                }
            }

            nativeVoxels.Dispose();
        }
        else
        {
            for (int x = 0; x < gridSize.x; x++)
            {
                for (int y = 0; y < gridSize.y; y++)
                {
                    for (int z = 0; z < gridSize.z; z++)
                    {
                        Vector3 worldPosition   = new Vector3(x, y, z) + chunkWorldPosition;
                        Vector3 World2DPosition = new Vector3(worldPosition.x, worldPosition.z);

                        float density = -worldPosition.y;
                        density += Noise.Perlin2DFractal(World2DPosition, generator.Frequency, 5) * 10f;
                        density += Noise.Perlin2DFractal(World2DPosition, generator.Frequency * 0.5f, 3) * 50f;
                        float height = Mathf.Clamp01((worldPosition.y + 25f) / 40f);

                        voxels[x, y, z].Density = density;
                        voxels[x, y, z].Height  = height;
                    }
                }
            }
        }
    }