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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
use super::map3base::*;
use super::math::*;
use super::*;

/// Saturates components.
pub struct Saturate {
    /// Amount (amount > 0) equals derivative at origin.
    amount: f32,
    texture: Box<dyn Texture>,
}

/// Saturates components.
impl Texture for Saturate {
    fn at(&self, point: Vec3a, frequency: Option<f32>) -> Vec3a {
        softsign(self.texture.at(point, frequency) * self.amount)
    }
    fn get_code(&self) -> String {
        format!("saturate({}, {})", self.amount, self.texture.get_code())
    }
    fn get_basis_code(&self) -> String {
        format!(
            "saturate({}, {})",
            self.amount,
            self.texture.get_basis_code()
        )
    }
}

/// Saturates components (amount > 0).
/// Amount equals derivative at origin. Amounts greater than 1 result in overdrive.
pub fn saturate(amount: f32, texture: Box<dyn Texture>) -> Box<dyn Texture> {
    assert!(amount > 0.0);
    Box::new(Saturate { amount, texture })
}

/// Reflect: applies a wavy function to texture values with an offset, which can
/// spread and reflect components.
pub struct Reflect {
    amount: f32,
    offset: Vec3a,
    texture: Box<dyn Texture>,
}

impl Texture for Reflect {
    fn at(&self, point: Vec3a, frequency: Option<f32>) -> Vec3a {
        wave(
            smooth3,
            self.offset + self.texture.at(point, frequency) * self.amount,
        )
    }
    fn get_code(&self) -> String {
        format!(
            "reflect({}, Vec3a({}, {}, {}), {})",
            self.amount,
            self.offset.x,
            self.offset.y,
            self.offset.z,
            self.texture.get_code()
        )
    }
    fn get_basis_code(&self) -> String {
        format!(
            "reflect({}, Vec3a({}, {}, {}), {})",
            self.amount,
            self.offset.x,
            self.offset.y,
            self.offset.z,
            self.texture.get_basis_code()
        )
    }
}

/// Applies a wavy function to texture values with an offset, which can
/// spread and reflect components. Amount is the scale (amount > 0),
/// roughly corresponding to number of reflections.
pub fn reflect(amount: f32, offset: Vec3a, texture: Box<dyn Texture>) -> Box<dyn Texture> {
    Box::new(Reflect {
        amount,
        offset,
        texture,
    })
}

/// Posterize: applies a smooth step function in proportion to texture value magnitude.
pub struct Posterize {
    levels: f32,
    sharpness: f32,
    texture: Box<dyn Texture>,
}

impl Texture for Posterize {
    fn at(&self, point: Vec3a, frequency: Option<f32>) -> Vec3a {
        let v = self.texture.at(point, frequency);
        let magnitude = self.levels * v.length();
        if magnitude > 0.0 {
            let base = magnitude.floor();
            let t = magnitude - base;
            let power: f32 = 1.0 + 50.0 * squared(squared(self.sharpness));
            let p = if t < 0.5 {
                0.5 * pow(2.0 * t, power)
            } else {
                1.0 - 0.5 * pow(2.0 * (1.0 - t), power)
            };
            v * ((base + p) / magnitude)
        } else {
            Vec3a::zero()
        }
    }
    fn get_code(&self) -> String {
        format!(
            "posterize({}, {}, {})",
            self.levels,
            self.sharpness,
            self.texture.get_code()
        )
    }
    fn get_basis_code(&self) -> String {
        format!(
            "posterize({}, {}, {})",
            self.levels,
            self.sharpness,
            self.texture.get_basis_code()
        )
    }
}

/// Applies a wavy function to texture values with an offset, which can
/// spread and reflect components. Amount is the scale (amount > 0),
/// roughly corresponding to number of reflections.
pub fn posterize(levels: f32, sharpness: f32, texture: Box<dyn Texture>) -> Box<dyn Texture> {
    Box::new(Posterize {
        levels,
        sharpness,
        texture,
    })
}

/// Saturates components while retaining component proportions.
pub struct Overdrive {
    /// Amount (amount > 0) equals derivative at origin.
    amount: f32,
    texture: Box<dyn Texture>,
}

impl Texture for Overdrive {
    fn at(&self, point: Vec3a, frequency: Option<f32>) -> Vec3a {
        let v = self.texture.at(point, frequency);
        // Use the 4-norm as a smooth proxy for the largest magnitude component.
        let magnitude = squared(v).length_squared();
        if magnitude > 0.0 {
            let m = sqrt(sqrt(magnitude));
            v / m * softsign(m * self.amount)
        } else {
            Vec3a::zero()
        }
    }
    fn get_code(&self) -> String {
        format!("overdrive({}, {})", self.amount, self.texture.get_code())
    }
    fn get_basis_code(&self) -> String {
        format!(
            "overdrive({}, {})",
            self.amount,
            self.texture.get_basis_code()
        )
    }
}

/// Saturates components (amount > 0).
/// Amount equals derivative at origin. Amounts greater than 1 result in overdrive.
pub fn overdrive(amount: f32, texture: Box<dyn Texture>) -> Box<dyn Texture> {
    assert!(amount > 0.0);
    Box::new(Overdrive { amount, texture })
}

/// Saturates components while retaining component proportions.
pub struct VReflect {
    /// Amount (amount > 0) equals derivative at origin.
    amount: f32,
    texture: Box<dyn Texture>,
}

impl Texture for VReflect {
    fn at(&self, point: Vec3a, frequency: Option<f32>) -> Vec3a {
        let v = self.texture.at(point, frequency);
        let m = v.length();
        if m > 0.0 {
            v * (sin(m * self.amount * f32::PI * 0.5) / m)
        } else {
            Vec3a::zero()
        }
    }
    fn get_code(&self) -> String {
        format!("vreflect({}, {})", self.amount, self.texture.get_code())
    }
    fn get_basis_code(&self) -> String {
        format!(
            "vreflect({}, {})",
            self.amount,
            self.texture.get_basis_code()
        )
    }
}

/// Saturates components (amount > 0).
/// Amount equals derivative at origin. Amounts greater than 1 result in overdrive.
pub fn vreflect(amount: f32, texture: Box<dyn Texture>) -> Box<dyn Texture> {
    assert!(amount > 0.0);
    Box::new(VReflect { amount, texture })
}

/// Rotates values of one texture with values from another texture.
pub struct Rotate {
    amount: f32,
    texture_a: Box<dyn Texture>,
    texture_b: Box<dyn Texture>,
}

impl Texture for Rotate {
    fn at(&self, point: Vec3a, frequency: Option<f32>) -> Vec3a {
        let u = self.texture_a.at(point, frequency);
        let v = self.texture_b.at(point, frequency);
        let length: f32 = u.length();
        if length > 1.0e-9 {
            let axis = u / length;
            Quat::from_axis_angle(Vec3::from(axis), self.amount * length) * v
        } else {
            Vec3a::zero()
        }
    }
    fn get_code(&self) -> String {
        format!(
            "rotate({}, {}, {})",
            self.amount,
            self.texture_a.get_code(),
            self.texture_b.get_code()
        )
    }
    fn get_basis_code(&self) -> String {
        format!(
            "rotate({}, {}, {})",
            self.amount,
            self.texture_a.get_basis_code(),
            self.texture_b.get_basis_code()
        )
    }
}

/// Amount is the maximum rotation in radians.
pub fn rotate(
    amount: f32,
    texture_a: Box<dyn Texture>,
    texture_b: Box<dyn Texture>,
) -> Box<dyn Texture> {
    assert!(amount > 0.0);
    Box::new(Rotate {
        amount,
        texture_a,
        texture_b,
    })
}

/// Mixes between two textures weighted with their vector magnitudes.
pub struct Softmix3 {
    amount: f32,
    texture_a: Box<dyn Texture>,
    texture_b: Box<dyn Texture>,
}

impl Texture for Softmix3 {
    fn at(&self, point: Vec3a, frequency: Option<f32>) -> Vec3a {
        let u = self.texture_a.at(point, frequency);
        let v = self.texture_b.at(point, frequency);
        let vw: f32 = softexp(v * self.amount).length_squared();
        let uw: f32 = softexp(u * self.amount).length_squared();
        let epsilon: f32 = 1.0e-9;
        (v * vw + u * uw) / (vw + uw + epsilon)
    }
    fn get_code(&self) -> String {
        format!(
            "softmix3({}, {}, {})",
            self.amount,
            self.texture_a.get_code(),
            self.texture_b.get_code()
        )
    }
    fn get_basis_code(&self) -> String {
        format!(
            "softmix3({}, {}, {})",
            self.amount,
            self.texture_a.get_basis_code(),
            self.texture_b.get_basis_code()
        )
    }
}

pub fn softmix3(
    amount: f32,
    texture_a: Box<dyn Texture>,
    texture_b: Box<dyn Texture>,
) -> Box<dyn Texture> {
    assert!(amount > 0.0);
    Box::new(Softmix3 {
        amount,
        texture_a,
        texture_b,
    })
}

/*
        let v0 = self.texture.at(point);
        let mut v = Vec3a(v0.x, v0.y, v0.z);
        let mut v1: Vec3a = v;
        let mut v2: Vec3a = v;
        let mut v3: Vec3a = v;
        for _i in 0 .. 12 {
            let u = Vec3a(v.x * v.x - v.y * v.y - v.z * v.z + v0.x, 2.0 * v.x * v.y + v0.y, 2.0 * (v.x - v.y) * v.z + v0.z);
            //u = Vec3a(cubed(v.x) - 3.0 * v.x * (squared(v.y) + squared(v.z)) + v0.x, -cubed(v.y) + 3.0 * v.y * squared(v.x) - v.y * squared(v.z) + v0.y, cubed(v.z) - 3.0 * v.z * squared(v.x) + v.z * squared(v.y) + v0.z);
            //u = Vec3a(v.x * v.x - v.y * v.y + v0.x, 2.0 * v.x * v.y + v0.y, 0.0);
            if u.length_squared() > 4.0 {
                let w = (u.length_squared() - 4.0).tanh();
                //return v0; // Vec3a(-1.0, -1.0, -1.0);
                return v3 * (1.0 - w) + v2 * w;
                //return v * (1.0 - w) * 0.5 + u * w * 0.5;
            }
            v3 = v2;
            v2 = v1;
            v1 = v;
            v = u;
        }
        //u * 0.5
        v3

*/

/// Displaces lookup of one texture by values from another texture.
pub struct Displace {
    amount: f32,
    texture_a: Box<dyn Texture>,
    texture_b: Box<dyn Texture>,
}

impl Texture for Displace {
    fn at(&self, point: Vec3a, frequency: Option<f32>) -> Vec3a {
        let u = self.texture_a.at(point, frequency);
        self.texture_b.at(point + u * self.amount, frequency)
    }
    fn get_code(&self) -> String {
        format!(
            "displace({}, {}, {})",
            self.amount,
            self.texture_a.get_code(),
            self.texture_b.get_code()
        )
    }
    fn get_basis_code(&self) -> String {
        format!(
            "displace({}, {}, {})",
            self.amount,
            self.texture_a.get_basis_code(),
            self.texture_b.get_basis_code()
        )
    }
}

pub fn displace(
    amount: f32,
    texture_a: Box<dyn Texture>,
    texture_b: Box<dyn Texture>,
) -> Box<dyn Texture> {
    assert!(amount > 0.0);
    Box::new(Displace {
        amount,
        texture_a,
        texture_b,
    })
}

pub struct Fractal {
    base_f: f32,
    octaves: usize,
    roughness: f32,
    displace: f32,
    texture: Box<dyn Texture>,
}

impl Texture for Fractal {
    fn at(&self, point: Vec3a, _frequency: Option<f32>) -> Vec3a {
        let mut f = self.base_f;
        let mut result = Vec3a::zero();
        let mut p = point;
        let mut w = 1.0;
        let mut total_w = 0.0;
        for _ in 0..self.octaves {
            let v = self.texture.at(p, Some(f));
            result += v * w;
            total_w += w;
            w *= self.roughness;
            p += v * self.displace / f;
            f *= 2.0;
        }
        result / total_w
    }
    fn get_code(&self) -> String {
        format!(
            "fractal({}, {}, {}, {3:.7}, {4:})",
            self.base_f,
            self.octaves,
            self.roughness,
            self.displace,
            self.texture.get_basis_code()
        )
    }
    fn get_basis_code(&self) -> String {
        self.get_code()
    }
}

pub fn fractal(
    base_f: f32,
    octaves: usize,
    roughness: f32,
    displace: f32,
    texture: Box<dyn Texture>,
) -> Box<dyn Texture> {
    Box::new(Fractal {
        texture,
        base_f,
        octaves,
        roughness,
        displace,
    })
}