SimplexPerlin3D queryable in JS

Hi! I have a question about noise that I’m not sure is in scope for this forum but here goes.

I’d like to be able to query the exact SimplexPerlin3D noise algorithm shown here in NME (like noise.getValue(x,z)) and get the value at that point as a float between 0 and 1. I know that it’s not possible to query the material from the CPU, but is there a way to run the exact same algorithm in the CPU so I have some data structure with the same data just for querying? I’d like the performance of the shader in my rendering, but i’d also like to have some way to access the data about the resulting shape, and that doesn’t have to be super performant.
Specifically:

  1. how do I get the worldPos seed so my output is guaranteed to be the same?
  2. is there already a JS version of this algorithm? if not, would it be possible for me to write one or is there some obstacle I’m not considering?

Thanks!
-George

I use this but I don’t know if the algorithm is exactly the same josephg/noisejs: Javascript 2D Perlin & Simplex noise functions (github.com)

What about generating a procedural texture by using the SimplexPerlin3D block and applying this texture on your mesh instead? You could then read back this texture into the CPU to get the values you want.

I don’t think you will be able to do things the way you would like, worldPos is coming from the geometry (those are the vertices of your geometry), and is interpolated by your GPU for each pixel. You would need to implement this same interpolation and in exactly the same way to be sure to get the exact same values. Also, the computations are probably not done in 64 bits by the GPU, which is the precision you would have in javascript because a number is 64 bits. So you would end up with numbers being different just because of that.

yeah that could work! how do i read the texture back into the CPU?

Texture.readPixels will do it.

will check it out, thanks!

Ok so I’m trying to get pixel data out a procedural texture but all I’m getting is a bunch of 0s.

https://playground.babylonjs.com/#K9GLE6#50

What am I doing wrong? I’ve also tried reading the buffer into an array but it’s still all 0s

The texture is all black at start, try to get it a bit later:

https://playground.babylonjs.com/#K9GLE6#52

thanks!

i’m still hitting issues though:

https://playground.babylonjs.com/#L8IJYI#6

this is closer to what i actually want to do where i get a node material’s perlin values, but it’s just returning a (51, 51, 76, 255) on repeat.

do you know what i’m doing wrong?

Is it the right PG? You simply dump the buffer as in my PG above.

yeah it’s the right PG–my issue is the dumped buffer doesn’t appear to have any relation to the perlin noise. every pixel in the dumped buffer has rgba value (51, 51, 76, 255) whereas the noise has variation and is grayscale so 51,51,76 appears nowhere on it.

That’s because you are using the mesh position in your shader but when generating a procedural texture with nodeMaterial.createProceduralTexture(256) there’s no mesh rendering taking place. So mesh position is (0,0,0) during the texture generation.

Your node material will only work when applied to a mesh, not to create a procedural texture.

ahh okay that makes sense. how do i generate the procedural texture using the block as you suggest here?

What about generating a procedural texture by using the SimplexPerlin3D block and applying this texture on your mesh instead? You could then read back this texture into the CPU to get the values you want.

is there a way to pass the mesh in when i call createProceduralTexture?

No, you can’t pass the mesh in createProceduralTexture. You should create a procedure texture in the NME without using the mesh data, only screen.position, something like:

https://nme.babylonjs.com/#2EHBK9

@george Here is the demo how to calc perlin noise in texture (ProceduralTexture) and read values from it with readPixels and then use it in your JS code

https://playground.babylonjs.com/#3SSUAB#2

I hope it should help you

awesome massive thanks both! will give it a shot