Estella 0.113.0
A fast 2D and 3D game engine powered by WebAssembly and ECS
Loading...
Searching...
No Matches
RippleNormals.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
3#include "RippleNormals.hpp"
4
5#include <algorithm>
6#include <cmath>
7
8namespace esengine {
9
10namespace {
11
12constexpr u32 WAVES = 96;
13constexpr f32 TAU = 6.28318530718f;
15constexpr f32 SLOPE_RMS = 0.22f;
16
17struct Wave { f32 kx, kz, slope, phase; };
18
19u32 next(u32& state) {
20 state ^= state << 13;
21 state ^= state >> 17;
22 state ^= state << 5;
23 return state;
24}
25
26f32 unit(u32& state) { return static_cast<f32>(next(state) >> 8) / 16777216.0f; }
27
30std::vector<Wave> spectrum(u32 seed) {
31 std::vector<Wave> waves;
32 waves.reserve(WAVES);
33 u32 state = seed;
34 while (waves.size() < WAVES) {
35 const i32 kx = static_cast<i32>(next(state) % 41) - 20;
36 const i32 kz = static_cast<i32>(next(state) % 41) - 20;
37 const f32 k = std::sqrt(static_cast<f32>(kx * kx + kz * kz));
38 if (k < static_cast<f32>(RIPPLE_NORMALS_LONGEST) || k > 20.0f) continue;
39 waves.push_back({static_cast<f32>(kx), static_cast<f32>(kz), std::pow(k, -0.55f), unit(state) * TAU});
40 }
41 return waves;
42}
43
44void field(u32 seed, std::vector<f32>& sx, std::vector<f32>& sz) {
45 const u32 n = RIPPLE_NORMALS_SIZE;
46 const std::vector<Wave> waves = spectrum(seed);
47 f64 power = 0.0;
48 for (u32 y = 0; y < n; ++y) {
49 for (u32 x = 0; x < n; ++x) {
50 const f32 u = static_cast<f32>(x) / static_cast<f32>(n), v = static_cast<f32>(y) / static_cast<f32>(n);
51 f32 ax = 0.0f, az = 0.0f;
52 for (const Wave& w : waves) {
53 const f32 c = std::cos(TAU * (w.kx * u + w.kz * v) + w.phase) * w.slope;
54 const f32 k = std::sqrt(w.kx * w.kx + w.kz * w.kz);
55 ax += c * w.kx / k;
56 az += c * w.kz / k;
57 }
58 sx[y * n + x] = ax;
59 sz[y * n + x] = az;
60 power += static_cast<f64>(ax) * ax + static_cast<f64>(az) * az;
61 }
62 }
63 const f32 gain = SLOPE_RMS / std::sqrt(static_cast<f32>(power / (2.0 * n * n)));
64 for (f32& v : sx) v *= gain;
65 for (f32& v : sz) v *= gain;
66}
67
68u8 pack(f32 v) { return static_cast<u8>(std::clamp(v * 0.5f + 0.5f, 0.0f, 1.0f) * 255.0f + 0.5f); }
69
70} // namespace
71
72std::vector<u8> buildRippleNormals() {
73 const u32 n = RIPPLE_NORMALS_SIZE;
74 std::vector<f32> ax(n * n), az(n * n), bx(n * n), bz(n * n);
75 field(0x9E3779B9u, ax, az);
76 field(0x7F4A7C15u, bx, bz);
77 std::vector<u8> pixels(n * n * 4);
78 for (u32 i = 0; i < n * n; ++i) {
79 const f32 la = 1.0f / std::sqrt(1.0f + ax[i] * ax[i] + az[i] * az[i]);
80 const f32 lb = 1.0f / std::sqrt(1.0f + bx[i] * bx[i] + bz[i] * bz[i]);
81 pixels[i * 4 + 0] = pack(ax[i] * la);
82 pixels[i * 4 + 1] = pack(az[i] * la);
83 pixels[i * 4 + 2] = pack(bx[i] * lb);
84 pixels[i * 4 + 3] = pack(bz[i] * lb);
85 }
86 return pixels;
87}
88
89} // namespace esengine
std::int32_t i32
Signed 32-bit integer.
Definition Types.hpp:54
constexpr u32 RIPPLE_NORMALS_SIZE
constexpr u32 RIPPLE_NORMALS_LONGEST
The longest wave in a field runs this many times across the texture, the shortest twenty: a field rea...
std::uint32_t u32
Unsigned 32-bit integer.
Definition Types.hpp:45
std::uint8_t u8
Unsigned 8-bit integer.
Definition Types.hpp:41
double f64
64-bit floating point
Definition Types.hpp:61
std::vector< u8 > buildRippleNormals()
RGBA8, RIPPLE_NORMALS_SIZE square. A texel's rg (and ba) is the xy of a unit normal whose z is up: th...
float f32
32-bit floating point
Definition Types.hpp:59