private void PlaceTrees(List <Vector3> placementPositions, TerrainLookup terrainLookup) { var treeInstances = new Dictionary <Terrain, List <TreeInstance> >(); for (var i = 0; i < placementPositions.Count; i++) { var terrain = terrainLookup.GetTerrain(placementPositions[i]); var treePositionOnTerrain = GetTreePosition(terrain, placementPositions[i]); var protoIndex = UnityEngine.Random.Range(0, _vegetationData.Count); //TODO: Added Weighted Probability based on Tree Probability value - Risul var treeInstance = _vegetationData.GetObjectAt(protoIndex); if (treeInstances.TryGetValue(terrain, out var trees) == false) { trees = new List <TreeInstance>(); treeInstances.Add(terrain, trees); } var proportionalScale = UnityEngine.Random.Range(treeInstance.MinScale, treeInstance.MaxScale); var tree = new TreeInstance { position = treePositionOnTerrain, rotation = UnityEngine.Random.Range(treeInstance.MinRotation, treeInstance.MaxRotation), prototypeIndex = protoIndex, color = Color.Lerp(treeInstance.Color1, treeInstance.Color2, UnityEngine.Random.Range(0, 1)), lightmapColor = treeInstance.LightColor, heightScale = proportionalScale, widthScale = proportionalScale }; trees.Add(tree); } foreach (var treesPerTerrain in treeInstances) { treesPerTerrain.Key.terrainData.treeInstances = treesPerTerrain.Value.ToArray(); } }
public void Vegetate(List <DrawableMap.DrawableRoad> roads, LayerMask terrainLayerMask, TerrainLookup terrainLookup) { var placementAreas = GetPlacementAreas(roads, _settings.PlacementDepth, _settings.RoadBoundOffset); var placementAreasNativeArray = new NativeArray <Quad>(placementAreas.Count, Allocator.TempJob); placementAreasNativeArray.CopyFrom(placementAreas.ToArray()); Profiler.BeginSample("Place Trees"); PlaceTrees(GetPlacementPositions(terrainLayerMask, placementAreasNativeArray, _settings.MinimumDistanceBetweenTrees), terrainLookup); Profiler.EndSample(); Profiler.BeginSample("Place Grass"); for (var i = 0; i < _detailsData.Count; i++) { PlaceGrass(GetPlacementPositions(terrainLayerMask, placementAreasNativeArray, _settings.MinimumDistanceBetweenGrass), i, terrainLookup); } Profiler.EndSample(); placementAreasNativeArray.Dispose(); }
public void PlaceGrass(List <Vector3> placementPositions, int detailLayerIndex, TerrainLookup terrainLookup) { var terrainDetail = new Dictionary <Terrain, int[, ]>(); for (var i = 0; i < placementPositions.Count; i++) { var terrain = terrainLookup.GetTerrain(placementPositions[i]); if (terrainDetail.TryGetValue(terrain, out var detailData) == false) { detailData = new int[terrain.terrainData.detailWidth, terrain.terrainData.detailHeight]; terrainDetail.Add(terrain, detailData); } var detailCord = GetDetailCoordinate(terrain, placementPositions[i]); detailData[detailCord.y, detailCord.x] = 1; } foreach (var terrain in terrainDetail) { terrain.Key.terrainData.SetDetailLayer(0, 0, detailLayerIndex, terrain.Value); } }