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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
//! Plank / siding texture generator using domain-warped anisotropic noise.
//!
//! The algorithm:
//! 1. Divide V into horizontal plank bands.
//! 2. Per band, add a deterministic phase offset to U so grain de-correlates
//! between planks.
//! 3. Sample anisotropic grain FBM (high U frequency, low V frequency) for the
//! wood-grain pattern; optionally warp it with a low-frequency FBM.
//! 4. Overlay sparse Worley knots at the scaled knot density.
//! 5. Apply a thin joint gap at plank boundaries.
use std::f64::consts::TAU;
use noise::core::worley::ReturnType;
use noise::{Fbm, MultiFractal, NoiseFn, Perlin, Worley};
use rayon::prelude::*;
use crate::{
generator::{TextureError, TextureGenerator, TextureMap, linear_to_srgb, validate_dimensions},
noise::{ToroidalNoise, normalize},
normal::{BoundaryMode, height_to_normal},
surface::lerp,
};
/// Configures the appearance of a [`PlankGenerator`].
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
pub struct PlankConfig {
/// PRNG seed for the deterministic noise pattern; different seeds give
/// statistically-different textures from otherwise-identical configs.
pub seed: u32,
/// Number of planks visible vertically.
pub plank_count: f64,
/// Grain spatial scale (controls how fine the grain lines are).
pub grain_scale: f64,
/// Gap between planks as a fraction of plank height \[0, 0.3\].
pub joint_width: f64,
/// Horizontal stagger of end-joints between adjacent planks \[0, 1\].
pub stagger: f64,
/// Worley-knot density: fraction of cells that contain a knot \[0, 1\].
pub knot_density: f64,
/// Domain-warp strength that bends grain lines \[0, 1\].
pub grain_warp: f64,
/// Light wood colour in linear RGB \[0, 1\].
pub color_wood_light: [f32; 3],
/// Dark wood colour in linear RGB \[0, 1\].
pub color_wood_dark: [f32; 3],
/// Normal-map strength.
pub normal_strength: f32,
}
impl Default for PlankConfig {
fn default() -> Self {
Self {
seed: 42,
plank_count: 5.0,
grain_scale: 12.0,
joint_width: 0.06,
stagger: 0.5,
knot_density: 0.25,
grain_warp: 0.35,
color_wood_light: [0.72, 0.52, 0.30],
color_wood_dark: [0.42, 0.26, 0.12],
normal_strength: 2.5,
}
}
}
/// Procedural wood-plank / siding texture generator.
///
/// Drives [`TextureGenerator::generate`] using a [`PlankConfig`]. Construct
/// via [`PlankGenerator::new`] and call `generate` directly, or spawn a
/// [`crate::async_gen::PendingTexture::plank`] task for non-blocking generation.
///
/// Noise objects are built in the constructor so that calling `generate`
/// multiple times (e.g. producing size variants of the same material)
/// does not repeat the initialisation cost. Worley knot noise is still
/// constructed in `generate()` because `Worley` is `!Send`.
pub struct PlankGenerator {
config: PlankConfig,
fbm_warp: Fbm<Perlin>,
grain_noise: ToroidalNoise<Fbm<Perlin>>,
}
impl PlankGenerator {
/// Create a new generator with the given configuration.
///
/// Builds the noise objects up front so that repeated
/// calls to [`generate`](TextureGenerator::generate) skip initialisation.
pub fn new(config: PlankConfig) -> Self {
let fbm_warp: Fbm<Perlin> = Fbm::new(config.seed).set_octaves(3);
let grain_fbm: Fbm<Perlin> = Fbm::new(config.seed.wrapping_add(100)).set_octaves(5);
let grain_noise = ToroidalNoise::new(grain_fbm, 1.0);
Self {
config,
fbm_warp,
grain_noise,
}
}
}
impl TextureGenerator for PlankGenerator {
fn generate(&self, width: u32, height: u32) -> Result<TextureMap, TextureError> {
validate_dimensions(width, height)?;
let c = &self.config;
// plank_count must be an integer for the grid to tile vertically.
let plank_count = c.plank_count.round();
let w = width as usize;
let h = height as usize;
let n = w * h;
// Precompute knot Worley grid (isotropic, shared across planks).
// `noise::Worley` holds an `Rc` and is `!Sync`, so each parallel row
// constructs its own instance — deterministic from the seed and only
// a few microseconds each.
let knot_grid: Vec<f64> = {
let freq = plank_count * 1.5;
let col_cos: Vec<f64> = (0..w)
.map(|x| (TAU * x as f64 / w as f64).cos() * freq)
.collect();
let col_sin: Vec<f64> = (0..w)
.map(|x| (TAU * x as f64 / w as f64).sin() * freq)
.collect();
let row_cos: Vec<f64> = (0..h)
.map(|y| (TAU * y as f64 / h as f64).cos() * freq)
.collect();
let row_sin: Vec<f64> = (0..h)
.map(|y| (TAU * y as f64 / h as f64).sin() * freq)
.collect();
let mut grid = vec![0.0f64; n];
grid.par_chunks_mut(w).enumerate().for_each(|(y, row)| {
let worley =
Worley::new(c.seed.wrapping_add(200)).set_return_type(ReturnType::Distance);
let knot_noise = ToroidalNoise::new(worley, freq);
for (x, slot) in row.iter_mut().enumerate() {
*slot =
knot_noise.get_precomputed(col_cos[x], col_sin[x], row_cos[y], row_sin[y]);
}
});
grid
};
// Grain anisotropic frequencies.
let g_freq_u = c.grain_scale;
let g_freq_v = c.grain_scale * 0.08; // very low V — long grain lines
let mut heights = vec![0.0f64; n];
let mut albedo = vec![0u8; n * 4];
let mut roughness_buf = vec![0u8; n * 4];
heights
.par_chunks_mut(w)
.zip(albedo.par_chunks_mut(w * 4))
.zip(roughness_buf.par_chunks_mut(w * 4))
.enumerate()
.for_each(|(y, ((height_row, albedo_row), orm_row))| {
let v = y as f64 / h as f64;
let v_scaled = v * plank_count;
let y_cell = v_scaled.floor() as i64;
let v_frac = v_scaled.fract();
// Per-plank de-correlation phase and stagger.
let row_phase = cell_hash(y_cell, 0, c.seed);
let stagger_phase = cell_hash(y_cell, 1, c.seed) * c.stagger;
// Joint gap at top and bottom of each plank.
let joint_half = c.joint_width * 0.5;
let in_joint = v_frac < joint_half || v_frac > 1.0 - joint_half;
// Precompute row torus coords for grain (V direction, low freq).
let v_grain = v_frac * 0.1 + row_phase * 0.3; // gently warped per plank
let g_nz = (TAU * v_grain).cos() * g_freq_v;
let g_nw = (TAU * v_grain).sin() * g_freq_v;
for (x, height_slot) in height_row.iter_mut().enumerate() {
let u = x as f64 / w as f64;
// Staggered end-joint.
let u_stagger = (u + stagger_phase).rem_euclid(1.0);
let stagger_frac = (u_stagger * 3.0).fract(); // ~3 short boards per plank
let in_end_joint = c.stagger > 0.01
&& (stagger_frac < c.joint_width * 0.5
|| stagger_frac > 1.0 - c.joint_width * 0.5);
let ai = x * 4;
if in_joint || in_end_joint {
// Joint / shadow line.
*height_slot = 0.0;
let jc = lerp3(c.color_wood_dark, [0.05, 0.03, 0.01], 0.5);
albedo_row[ai] = linear_to_srgb(jc[0]);
albedo_row[ai + 1] = linear_to_srgb(jc[1]);
albedo_row[ai + 2] = linear_to_srgb(jc[2]);
albedo_row[ai + 3] = 255;
orm_row[ai] = 255;
orm_row[ai + 1] = (0.92 * 255.0) as u8;
orm_row[ai + 2] = 0;
orm_row[ai + 3] = 255;
continue;
}
// Domain warp: low-freq FBM nudges grain coordinate.
let warp_u = self.fbm_warp.get([u * 2.0, v * 2.0]) * c.grain_warp * 0.08;
// Anisotropic grain: per-plank phase shift on U.
let u_grain = (u + row_phase * 0.7 + warp_u).rem_euclid(1.0);
let g_nx = (TAU * u_grain).cos() * g_freq_u;
let g_ny = (TAU * u_grain).sin() * g_freq_u;
let grain_raw = self.grain_noise.get_precomputed(g_nx, g_ny, g_nz, g_nw);
let grain_t = normalize(grain_raw); // [0, 1]
// Knot: Worley cell distance → circular depression.
let knot_raw = knot_grid[y * w + x];
// Invert: low distance = near knot centre = depression.
let knot_t = ((0.5 - knot_raw * 0.5) - (1.0 - c.knot_density))
.max(0.0)
.min(c.knot_density)
/ c.knot_density.max(0.01);
let knot_depression = knot_t.powi(2);
// Height: grain + knot depression.
let h_val = (grain_t * (1.0 - knot_depression * 0.6)).clamp(0.0, 1.0);
*height_slot = h_val;
// Colour: lerp light ↔ dark by grain, darken at knots.
let color_t = (grain_t as f32 - knot_depression as f32 * 0.4).clamp(0.0, 1.0);
let r = lerp(c.color_wood_dark[0], c.color_wood_light[0], color_t);
let gr = lerp(c.color_wood_dark[1], c.color_wood_light[1], color_t);
let b = lerp(c.color_wood_dark[2], c.color_wood_light[2], color_t);
albedo_row[ai] = linear_to_srgb(r);
albedo_row[ai + 1] = linear_to_srgb(gr);
albedo_row[ai + 2] = linear_to_srgb(b);
albedo_row[ai + 3] = 255;
// ORM: knots and dark grain are rougher.
let rough = 0.50 + (1.0 - color_t) * 0.35;
orm_row[ai] = 255;
orm_row[ai + 1] = (rough * 255.0).round() as u8;
orm_row[ai + 2] = 0;
orm_row[ai + 3] = 255;
}
});
let normal = height_to_normal(
&heights,
width,
height,
c.normal_strength,
BoundaryMode::Wrap,
);
Ok(TextureMap {
albedo,
normal,
roughness: roughness_buf,
width,
height,
mip_level_count: 1,
emissive: None,
})
}
}
// --- helpers ----------------------------------------------------------------
fn cell_hash(cell: i64, salt: u64, seed: u32) -> f64 {
let mut h = seed as u64;
h ^= (cell as u64).wrapping_mul(6_364_136_223_846_793_005);
h ^= salt.wrapping_mul(1_442_695_040_888_963_407);
h ^= h >> 33;
h = h.wrapping_mul(0xff51_afd7_ed55_8ccd);
h ^= h >> 33;
(h as f64) * (1.0 / u64::MAX as f64)
}
#[inline]
fn lerp3(a: [f32; 3], b: [f32; 3], t: f32) -> [f32; 3] {
[
lerp(a[0], b[0], t),
lerp(a[1], b[1], t),
lerp(a[2], b[2], t),
]
}