Using Perlin Noise to Generate "Objects" On Terrain

What do you want to achieve?
For my Black Forest game, I’m planning on making a large map with different terrain elevation, and I’d like to know how to use noise to generate something like Trees across the whole map. (Dense)

What is the issue?
The thing is, I don’t know how to do it, and my only concern is how would it generate across different elevations? Would the trees sink into the ground or float?

What solutions have you tried so far?
I’ve tried looking into the dev forum, but all I got was how to generate TERRAIN with perlin noise, but decoration. I’ve searched other places, found nothing.

Additional details
This is the map I’m trying to generate on. It’s larger, but I’d like to make a map with terrain and have it generate the decor itself.

Help would be appriciated, and this would be a HUGE help to my game! :slight_smile:

Raycasting might be something worth looking into!
It gives you the normal vector and the position where it intercepts the terrain.

And then…

Give the math.noise function your X and Z values and scale it to a size of your choosing.
(Multiple X and Z for more sparse placements).

Do you mean the X and Z values being the area in which I’d like to generate in? And,

Could you tell me how to do this? I’ve never done it before (except from a tutorial), so now this makes me curious! :smiley: Cause for example, I’d like to generate it to look like what it does in the picture:

Yes.

Here’s a snippet:

local raycastResult = workspace:Raycast(position, Vector3.yAxis * -1000)

raycastResult would be nil if it doesn’t hit anything.
If it does hit something, you can check its properties:

local hitPos = raycastResult.Position
local normal = raycastResult.Normal

Whatever X and Z axis you use should be from that position variable in the code snippet.

While the documentation says it simply accepts a Vector3 value in the second parameter, it’s basically asking you for a laser and how long it should be.

So, Vector3.yAxis would be the direction and -1000 would be the length (downwards).

You’ll probably also want to add in a RaycastParams into the third parameter of the :Raycast method so the trees don’t spawn on top of each other.

Thank you. Now, my other questions would be, first, how would I prevent visuals of for example, the provided decoration floating a tiny bit over the terrain?

Secondly, how would I give it a way to choose a random decoration to put down, like: A random ROCK, or a random TREE, a random BUSH, or one of these three: Log, Bush, or Normal Bush?

Third of all, how would I make it so when it places it, it sets a random size (x,y,z being all the same) and prevents “offset” or floating, like mentioned in the first question?

And, can you explain that for me please?

You mean like this?

I’d recommend to also use a different perlin noise (the math.noise function but your X or Z value offset by some constant) and then compare it to some value (so if between 0 and 0,.2 should be a tree, etc. This way, trees are grouped together, bushes are grouped together.)

For rocks and stuff, probably just a math.random() would do. So maybe a trailing else statement if a tree or bush wasn’t chosen.

So you’d want to iterate through the X and Z axis across your entire terrain. That’s where you get your position from.

There’s an edge case if your terrain gets too high, which could result in the raycast starting under the terrain and result in no trees spawning.

Yes, exactly what I mean.
And, how would I iterate through the X and Z axis across my entire terrain, so I could get my position, and where would I put it?

local position --[[Y position : 10.031]]
local noise = math.noise()

local raycastResult = game.Workspace:Raycast(position, Vector3.yAxis * -1250)
local hitPos = raycastResult.Position
local normal = raycastResult.Normal	

A for loop!

So:

for x = startingX, endX, step do
    for z = startingZ, endZ, step do
        -- blah blah
        local position = Vector3.new(x, YOUR_Y_VALUE_HERE, z)
        -- etc, etc.
    end
end

Now, before I try to type an example of what you mean, what would step be?

Probably a decently large value - 5 or so?

As for the tree placement thing, the first thing that pops into my mind is doing additional raycasts slightly offset from the centre (the offset would be the radius of the tree trunk).

But that’s double the work for each tree. Gimme a 'mo to think of something better.

Would this be what you mean, hopefully?

local position
local startingX, endX = -45.093, 82.651
local startingZ, endZ = -23.093, 99.062
local step = 5

for x = startingX, endX, step do
	for z = startingZ, endZ, step do
		-- blah blah
		position = Vector3.new(x, 10.031, z)
		-- etc, etc.
	end
end
local noise = math.noise()

local raycastResult = game.Workspace:Raycast(position, Vector3.yAxis * -1250)
local hitPos = raycastResult.Position
local normal = raycastResult.Normal	

You’ll need to place all that code at the bottom inside the for loop, otherwise you’re just… placing one tree.

Since you mentioned putting that code to the very bottom of the for loop, I came up with this:

local position
local startingX, endX = -45.093, 82.651
local startingZ, endZ = -23.093, 99.062
local step = 5

for x = startingX, endX, step do
	for z = startingZ, endZ, step do
		
		-- Set the position to the current coordinates
		-- that have been gathered from startingX to endX and from
		-- startingZ to endZ, with a step of 5.
		position = Vector3.new(x, 10.031, z)
		local noise = math.noise(18.381, 10.031, 37.164)

		local raycastResult = game.Workspace:Raycast(position, Vector3.yAxis * -1250)
		local hitPos = raycastResult.Position
		local normal = raycastResult.Normal	
	end
end

Now how would I place the tree?

Here’s a visualisation of what I meant:

The centre red ray is the raycast that you’ve already got in your code.

Wait, this is confusing:


I have this, which is basically flat terrain. What would I set it to then?

Set what? The yAxis? Something decently high (100 studs?)

what would I change to 100 studs?

		position = Vector3.new(x, 10.031, z)
		local noise = math.noise(18.381, 10.031, 37.164)

		local raycastResult = game.Workspace:Raycast(position, Vector3.yAxis * -1250)
		local hitPos = raycastResult.Position
		local normal = raycastResult.Normal