How to add seed to Perlin noise?

General discussion about LÖVE, Lua, game development, puns, and unicorns.
User avatar
Rishavs
Party member
Posts: 103
Joined: Sat Oct 17, 2009 5:29 am
Contact:

How to add seed to Perlin noise?

Post by Rishavs »

Hi

I am trying some terrain generation using Redblob games articles as ref.

For Perlin noise almost all resources on the net have some form of seed to change the noise output.
However the inbuilt love noise doesnt allows me to add a seed in it.

How can i add a seed value to change the noise output?
I tried adding a seed as a 3rd dimension but that made my heightmap very fuzzy.

I have tried noise (freq/seed * x, freq/seed * y). But that completely messes up my map(as i first convert the seed into a decimal between 0-1)

Code: Select all

            local merged_noise =    
                      1   * love.math.noise (1 * seed * nx, 1 * seed * ny)
                +  0.5  * love.math.noise (2 * seed * nx, 2 * seed * ny)
                + 0.25  * love.math.noise (4 * seed * nx, 4 * seed * ny)
                + 0.13  * love.math.noise (8 * seed * nx, 8 * seed * ny)
                + 0.06  * love.math.noise (16 * seed * nx, 16 * seed * ny)
                + 0.03  * love.math.noise (32 * seed * nx, 32 * seed * ny)
                
Am i am missing something?
How do you do it?

This is how redblob games ad the seed, but only because the lib allows it.
I'd love to get my output as similar to his as possible.

Code: Select all

var rng1 = PM_PRNG.create(seed1);
var rng2 = PM_PRNG.create(seed2);
var gen1 = new SimplexNoise(rng1.nextDouble.bind(rng1));
var gen2 = new SimplexNoise(rng2.nextDouble.bind(rng2));
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: How to add seed to Perlin noise?

Post by raidho36 »

There is a function for this.

https://love2d.org/wiki/love.math.setRandomSeed

But yeah you might be missing something. Seed is only of any importance if you want to generate specific pattern again. Since this random is not actually random and is perfectly deterministic, starting with specific seed results in exactly the same output every time. This is probably not what you're looking for.
User avatar
Rishavs
Party member
Posts: 103
Joined: Sat Oct 17, 2009 5:29 am
Contact:

Re: How to add seed to Perlin noise?

Post by Rishavs »

Thanks but i dont think random seed affects noise at all.
Randomseed is only for random function.

I tried using it in my algorithm but it seems to have to no effect on my map.

Can you share a snippet on how you use it?
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: How to add seed to Perlin noise?

Post by raidho36 »

Huh, I just kinda assumed it affects all random functions.

Inspecting manual page more closely, it reads as
The return value will always be the same, given the same arguments.
So it must internally use same fixed seed and you generate different noise by passing different coordinates.
User avatar
zorg
Party member
Posts: 3465
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: How to add seed to Perlin noise?

Post by zorg »

You could use a 3rd (and 4th, if you want) coordinate as seed... love.math.noise supports up to 4 params, you know :3
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
User avatar
Rishavs
Party member
Posts: 103
Joined: Sat Oct 17, 2009 5:29 am
Contact:

Re: How to add seed to Perlin noise?

Post by Rishavs »

I checked the love source. They use a c++ lib called noise1234 for the simplex noise generation. As such i dont think there is anyway the noise is affected by an internal love2d function for setRandomSeed.

@zorg: I tried it. It seems to kill my island shape and just give a fuzzier regular shape. :(

this is my elevation function:

Code: Select all


function set_pixel_gradient()
    local min_elv = 1
    local max_elv = 0
    love.math.setRandomSeed(os.time())
    local seed =  love.math.random()
    print("Seed : " .. seed)
    
    local var_a = 0.05
    local var_b = 0.5
    local var_c = 0.01
    
    for w = 0, love.graphics.getWidth(), 2 do
        for h = 0, love.graphics.getHeight(), 2 do

            
            -- local p_grad = love.math.random( )
            local nx = w / love.graphics.getWidth() - 0.5 
            local ny = h / love.graphics.getHeight() - 0.5;
            

            local p_id = w .. "x" .. h .. "y"
            
            local manhattan_dist = 2*math.max(math.abs(nx), math.abs(ny))
            
            local merged_noise =    
                      1   * love.math.noise (1 * nx, 1 * ny, seed)
                +  0.5  * love.math.noise (2 * nx, 2 * ny, seed)
                + 0.25  * love.math.noise (4 * nx, 4 * ny, seed)
                + 0.13  * love.math.noise (8 * nx, 8 * ny, seed)
                + 0.06  * love.math.noise (16 * nx, 16 * ny, seed)
                + 0.03  * love.math.noise (32 * nx, 32 * ny, seed)

                
            -- elevation = merged_noise  + var_a - (var_b * (manhattan_dist ^ var_c))
            local elevation = (merged_noise + var_a) * (1 - (var_b*manhattan_dist^var_c))
            
            local dx = 2 * w / love.graphics.getWidth() - 1
            local dy = 2 * h / love.graphics.getHeight() - 1
            -- at this point 0 <= dx <= 1 and 0 <= dy <= 1
            local d_sqr =  dx*dx + dy*dy
            
            if elevation < 0.3 + 0.4 * d_sqr then
                elevation = 0
            end
            
            pixel_gradient_obj[p_id] = {
                x = w,
                y = h,
                elevation = elevation,
                lum = math.min(round(elevation * 255 ), 255)
            }
            
            -- just debug info to find the lowest elevation
            if elevation < min_elv then
                min_elv = elevation
            end
            -- just debug info to find the highest elevation
            if elevation > max_elv then
                max_elv = elevation
            end
            
        end
    end
    print("Min Elevation: " .. min_elv)
    print("Max Elevation: " .. max_elv)
end

User avatar
zorg
Party member
Posts: 3465
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: How to add seed to Perlin noise?

Post by zorg »

Yeah well, modifying another two dimensions will, for one, use simplex instead of perlin (i think) since the latter is patented above 2 dimensions, and it will change the properities of the noise. Sorry i didn't write this before, i didn't exactly remember the word "properities" :crazy:

What you can really do is to generate your one or two seeds (in the range of [0,1) as love.math.random gives it, or maybe create your own randomgenerator object, whichever you prefer) once, and you simply add the resultant number(s) as offsets to your landscape generator's noise function. It should work better that way.

Code: Select all

local merged_noise =    
      1.00  * love.math.noise (( 1 + seed) * nx, ( 1 + seed) * ny)
    + 0.50  * love.math.noise (( 2 + seed) * nx, ( 2 + seed) * ny)
    + 0.25  * love.math.noise (( 4 + seed) * nx, ( 4 + seed) * ny)
    + 0.13  * love.math.noise (( 8 + seed) * nx, ( 8 + seed) * ny)
    + 0.06  * love.math.noise ((16 + seed) * nx, (16 + seed) * ny)
    + 0.03  * love.math.noise ((32 + seed) * nx, (32 + seed) * ny)
That said, i tried the same approach written on redblobgames to implement colored noise as audio... maybe i did it wrong, but while i do get noise, it's horrible in aural quality. :3
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
User avatar
Rishavs
Party member
Posts: 103
Joined: Sat Oct 17, 2009 5:29 am
Contact:

Re: How to add seed to Perlin noise?

Post by Rishavs »

this is perfect!
this gives me new shapes everytime. Thank you!

regarding using noise for audio, well thats kinda expected, isnt it? :3
User avatar
zorg
Party member
Posts: 3465
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: How to add seed to Perlin noise?

Post by zorg »

Rishavs wrote:this is perfect!
this gives me new shapes everytime. Thank you!

regarding using noise for audio, well thats kinda expected, isnt it? :3
Well, i meant that there are articles about colored noise on wikipedia, with example sounds.
And i found a few other algorithms for a few specific types of noise (White is easiest, pink and red being filtered from that) and they sounded similar to the samples on wikipedia.
On the other hand, redblobgames' example is "additive" in the sense that it generates noise by partials, and for one reason or another (maybe it's just me messing up the implementation), they sound wildly dissimilar to the ones i heard before, which is bad.
Noise is (pseudo)random, true, but they should have similar spectral characteristics (just as you have with the landscapes), and my implementation is off. Sad, since i still need a normal blue and violet noise implenentation.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
User avatar
Rishavs
Party member
Posts: 103
Joined: Sat Oct 17, 2009 5:29 am
Contact:

Re: How to add seed to Perlin noise?

Post by Rishavs »

oh well, you are way ahead of me on the curve. I just started playing with noise this week.
Best of luck to you and thanks again for helping me.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest