Perli noise - hashing function doesn't return the same vector

Viewed 31

I'm trying to implement perlin noise using permutation table and hashing function.

The hashing function should return same vector for a grid point, but it seems that it's not doing it.

I've made some test with a small permutation table P = [0, 3, 1, 2] where the values 0 to 3 are then used to assaign a vector

def get_constant_vector(p):
    # p is the value from the permutation table
    h = p % 3 # p & 3
    
    if h == 0:
        return [1.0, 1.0]
    elif h == 1:
        return [-1.0, 1.0]
    elif h == 2:
        return [1.0, -1.0]
    else:
        return [-1.0, -1.0]

so if the value from the table is the same and the vector will be the same. This is how I determine the value from the permutation table for each corner of the bounding box around a point.

P = [0, 3, 1, 2]
P = P * 42 # to absolutely prevent index out of range

for x in range(5):
    x = x + 0.5
    for y in range(5):
        y = y + 0.5

        X = int(x) % 4
        Y = int(y) % 4
        
        value_TL = P[P[X] + Y + 1] # top left corner
        value_TR = P[P[X + 1] + Y + 1] # top right corner
        value_BR = P[P[X + 1] + Y] # bottom right corner
        value_BL = P[P[X] + Y] # bottom left corner
        
        print(f"x={x}, y={y}, BL = {value_BL}, BR = {value_BR}, TL = {value_TL}, TR = {value_TR}")

This produces the following:

x=1.5, y=1.5, BL = 0, BR = 1, TL = 3, TR = 2    vector here is BR   1
x=1.5, y=2.5, BL = 3, BR = 2, TL = 1, TR = 0    vector here is TR   0
x=2.5, y=1.5, BL = 1, BR = 2, TL = 2, TR = 0    vector here is BL   1
x=2.5, y=2.5, BL = 2, BR = 0, TL = 0, TR = 3    vector here is TL   0

They suppose to be the same, but they're not.

Where am I wrong in my assumptions?

0 Answers
Related