Thoughts on my 3D perlin noise script?

I made a script that generates a 3D perlin noise map / cube. Here is how it looks like:

Here is the script:

local MapSize = 64
math.randomseed(tick())
local Seed = math.random(-1000000000, 1000000000) % math.random(-1000000000, 1000000000)
math.randomseed(Seed)
local Seed = math.random(-1000000000, 1000000000) % math.random(-1000000000, 1000000000)
local NoiseScale = 10
local Amp = 20
local BlockSize = 2
local MaxDensity = -14 -- Must be a  Number between -10, 10

-- -969535336

for x = 0, MapSize do
	for z = 0, MapSize do
		for y = 0, MapSize do
			local Xnoise = math.noise(y / NoiseScale, z / NoiseScale, Seed) * Amp
			local Ynoise = math.noise(x / NoiseScale, z / NoiseScale, Seed) * Amp
			local Znoise = math.noise(x / NoiseScale, y / NoiseScale, Seed) * Amp
			
			local Density = Xnoise + Ynoise + Znoise
			
			if Density < MaxDensity then
				local Part = Instance.new("Part", workspace)
				
				Part.Anchored = true
		        Part.Reflectance = 1		
				Part.Size = Vector3.new(BlockSize, BlockSize, BlockSize)
				
				Part.CFrame = CFrame.new(x*BlockSize, y*BlockSize, z*BlockSize)
			end
		end
	end
	wait()
end

One thing you can do is go to line 20 (Where the local density variable is), then you add + y to the equation. This will give you a result similar to this:

I was wondering if someone has feedback on this.

5 Likes

There is no need to pass arguments to your math.random because it will generate a random number with lots of decimal points anyway. Just multiply it by a constant if you feel results aren’t random enough. You can use ticks for seeds too.

Also, consider using the new class Random. Apparently it is more efficient, more random, and a better interface.

What is the purpose of the wait() at the end of the first for loop? This shouldn’t be in a game because it would just slow down the generation. If it’s to prevent studio crashing or to visualise it then I get it.

I’d also suggest you move to camel case for variables because it increases readability and conforms with standards but I suppose that’s just a personal preference. I used to use Pascal case for everything for about 5 years then suddenly switched a few years ago. Was 100% worth it.

Apart from that the generation algorithm looks very good.

1 Like

Do you mean just multiply the random number by a number?

It is quite obvious lol. To prevent crashing.

What is camel case? I have been using local variables the whole time I have been scripting on roblox.

Yes the algorithm is logical. We generate enough 2D noise maps with the right arguments every time then we combine them in the “Density” variable by adding them.

1 Like

Yes

There’s no reason for it to crash.

Camel case is the naming of variables likeThis, as opposed to LikeThis. More information here.

2 Likes

Well mine slows down. Especially if generating a large world at once.

1 Like

I suppose if you want it to behave that way it is fine. Good script.

2 Likes

Wouldn’t hurt to set parent last.

9 Likes

Oh big oof… how didn’t I notice this… tldr that article using the 2nd parameter of Instance.new is really slow so don’t do it

3 Likes

Pls like the post of the guy who pointed it out not me lol

2 Likes

Your generation looks weird, it feels as if you’ve inverted everything. The caves are full of blocks and the normal ground is empty.

Is this purposeful?

1 Like

Maybe mess around with the variables in the script?

It is up to you to find that out. If you find a purpose for this then use it.

1 Like

Minecraft uses 3D perlin noise to make terrain. So you could make a game similar to minecraft with this script.