private IEnumerator Populate(CellMatrixData data) { var accessor = data.GetAccessor(); //Get the height lookup _heightLookup = CreateHeightLookup(accessor.heightEntries); for (int x = 0; x < _heightMapSizeX; x++) { for (int z = 0; z < _heightMapSizeZ; z++) { var idx = (x * _heightMapSizeZ) + z; var height = accessor.GetHeight(idx); _heightLookup.Add(x, z, height); yield return(null); } } _heightLookup.Cleanup(); //Init the cells var heightSampler = GetHeightSampler(); var cellFactory = GameServices.cellConstruction.cellFactory; var maxHeight = _origin.y + _upperBoundary; var minHeight = _origin.y - _lowerBoundary; for (int x = 0; x < this.columns; x++) { for (int z = 0; z < this.rows; z++) { var idx = (z * this.columns) + x; var position = new Vector3(_start.x + (x * _cellSize) + (_cellSize / 2.0f), _origin.y, _start.z + (z * _cellSize) + (_cellSize / 2.0f)); position.y = Mathf.Clamp(heightSampler.SampleHeight(position, this), minHeight, maxHeight); var blocked = accessor.IsPermaBlocked(idx); var cell = cellFactory.Create(this, position, x, z, blocked); accessor.InjectData(cell, idx); this.rawMatrix[x, z] = cell; yield return(null); } } //For now we don't bake clearance data var cellConstruct = GameServices.cellConstruction; if (cellConstruct.clearanceProvider != null) { var iter = cellConstruct.clearanceProvider.SetClearance(this); while (iter.MoveNext()) { yield return(null); } } }
private void InitHeightMapForEditor(CellMatrixData data) { var accessor = data.GetAccessor(); //Get the height map _heightLookup = CreateHeightLookup(accessor.heightEntries); for (int x = 0; x < _heightMapSizeX; x++) { for (int z = 0; z < _heightMapSizeZ; z++) { var idx = (x * _heightMapSizeZ) + z; var height = accessor.GetHeight(idx); _heightLookup.Add(x, z, height); } } _heightLookup.Cleanup(); }
private IEnumerator Populate() { //Create the height map _heightLookup = CreateHeightLookup(0); if (_generateHeightMap) { var detailedHeightMap = (GameServices.heightStrategy.heightMapDetail == HeightMapDetailLevel.High); var plotRange = _lowerBoundary + _upperBoundary; var down = Vector3.down; Vector3 samplePos; samplePos.y = _start.y + _upperBoundary; RaycastHit hit; for (int x = 0; x < _heightMapSizeX; x++) { samplePos.x = detailedHeightMap ? (float)Math.Round(_start.x + (x * _granularity), 4) : _start.x + (x * _granularity); for (int z = 0; z < _heightMapSizeZ; z++) { samplePos.z = detailedHeightMap ? (float)Math.Round(_start.z + (z * _granularity), 4) : _start.z + (z * _granularity); if (UnityServices.physics.Raycast(samplePos, down, out hit, plotRange, Layers.terrain)) { _heightLookup.Add(x, z, hit.point.y); } else { _heightLookup.Add(x, z, Consts.InfiniteDrop); } yield return(null); } } _heightLookup.Cleanup(); } //Populate the cell matrix var heightSampler = GetHeightSampler(); var calculateHeights = (GameServices.heightStrategy.heightMode != HeightSamplingMode.NoHeightSampling); var maxHeight = _origin.y + _upperBoundary; var minHeight = _origin.y - _lowerBoundary; var blockThreshold = Mathf.Max(_obstacleSensitivityRange, 0.01f); for (int x = 0; x < this.columns; x++) { for (int z = 0; z < this.rows; z++) { var position = new Vector3(_start.x + (x * _cellSize) + (_cellSize / 2.0f), _origin.y, _start.z + (z * _cellSize) + (_cellSize / 2.0f)); if (calculateHeights) { position.y = Mathf.Clamp(heightSampler.SampleHeight(position, this), minHeight, maxHeight); } var blocked = IsBlocked(position, blockThreshold); this.rawMatrix[x, z] = _cellFactory.Create(this, position, x, z, blocked); yield return(null); } } //Set the height block status of each cell if (calculateHeights) { var entireMatrix = new MatrixBounds(0, 0, this.columns - 1, this.rows - 1); //Set height map settings for all cells var iter = _heightSettingsProvider.AssignHeightSettings(entireMatrix); while (iter.MoveNext()) { yield return(null); } } }