OpenSimplex noise generation function for 2D, 3D, and 4D gradient noise without patent issues or directional artifacts.
npx @tessl/cli install tessl/pypi-opensimplex@0.4.0OpenSimplex is a noise generation function that provides 2D, 3D, and 4D gradient noise without the patent issues of Simplex noise or the directional artifacts of Perlin noise. It offers both single-value and high-performance array-based operations for procedural generation, computer graphics, and scientific applications.
pip install opensimplexnumpy>=1.22>=3.8import opensimplexFor commonly used functions:
from opensimplex import seed, noise2, noise3, noise4For array operations:
from opensimplex import noise2array, noise3array, noise4arrayFor class-based API:
from opensimplex import OpenSimpleximport opensimplex
import numpy as np
# Seed the noise generator for consistent results
opensimplex.seed(1234)
# Generate 2D noise
value_2d = opensimplex.noise2(x=10.0, y=10.0)
print(f"2D noise: {value_2d}") # Output: -0.43906247097569345
# Generate 3D noise
value_3d = opensimplex.noise3(x=0.5, y=0.5, z=0.5)
print(f"3D noise: {value_3d}") # Output: 0.39504955501618155
# Generate 4D noise
value_4d = opensimplex.noise4(x=0.5, y=0.5, z=0.5, w=0.5)
print(f"4D noise: {value_4d}") # Output: 0.04520359600370195
# High-performance array operations
rng = np.random.default_rng(seed=0)
x_coords = rng.random(100)
y_coords = rng.random(100)
noise_array = opensimplex.noise2array(x_coords, y_coords)
print(f"Array shape: {noise_array.shape}") # Output: (100, 100)Control the random seed for deterministic noise generation.
def seed(seed: int = 3) -> None:
"""
Seeds the underlying permutation array using a 64-bit integer.
If no value is provided, a static default (3) will be used.
Args:
seed: Integer seed value for deterministic noise generation
"""
def random_seed() -> None:
"""
Seeds using system time in nanoseconds.
Not guaranteed to be random, use at your own risk.
"""
def get_seed() -> int:
"""
Return the value used to seed the initial state.
Returns:
Current seed as integer
"""Generate 2D OpenSimplex noise from X,Y coordinates.
def noise2(x: float, y: float) -> float:
"""
Generate 2D OpenSimplex noise from X,Y coordinates.
Args:
x: x coordinate as float
y: y coordinate as float
Returns:
Generated 2D noise as float, between -1.0 and 1.0
"""
def noise2array(x: np.ndarray, y: np.ndarray) -> np.ndarray:
"""
Generates 2D OpenSimplex noise using NumPy arrays for increased performance.
Args:
x: numpy array of x-coords
y: numpy array of y-coords
Returns:
2D numpy array of shape (y.size, x.size) with generated noise
for the supplied coordinates
"""Generate 3D OpenSimplex noise from X,Y,Z coordinates.
def noise3(x: float, y: float, z: float) -> float:
"""
Generate 3D OpenSimplex noise from X,Y,Z coordinates.
Args:
x: x coordinate as float
y: y coordinate as float
z: z coordinate as float
Returns:
Generated 3D noise as float, between -1.0 and 1.0
"""
def noise3array(x: np.ndarray, y: np.ndarray, z: np.ndarray) -> np.ndarray:
"""
Generates 3D OpenSimplex noise using NumPy arrays for increased performance.
Args:
x: numpy array of x-coords
y: numpy array of y-coords
z: numpy array of z-coords
Returns:
3D numpy array of shape (z.size, y.size, x.size) with generated
noise for the supplied coordinates
"""Generate 4D OpenSimplex noise from X,Y,Z,W coordinates.
def noise4(x: float, y: float, z: float, w: float) -> float:
"""
Generate 4D OpenSimplex noise from X,Y,Z,W coordinates.
Args:
x: x coordinate as float
y: y coordinate as float
z: z coordinate as float
w: w coordinate as float
Returns:
Generated 4D noise as float, between -1.0 and 1.0
"""
def noise4array(x: np.ndarray, y: np.ndarray, z: np.ndarray, w: np.ndarray) -> np.ndarray:
"""
Generates 4D OpenSimplex noise using NumPy arrays for increased performance.
Args:
x: numpy array of x-coords
y: numpy array of y-coords
z: numpy array of z-coords
w: numpy array of w-coords
Returns:
4D numpy array of shape (w.size, z.size, y.size, x.size) with
generated noise for the supplied coordinates
"""Backward-compatible class-based interface for multiple independent noise generators.
class OpenSimplex:
"""
OpenSimplex noise generator class.
Provided for backward compatibility - might disappear in future versions.
"""
def __init__(self, seed: int) -> None:
"""
Initialize OpenSimplex instance with specific seed.
Args:
seed: Integer seed for noise generation
"""
def get_seed(self) -> int:
"""
Return the seed value used to initialize this instance.
Returns:
Seed as integer
"""
def noise2(self, x: float, y: float) -> float:
"""Generate 2D noise for this instance."""
def noise2array(self, x: np.ndarray, y: np.ndarray) -> np.ndarray:
"""Generate 2D array noise for this instance."""
def noise3(self, x: float, y: float, z: float) -> float:
"""Generate 3D noise for this instance."""
def noise3array(self, x: np.ndarray, y: np.ndarray, z: np.ndarray) -> np.ndarray:
"""Generate 3D array noise for this instance."""
def noise4(self, x: float, y: float, z: float, w: float) -> float:
"""Generate 4D noise for this instance."""
def noise4array(self, x: np.ndarray, y: np.ndarray, z: np.ndarray, w: np.ndarray) -> np.ndarray:
"""Generate 4D array noise for this instance."""DEFAULT_SEED = 3 # Default seed value used when no seed is providedimport opensimplex
import numpy as np
import matplotlib.pyplot as plt
# Set up parameters
opensimplex.seed(42)
width, height = 256, 256
scale = 0.05
# Create coordinate arrays
x = np.linspace(0, width * scale, width)
y = np.linspace(0, height * scale, height)
# Generate 2D noise array for terrain
terrain = opensimplex.noise2array(x, y)
# Normalize to 0-1 range for display
terrain_normalized = (terrain + 1) / 2
# Display the terrain
plt.imshow(terrain_normalized, cmap='terrain', origin='lower')
plt.colorbar(label='Height')
plt.title('Generated Terrain Heightmap')
plt.show()import opensimplex
import numpy as np
# Initialize
opensimplex.seed(123)
frames = 100
resolution = 64
# Generate animated 3D noise
for frame in range(frames):
time = frame * 0.1
# Create coordinate grids
x = np.linspace(0, 4, resolution)
y = np.linspace(0, 4, resolution)
z = np.full_like(x, time) # Time as Z coordinate
# Generate noise frame
noise_frame = opensimplex.noise3array(x, y, z)
# Process frame (save, display, etc.)
print(f"Frame {frame}: noise range [{noise_frame.min():.3f}, {noise_frame.max():.3f}]")import opensimplex
# Create separate generators with different seeds
terrain_gen = opensimplex.OpenSimplex(seed=1001)
cloud_gen = opensimplex.OpenSimplex(seed=2002)
water_gen = opensimplex.OpenSimplex(seed=3003)
# Generate different types of noise independently
x, y = 10.5, 20.3
terrain_noise = terrain_gen.noise2(x, y)
cloud_noise = cloud_gen.noise2(x, y)
water_noise = water_gen.noise2(x, y)
print(f"Terrain: {terrain_noise:.3f}")
print(f"Clouds: {cloud_noise:.3f}")
print(f"Water: {water_noise:.3f}")