Question about procedural forest/grass generation
Hello,
I have been looking into this topic for quite some time now and can't really find a solotion other then randomly placing objects in a specified area using Random.Range..
What I would like to do is generate plots for trees and grass to spawn using something like peril noise but I can't figure it out. Can someone just simply explain to me how you would generate a forest using something like peril noise similar to this example:
Were all the dots are trees.
Current system I have just generates trees way to far apart and makes the world seem bare:
Answer by jonasmortensen · Aug 05, 2017 at 12:22 PM
This script generates a forest based on some noise texture. The noise texture can be though of as a "likelyness" map describing what the change of a tree spawning is at a given position. This can be controlled with the density variable. With a density of 1 the script produces this result:
The script picks a random size based on two values. This can of course be changed as well as doing the same for rotation.
using UnityEngine;
public class ForestGenerator : MonoBehaviour {
public GameObject tree;
public float minTreeSize;
public float maxTreeSize;
public Texture2D noiseImage;
public float forestSize;
public float treeDensity;
private float baseDensity = 5.0f;
// Use this for initialization
void Start () {
Generate();
}
public void Generate() {
for(int y = 0; y < forestSize; y++) {
for(int x = 0; x < forestSize; x++) {
float chance = noiseImage.GetPixel(x, y).r / (baseDensity/treeDensity);
if (ShouldPlaceTree(chance)) {
float size = Random.Range(minTreeSize, maxTreeSize);
GameObject newTree = Instantiate(tree);
newTree.transform.localScale = Vector3.one * size;
newTree.transform.position = new Vector3(x, 0, y);
newTree.transform.parent = transform;
}
}
}
}
//Returns true or false given some chance from 0 to 1
public bool ShouldPlaceTree(float chance) {
if (Random.Range(0.0f, 1.0f) <= chance) {
return true;
}
return false;
}
}
Hope it helps as inspiration ;)
The texture used in the image is a simple googled perlin noise. You could do anything else that returns some float value from 0-1 given a position.
You might want to check out Coherent Noise. Havent played with it yet but i think it rocks.
So for the size of the actual grid were the objects spawn at is that the forest size? Because I'm trying to get it to spawn within a specified area. But YES this really does help!
Because when I try to set it to the map size (196) it just crashes haha.
Yes that is the forest size.
Does it crash on lower sizes? I just tried the script on my computer on a 500 size forest. It had a terrible frame rate but did'nt crash. Try reducing the density.
Your answer
Follow this Question
Related Questions
Input Command Issues 0 Answers
Unity HUD Tracking and Target Selection 0 Answers
How do I detect UI Raycasts using the Vive controllers? 0 Answers
My gameobject stops moving after entering the trigger?? 2 Answers
Tile based river generation 0 Answers