Changeset View
Changeset View
Standalone View
Standalone View
binaries/data/mods/public/maps/random/rmgen/valueNoise2d.js
- This file was added.
/** | |||||
* @returns {Number} - Range [-1,1] | |||||
*/ | |||||
function randomNoiseHashx(x, y) { | |||||
const v = Math.sin(x * 17.19 + y * 7238) * 3452; | |||||
lyv: In shaders this is is popular because there really is no other way on a GPU without passing… | |||||
naniAuthorUnsubmitted Done Inline ActionsNotice that this is a hash, the output must be dependent of the input and always return the same value for the same input. nani: Notice that this is a hash, the output must be dependent of the input and always return the… | |||||
lyvUnsubmitted Not Done Inline ActionsI believe this hash is just a clever way to get a random value due to environment constraints. Regardless, if this is uniform white noise as advertised, it wouldn't matter. And given this is uniform noise, not sure why it is so dependant on input. lyv: I believe this hash is just a clever way to get a random value due to environment constraints. | |||||
return (v - Math.floor(v)) * 2 - 1; | |||||
} | |||||
/** | |||||
* @returns {Number} - Range [-1,1] | |||||
*/ | |||||
function randomNoiseHashy(x, y) { | |||||
const v = Math.sin(x * 7.65 + y * 2345) * 5604; | |||||
return (v - Math.floor(v)) * 2 - 1; | |||||
} | |||||
/** | |||||
* Smooth uniform distributed noise using simple bilinear interpolation. | |||||
* @param {Number} x - x coordinate. 4 units equals to 1 map tile unit. | |||||
* @param {Number} y - y coordinate. 4 units equals to 1 map tile unit. | |||||
* @returns {Number} - Range [-1,1]. | |||||
*/ | |||||
function valueNoise2d(x, y) { | |||||
const ix = Math.floor(x); | |||||
const iy = Math.floor(y); | |||||
const v1 = randomNoiseHashx(ix, iy); | |||||
const v2 = randomNoiseHashx(ix + 1, iy); | |||||
const v3 = randomNoiseHashx(ix, iy + 1); | |||||
const v4 = randomNoiseHashx(ix + 1, iy + 1); | |||||
const ux = easeCurve(x - ix); | |||||
lyvUnsubmitted Not Done Inline ActionsI might advocate for a gaussian blur here. This function is essentially generating smoothed white noise. Might even be a bit a faster. lyv: I might advocate for a gaussian blur here. This function is essentially generating smoothed… | |||||
naniAuthorUnsubmitted Done Inline ActionsIdk how gaussian blur would fit here. nani: Idk how gaussian blur would fit here. | |||||
const uy = easeCurve(y - iy); | |||||
const mix = (x, y, a) => x * (1 - a) + y * a; | |||||
const mix12 = mix(v1, v2, ux); | |||||
const mix34 = mix(v3, v4, ux); | |||||
const mix1234 = mix(mix12, mix34, uy); | |||||
return mix1234; | |||||
} |
Wildfire Games · Phabricator
In shaders this is is popular because there really is no other way on a GPU without passing some uniform, at least not to my knowledge.
If this is indeed uniformly distributed or you want this to be, what would be wrong with Math.random() which uses boost’s mersenne twister rng?
That being said, I do not think thats what you want. Might even be nicer with something like this. What comes out will have 0 mean and an sd 1. Slower though, I suppose.