annotate semiconginev2/contrib/algorithms/noise.nim @ 1242:e8b3dc80e48e

add: PNG loading and tests for it
author sam <sam@basx.dev>
date Mon, 22 Jul 2024 15:53:32 +0700
parents c8e3037aca66
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1226
c8e3037aca66 add: contrib stuff
sam <sam@basx.dev>
parents:
diff changeset
1 proc randomGradient(pos: Vec2f, seed: int32 = 0): Vec2f =
c8e3037aca66 add: contrib stuff
sam <sam@basx.dev>
parents:
diff changeset
2 let randomAngle: float32 = TAU * (float32(int(hash((pos.x, pos.y, seed)))) / float32(high(int)))
c8e3037aca66 add: contrib stuff
sam <sam@basx.dev>
parents:
diff changeset
3 return NewVec2f(cos(randomAngle), sin(randomAngle))
c8e3037aca66 add: contrib stuff
sam <sam@basx.dev>
parents:
diff changeset
4
c8e3037aca66 add: contrib stuff
sam <sam@basx.dev>
parents:
diff changeset
5 proc interpolate(a: float32, b: float32, weight: float32): float32 =
c8e3037aca66 add: contrib stuff
sam <sam@basx.dev>
parents:
diff changeset
6 # with Smootherstep
c8e3037aca66 add: contrib stuff
sam <sam@basx.dev>
parents:
diff changeset
7 (b - a) * ((weight * (weight * 6.0 - 15.0) + 10.0) * weight * weight * weight) + a;
c8e3037aca66 add: contrib stuff
sam <sam@basx.dev>
parents:
diff changeset
8
c8e3037aca66 add: contrib stuff
sam <sam@basx.dev>
parents:
diff changeset
9 proc Perlin*(pos: Vec2f, seed: int32 = 0): float32 =
c8e3037aca66 add: contrib stuff
sam <sam@basx.dev>
parents:
diff changeset
10 let
c8e3037aca66 add: contrib stuff
sam <sam@basx.dev>
parents:
diff changeset
11 # grid coordinates around target point
c8e3037aca66 add: contrib stuff
sam <sam@basx.dev>
parents:
diff changeset
12 topleft = NewVec2f(trunc(pos.x), trunc(pos.y))
c8e3037aca66 add: contrib stuff
sam <sam@basx.dev>
parents:
diff changeset
13 topright = topleft + NewVec2f(1, 0)
c8e3037aca66 add: contrib stuff
sam <sam@basx.dev>
parents:
diff changeset
14 bottomleft = topleft + NewVec2f(0, 1)
c8e3037aca66 add: contrib stuff
sam <sam@basx.dev>
parents:
diff changeset
15 bottomright = topleft + NewVec2f(1, 1)
c8e3037aca66 add: contrib stuff
sam <sam@basx.dev>
parents:
diff changeset
16 # products for weights
c8e3037aca66 add: contrib stuff
sam <sam@basx.dev>
parents:
diff changeset
17 topleft_dot = topleft.randomGradient(seed).Dot((pos - topleft))
c8e3037aca66 add: contrib stuff
sam <sam@basx.dev>
parents:
diff changeset
18 topright_dot = topright.randomGradient(seed).Dot((pos - topright))
c8e3037aca66 add: contrib stuff
sam <sam@basx.dev>
parents:
diff changeset
19 bottomleft_dot = bottomleft.randomGradient(seed).Dot((pos - bottomleft))
c8e3037aca66 add: contrib stuff
sam <sam@basx.dev>
parents:
diff changeset
20 bottomright_dot = bottomright.randomGradient(seed).Dot((pos - bottomright))
c8e3037aca66 add: contrib stuff
sam <sam@basx.dev>
parents:
diff changeset
21 xinterpol = pos.x - topleft.x
c8e3037aca66 add: contrib stuff
sam <sam@basx.dev>
parents:
diff changeset
22 yinterpol = pos.y - topleft.y
c8e3037aca66 add: contrib stuff
sam <sam@basx.dev>
parents:
diff changeset
23
c8e3037aca66 add: contrib stuff
sam <sam@basx.dev>
parents:
diff changeset
24 return interpolate(
c8e3037aca66 add: contrib stuff
sam <sam@basx.dev>
parents:
diff changeset
25 interpolate(topleft_dot, bottomleft_dot, yinterpol),
c8e3037aca66 add: contrib stuff
sam <sam@basx.dev>
parents:
diff changeset
26 interpolate(topright_dot, bottomright_dot, yinterpol),
c8e3037aca66 add: contrib stuff
sam <sam@basx.dev>
parents:
diff changeset
27 xinterpol
c8e3037aca66 add: contrib stuff
sam <sam@basx.dev>
parents:
diff changeset
28 )