- Home /
Question by
naderlabbad309 · Jul 27, 2019 at 07:48 PM ·
c#generatortreeprototype
help change script GrassGenerator !!
hi
i need help script GrassGenerator There is a button named Generate When pressed (it clear tree)Is there a way to stop clear tree just add tree
using Assets.Scripts.MapGenerator.Maps;
using System.Collections.Generic;
using UnityEngine;
namespace Assets.Scripts.MapGenerator.Generators
{
public class GrassGenerator : MonoBehaviour, IGenerator
{
public int Octaves = 4;
public float Scale = 40;
public float Lacunarity = 2f;
[Range(0, 1)]
public float Persistance = 0.5f;
public float Offset = 100f;
public float MinLevel = 0;
public float MaxLevel = 100;
[Range(0, 90)]
public float MaxSteepness = 70;
[Range(-1, 1)]
public float IslandsSize = 0;
[Range(1, 100)]
public int Density = 10;
public bool Randomize;
public bool AutoUpdate;
public List<GameObject> GrassTextures;
//DetailPrototype
public void Generate()
{
if (Randomize)
{
Offset = Random.Range(0f, 9999f);
}
List<TreePrototype> treePrototypes = new List<TreePrototype>();
foreach (var t in GrassTextures)
{
treePrototypes.Add(new TreePrototype() { prefab = t });
}
TerrainData terrainData = Terrain.activeTerrain.terrainData;
terrainData.treePrototypes = treePrototypes.ToArray();
terrainData.treeInstances = new TreeInstance[0];
List<Vector3> treePos = new List<Vector3>();
float maxLocalNoiseHeight;
float minLocalNoiseHeight;
float[,] noiseMap = new PerlinMap()
{
Size = terrainData.alphamapWidth,
Octaves = Octaves,
Scale = Scale,
Offset = Offset,
Persistance = Persistance,
Lacunarity = Lacunarity
}.Generate(out maxLocalNoiseHeight, out minLocalNoiseHeight);
for (int x = 0; x < terrainData.alphamapWidth; x++)
{
for (int y = 0; y < terrainData.alphamapHeight; y++)
{
float height = terrainData.GetHeight(x, y);
float heightScaled = height / terrainData.size.y;
float xScaled = (x + Random.Range(-1f, 1f)) / terrainData.alphamapWidth;
float yScaled = (y + Random.Range(-1f, 1f)) / terrainData.alphamapHeight;
float steepness = terrainData.GetSteepness(xScaled, yScaled);
float noiseStep = Random.Range(0f, 1f);
float noiseVal = noiseMap[x, y];
if
(
noiseStep < Density &&
noiseVal < IslandsSize &&
steepness < MaxSteepness &&
height > MinLevel &&
height < MaxLevel
)
{
treePos.Add(new Vector3(xScaled, heightScaled, yScaled));
}
}
}
TreeInstance[] treeInstances = new TreeInstance[treePos.Count];
for (int ii = 0; ii < treeInstances.Length; ii++)
{
treeInstances[ii].position = treePos[ii];
treeInstances[ii].prototypeIndex = Random.Range(0, treePrototypes.Count);
treeInstances[ii].color = new Color(Random.Range(100, 255), Random.Range(100, 255), Random.Range(100, 255));
treeInstances[ii].lightmapColor = Color.white;
treeInstances[ii].heightScale = 1.0f + Random.Range(-0.25f, 0.5f);
treeInstances[ii].widthScale = 1.0f + Random.Range(-0.5f, 0.25f);
}
terrainData.treeInstances = treeInstances;
Debug.Log(treeInstances.Length + " trees were created");
}
public void Clear()
{
Terrain.activeTerrain.terrainData.treePrototypes = null;
}
}
}
I appreciate any help
Comment
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Illuminating a 3D object's edges OnMouseOver (script in c#)? 1 Answer
Terrain Generator Producing Completely Flat Results. 1 Answer
Flip over an object (smooth transition) 3 Answers