jwagner / simplex-noise.js

A fast simplex noise implementation in Javascript / Typescript.
MIT License
1.61k stars 130 forks source link

Move permutation table creation to a separate class method when using own PRNG #47

Closed dmnsgn closed 2 years ago

dmnsgn commented 2 years ago

Feature request

Move the following into a SimplexNoise.init() (or other name) method: https://github.com/jwagner/simplex-noise.js/blob/37b3125a48ec3347c7a66d6e12ce2e78447ada99/simplex-noise.ts#L80-L87

Use case

When updating the random number generator, we are forced to recreate an instance of SimplexNoise. With this, we could create a single instance and reinit it if the PRNG changed:

// Use default Math.random (non predictable)
let simplex = new SimplexNoise(Math.random);

function setSeed(s) {
  // Update seed to make Math.random predictable
  seedMathRandom(s)

  // Before:
  // Recreate noise instance
  simplex = new SimplexNoise(Math.random);

  // After proposed change:
  // Reset permutation table
  simplex.init(Math.random);
}

setSeed('0')

Let me know how that sounds and if you need a PR.

jwagner commented 2 years ago

Hey Damien,

Thanks for the feature request. I don't quite understand your use case yet. A simplex noise instance contains nothing but the permutation table.

Why do you need to reinitialize and existing instance of simplex noise, why couldn't you just recreate it?

On a side note, I would strongly advice against 'monkey patching' the built in Math.random. Patching built-ins can disable optimizations and break third party code. Just pass in your own random function.

Cheers, Jonas

dmnsgn commented 2 years ago

A simplex noise instance contains nothing but the permutation table. Why do you need to reinitialize and existing instance of simplex noise, why couldn't you just recreate it?

It is true, I could recreate it, the feature request is probably a very small optimisation here eg. not having to instance the SimplexNoise class when changing the seed function (which sometimes happens often in creative-coding land). My assumption is that keeping the instance would be faster than recreate it (but it might be negligible).

Just pass in your own random function.

Yep 100% right, I am trying to change that too in our lib.

jwagner commented 2 years ago

I'm closing this issue since there aren't any more classes in 4.0.