载入中...
搜索中...
未找到
PotteryTextures.cpp
浏览该文件的文档.
2
3#include <algorithm>
4#include <cmath>
5#include <utility>
6
7namespace eve::procgen {
8namespace {
9constexpr float tau = 6.283185307179586f;
10enum class Finish { Clay, Glaze, Porcelain, Celadon, Stoneware };
11
12NoiseField layer(const NoiseField& source, int frequency, uint32_t stream) {
13 auto noise = source;
14 noise.seed ^= stream;
15 // Scale the lattice period with frequency, preserving the complete UV tile
16 // instead of repeating a tiny noise cell many times around the vessel.
17 noise.periodX *= frequency;
18 noise.periodY *= frequency;
19 return noise;
20}
21float grain(const NoiseField& source, float u, float v, int frequency, uint32_t stream) {
22 const auto noise = layer(source, frequency, stream);
23 return noise.valueNoise(u * frequency, v * frequency);
24}
25float smooth(float low, float high, float x) {
26 const float t = std::clamp((x - low) / (high - low), 0.f, 1.f);
27 return t * t * (3.f - 2.f * t);
28}
29struct Cells {
30 float pore = 0.f;
31 float crack = 0.f;
32};
33Cells cells(const NoiseField& source, float u, float v, int frequency) {
34 const auto noise = layer(source, frequency, 0xa511e9b3u);
35 const float x = u * frequency, y = v * frequency;
36 const int ix = int(std::floor(x)), iy = int(std::floor(y));
37 float first = 100.f, second = 100.f, pore = 0.f;
38 for (int j = -1; j <= 1; ++j) {
39 for (int i = -1; i <= 1; ++i) {
40 const int cx = ix + i, cy = iy + j;
41 const float sx = cx + 0.2f + 0.6f * noise.hash01(cx, cy);
42 const auto offset = layer(source, frequency, 0x63d83595u);
43 const float sy = cy + 0.2f + 0.6f * offset.hash01(cx, cy);
44 const float distance = std::hypot(x - sx, y - sy);
45 if (distance < first) {
46 second = first;
48 } else
49 second = std::min(second, distance);
50 const auto pits = layer(source, frequency, 0xb5297a4du);
51 const float selection = pits.hash01(cx, cy);
52 if (selection > 0.58f) {
53 const float radius = 0.07f + 0.10f * selection;
54 pore = std::max(pore, 1.f - smooth(radius * 0.25f, radius, distance));
55 }
56 }
57 }
58 return {pore, 1.f - smooth(0.012f, 0.055f, second - first)};
59}
60float surfaceHeight(Finish finish, float u, float v, const NoiseField& noise) {
61 const float broad = 0.65f * grain(noise, u, v, 1, 0x9e3779b9u) + 0.35f * grain(noise, u, v, 3, 0x68e31da4u);
62 const float fine = grain(noise, u, v, 24, 0x1b56c4e9u) - 0.5f;
63 const float wave = grain(noise, u, v, 2, 0x7f4a7c15u) - 0.5f;
64 const float rings = std::sin(tau * (v * 12.f + wave * 0.12f + noise.hash01(0, 0)));
65 if (finish == Finish::Celadon) {
66 const auto cell = cells(noise, u + wave * 0.035f, v + wave * 0.035f, 4);
67 return std::clamp(0.63f + 0.18f * (broad - 0.5f) + 0.045f * fine - 0.31f * cell.crack + 0.007f * rings, 0.f,
68 1.f);
69 }
70 const auto cell = cells(noise, u, v, 12);
71 switch (finish) {
72 case Finish::Clay:
73 return std::clamp(0.55f + 0.34f * (broad - 0.5f) + 0.23f * fine - 0.30f * cell.pore + 0.008f * rings, 0.f,
74 1.f);
75 case Finish::Glaze: {
76 const float flow = grain(noise, u + wave * 0.08f, v, 6, 0x94d049bbu);
77 return std::clamp(0.53f + 0.55f * (broad - 0.5f) + 0.20f * (flow - 0.5f) + 0.09f * fine -
78 0.055f * cell.pore + 0.01f * rings,
79 0.f, 1.f);
80 }
81 case Finish::Porcelain:
82 return std::clamp(0.60f + 0.12f * (broad - 0.5f) + 0.07f * fine - 0.04f * cell.pore + 0.004f * rings, 0.f,
83 1.f);
84 case Finish::Stoneware:
85 return std::clamp(0.54f + 0.55f * (broad - 0.5f) + 0.26f * fine - 0.25f * cell.pore + 0.012f * rings, 0.f,
86 1.f);
87 case Finish::Celadon: break;
88 }
89 return 0.5f;
90}
91} // namespace
92
93std::vector<TextureRecipeDef> potteryTextureDefinitions() {
94 struct Surface {
95 const char* id;
96 Finish finish;
97 Rgba8 low, high;
98 float roughLow, roughHigh, normal, relief;
99 };
100 const Surface surfaces[] = {
101 {"tex.terracotta", Finish::Clay, {74, 30, 17, 255}, {218, 137, 83, 255}, 0.95f, 0.72f, 1.3f, 0.16f},
102 {"tex.glazed_ceramic", Finish::Glaze, {8, 23, 55, 255}, {53, 135, 177, 255}, 0.28f, 0.12f, 0.7f, 0.025f},
103 {"tex.porcelain", Finish::Porcelain, {210, 211, 204, 255}, {255, 252, 240, 255}, 0.19f, 0.11f, 0.8f, 0.08f},
104 {"tex.celadon", Finish::Celadon, {67, 69, 51, 255}, {166, 201, 169, 255}, 0.40f, 0.15f, 0.9f, 0.025f},
105 {"tex.stoneware", Finish::Stoneware, {37, 22, 13, 255}, {181, 137, 87, 255}, 0.76f, 0.40f, 1.1f, 0.12f}};
106 std::vector<TextureRecipeDef> definitions;
107 for (const auto& surface : surfaces) {
109 def.id = surface.id;
110 def.albedo.add(0.f, surface.low.r, surface.low.g, surface.low.b);
111 def.albedo.add(1.f, surface.high.r, surface.high.g, surface.high.b);
112 const auto finish = surface.finish;
113 def.height = [finish](float u, float v, const NoiseField& noise) { return surfaceHeight(finish, u, v, noise); };
114 def.pbr.roughnessLow = surface.roughLow;
115 def.pbr.roughnessHigh = surface.roughHigh;
116 def.pbr.normalStrength = surface.normal;
117 def.pbr.heightStrength = surface.relief;
118 def.pbr.aoStrength = surface.finish == Finish::Clay || surface.finish == Finish::Stoneware ? 0.8f : 0.2f;
119 def.pbr.metallic = 0.f;
120 definitions.push_back(std::move(def));
121 }
122 return definitions;
123}
124} // namespace eve::procgen
double y
double x
float cx
Definition CardTypes.cpp:36
float cy
Definition CardTypes.cpp:37
float grain
std::vector< WingSurface > surfaces
float u
Definition Grass.cpp:233
float v
std::int32_t second
std::int32_t first
std::map< unsigned, Colour > definitions
size_t offset
std::string id
double radius
Definition MeleeHit.cpp:42
float distance
Texture * normal
TileLayer * layer
std::array< PixelCell, kPixelChunkSize *kPixelChunkSize > cells
float crack
float pore
float t
std::string_view source
Cell cell
float relief
Definition TreeMesh.cpp:274
std::vector< TextureRecipeDef > potteryTextureDefinitions()
Create terracotta, glazed ceramic, porcelain, celadon and stoneware CPU texture definitions.
void add(float t, uint8_t r, uint8_t g, uint8_t b, uint8_t a=255)
Adds .
Definition ColorRamp.cpp:8
Deterministic 2D noise helpers for pixel textures and masks.
Definition NoiseField.h:24
Rgba8 public API.
Definition ColorRamp.h:11
A procedural texture is described by an albedo ramp, displacement, optional independent PBR pigment f...
std::function< float(float, float, const NoiseField &)> height
Float.