tumult icon indicating copy to clipboard operation
tumult copied to clipboard

TypeError: grad3(...) is undefined while using Perlin3.

Open nrayamajhee opened this issue 5 years ago • 2 comments

I am using this library from npm and when I run the perlin3 function with -1 as the z parameter, I keep getting this error.

const perlin = new tumult.Perlin3(Math.random());
console.log(perlin.get(0,0,-1));
TypeError: grad3(...) is undefined

nrayamajhee avatar Apr 06 '19 00:04 nrayamajhee

I'm kinda low on bandwidth at the moment; it's currently exam season for me.

I might be able to fix the bug after this weekend.

This particular issue is caused by Math.trunc(x) % 256 returning a negative number if x is negative. I use the result to deference a lookup array, which then returns undefined. I'll use x & 255 instead.

I've experimented and noticed other odd bugs, such as perlin.gen(0, 0, 512) returning numbers outside the [-1, 1] bound. This is why I'm holding back on a "hot fix"; I want to fix it properly when I get around to it.

Dunno what you're using the library for, but I know the library works well for positive fractional input

Eg.

const tumult = require('tumult')
const perlin = new tumult.Perlin3(Math.random())

for (let i = 0; i < 255; i++) {
  for (let j = 0; j < 255; j++) {
    for (let k = 0; k < 255; k++) {
      console.log(perlin.gen(i / 255, j / 255, k / 255))
    }
  }
}

I've created NodeJS bindings for a C++ noise library which you may want to use as an alternative: https://www.npmjs.com/package/fastnoisejs

philipjscott avatar Apr 06 '19 10:04 philipjscott

Perlin 3 is still broken, it actually doesn't work at all for me. Throws an error Cannot read properties of undefined (reading 'dot'). grad3 is supposed to return an object that has a dot function and it returns undefined. All the noise is actually broken in this library sadly, it just returns 1 past a certain fairly low input (not sure the exact value). I'm not passing any negative numbers in.

scott-cornwell avatar Jun 01 '23 17:06 scott-cornwell