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
//std
use std::sync::Arc;
// others
// use time::PreciseTime;
// pbrt
use crate::core::bssrdf::compute_beam_diffusion_bssrdf;
use crate::core::bssrdf::BssrdfTable;
use crate::core::bssrdf::TabulatedBssrdf;
use crate::core::interaction::SurfaceInteraction;
use crate::core::material::{Material, TransportMode};
use crate::core::medium::get_medium_scattering_properties;
use crate::core::microfacet::{MicrofacetDistribution, TrowbridgeReitzDistribution};
use crate::core::paramset::TextureParams;
use crate::core::pbrt::{Float, Spectrum};
use crate::core::reflection::{
    Bsdf, Bxdf, Fresnel, FresnelDielectric, FresnelSpecular, MicrofacetReflection,
    MicrofacetTransmission, SpecularReflection, SpecularTransmission,
};
use crate::core::texture::Texture;

// see subsurface.h

pub struct SubsurfaceMaterial {
    pub scale: Float,                                 // default: 1.0
    pub kr: Arc<dyn Texture<Spectrum> + Sync + Send>, // default: 1.0
    pub kt: Arc<dyn Texture<Spectrum> + Sync + Send>, // default: 1.0
    pub sigma_a: Arc<dyn Texture<Spectrum> + Sync + Send>,
    pub sigma_s: Arc<dyn Texture<Spectrum> + Sync + Send>,
    pub u_roughness: Arc<dyn Texture<Float> + Sync + Send>, // default: 0.0
    pub v_roughness: Arc<dyn Texture<Float> + Sync + Send>, // default: 0.0
    pub bump_map: Option<Arc<dyn Texture<Float> + Send + Sync>>,
    pub eta: Float,            // default: 1.33
    pub remap_roughness: bool, // default: true
    pub table: Arc<BssrdfTable>,
}

impl SubsurfaceMaterial {
    pub fn new(
        scale: Float,
        kr: Arc<dyn Texture<Spectrum> + Sync + Send>,
        kt: Arc<dyn Texture<Spectrum> + Sync + Send>,
        sigma_a: Arc<dyn Texture<Spectrum> + Sync + Send>,
        sigma_s: Arc<dyn Texture<Spectrum> + Sync + Send>,
        g: Float,
        eta: Float,
        u_roughness: Arc<dyn Texture<Float> + Sync + Send>,
        v_roughness: Arc<dyn Texture<Float> + Sync + Send>,
        bump_map: Option<Arc<dyn Texture<Float> + Sync + Send>>,
        remap_roughness: bool,
    ) -> Self {
        let mut table: BssrdfTable = BssrdfTable::new(100, 64);
        compute_beam_diffusion_bssrdf(g, eta, &mut table);
        SubsurfaceMaterial {
            scale,
            kr,
            kt,
            sigma_a,
            sigma_s,
            u_roughness,
            v_roughness,
            bump_map,
            eta,
            remap_roughness,
            table: Arc::new(table),
        }
    }
    pub fn create(mp: &mut TextureParams) -> Arc<Material> {
        let sig_a_rgb: [Float; 3] = [0.0011, 0.0024, 0.014];
        let sig_s_rgb: [Float; 3] = [2.55, 3.21, 3.77];
        let mut sig_a: Spectrum = Spectrum::from_rgb(&sig_a_rgb);
        let mut sig_s: Spectrum = Spectrum::from_rgb(&sig_s_rgb);
        let name: String = mp.find_string("name", String::from(""));
        let found: bool = get_medium_scattering_properties(&name, &mut sig_a, &mut sig_s);
        let mut g: Float = mp.find_float("g", 0.0 as Float);
        if name != "" {
            if !found {
                println!(
                    "WARNING: Named material {:?} not found.  Using defaults.",
                    name
                );
            } else {
                // enforce g=0 (the database specifies reduced
                // scattering coefficients)
                g = 0.0;
            }
        }
        let scale: Float = mp.find_float("scale", 1.0 as Float);
        let eta: Float = mp.find_float("eta", 1.33 as Float);
        let sigma_a: Arc<dyn Texture<Spectrum> + Sync + Send> =
            mp.get_spectrum_texture("sigma_a", sig_a);
        let sigma_s: Arc<dyn Texture<Spectrum> + Sync + Send> =
            mp.get_spectrum_texture("sigma_s", sig_s);
        let kr: Arc<dyn Texture<Spectrum> + Sync + Send> =
            mp.get_spectrum_texture("Kr", Spectrum::new(1.0));
        let kt: Arc<dyn Texture<Spectrum> + Sync + Send> =
            mp.get_spectrum_texture("Kr", Spectrum::new(1.0));
        let roughu: Arc<dyn Texture<Float> + Sync + Send> =
            mp.get_float_texture("uroughness", 0.0 as Float);
        let roughv: Arc<dyn Texture<Float> + Sync + Send> =
            mp.get_float_texture("vroughness", 0.0 as Float);
        let bump_map = mp.get_float_texture_or_null("bumpmap");
        let remap_roughness: bool = mp.find_bool("remaproughness", true);
        // let start = PreciseTime::now();
        //let tmp =
        Arc::new(Material::Subsurface(Box::new(SubsurfaceMaterial::new(
            scale,
            kr,
            kt,
            sigma_a,
            sigma_s,
            g,
            eta,
            roughu,
            roughv,
            bump_map,
            remap_roughness,
        ))))
        //;
        // let end = PreciseTime::now();
        // println!(
        //     "{} seconds for SubsurfaceMaterial::new() ...",
        //     start.to(end)
        // );
        // tmp
    }
    // Material
    pub fn compute_scattering_functions(
        &self,
        si: &mut SurfaceInteraction,
        // arena: &mut Arena,
        mode: TransportMode,
        allow_multiple_lobes: bool,
        material: Option<Arc<Material>>,
        scale_opt: Option<Spectrum>,
    ) {
        let mut use_scale: bool = false;
        let mut sc: Spectrum = Spectrum::default();
        if let Some(scale) = scale_opt {
            use_scale = true;
            sc = scale;
        }
        if let Some(ref bump) = self.bump_map {
            Material::bump(bump, si);
        }
        // initialize BSDF for _SubsurfaceMaterial_
        let r: Spectrum = self
            .kr
            .evaluate(si)
            .clamp(0.0 as Float, std::f32::INFINITY as Float);
        let t: Spectrum = self
            .kt
            .evaluate(si)
            .clamp(0.0 as Float, std::f32::INFINITY as Float);
        let mut urough: Float = self.u_roughness.evaluate(si);
        let mut vrough: Float = self.v_roughness.evaluate(si);
        // initialize _bsdf_ for smooth or rough dielectric
        if r.is_black() && t.is_black() {
            return;
        }
        let is_specular: bool = urough == 0.0 as Float && vrough == 0.0 as Float;
        si.bsdf = Some(Bsdf::new(si, self.eta));
        if let Some(bsdf) = &mut si.bsdf {
            if is_specular && allow_multiple_lobes {
                if use_scale {
                    bsdf.add(Bxdf::FresnelSpec(FresnelSpecular::new(
                        r,
                        t,
                        1.0 as Float,
                        self.eta,
                        mode,
                        Some(sc),
                    )));
                } else {
                    bsdf.add(Bxdf::FresnelSpec(FresnelSpecular::new(
                        r,
                        t,
                        1.0 as Float,
                        self.eta,
                        mode,
                        None,
                    )));
                }
            } else {
                if self.remap_roughness {
                    urough = TrowbridgeReitzDistribution::roughness_to_alpha(urough);
                    vrough = TrowbridgeReitzDistribution::roughness_to_alpha(vrough);
                }
                if !r.is_black() {
                    let fresnel = Fresnel::Dielectric(FresnelDielectric {
                        eta_i: 1.0 as Float,
                        eta_t: self.eta,
                    });
                    if is_specular {
                        if use_scale {
                            bsdf.add(Bxdf::SpecRefl(SpecularReflection::new(
                                r,
                                fresnel,
                                Some(sc),
                            )));
                        } else {
                            bsdf.add(Bxdf::SpecRefl(SpecularReflection::new(r, fresnel, None)));
                        }
                    } else {
                        let distrib = MicrofacetDistribution::TrowbridgeReitz(
                            TrowbridgeReitzDistribution::new(urough, vrough, true),
                        );
                        if use_scale {
                            bsdf.add(Bxdf::MicrofacetRefl(MicrofacetReflection::new(
                                r,
                                distrib,
                                fresnel,
                                Some(sc),
                            )));
                        } else {
                            bsdf.add(Bxdf::MicrofacetRefl(MicrofacetReflection::new(
                                r, distrib, fresnel, None,
                            )));
                        }
                    }
                }
                if !t.is_black() {
                    if is_specular {
                        if use_scale {
                            bsdf.add(Bxdf::SpecTrans(SpecularTransmission::new(
                                t,
                                1.0,
                                self.eta,
                                mode,
                                Some(sc),
                            )));
                        } else {
                            bsdf.add(Bxdf::SpecTrans(SpecularTransmission::new(
                                t, 1.0, self.eta, mode, None,
                            )));
                        }
                    } else {
                        let distrib = MicrofacetDistribution::TrowbridgeReitz(
                            TrowbridgeReitzDistribution::new(urough, vrough, true),
                        );
                        if use_scale {
                            bsdf.add(Bxdf::MicrofacetTrans(MicrofacetTransmission::new(
                                t,
                                distrib,
                                1.0,
                                self.eta,
                                mode,
                                Some(sc),
                            )));
                        } else {
                            bsdf.add(Bxdf::MicrofacetTrans(MicrofacetTransmission::new(
                                t, distrib, 1.0, self.eta, mode, None,
                            )));
                        }
                    }
                }
            }
            let sig_a: Spectrum = self.scale
                * self
                    .sigma_a
                    .evaluate(si)
                    .clamp(0.0 as Float, std::f32::INFINITY as Float);
            let sig_s: Spectrum = self.scale
                * self
                    .sigma_s
                    .evaluate(si)
                    .clamp(0.0 as Float, std::f32::INFINITY as Float);
            si.bssrdf = Some(TabulatedBssrdf::new(
                si,
                material,
                mode,
                self.eta,
                &sig_a,
                &sig_s,
                self.table.clone(),
            ));
        }
    }
}