コード例 #1
0
 public TerrainInteractiblesCreator(Vector2 meshCenter, Transform parent,
                                    LevelSettings levelSettings, ClearingSettings clearingSettings)
 {
     _meshCenter       = new Vector3(meshCenter.x, 0, meshCenter.y);
     _parent           = parent;
     _levelSettings    = levelSettings;
     _clearingSettings = clearingSettings;
 }
コード例 #2
0
        public Trees(Vector2 meshCenter, TreeSettings treeSettings, ClearingSettings clearingSettings)
        {
            _meshCenter       = new Vector3(meshCenter.x, 0, meshCenter.y);
            _treeSettings     = treeSettings;
            _clearingSettings = clearingSettings;

            _trees      = new GameObject[0];
            _treePoints = new Vector3[0];
        }
コード例 #3
0
        public TerrainChunk(Vector2 coord, HeightMapSettings heightMapSettings,
                            MeshSettings meshSettings, TreeSettings treeSettings,
                            LevelSettings levelSettings, ClearingSettings clearingSettings, LODInfo[] detailLevels,
                            int colliderLODIndex, Transform parent, Transform viewer, Material material,
                            bool createEnemies)
        {
            this.coord = coord;

            _droidsRequested     = !createEnemies;
            _meshDatSentForTower = !createEnemies;

            _detailLevels      = detailLevels;
            _prevLODIndex      = -1;
            _colliderLODIndex  = colliderLODIndex;
            _heightMapSettings = heightMapSettings;
            _meshSettings      = meshSettings;
            _viewer            = viewer;

            _sampleCenter = coord * meshSettings.meshWorldSize / meshSettings.meshScale;
            var position = coord * meshSettings.meshWorldSize;

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

            _meshObject = new GameObject("Terrain Chunk");
            _meshObject.transform.position = new Vector3(position.x, 0, position.y);
            _meshObject.transform.SetParent(parent);
            _meshObject.layer = 11;
            _meshObject.tag   = TagManager.Terrain;

            _meshRenderer          = _meshObject.AddComponent <MeshRenderer>();
            _meshRenderer.material = material;
            _meshFilter            = _meshObject.AddComponent <MeshFilter>();
            _meshCollider          = _meshObject.AddComponent <MeshCollider>();

            _chunkTrees           = new Trees(position, treeSettings, clearingSettings);
            _terrainInteractibles = new TerrainInteractiblesCreator(position, _meshObject.transform,
                                                                    levelSettings, clearingSettings);

            SetVisible(false);

            _lodMeshes = new LODMesh[detailLevels.Length];
            for (var 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;
        }
コード例 #4
0
        public static Vector3[] SelectTreePoints(Vector3[] vertices, int chunkSizeIndex,
                                                 Vector3 meshCenter, TreeSettings treeSettings, ClearingSettings clearingSettings)
        {
            var random          = new Random();
            var chunkSize       = MeshSettings.supportedChunkSizes[chunkSizeIndex];
            var totalTreePoints = Mathf.FloorToInt
                                      (random.Next(treeSettings.minTreesInMaxChunkSize, treeSettings.maxTreesInMaxChunkSize) /
                                      (float)MeshSettings.supportedChunkSizes[MeshSettings.supportedChunkSizes.Length - 1] *
                                      chunkSize);

            var selectedPoints = new Vector3[totalTreePoints];
            var index          = 0;
            var tileCenter     = clearingSettings.useOnlyCenterTile ? meshCenter : Vector3.zero;

            for (var i = 0; i < vertices.Length; i++)
            {
                if (clearingSettings.createClearing)
                {
                    var modifiedVertices = vertices[i] + tileCenter;

                    if (modifiedVertices.x > clearingSettings.clearingBottomLeft.x &&
                        modifiedVertices.z < clearingSettings.clearingTopRight.x &&
                        modifiedVertices.z > clearingSettings.clearingBottomLeft.y &&
                        modifiedVertices.z < clearingSettings.clearingTopRight.y)
                    {
                        continue;
                    }
                }

                var selectionProbability = (float)totalTreePoints / (vertices.Length - i);
                var randomValue          = (float)random.NextDouble();

                if (selectionProbability >= randomValue)
                {
                    selectedPoints[index] = vertices[i];
                    index           += 1;
                    totalTreePoints -= 1;
                }
            }

            return(selectedPoints);
        }