Skip to main content

gizmo_renderer/components/
material.rs

1use std::sync::Arc;
2
3#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
4pub enum MaterialType {
5    Pbr,
6    Unlit,
7    Skybox,
8    Water,
9    Grid,
10}
11
12#[derive(Clone)]
13pub struct Material {
14    pub bind_group: Arc<wgpu::BindGroup>,
15    pub albedo: gizmo_math::Vec4,
16    pub roughness: f32,
17    pub metallic: f32,
18    pub texture_source: Option<String>,
19    pub material_type: MaterialType,
20    pub is_transparent: bool,
21    pub is_double_sided: bool,
22}
23
24impl Material {
25    pub fn new(bind_group: Arc<wgpu::BindGroup>) -> Self {
26        Self {
27            bind_group,
28            albedo: gizmo_math::Vec4::new(1.0, 1.0, 1.0, 1.0),
29            roughness: 0.5,
30            metallic: 0.0,
31            texture_source: None,
32            material_type: MaterialType::Pbr,
33            is_transparent: false,
34            is_double_sided: false,
35        }
36    }
37
38    /// PBR materyali olarak yapılandırır.
39    /// Not: Eğer `albedo.w < 1.0` verilirse `is_transparent` otomatik olarak `true` yapılır.
40    /// `roughness` ve `metallic` değerleri [0.0, 1.0] aralığına sınırlandırılır.
41    pub fn with_pbr(mut self, albedo: gizmo_math::Vec4, roughness: f32, metallic: f32) -> Self {
42        self.albedo = albedo;
43        self.roughness = roughness.clamp(0.0, 1.0);
44        self.metallic = metallic.clamp(0.0, 1.0);
45        self.material_type = MaterialType::Pbr;
46        if albedo.w < 1.0 {
47            self.is_transparent = true;
48        }
49        self
50    }
51
52    /// Saydamlığı manuel olarak belirler.
53    /// Uyarı: `with_pbr`, `with_unlit` veya `with_water` metodları albedo'nun alpha değerine (w) bakarak
54    /// saydamlığı otomatik değiştirebilir. Kesin bir saydamlık istiyorsanız, bu metodu builder zincirinin en sonunda çağırın.
55    pub fn with_transparent(mut self, transparent: bool) -> Self {
56        self.is_transparent = transparent;
57        self
58    }
59
60    pub fn with_double_sided(mut self, double_sided: bool) -> Self {
61        self.is_double_sided = double_sided;
62        self
63    }
64
65    /// Işıklandırmadan etkilenmeyen (Unlit) materyal olarak yapılandırır.
66    /// Not: Eğer `albedo.w < 1.0` verilirse `is_transparent` otomatik olarak `true` yapılır.
67    pub fn with_unlit(mut self, albedo: gizmo_math::Vec4) -> Self {
68        self.albedo = albedo;
69        self.material_type = MaterialType::Unlit;
70        if albedo.w < 1.0 {
71            self.is_transparent = true;
72        }
73        self
74    }
75
76    pub fn with_skybox(mut self) -> Self {
77        self.material_type = MaterialType::Skybox;
78        self
79    }
80
81    /// Su materyali olarak yapılandırır.
82    /// `roughness` 0.05, `metallic` 0.0 olarak varsayılan su değerlerine ayarlanır.
83    /// Not: Eğer `base_albedo.w < 1.0` verilirse `is_transparent` otomatik olarak `true` yapılır.
84    pub fn with_water(mut self, base_albedo: gizmo_math::Vec4) -> Self {
85        self.albedo = base_albedo;
86        self.roughness = 0.05;
87        self.metallic = 0.0;
88        self.material_type = MaterialType::Water;
89        if base_albedo.w < 1.0 {
90            self.is_transparent = true;
91        }
92        self
93    }
94
95    pub fn with_texture_source(mut self, path: String) -> Self {
96        self.texture_source = Some(path);
97        self
98    }
99}