Estella
0.113.0
A fast 2D and 3D game engine powered by WebAssembly and ECS
Loading...
Searching...
No Matches
ParticleNoise.hpp
Go to the documentation of this file.
1
// SPDX-License-Identifier: Apache-2.0
2
// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
15
#pragma once
16
17
#include "
../core/Types.hpp
"
18
#include "
../math/Math.hpp
"
19
20
#include <cmath>
21
22
namespace
esengine::particle::noise
{
23
24
// A cheap integer hash → [0,1). Self-contained (no 256-entry permutation table)
25
// so the field is fully determined by the lattice coordinate and nothing else.
26
inline
f32
hash2
(
i32
x,
i32
y) {
27
u32
h =
static_cast<
u32
>
(x) * 374761393u +
static_cast<
u32
>
(y) * 668265263u;
28
h = (h ^ (h >> 13)) * 1274126177u;
29
h ^= h >> 16;
30
return
static_cast<
f32
>
(h) * (1.0f / 4294967295.0f);
31
}
32
33
// Pseudo-random unit gradient at a lattice point (angle picked from the hash).
34
inline
glm::vec2
grad2
(
i32
x,
i32
y) {
35
f32
a =
hash2
(x, y) *
math::TWO_PI
;
36
return
glm::vec2(std::cos(a), std::sin(a));
37
}
38
39
// Perlin quintic fade — C² continuous, so the finite-difference curl below is smooth.
40
inline
f32
fade
(
f32
t) {
return
t * t * t * (t * (t * 6.0f - 15.0f) + 10.0f); }
41
42
// Classic 2D gradient (Perlin) noise, output ~[-1, 1].
43
inline
f32
perlin2
(glm::vec2 p) {
44
glm::vec2 pi = glm::floor(p);
45
glm::vec2 pf = p - pi;
46
auto
x0 =
static_cast<
i32
>
(pi.x);
47
auto
y0 =
static_cast<
i32
>
(pi.y);
48
49
f32
n00 = glm::dot(
grad2
(x0, y0), pf - glm::vec2(0.0f, 0.0f));
50
f32
n10 = glm::dot(
grad2
(x0 + 1, y0), pf - glm::vec2(1.0f, 0.0f));
51
f32
n01 = glm::dot(
grad2
(x0, y0 + 1), pf - glm::vec2(0.0f, 1.0f));
52
f32
n11 = glm::dot(
grad2
(x0 + 1, y0 + 1), pf - glm::vec2(1.0f, 1.0f));
53
54
f32
ux =
fade
(pf.x);
55
f32
uy =
fade
(pf.y);
56
f32
nx0 =
math::lerp
(n00, n10, ux);
57
f32
nx1 =
math::lerp
(n01, n11, ux);
58
return
math::lerp
(nx0, nx1, uy);
59
}
60
61
// Fractal-Brownian sum: `octaves` layers at doubling frequency / halving amplitude.
62
// octaves == 1 is plain Perlin; more octaves add the fine detail that reads as
63
// "turbulence". Normalised so the result stays ~[-1, 1] regardless of octave count.
64
inline
f32
fbm2
(glm::vec2 p,
i32
octaves) {
65
f32
sum = 0.0f, amp = 1.0f, freq = 1.0f, norm = 0.0f;
66
i32
n = octaves < 1 ? 1 : (octaves > 8 ? 8 : octaves);
67
for
(
i32
i = 0; i < n; ++i) {
68
sum += amp *
perlin2
(p * freq);
69
norm += amp;
70
freq *= 2.0f;
71
amp *= 0.5f;
72
}
73
return
norm > 0.0f ? sum / norm : 0.0f;
74
}
75
76
// Divergence-free 2D flow: curl of the fbm scalar potential via central differences.
77
// The result is a ~unit-scale velocity direction+magnitude the sim scales by the
78
// emitter's noiseStrength. `sample` is already in noise space (position × frequency,
79
// plus the scroll offset), so callers fold frequency/scroll in before calling.
80
inline
glm::vec2
curl
(glm::vec2 sample,
i32
octaves) {
81
constexpr
f32
e = 0.1f;
// finite-difference epsilon, a fraction of a lattice cell
82
f32
dpdx =
fbm2
(sample + glm::vec2(e, 0.0f), octaves) -
83
fbm2
(sample - glm::vec2(e, 0.0f), octaves);
84
f32
dpdy =
fbm2
(sample + glm::vec2(0.0f, e), octaves) -
85
fbm2
(sample - glm::vec2(0.0f, e), octaves);
86
// curl(ψ) = (∂ψ/∂y, -∂ψ/∂x); the 0.25 tames the /(2e) gain so noiseStrength
87
// reads roughly as a peak advection speed in px/s.
88
return
glm::vec2(dpdy, -dpdx) * (0.25f / (2.0f * e));
89
}
90
91
}
// namespace esengine::particle::noise
Math.hpp
Math utilities and GLM configuration for ESEngine.
Types.hpp
Core type definitions and utilities for ESEngine.
esengine::math::lerp
T lerp(const T &a, const T &b, float t)
Linear interpolation between two values.
Definition
Math.hpp:142
esengine::math::TWO_PI
constexpr float TWO_PI
Two times pi (full circle in radians).
Definition
Math.hpp:70
esengine::particle::noise
Definition
ParticleNoise.hpp:22
esengine::particle::noise::grad2
glm::vec2 grad2(i32 x, i32 y)
Definition
ParticleNoise.hpp:34
esengine::particle::noise::fade
f32 fade(f32 t)
Definition
ParticleNoise.hpp:40
esengine::particle::noise::fbm2
f32 fbm2(glm::vec2 p, i32 octaves)
Definition
ParticleNoise.hpp:64
esengine::particle::noise::curl
glm::vec2 curl(glm::vec2 sample, i32 octaves)
Definition
ParticleNoise.hpp:80
esengine::particle::noise::perlin2
f32 perlin2(glm::vec2 p)
Definition
ParticleNoise.hpp:43
esengine::particle::noise::hash2
f32 hash2(i32 x, i32 y)
Definition
ParticleNoise.hpp:26
esengine::i32
std::int32_t i32
Signed 32-bit integer.
Definition
Types.hpp:54
esengine::u32
std::uint32_t u32
Unsigned 32-bit integer.
Definition
Types.hpp:45
esengine::f32
float f32
32-bit floating point
Definition
Types.hpp:59
src
esengine
particle
ParticleNoise.hpp
Generated by
1.15.0