TerrainGen coverage


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

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