Mercurial > games > semicongine
comparison semiconginev2/old/noise.nim @ 1218:56781cc0fc7c compiletime-tests
did: renamge main package
author | sam <sam@basx.dev> |
---|---|
date | Wed, 17 Jul 2024 21:01:37 +0700 |
parents | semicongine/old/noise.nim@a3eb305bcac2 |
children |
comparison
equal
deleted
inserted
replaced
1217:f819a874058f | 1218:56781cc0fc7c |
---|---|
1 import hashes | |
2 import math | |
3 | |
4 import ./core/vector | |
5 | |
6 | |
7 proc randomGradient(pos: Vec2f, seed: int32 = 0): Vec2f = | |
8 let randomAngle: float32 = TAU * (float32(int(hash((pos.x, pos.y, seed)))) / float32(high(int))) | |
9 return NewVec2f(cos(randomAngle), sin(randomAngle)) | |
10 | |
11 proc interpolate(a: float32, b: float32, weight: float32): float32 = | |
12 # with Smootherstep | |
13 (b - a) * ((weight * (weight * 6.0 - 15.0) + 10.0) * weight * weight * weight) + a; | |
14 | |
15 proc Perlin*(pos: Vec2f, seed: int32 = 0): float32 = | |
16 let | |
17 # grid coordinates around target point | |
18 topleft = NewVec2f(trunc(pos.x), trunc(pos.y)) | |
19 topright = topleft + NewVec2f(1, 0) | |
20 bottomleft = topleft + NewVec2f(0, 1) | |
21 bottomright = topleft + NewVec2f(1, 1) | |
22 # products for weights | |
23 topleft_dot = topleft.randomGradient(seed).Dot((pos - topleft)) | |
24 topright_dot = topright.randomGradient(seed).Dot((pos - topright)) | |
25 bottomleft_dot = bottomleft.randomGradient(seed).Dot((pos - bottomleft)) | |
26 bottomright_dot = bottomright.randomGradient(seed).Dot((pos - bottomright)) | |
27 xinterpol = pos.x - topleft.x | |
28 yinterpol = pos.y - topleft.y | |
29 | |
30 return interpolate( | |
31 interpolate(topleft_dot, bottomleft_dot, yinterpol), | |
32 interpolate(topright_dot, bottomright_dot, yinterpol), | |
33 xinterpol | |
34 ) |