truck_rendimpl/
lib.rs

1//! Visualization of shape and polygon mesh based on platform
2
3#![cfg_attr(not(debug_assertions), deny(warnings))]
4#![deny(clippy::all, rust_2018_idioms)]
5#![warn(
6    missing_docs,
7    missing_debug_implementations,
8    trivial_casts,
9    trivial_numeric_casts,
10    unsafe_code,
11    unstable_features,
12    unused_import_braces,
13    unused_qualifications
14)]
15
16use bytemuck::{Pod, Zeroable};
17use image::DynamicImage;
18use std::sync::Arc;
19use truck_platform::{wgpu::*, *};
20
21/// Re-exports `truck_polymesh`.
22pub mod polymesh {
23    pub use truck_polymesh::*;
24}
25pub use polymesh::*;
26
27/// Material information.
28///
29/// Each instance is rendered based on the microfacet theory.
30#[derive(Debug, Clone, Copy)]
31pub struct Material {
32    /// albedo, base color, [0, 1]-normalized rgba. Default is `Vector4::new(1.0, 1.0, 1.0, 1.0)`.  
33    /// Transparent by alpha is not yet supported in the current standard shader.
34    pub albedo: Vector4,
35    /// roughness of the surface: [0, 1]. Default is 0.5.
36    pub roughness: f64,
37    /// ratio of specular: [0, 1]. Default is 0.25.
38    pub reflectance: f64,
39    /// ratio of ambient: [0, 1]. Default is 0.02.
40    pub ambient_ratio: f64,
41    /// ratio of blending background color: [0, 1]. Default is 0.0.
42    pub background_ratio: f64,
43    /// alpha blend flag
44    pub alpha_blend: bool,
45}
46
47/// Configures of instances.
48#[derive(Clone, Debug)]
49pub struct PolygonState {
50    /// instance matrix
51    pub matrix: Matrix4,
52    /// material of instance
53    pub material: Material,
54    /// texture of instance
55    pub texture: Option<Arc<Texture>>,
56    /// If this parameter is true, the backface culling will be activated.
57    pub backface_culling: bool,
58}
59
60/// Configures of `WireFrameInstance`.
61#[derive(Clone, Debug)]
62pub struct WireFrameState {
63    /// instance matrix
64    pub matrix: Matrix4,
65    /// color of instance
66    pub color: Vector4,
67}
68
69/// shaders for rendering polygons
70#[derive(Debug, Clone)]
71pub struct PolygonShaders {
72    vertex_module: Arc<ShaderModule>,
73    vertex_entry: &'static str,
74    fragment_module: Arc<ShaderModule>,
75    fragment_entry: &'static str,
76    tex_fragment_module: Arc<ShaderModule>,
77    tex_fragment_entry: &'static str,
78}
79
80/// shaders for rendering wireframes
81#[derive(Debug, Clone)]
82pub struct WireShaders {
83    vertex_module: Arc<ShaderModule>,
84    vertex_entry: &'static str,
85    fragment_module: Arc<ShaderModule>,
86    fragment_entry: &'static str,
87}
88
89/// Instance of polygon
90///
91/// One can duplicate polygons with different postures and materials
92/// that have the same mesh data.
93/// To save memory, mesh data on the GPU can be used again.
94///
95/// The duplicated polygon by `Clone::clone` has the same mesh data and descriptor
96/// with original, however, its render id is different from the one of original.
97#[derive(Debug)]
98pub struct PolygonInstance {
99    polygon: (Arc<BufferHandler>, Arc<BufferHandler>),
100    state: PolygonState,
101    shaders: PolygonShaders,
102    id: RenderID,
103}
104
105/// Wire frame rendering
106#[derive(Debug)]
107pub struct WireFrameInstance {
108    vertices: Arc<BufferHandler>,
109    strips: Arc<BufferHandler>,
110    state: WireFrameState,
111    shaders: WireShaders,
112    id: RenderID,
113}
114
115/// Constroctor for instances
116#[derive(Debug, Clone)]
117pub struct InstanceCreator {
118    handler: DeviceHandler,
119    polygon_shaders: PolygonShaders,
120    wire_shaders: WireShaders,
121}
122
123/// for creating `InstanceCreator`
124pub trait CreatorCreator {
125    /// create `InstanceCreator`
126    fn instance_creator(&self) -> InstanceCreator;
127}
128
129/// The trait for Buffer Objects.
130pub trait CreateBuffers {
131    /// Creates buffer handlers of attributes and indices.
132    fn buffers(
133        &self,
134        vertex_usage: BufferUsages,
135        index_usage: BufferUsages,
136        device: &Device,
137    ) -> (BufferHandler, BufferHandler);
138}
139
140/// The trait for generating `Instance` from `Self`.
141pub trait ToInstance<I: Instance> {
142    /// Configuration descriptor for instance.
143    type State;
144    /// Creates `Instance` from `self`.
145    fn to_instance(&self, handler: &DeviceHandler, shaders: &I::Shaders, desc: &Self::State) -> I;
146}
147
148/// Instance for rendering
149pub trait Instance {
150    #[doc(hidden)]
151    type Shaders;
152    /// Get standard shaders from instance creator.
153    #[doc(hidden)]
154    fn standard_shaders(creator: &InstanceCreator) -> Self::Shaders;
155}
156
157#[repr(C)]
158#[derive(Debug, Clone, Copy, Zeroable, Pod)]
159struct AttrVertex {
160    pub position: [f32; 3],
161    pub uv_coord: [f32; 2],
162    pub normal: [f32; 3],
163}
164
165/// utility for creating `Texture`
166pub mod image2texture;
167mod instance_creator;
168mod instance_descriptor;
169mod polygon_instance;
170mod polyrend;
171mod wireframe_instance;