public void Initialise()
 {
     if (!initialised)
     {
         var typeManager = (VoxelTypeManager)FindObjectOfType(typeof(VoxelTypeManager));
         BiomeDatabase = ConfigToNative(config, typeManager);
     }
 }
        public NativeBiomeDatabase ConfigToNative(SOBiomeConfiguration config, VoxelTypeManager typeManager)
        {
            List <NativeVoxelRange> allLayersList   = new List <NativeVoxelRange>();
            List <StartEndRange>    biomeLayersList = new List <StartEndRange>();
            List <NativeBiomeMoistureDefinition> allMoistureDefsList   = new List <NativeBiomeMoistureDefinition>();
            List <NativeElevationZone>           allElevationZonesList = new List <NativeElevationZone>();


            foreach (var elevationEntry in config.elevationLowToHigh)
            {
                NativeElevationZone elevationZone = new NativeElevationZone();
                elevationZone.maxElevationPercentage = elevationEntry.max;
                elevationZone.moistureLevels         = new StartEndRange()
                {
                    start = allMoistureDefsList.Count
                };

                foreach (var moistureEntry in elevationEntry.moistureLevelsLowToHigh)
                {
                    NativeBiomeMoistureDefinition moistureDef = new NativeBiomeMoistureDefinition();
                    moistureDef.maxMoisturePercentage = moistureEntry.max;

                    if (!biomeIds.TryGetValue(moistureEntry.biomeDefinition, out var id))
                    {
                        //Create new biome data
                        id = biomeLayersList.Count;
                        biomeIds.Add(moistureEntry.biomeDefinition, id);
                        biomeDefinitionsById.Add(moistureEntry.biomeDefinition);

                        Assert.IsTrue(moistureEntry.biomeDefinition.topLayers.Count > 0, $"All biome definitions must have at least one layer,{moistureEntry.biomeDefinition.name} does not");
                        StartEndRange layersForThisBiome = new StartEndRange()
                        {
                            start = allLayersList.Count
                        };

                        foreach (var layer in moistureEntry.biomeDefinition.topLayers)
                        {
                            NativeVoxelRange nativeLayer = new NativeVoxelRange();
                            nativeLayer.depth   = layer.depth;
                            nativeLayer.voxelID = typeManager.GetId(layer.voxelType);
                            allLayersList.Add(nativeLayer);
                        }

                        layersForThisBiome.end = allLayersList.Count;
                        biomeLayersList.Add(layersForThisBiome);
                    }

                    moistureDef.biomeID = id;

                    allMoistureDefsList.Add(moistureDef);
                }

                elevationZone.moistureLevels.end = allMoistureDefsList.Count;
                allElevationZonesList.Add(elevationZone);
            }

            NativeBiomeDatabase biomeDatabase = new NativeBiomeDatabase();

            VoxelTypeID defaultVoxelType = new VoxelTypeID(VoxelTypeID.AIR_ID);

            if (config.defaultVoxelType != null)
            {
                defaultVoxelType = typeManager.GetId(config.defaultVoxelType);
            }

            biomeDatabase.defaultVoxelType = defaultVoxelType;

            biomeDatabase.allLayers         = new NativeArray <NativeVoxelRange>(allLayersList.ToArray(), Allocator.Persistent);
            biomeDatabase.biomeLayers       = new NativeArray <StartEndRange>(biomeLayersList.ToArray(), Allocator.Persistent);
            biomeDatabase.allMoistureDefs   = new NativeArray <NativeBiomeMoistureDefinition>(allMoistureDefsList.ToArray(), Allocator.Persistent);
            biomeDatabase.allElevationZones = new NativeArray <NativeElevationZone>(allElevationZonesList.ToArray(), Allocator.Persistent);

            return(biomeDatabase);
        }