./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
35 / 35
100.0%
Branches:
-
-%
Functions:
5 / 5
100.0%

include/terraingen/noise/value.h
Line Branch Exec Source
1 // SPDX-License-Identifier: BSD-3-Clause
2
3 /// @file
4 /// @brief Value noise in 2D and 3D.
5 #pragma once
6
7 #include "terraingen/noise/hash.h"
8 #include "terraingen/noise/lattice.h"
9
10 namespace terraingen {
11
12 namespace detail {
13
14 /// The value in [-1, 1) that value noise gives the lattice point of `pcg3d` key `key`.
15 121060912 inline float LatticeValue(UVec3 key) {
16 121060912 return 2.0f * HashToUnitFloat(Pcg3dHash(key).y) - 1.0f;
17 }
18
19 /// The value in [-1, 1) of one corner of `point`'s cell in 3D value noise.
20 /// @param corner The corner as bits: 1 for +x, 2 for +y and 4 for +z.
21 /// @param seedKey `PcgHash` of the noise seed.
22 80968 inline float ValueCorner3(LatticePoint3 point, uint32_t corner, uint32_t seedKey) {
23 80968 return LatticeValue(ValueLatticeKey3(uint32_t(point.ix) + (corner & 1u),
24 80968 uint32_t(point.iy) + ((corner >> 1u) & 1u),
25 80968 uint32_t(point.iz) + (corner >> 2u), seedKey));
26 }
27
28 } // namespace detail
29
30 /// 2D value noise at a lattice cell plus fraction: random lattice values blended with the
31 /// quintic fade.
32 /// @param fx Fraction in x, in [0, 1).
33 /// @param fy Fraction in y, in [0, 1).
34 /// @return Noise in [-1, 1].
35 30244987 inline float ValueNoise2(int32_t ix, int32_t iy, float fx, float fy, uint32_t seed) {
36 30244987 const uint32_t x0 = uint32_t(ix);
37 30244987 const uint32_t y0 = uint32_t(iy);
38 30244987 const float v00 = detail::LatticeValue(detail::SeededLatticeKey2(x0, y0, seed));
39 30244987 const float v10 = detail::LatticeValue(detail::SeededLatticeKey2(x0 + 1u, y0, seed));
40 30244987 const float v01 = detail::LatticeValue(detail::SeededLatticeKey2(x0, y0 + 1u, seed));
41 30244987 const float v11 = detail::LatticeValue(detail::SeededLatticeKey2(x0 + 1u, y0 + 1u, seed));
42 30244987 const float u = detail::PerlinFade(fx);
43 30244987 const float v = detail::PerlinFade(fy);
44 30244987 return detail::Lerp(v, detail::Lerp(u, v00, v10), detail::Lerp(u, v01, v11));
45 }
46
47 /// 2D value noise at a point in lattice units.
48 /// @return Noise in [-1, 1].
49 30177954 inline float ValueNoise2(float x, float y, uint32_t seed) {
50 30177954 const LatticePoint2 point = SplitLattice2(x, y);
51 30177954 return ValueNoise2(point.ix, point.iy, point.fx, point.fy, seed);
52 }
53
54 /// 3D value noise at a lattice cell and fraction, blended trilinearly with the quintic
55 /// fade.
56 /// @return Noise in [-1, 1].
57 10121 inline float ValueNoise3(LatticePoint3 point, uint32_t seed) {
58 10121 const uint32_t key = PcgHash(seed);
59 10121 const float v000 = detail::ValueCorner3(point, 0u, key);
60 10121 const float v100 = detail::ValueCorner3(point, 1u, key);
61 10121 const float v010 = detail::ValueCorner3(point, 2u, key);
62 10121 const float v110 = detail::ValueCorner3(point, 3u, key);
63 10121 const float v001 = detail::ValueCorner3(point, 4u, key);
64 10121 const float v101 = detail::ValueCorner3(point, 5u, key);
65 10121 const float v011 = detail::ValueCorner3(point, 6u, key);
66 10121 const float v111 = detail::ValueCorner3(point, 7u, key);
67 10121 const float u = detail::PerlinFade(point.fx);
68 10121 const float v = detail::PerlinFade(point.fy);
69 10121 const float w = detail::PerlinFade(point.fz);
70 10121 const float near = detail::Lerp(v, detail::Lerp(u, v000, v100), detail::Lerp(u, v010, v110));
71 10121 const float far = detail::Lerp(v, detail::Lerp(u, v001, v101), detail::Lerp(u, v011, v111));
72 10121 return detail::Lerp(w, near, far);
73 }
74
75 } // namespace terraingen
76