private TreePrototype[] GetTreePrototypes()
        {
            var treePrototypes = new TreePrototype[_vegetationData.Count];

            for (var i = 0; i < _vegetationData.Count; i++)
            {
                var item = _vegetationData.GetObjectAt(i);
                treePrototypes[i] = new TreePrototype
                {
                    prefab = item.Mesh
                };
            }

            return(treePrototypes);
        }
Ejemplo n.º 2
0
        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();
            }
        }