three_d/renderer/
geometry.rs

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

macro_rules! impl_geometry_body {
    ($inner:ident) => {
        fn draw(&self, viewer: &dyn Viewer, program: &Program, render_states: RenderStates) {
            self.$inner().draw(viewer, program, render_states)
        }

        fn vertex_shader_source(&self) -> String {
            self.$inner().vertex_shader_source()
        }

        fn id(&self) -> GeometryId {
            self.$inner().id()
        }

        fn render_with_material(
            &self,
            material: &dyn Material,
            viewer: &dyn Viewer,
            lights: &[&dyn Light],
        ) {
            self.$inner().render_with_material(material, viewer, lights)
        }

        fn render_with_effect(
            &self,
            material: &dyn Effect,
            viewer: &dyn Viewer,
            lights: &[&dyn Light],
            color_texture: Option<ColorTexture>,
            depth_texture: Option<DepthTexture>,
        ) {
            self.$inner()
                .render_with_effect(material, viewer, lights, color_texture, depth_texture)
        }

        fn aabb(&self) -> AxisAlignedBoundingBox {
            self.$inner().aabb()
        }
    };
}

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

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

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

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

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

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

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

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

use crate::core::*;
use crate::renderer::*;

pub use three_d_asset::{
    Geometry as CpuGeometry, Indices, KeyFrameAnimation, KeyFrames, PointCloud, Positions,
    TriMesh as CpuMesh,
};

///
/// Represents a 3D geometry that, together with a [material], 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].
///
/// If requested by the material, the geometry has to support the following attributes in the vertex shader source code.
/// - position: `out vec3 pos;` (must be in world space)
/// - normal: `out vec3 nor;`
/// - tangent: `out vec3 tang;`
/// - bitangent: `out vec3 bitang;`
/// - uv coordinates: `out vec2 uvs;` (must be flipped in v compared to standard uv coordinates, ie. do `uvs = vec2(uvs.x, 1.0 - uvs.y);` in the vertex shader or do the flip before constructing the uv coordinates vertex buffer)
/// - color: `out vec4 col;`
///
/// In addition, for the geometry to be pickable using the [pick] or [ray_intersect] methods (ie. combined with the [IntersectionMaterial]),
/// it needs to support `flat out int instance_id;`. Simply set it to the built-in glsl variable: `gl_InstanceID`.
///
pub trait Geometry {
    ///
    /// Draw this geometry.
    ///
    fn draw(&self, viewer: &dyn Viewer, program: &Program, render_states: RenderStates);

    ///
    /// Returns the vertex shader source for this geometry given that the fragment shader needs the given vertex attributes.
    ///
    fn vertex_shader_source(&self) -> String;

    ///
    /// Returns a unique ID for each variation of the shader source returned from `Geometry::vertex_shader_source`.
    ///
    /// **Note:** The last bit is reserved to internally implemented geometries, so if implementing the `Geometry` trait
    /// outside of this crate, always return an id in the public use range as defined by [GeometryId].
    ///
    fn id(&self) -> GeometryId;

    ///
    /// Render the geometry with the given [Material].
    /// Must be called in the callback given as input to a [RenderTarget], [ColorTarget] or [DepthTarget] write method.
    /// Use an empty array for the `lights` argument, if the material does not require lights to be rendered.
    ///
    fn render_with_material(
        &self,
        material: &dyn Material,
        viewer: &dyn Viewer,
        lights: &[&dyn Light],
    );

    ///
    /// Render the geometry with the given [Effect].
    /// Must be called in the callback given as input to a [RenderTarget], [ColorTarget] or [DepthTarget] write method.
    /// Use an empty array for the `lights` argument, if the material does not require lights to be rendered.
    ///
    fn render_with_effect(
        &self,
        material: &dyn Effect,
        viewer: &dyn Viewer,
        lights: &[&dyn Light],
        color_texture: Option<ColorTexture>,
        depth_texture: Option<DepthTexture>,
    );

    ///
    /// Returns the [AxisAlignedBoundingBox] for this geometry in the global coordinate system.
    ///
    fn aabb(&self) -> AxisAlignedBoundingBox;

    ///
    /// For updating the animation of this geometry if it is animated, if not, this method does nothing.
    /// The time parameter should be some continious time, for example the time since start.
    ///
    fn animate(&mut self, _time: f32) {}
}

use std::ops::Deref;
impl<T: Geometry + ?Sized> Geometry for &T {
    impl_geometry_body!(deref);
}

impl<T: Geometry + ?Sized> Geometry for &mut T {
    impl_geometry_body!(deref);

    fn animate(&mut self, time: f32) {
        self.deref().animate(time)
    }
}

impl<T: Geometry> Geometry for Box<T> {
    impl_geometry_body!(as_ref);
}

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

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

impl<T: Geometry> Geometry for std::cell::RefCell<T> {
    impl_geometry_body!(borrow);

    fn animate(&mut self, time: f32) {
        self.borrow_mut().animate(time)
    }
}

impl<T: Geometry> Geometry for std::sync::RwLock<T> {
    fn draw(&self, viewer: &dyn Viewer, program: &Program, render_states: RenderStates) {
        self.read().unwrap().draw(viewer, program, render_states)
    }

    fn vertex_shader_source(&self) -> String {
        self.read().unwrap().vertex_shader_source()
    }

    fn id(&self) -> GeometryId {
        self.read().unwrap().id()
    }

    fn render_with_material(
        &self,
        material: &dyn Material,
        viewer: &dyn Viewer,
        lights: &[&dyn Light],
    ) {
        self.read()
            .unwrap()
            .render_with_material(material, viewer, lights)
    }

    fn render_with_effect(
        &self,
        material: &dyn Effect,
        viewer: &dyn Viewer,
        lights: &[&dyn Light],
        color_texture: Option<ColorTexture>,
        depth_texture: Option<DepthTexture>,
    ) {
        self.read().unwrap().render_with_effect(
            material,
            viewer,
            lights,
            color_texture,
            depth_texture,
        )
    }

    fn aabb(&self) -> AxisAlignedBoundingBox {
        self.read().unwrap().aabb()
    }

    fn animate(&mut self, time: f32) {
        self.write().unwrap().animate(time)
    }
}

///
/// The index buffer used to determine the three vertices for each triangle in a mesh.
/// A triangle is defined by three consequitive indices in the index buffer.
/// Each index points to a position in the vertex buffers.
///
pub enum IndexBuffer {
    /// No index buffer is used, ie. every triangle consist of three consequitive vertices.
    None,
    /// Use an index buffer with indices defined in `u8` format.
    U8(ElementBuffer<u8>),
    /// Use an index buffer with indices defined in `u16` format.
    U16(ElementBuffer<u16>),
    /// Use an index buffer with indices defined in `u32` format.
    U32(ElementBuffer<u32>),
}

struct BaseMesh {
    indices: IndexBuffer,
    positions: VertexBuffer<Vec3>,
    normals: Option<VertexBuffer<Vec3>>,
    tangents: Option<VertexBuffer<Vec4>>,
    uvs: Option<VertexBuffer<Vec2>>,
    colors: Option<VertexBuffer<Vec4>>,
}

impl BaseMesh {
    pub fn new(context: &Context, cpu_mesh: &CpuMesh) -> Self {
        #[cfg(debug_assertions)]
        cpu_mesh.validate().expect("invalid cpu mesh");

        Self {
            indices: match &cpu_mesh.indices {
                Indices::U8(ind) => IndexBuffer::U8(ElementBuffer::new_with_data(context, ind)),
                Indices::U16(ind) => IndexBuffer::U16(ElementBuffer::new_with_data(context, ind)),
                Indices::U32(ind) => IndexBuffer::U32(ElementBuffer::new_with_data(context, ind)),
                Indices::None => IndexBuffer::None,
            },
            positions: VertexBuffer::new_with_data(context, &cpu_mesh.positions.to_f32()),
            normals: cpu_mesh
                .normals
                .as_ref()
                .map(|data| VertexBuffer::new_with_data(context, data)),
            tangents: cpu_mesh
                .tangents
                .as_ref()
                .map(|data| VertexBuffer::new_with_data(context, data)),
            uvs: cpu_mesh.uvs.as_ref().map(|data| {
                VertexBuffer::new_with_data(
                    context,
                    &data
                        .iter()
                        .map(|uv| vec2(uv.x, 1.0 - uv.y))
                        .collect::<Vec<_>>(),
                )
            }),
            colors: cpu_mesh.colors.as_ref().map(|data| {
                VertexBuffer::new_with_data(
                    context,
                    &data.iter().map(|c| c.to_linear_srgb()).collect::<Vec<_>>(),
                )
            }),
        }
    }

    pub fn draw(&self, program: &Program, render_states: RenderStates, viewer: &dyn Viewer) {
        self.use_attributes(program);

        match &self.indices {
            IndexBuffer::None => program.draw_arrays(
                render_states,
                viewer.viewport(),
                self.positions.vertex_count(),
            ),
            IndexBuffer::U8(element_buffer) => {
                program.draw_elements(render_states, viewer.viewport(), element_buffer)
            }
            IndexBuffer::U16(element_buffer) => {
                program.draw_elements(render_states, viewer.viewport(), element_buffer)
            }
            IndexBuffer::U32(element_buffer) => {
                program.draw_elements(render_states, viewer.viewport(), element_buffer)
            }
        }
    }

    pub fn draw_instanced(
        &self,
        program: &Program,
        render_states: RenderStates,
        viewer: &dyn Viewer,
        instance_count: u32,
    ) {
        self.use_attributes(program);

        match &self.indices {
            IndexBuffer::None => program.draw_arrays_instanced(
                render_states,
                viewer.viewport(),
                self.positions.vertex_count(),
                instance_count,
            ),
            IndexBuffer::U8(element_buffer) => program.draw_elements_instanced(
                render_states,
                viewer.viewport(),
                element_buffer,
                instance_count,
            ),
            IndexBuffer::U16(element_buffer) => program.draw_elements_instanced(
                render_states,
                viewer.viewport(),
                element_buffer,
                instance_count,
            ),
            IndexBuffer::U32(element_buffer) => program.draw_elements_instanced(
                render_states,
                viewer.viewport(),
                element_buffer,
                instance_count,
            ),
        }
    }

    fn use_attributes(&self, program: &Program) {
        program.use_vertex_attribute("position", &self.positions);

        if program.requires_attribute("normal") {
            if let Some(normals) = &self.normals {
                program.use_vertex_attribute("normal", normals);
            }
        }

        if program.requires_attribute("tangent") {
            if let Some(tangents) = &self.tangents {
                program.use_vertex_attribute("tangent", tangents);
            }
        }

        if program.requires_attribute("uv_coordinates") {
            if let Some(uvs) = &self.uvs {
                program.use_vertex_attribute("uv_coordinates", uvs);
            }
        }

        if program.requires_attribute("color") {
            if let Some(colors) = &self.colors {
                program.use_vertex_attribute("color", colors);
            }
        }
    }

    fn vertex_shader_source(&self) -> String {
        format!(
            "{}{}{}{}{}{}",
            if self.normals.is_some() {
                "#define USE_NORMALS\n"
            } else {
                ""
            },
            if self.tangents.is_some() {
                "#define USE_TANGENTS\n"
            } else {
                ""
            },
            if self.uvs.is_some() {
                "#define USE_UVS\n"
            } else {
                ""
            },
            if self.colors.is_some() {
                "#define USE_VERTEX_COLORS\n"
            } else {
                ""
            },
            include_str!("../core/shared.frag"),
            include_str!("geometry/shaders/mesh.vert"),
        )
    }
}