truck_rendimpl/
instance_descriptor.rs1use crate::SamplerBindingType::Filtering;
2use crate::*;
3
4impl Default for Material {
5 #[inline(always)]
6 fn default() -> Material {
7 Material {
8 albedo: Vector4::new(1.0, 1.0, 1.0, 1.0),
9 roughness: 0.5,
10 reflectance: 0.25,
11 ambient_ratio: 0.02,
12 background_ratio: 0.0,
13 alpha_blend: false,
14 }
15 }
16}
17
18impl Material {
19 #[inline(always)]
32 pub fn buffer(&self, device: &Device) -> BufferHandler {
33 let material_data: [f32; 8] = [
34 self.albedo[0] as f32,
35 self.albedo[1] as f32,
36 self.albedo[2] as f32,
37 self.albedo[3] as f32,
38 self.roughness as f32,
39 self.reflectance as f32,
40 self.ambient_ratio as f32,
41 self.background_ratio as f32,
42 ];
43 BufferHandler::from_slice(&material_data, device, BufferUsages::UNIFORM)
44 }
45
46 #[doc(hidden)]
47 #[inline(always)]
48 pub fn bgl_entry() -> PreBindGroupLayoutEntry {
49 PreBindGroupLayoutEntry {
50 visibility: ShaderStages::FRAGMENT,
51 ty: BindingType::Buffer {
52 ty: BufferBindingType::Uniform,
53 has_dynamic_offset: false,
54 min_binding_size: None,
55 },
56 count: None,
57 }
58 }
59}
60
61impl Default for PolygonState {
62 #[inline(always)]
63 fn default() -> PolygonState {
64 PolygonState {
65 matrix: Matrix4::identity(),
66 material: Default::default(),
67 texture: None,
68 backface_culling: true,
69 }
70 }
71}
72
73impl PolygonState {
74 #[inline(always)]
84 pub fn matrix_buffer(&self, device: &Device) -> BufferHandler {
85 let matrix_data: [[f32; 4]; 4] = self.matrix.cast::<f32>().unwrap().into();
86 BufferHandler::from_slice(&matrix_data, device, BufferUsages::UNIFORM)
87 }
88
89 #[doc(hidden)]
90 #[inline(always)]
91 pub fn matrix_bgl_entry() -> PreBindGroupLayoutEntry {
92 PreBindGroupLayoutEntry {
93 visibility: ShaderStages::VERTEX | ShaderStages::FRAGMENT,
94 ty: BindingType::Buffer {
95 ty: BufferBindingType::Uniform,
96 has_dynamic_offset: false,
97 min_binding_size: None,
98 },
99 count: None,
100 }
101 }
102
103 #[inline(always)]
116 pub fn material_buffer(&self, device: &Device) -> BufferHandler { self.material.buffer(device) }
117
118 #[doc(hidden)]
119 #[inline(always)]
120 pub fn material_bgl_entry() -> PreBindGroupLayoutEntry { Material::bgl_entry() }
121
122 pub fn textureview_and_sampler(&self, device: &Device) -> (TextureView, Sampler) {
131 let texture = self.texture.as_ref().unwrap();
132 let view = texture.create_view(&Default::default());
133 let sampler = device.create_sampler(&SamplerDescriptor {
134 mag_filter: FilterMode::Linear,
135 min_filter: FilterMode::Nearest,
136 mipmap_filter: FilterMode::Nearest,
137 lod_min_clamp: 0.0,
138 lod_max_clamp: 100.0,
139 ..Default::default()
140 });
141 (view, sampler)
142 }
143
144 #[doc(hidden)]
145 #[inline(always)]
146 pub fn textureview_bgl_entry() -> PreBindGroupLayoutEntry {
147 PreBindGroupLayoutEntry {
148 visibility: ShaderStages::FRAGMENT,
149 ty: BindingType::Texture {
150 view_dimension: TextureViewDimension::D2,
151 sample_type: TextureSampleType::Float { filterable: true },
152 multisampled: false,
153 },
154 count: None,
155 }
156 }
157
158 #[doc(hidden)]
159 #[inline(always)]
160 pub fn sampler_bgl_entry() -> PreBindGroupLayoutEntry {
161 PreBindGroupLayoutEntry {
162 visibility: ShaderStages::FRAGMENT,
163 ty: BindingType::Sampler(Filtering),
164 count: None,
165 }
166 }
167}