Generate pseudo-random noise values for specific coodinates using the OpenSimplex 2 algorithm (smooth version / SuperSimplex).
1import numpy as np 2 3 4def setup(): 5 py5.os_noise_seed(42) 6 x, y = np.meshgrid(np.linspace(0, 5, py5.width), np.linspace(0, 5, py5.height)) 7 new_pixels = py5.remap(py5.os_noise(x, y), -1, 1, 0, 255).astype(np.uint8) 8 py5.set_np_pixels(new_pixels, bands='L')
1import numpy as np 2 3 4def setup(): 5 global x, y 6 py5.size(200, 200) 7 py5.os_noise_seed(42) 8 x, y = np.meshgrid( 9 np.linspace( 10 0, 5, py5.width), np.linspace( 11 0, 5, py5.height)) 12 13 14def draw(): 15 new_pixels = py5.remap( 16 py5.os_noise(py5.frame_count / 100, x, py5.frame_count / 100, y), -1, 1, 0, 255).astype(np.uint8) 17 py5.set_np_pixels(new_pixels, bands='L')
1def setup(): 2 py5.os_noise_seed(42) 3 py5.stroke(0, 10) 4 5 6def draw(): 7 n = py5.remap(py5.os_noise(0.2, py5.frame_count / 100), -1, 1, 0, py5.width) 8 py5.line(n, 0, n, py5.height)
1def setup(): 2 global xpos, ypos 3 py5.rect_mode(py5.CENTER) 4 py5.os_noise_seed(42) 5 xpos = py5.width / 2 6 ypos = py5.height / 2 7 8 9def draw(): 10 global xpos, ypos 11 py5.background(128) 12 xpos = (xpos + py5.os_noise(0.2, py5.frame_count / 250)) % py5.width 13 ypos = (ypos + py5.os_noise(5.8, py5.frame_count / 250)) % py5.height 14 py5.square(xpos, ypos, 25)
Generate pseudo-random noise values for specific coodinates using the OpenSimplex 2 algorithm (smooth version / SuperSimplex). Noise functions are random sequence generators that produce a more natural, harmonic succession of numbers compared to the random() method.
In contrast to the random() method, noise is defined in an n-dimensional space, in which each coordinate corresponds to a fixed pseudo-random value (fixed only for the lifespan of the program). The noise value can be animated by moving through the noise space, as demonstrated in the examples. Any dimension can also be interpreted as time. An easy way to animate the noise value is to pass the
os_noise() method the frame_count divided by a scaling factor, as is done in a few of the examples.
The generated noise values for this method will be between -1 and 1, and can be generated in 2, 3, or 4 dimensions. To generate noise in 1 dimension, add a constant value as an extra parameter, as shown in a few examples. Py5 also provides the noise() method, which generates noise using Processing’s noise algorithm. That algorithm typically generates noise values between 0 and 1, and can be generated in 1, 2, or 3 dimensions. Be aware of both of these differences when modifying your code to switch from one to the other. There are other differences in the character of the noise values generated by both methods, so you’ll need to do some experimentation to get the results you want.
The nature of the noise values returned can be adjusted with os_noise_seed().
Another way to adjust the character of the resulting sequence is the scale of the input coordinates. As the method works within an infinite space, the value of the coordinates doesn’t matter as such; only the distance between successive coordinates is important. As a general rule, the smaller the difference between coordinates, the smoother the resulting noise sequence. Steps of 0.005-0.03 work best for most applications, but this will differ depending on the use case and the noise settings.
os_noise() method can also accept numpy arrays as parameters. It will use broadcasting when needed and calculate the values efficiently. Using numpy array parameters will be much faster and efficient than calling the
os_noise() method repeatedly in a loop. See the examples to see how this can be done. The noise algorithm for this method is implemented in Java.
Noise generation is a rich and complex topic, and there are many noise algorithms and libraries available that are worth learning about. Early versions of py5 used the Python “noise” library, which can generate noise using the “Improved Perlin Noise” algorithm (as described in Ken Perlin’s 2002 SIGGRAPH paper) and the Simplex Noise algorithm (also developed by Ken Perlin). That Python library was removed from py5 because it has some bugs and hasn’t had a release in years. Nevertheless, it might be useful to you, and can be installed separately like any other Python package. You can also try the Python library “vnoise”, which is a pure Python implementation of the Improved Perlin Noise algorithm. Note that py5 can also employ Java libraries, so consider “FastNoise Lite” to experiment with a large selection of noise algorithms with efficient implementations.
os_noise(x: Union[float, npt.NDArray], y: Union[float, npt.NDArray], /) -> Union[float, npt.NDArray] os_noise(x: Union[float, npt.NDArray], y: Union[float, npt.NDArray], z: Union[float, npt.NDArray], /) -> Union[float, npt.NDArray] os_noise(x: Union[float, npt.NDArray], y: Union[float, npt.NDArray], z: Union[float, npt.NDArray], w: Union[float, npt.NDArray], /) -> Union[float, npt.NDArray]
w: Union[float, npt.NDArray] - w-coordinate in noise space
x: Union[float, npt.NDArray] - x-coordinate in noise space
y: Union[float, npt.NDArray] - y-coordinate in noise space
z: Union[float, npt.NDArray] - z-coordinate in noise space
Updated on March 22, 2022 21:53:01pm UTC