annotate semiconginev2/old/noise.nim @ 1224:a3fa15c25026 compiletime-tests

did: cleanup, add audio, change platform-dependent structure
author sam <sam@basx.dev>
date Wed, 17 Jul 2024 22:02:11 +0700
parents 56781cc0fc7c
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
1 import hashes
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
2 import math
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
3
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
4 import ./core/vector
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
5
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
6
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
7 proc randomGradient(pos: Vec2f, seed: int32 = 0): Vec2f =
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
8 let randomAngle: float32 = TAU * (float32(int(hash((pos.x, pos.y, seed)))) / float32(high(int)))
1136
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 920
diff changeset
9 return NewVec2f(cos(randomAngle), sin(randomAngle))
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
10
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
11 proc interpolate(a: float32, b: float32, weight: float32): float32 =
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
12 # with Smootherstep
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
13 (b - a) * ((weight * (weight * 6.0 - 15.0) + 10.0) * weight * weight * weight) + a;
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
14
1138
02e1d2658ff5 did: more renaming
sam <sam@basx.dev>
parents: 1136
diff changeset
15 proc Perlin*(pos: Vec2f, seed: int32 = 0): float32 =
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
16 let
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
17 # grid coordinates around target point
1136
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 920
diff changeset
18 topleft = NewVec2f(trunc(pos.x), trunc(pos.y))
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 920
diff changeset
19 topright = topleft + NewVec2f(1, 0)
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 920
diff changeset
20 bottomleft = topleft + NewVec2f(0, 1)
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 920
diff changeset
21 bottomright = topleft + NewVec2f(1, 1)
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
22 # products for weights
1136
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 920
diff changeset
23 topleft_dot = topleft.randomGradient(seed).Dot((pos - topleft))
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 920
diff changeset
24 topright_dot = topright.randomGradient(seed).Dot((pos - topright))
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 920
diff changeset
25 bottomleft_dot = bottomleft.randomGradient(seed).Dot((pos - bottomleft))
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 920
diff changeset
26 bottomright_dot = bottomright.randomGradient(seed).Dot((pos - bottomright))
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
27 xinterpol = pos.x - topleft.x
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
28 yinterpol = pos.y - topleft.y
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
29
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
30 return interpolate(
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
31 interpolate(topleft_dot, bottomleft_dot, yinterpol),
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
32 interpolate(topright_dot, bottomright_dot, yinterpol),
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
33 xinterpol
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
34 )