Skip to content

Commit

Permalink
Add own Perlin noise generator
Browse files Browse the repository at this point in the history
  • Loading branch information
aelmiger committed Apr 16, 2024
1 parent b28143e commit 7b4a7bb
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 16 deletions.
1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ filelock==3.9.0
GitPython==3.1.29
jsonschema==4.17.3
opencv-python==4.6.0.66
perlin-numpy @ git+https://github.com/pvigier/perlin-numpy@5e26837db14042e51166eb6cad4c0df2c1907016
Pillow==9.4.0
PyYAML==6.0.1
requests==2.27.1
Expand Down
1 change: 0 additions & 1 deletion syclops/__example_assets__/example_job.syclops.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ textures:
num_textures: 2
ops:
- perlin:
res: 4
octaves: 4
- math_expression: "((x-0.5) * 100 + 65535 / 2)/65535"

Expand Down
71 changes: 60 additions & 11 deletions syclops/preprocessing/texture_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import cv2
import numpy as np
import ruamel.yaml as yaml
from perlin_numpy import generate_fractal_noise_2d, generate_perlin_noise_2d


def read_yaml(path: Path) -> dict:
Expand All @@ -15,16 +14,65 @@ def read_yaml(path: Path) -> dict:
return None


def fade(t):
return 6 * t**5 - 15 * t**4 + 10 * t**3


def lerp(a, b, t):
return a + (b - a) * t


def gradient(h, x, y):
vectors = np.array([[0, 1], [0, -1], [1, 0], [-1, 0]])
g = vectors[h % 4]
return g[:, :, 0] * x + g[:, :, 1] * y


def perlin_octave(shape, frequency, amplitude):
x = np.linspace(0, frequency, shape[1], endpoint=False)
y = np.linspace(0, frequency, shape[0], endpoint=False)
y = y.reshape(-1, 1)

grid_x, grid_y = np.meshgrid(x, y)

p = np.arange(256, dtype=int)
np.random.shuffle(p)
p = np.stack([p, p]).flatten()

xi = grid_x.astype(int)
yi = grid_y.astype(int)
xf = grid_x - xi
yf = grid_y - yi

u = fade(xf)
v = fade(yf)

n00 = gradient(p[p[xi] + yi], xf, yf)
n01 = gradient(p[p[xi] + yi + 1], xf, yf - 1)
n11 = gradient(p[p[xi + 1] + yi + 1], xf - 1, yf - 1)
n10 = gradient(p[p[xi + 1] + yi], xf - 1, yf)

x1 = lerp(n00, n10, u)
x2 = lerp(n01, n11, u)
return lerp(x1, x2, v) * amplitude


def perlin(texture: np.ndarray, config: dict, textures: dict, current_frame: int):
"""Generate Perlin Noise"""
res = config["res"]
octaves = config["octaves"]
texture = generate_fractal_noise_2d(
shape=texture.shape, res=(res, res), octaves=octaves
)
texture = (texture + 1) / 2
# Clip the texture to 0-1
texture = np.clip(texture, 0, 1)
persistence = config.get("persistence", 0.5)
lacunarity = config.get("lacunarity", 2.0)

shape = texture.shape
noise = np.zeros(shape)
frequency = 1
amplitude = 1
for _ in range(octaves):
noise += perlin_octave(shape, frequency, amplitude)
frequency *= lacunarity
amplitude *= persistence

texture[:] = np.clip(noise, -1, 1) * 0.5 + 0.5
return texture


Expand Down Expand Up @@ -167,9 +215,10 @@ def process_texture(tex_name: str, tex_dict: dict, textures: dict, current_frame
image_size = tex_dict["config"]["image_size"]
texture = np.zeros((image_size[0], image_size[1]), np.float32)

# Set numpy seed
if "seed" in tex_dict["config"]:
np.random.seed(tex_dict["config"]["seed"])
# Set numpy random seed
seed = tex_dict["config"].get("seed", None)
if seed is not None:
np.random.seed(seed)

for operation in tex_dict["ops"]:
operation_name = list(operation.keys())[0]
Expand Down
4 changes: 1 addition & 3 deletions syclops/schema/base_schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -360,11 +360,9 @@ definitions:
description: Create a Perlin noise texture
type: object
properties:
res:
type: integer
octaves:
type: integer
required: [res, octaves]
required: [octaves]
additionalProperties: false
additionalProperties: false

Expand Down

0 comments on commit 7b4a7bb

Please sign in to comment.