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
//!
//! A collection of materials implementing the [Material] trait.
//!
//! A material together with a [geometry] can be rendered directly (using [Geometry::render_with_material] or [Geometry::render_with_effect]).
//! A [Material] can also be combined into an [object] (see [Gm]) and be used in a render call, for example [RenderTarget::render].
//!

macro_rules! impl_material_body {
    ($inner:ident) => {
        fn fragment_shader_source(&self, lights: &[&dyn Light]) -> String {
            self.$inner().fragment_shader_source(lights)
        }
        fn fragment_attributes(&self) -> FragmentAttributes {
            self.$inner().fragment_attributes()
        }
        fn use_uniforms(&self, program: &Program, camera: &Camera, lights: &[&dyn Light]) {
            self.$inner().use_uniforms(program, camera, lights)
        }
        fn render_states(&self) -> RenderStates {
            self.$inner().render_states()
        }
        fn material_type(&self) -> MaterialType {
            self.$inner().material_type()
        }
        fn id(&self) -> u16 {
            self.$inner().id()
        }
    };
}

use crate::renderer::*;

pub use three_d_asset::material::{
    GeometryFunction, LightingModel, NormalDistributionFunction, PbrMaterial as CpuMaterial,
};

mod color_material;
#[doc(inline)]
pub use color_material::*;

mod depth_material;
#[doc(inline)]
pub use depth_material::*;

mod normal_material;
#[doc(inline)]
pub use normal_material::*;

mod orm_material;
#[doc(inline)]
pub use orm_material::*;

mod position_material;
#[doc(inline)]
pub use position_material::*;

mod uv_material;
#[doc(inline)]
pub use uv_material::*;

mod physical_material;
#[doc(inline)]
pub use physical_material::*;

mod deferred_physical_material;
#[doc(inline)]
pub use deferred_physical_material::*;

mod skybox_material;
#[doc(inline)]
pub(in crate::renderer) use skybox_material::*;

mod isosurface_material;
#[doc(inline)]
pub use isosurface_material::*;

use std::{ops::Deref, sync::Arc};

///
/// A reference to a 2D texture and a texture transformation.
///
#[derive(Clone)]
pub struct Texture2DRef {
    /// A reference to the texture.
    pub texture: Arc<Texture2D>,
    /// A transformation applied to the uv coordinates before reading a texel value at those uv coordinates.
    /// This is primarily used in relation to texture atlasing.
    pub transformation: Mat3,
}

impl Texture2DRef {
    /// Creates a new [Texture2DRef] with an identity transformation from a [CpuTexture].
    pub fn from_cpu_texture(context: &Context, cpu_texture: &CpuTexture) -> Self {
        Self {
            texture: Arc::new(Texture2D::new(context, cpu_texture)),
            transformation: Mat3::identity(),
        }
    }

    /// Creates a new [Texture2DRef] with an identity transformation from a [Texture2D].
    pub fn from_texture(texture: Texture2D) -> Self {
        Self {
            texture: Arc::new(texture),
            transformation: Mat3::identity(),
        }
    }
}

impl std::ops::Deref for Texture2DRef {
    type Target = Texture2D;
    fn deref(&self) -> &Self::Target {
        &self.texture
    }
}

impl std::convert::From<Texture2D> for Texture2DRef {
    fn from(texture: Texture2D) -> Self {
        Self::from_texture(texture)
    }
}

impl std::convert::From<Arc<Texture2D>> for Texture2DRef {
    fn from(texture: Arc<Texture2D>) -> Self {
        Self {
            texture,
            transformation: Mat3::identity(),
        }
    }
}

///
/// Defines the material type which is needed to render the objects in the correct order.
/// For example, transparent objects need to be rendered back to front, whereas opaque objects need to be rendered front to back.
///
#[derive(Clone, Copy, PartialEq, PartialOrd, Ord, Eq, Debug)]
pub enum MaterialType {
    /// Forward opaque
    Opaque,
    /// Forward transparent
    Transparent,
    /// Deferred opaque
    Deferred,
}

///
/// Describes the set of attributes provided by a [geometry] and consumed by a [Material], ie. calculated in the vertex shader and then sent to the fragment shader.
/// To use an attribute for a material, add the relevant shader code to the fragment shader source (documented for each attribute) and return this struct from [Material::fragment_attributes] with the relevant attribute set to true.
///
#[derive(Clone, Copy, Debug)]
pub struct FragmentAttributes {
    /// Position in world space: `in vec3 pos;`
    pub position: bool,
    /// Normal: `in vec3 nor;`,
    pub normal: bool,
    /// Tangent and bitangent: `in vec3 tang; in vec3 bitang;`
    pub tangents: bool,
    /// UV coordinates: `in vec2 uvs;`
    pub uv: bool,
    /// Color: `in vec4 col;`
    pub color: bool,
}

impl FragmentAttributes {
    /// All attributes
    pub const ALL: Self = Self {
        position: true,
        normal: true,
        tangents: true,
        uv: true,
        color: true,
    };
    /// No attributes
    pub const NONE: Self = Self {
        position: false,
        normal: false,
        tangents: false,
        uv: false,
        color: false,
    };
}

///
/// Represents a material that, together with a [geometry], can be rendered using [Geometry::render_with_material].
/// Alternatively, a geometry and a material can be combined in a [Gm],
/// thereby creating an [Object] which can be used in a render call, for example [RenderTarget::render].
///
pub trait Material {
    ///
    /// Returns the fragment shader source for this material.
    ///
    fn fragment_shader_source(&self, lights: &[&dyn Light]) -> String;

    ///
    /// Returns a unique ID for each variation of the shader source returned from [Material::fragment_shader_source].
    ///
    /// **Note:** The last bit is reserved to internally implemented materials, so if implementing the [Material] trait
    /// outside of this crate, always return an id that is smaller than `0b1u16 << 15`.
    ///
    fn id(&self) -> u16;

    ///
    /// Returns a [FragmentAttributes] struct that describes which fragment attributes,
    /// ie. the input from the vertex shader, are required for rendering with this material.
    ///
    fn fragment_attributes(&self) -> FragmentAttributes;

    ///
    /// Sends the uniform data needed for this material to the fragment shader.
    ///
    fn use_uniforms(&self, program: &Program, camera: &Camera, lights: &[&dyn Light]);

    ///
    /// Returns the render states needed to render with this material.
    ///
    fn render_states(&self) -> RenderStates;

    ///
    /// Returns the type of material.
    ///
    fn material_type(&self) -> MaterialType;
}

///
/// Implement this for a [Material] that can be created from a [CpuMaterial].
///
pub trait FromCpuMaterial: std::marker::Sized {
    ///
    /// Creates a new material that can be used for rendering from a [CpuMaterial].
    ///
    fn from_cpu_material(context: &Context, cpu_material: &CpuMaterial) -> Self;
}

///
/// Implement this for a [Material] that can be created from a [CpuVoxelGrid].
///
pub trait FromCpuVoxelGrid: std::marker::Sized {
    ///
    /// Creates a new material that can be used for rendering from a [CpuVoxelGrid].
    ///
    fn from_cpu_voxel_grid(context: &Context, cpu_voxel_grid: &CpuVoxelGrid) -> Self;
}

impl<T: Material + ?Sized> Material for &T {
    impl_material_body!(deref);
}

impl<T: Material + ?Sized> Material for &mut T {
    impl_material_body!(deref);
}

impl<T: Material> Material for Box<T> {
    impl_material_body!(as_ref);
}

impl<T: Material> Material for std::rc::Rc<T> {
    impl_material_body!(as_ref);
}

impl<T: Material> Material for std::sync::Arc<T> {
    impl_material_body!(as_ref);
}

impl<T: Material> Material for std::cell::RefCell<T> {
    impl_material_body!(borrow);
}

impl<T: Material> Material for std::sync::RwLock<T> {
    fn fragment_shader_source(&self, lights: &[&dyn Light]) -> String {
        self.read().unwrap().fragment_shader_source(lights)
    }
    fn fragment_attributes(&self) -> FragmentAttributes {
        self.read().unwrap().fragment_attributes()
    }
    fn use_uniforms(&self, program: &Program, camera: &Camera, lights: &[&dyn Light]) {
        self.read().unwrap().use_uniforms(program, camera, lights)
    }
    fn render_states(&self) -> RenderStates {
        self.read().unwrap().render_states()
    }
    fn material_type(&self) -> MaterialType {
        self.read().unwrap().material_type()
    }
    fn id(&self) -> u16 {
        self.read().unwrap().id()
    }
}

fn is_transparent(cpu_material: &CpuMaterial) -> bool {
    cpu_material.albedo.a != 255
        || cpu_material
            .albedo_texture
            .as_ref()
            .map(|t| match &t.data {
                TextureData::RgbaU8(data) => data.iter().any(|d| d[3] != 255),
                TextureData::RgbaF16(data) => data.iter().any(|d| d[3] < f16::from_f32(0.99)),
                TextureData::RgbaF32(data) => data.iter().any(|d| d[3] < 0.99),
                _ => false,
            })
            .unwrap_or(false)
}