-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTexture.cpp
More file actions
78 lines (65 loc) · 2.39 KB
/
Texture.cpp
File metadata and controls
78 lines (65 loc) · 2.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include "Texture.h"
Color PerlinNoise::v(const Vec2 &uv, const Vec3 &p) const {
Vec3 P{ (p + offset) * scale };
// Fractal, or turbulence
double color{ plainPerlin(P) };
for (int i = 1; i <= octaves; ++i) {
color += pow(roughness, i) * plainPerlin(P * lacunarity * i);
}
// Fit to [-1, 1]
// 2.5: ¾ÑéÖµ
color *= normalizeFactor * 2.5;
// Clamp
color = color < -1.0 ? -1.0 : (color <= 1.0 ? color : 1.0);
if (fold) {
color = color < 0.0 ? -color : color;
return Color(color);
} else return Color(color * 0.5 + 0.5);
}
double PerlinNoise::plainPerlin(const Vec3 &p) const {
/*
Corner notation inside specific int lattice
¡ü y
|
|
E--------F
/| /|
/ | / |
H--------G |
| A-----|--B--------¡ú x
| / | /
|/ |/
D--------C
/
¡ý z
*/
// 8 lattice corners
Vec3 A{ p.vecFloor() };
Vec3 B{ A + Vec3(1.0, 0.0, 0.0) };
Vec3 C{ A + Vec3(1.0, 0.0, 1.0) };
Vec3 D{ A + Vec3(0.0, 0.0, 1.0) };
Vec3 E{ A + Vec3(0.0, 1.0, 0.0) };
Vec3 F{ A + Vec3(1.0, 1.0, 0.0) };
Vec3 G{ A + Vec3(1.0, 1.0, 1.0) };
Vec3 H{ A + Vec3(0.0, 1.0, 1.0) };
// dotX = XP * gradiant
double dotA{ (p - A) * randomGradiant(A) };
double dotB{ (p - B) * randomGradiant(B) };
double dotC{ (p - C) * randomGradiant(C) };
double dotD{ (p - D) * randomGradiant(D) };
double dotE{ (p - E) * randomGradiant(E) };
double dotF{ (p - F) * randomGradiant(F) };
double dotG{ (p - G) * randomGradiant(G) };
double dotH{ (p - H) * randomGradiant(H) };
// lattice uvw
double ul{ p.x - A.x }, vl{ p.y - A.y }, wl{ p.z - A.z };
double interpolatedYA{ smoothstep(dotA, dotE, vl) };
double interpolatedYB{ smoothstep(dotB, dotF, vl) };
double interpolatedYC{ smoothstep(dotC, dotG, vl) };
double interpolatedYD{ smoothstep(dotD, dotH, vl) };
double interpolatedZAD{ smoothstep(interpolatedYA, interpolatedYD, wl) };
double interpolatedZBC{ smoothstep(interpolatedYB, interpolatedYC, wl) };
double final{ smoothstep(interpolatedZAD, interpolatedZBC, ul) };
// final : [-sqrt(2)/2, -sqrt(2)/2], fit to [-1, 1]
return final * 1.414;
}