jeudi 9 avril 2020

How to Create a Multidimenisonal PRNG?

I am working on a procedural terrain generator, but the 3d Map is constantly morphing and changing, calling for at least 4d noise (5d if I need to make it loop). I haven't found a good perlin/simplex noise library that will work in this many dimensions, so I thought this would be a good time to learn how those algorithms work. After starting to make my own "perlin" noise, I found a large problem. I need to get a psudo random value based on the nD coordinates of that point. So far I have found solutions online that use the dot product of a single point and a vector generated by the inputs, but those became very predictable very fast (I'm not sure why). I then tried a recursive approach (below), and this worked ok, but I got some weird behavior towards the edges. Recursive 3d randomness attempt:

function Rand(seed  = 123456, deg = 1){
    let s = seed % 2147483647;
    s = s < 1 ? s + 2147483647 : s;
    while(deg > 0){
        s = s * 16807 % 2147483647;
        deg--;
    }

    return (s - 1) / 2147483646;
}
function DimRand(seed, args){
    if(args.length < 2){
        return Rand(seed, args[0]);
    }else{
        let zero = args[0];
        args.shift();
        return DimRand(Rand(seed, zero), args);
    }
}
var T = 1;
var c = document.getElementById('canvas').getContext('2d');
document.getElementById('canvas').height = innerHeight;
document.getElementById('canvas').width = innerWidth;
c.width = innerWidth;
c.height = innerHeight;
var size = 50;
function display(){
    for(let i = 0; i < 20; i ++){
        for(let j = 0; j < 20; j ++){
            var bright = DimRand(89,[i,j])*255
            c.fillStyle = `rgb(${bright},${bright},${bright})`
            c.fillRect(i*size, j*size, size, size);
        }   
    }
    T++;
}

window.onmousedown=display}

And here is the result:

Recursive approach The top row was always 1 (White), the 2d row and first column were all 0 (Black), and the 3d row was always very dark (less than ≈ 0.3)

This might just be a bug, or I might have to just deal with it, but I was wondering if there was a better approach.




Aucun commentaire:

Enregistrer un commentaire