Page MenuHomeWildfire Games

Add 2d value noise for rmgen.
Needs ReviewPublic

Authored by nani on Dec 6 2019, 4:25 PM.

Details

Reviewers
FeXoR
Summary

Needed for dune generation.
Advantages over Noise.js implementation:

  • No need to precompute or set a frequency.
  • Guarantees a uniform range of [-1,1].
  • Faster.
Test Plan

Check output correctness.

Diff Detail

Repository
rP 0 A.D. Public Repository
Branch
/ps/trunk
Lint
Lint OK
Unit
No Unit Test Coverage
Build Status
Buildable 10201
Build 17315: arc lint + arc unit

Event Timeline

nani created this revision.Dec 6 2019, 4:25 PM
Owners added a subscriber: Restricted Owners Package.Dec 6 2019, 4:25 PM
FeXoR added a reviewer: FeXoR.EditedDec 7 2019, 9:50 PM
FeXoR added a subscriber: FeXoR.

I'm a bit confused about the seemingly random numbers in randomNoiseHashx and randomNoiseHashx.
(Is there a particular reason to chose them like this? Would it be useful to make them optional arguments?)

Otherwise looks good.
How would I "check output correctness"? Generate a map with the heightmap set to those values?
EDIT: Or do you mean just testing the dune test map with the changed patches?

nani added a comment.Dec 9 2019, 11:07 PM
In D2454#102824, @FeXoR wrote:

I'm a bit confused about the seemingly random numbers in randomNoiseHashx and randomNoiseHashx.
(Is there a particular reason to chose them like this? Would it be useful to make them optional arguments?)

The numbers are indeed "random" but they have a meaning. The idea behind is to make the sin wave have a frequency as high as possible and then multiplying that sin output by some large number then use the decimal part of that number (this is a quite known way to make noise in glsl shaders :) ) Some combinations of numbers give visible patterns and some others not because of aliasing, floating point precision, etc. I first did the testing in https://www.shadertoy.com/new to find the numbers that give these good enough result.
These hash' probability distribution output should be uniform by my rough tests.

Otherwise looks good.
How would I "check output correctness"? Generate a map with the heightmap set to those values?
EDIT: Or do you mean just testing the dune test map with the changed patches?

A way would be to check the output by logging the result for random points and seeing it always returns in the range [-1,1] and uniformly smooth random way.
Another would be to plot it in some way (I used https://www.shadertoy.com/new).
Another would be using it directly on the map terrain or something.

Take in consideration that the "randomness" hash in randomNoiseHashx doesn't expect input values very large or very small but in the range of 0.1 to lets say 5000.

smiley added a subscriber: smiley.Dec 10 2019, 6:42 AM
smiley added inline comments.
binaries/data/mods/public/maps/random/rmgen/valueNoise2d.js
5

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.

let v = Math.cos(2*Math.Pi * Math.random());
return Math.sqrt(-2*Math.log(Math.random()) * v;
33

I might advocate for a gaussian blur here. This function is essentially generating smoothed white noise.

Might even be a bit a faster.

nani marked 2 inline comments as done.Dec 10 2019, 7:36 AM
nani added inline comments.
binaries/data/mods/public/maps/random/rmgen/valueNoise2d.js
5

Notice that this is a hash, the output must be dependent of the input and always return the same value for the same input.

33

Idk how gaussian blur would fit here.

smiley added a comment.EditedDec 10 2019, 8:53 AM

https://smiley3.github.io/ The darker one is what I came up with.

It's pretty much the same. Except that yours is always the exact same output. Good enough noise for shaders, but I really do not think we should limit ourselves in this environment and tbh identical output is a really big one (no matter how hard one ctrl-r, the top image just won't change :p ). If the current way is deemed acceptable, at least make it O(1), you can easily store all the values computed for a map of the size we support.

Granted, my solution requires computing all the values in the *specific query* even if some would be discarded, but that is only if you need smoothing. That implementation is pretty cheap though.

If you can make the case for this instead of fully random noise generation, I would be open to change.

binaries/data/mods/public/maps/random/rmgen/valueNoise2d.js
5

I 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.