コード例 #1
0
        /// <summary>
        /// Setup is called when the seed is set through the SetSeed RPC call
        /// </summary>
        private void Setup()
        {
            // Collect and fill the biomes
            Biome[] biomes = BiomeMapSettings.Biomes;

            BiomeResources = new Dictionary <int, BiomeResources>();
            for (int i = 0; i < biomes.Length; i++)
            {
                int biomeIndex = (int)biomes[i].BiomeType;

                if (!BiomeResources.ContainsKey(biomeIndex))
                {
                    BiomeResources.Add(biomeIndex, new BiomeResources(biomes[i].Name, biomeIndex));
                }
            }

            foreach (var worldResourceEntry in ResourceMapSettings.WorldResourceEntries)
            {
                List <int> selectedBiomes = worldResourceEntry.GetBiomes();

                foreach (var selectedBiome in selectedBiomes)
                {
                    if (BiomeResources.ContainsKey(selectedBiome))
                    {
                        BiomeResources[selectedBiome].worldResourceEntries.Add(worldResourceEntry);
                    }
                    else
                    {
                        Debug.LogError("Given biomeId does not exist!");
                    }
                }
            }

            HeightMapSettings.UpdateMeshHeights(TerrainMeshMaterial, HeightMapSettings.MinHeight, HeightMapSettings.MaxHeight);
            HeightMapSettings.ApplyToMaterial(TerrainMeshMaterial);

            float maxViewDistance = detailLevels[detailLevels.Length - 1].VisibleDistanceThreshold;

            meshWorldSize = MeshSettings.MeshWorldSize;
            chunksVisibleInViewDistance = Mathf.RoundToInt(maxViewDistance / meshWorldSize);

            primaryViewer = new TerrainViewer(PlayerNetwork.LocalPlayer.transform, TerrainViewer.ViewerTypes.primary);

            UpdateVisibleChunks();

            IsSetupFinished = true;
            OnSetupFinished?.Invoke();
        }
コード例 #2
0
        public TerrainChunk(Vector2 coord, HeightMapSettings heightMapSettings, BiomeMapSettings biomeMapSettings, ResourceMapSettings resourceMapSettings, MeshSettings meshSettings, LODInfo[] detailLevels, int colliderLODIndex, Transform parent, TerrainViewer viewer, Material terrainMeshMaterial)
        {
            this.Coord               = coord;
            this.HeightMapSettings   = heightMapSettings;
            this.BiomeMapSettings    = biomeMapSettings;
            this.ResourceMapSettings = resourceMapSettings;
            this.MeshSettings        = meshSettings;
            this.detailLevels        = detailLevels;
            this.colliderLODIndex    = colliderLODIndex;
            this.Viewer              = viewer;

            SampleCenter = coord * meshSettings.MeshWorldSize / meshSettings.MeshScale;
            Vector2 position = coord * meshSettings.MeshWorldSize;

            Bounds = new Bounds(position, Vector2.one * meshSettings.MeshWorldSize);

            MeshObject            = new GameObject("Terrain Chunk");
            meshRenderer          = MeshObject.AddComponent <MeshRenderer>();
            meshFilter            = MeshObject.AddComponent <MeshFilter>();
            meshCollider          = MeshObject.AddComponent <MeshCollider>();
            meshRenderer.material = terrainMeshMaterial;

            MeshObject.AddComponent <TerrainChunkInteraction>().TerrainChunk = this;

            MeshObject.transform.position = new Vector3(position.x, 0, position.y);
            MeshObject.transform.parent   = parent;
            MeshObject.layer = parent.gameObject.layer;

            SetVisible(false);

            lodMeshes = new LODMesh[detailLevels.Length];
            for (int i = 0; i < detailLevels.Length; i++)
            {
                lodMeshes[i] = new LODMesh(detailLevels[i].Lod);
                lodMeshes[i].UpdateCallback += UpdateTerrainChunk;
                if (i == colliderLODIndex)
                {
                    lodMeshes[i].UpdateCallback += UpdateCollisionMesh;
                }
            }

            MaxViewDistance = detailLevels[detailLevels.Length - 1].VisibleDistanceThreshold;
        }