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
// Oliver Berzs
// https://github.com/oberzs/duku

use super::Descriptor;
use super::ShaderMaterial;
use super::Uniforms;
use crate::buffer::Buffer;
use crate::buffer::BufferUsage;
use crate::color::Rgbf;
use crate::device::Device;
use crate::error::Result;
use crate::image::Canvas;
use crate::image::Texture;
use crate::math::Vec4;
use crate::resources::Handle;

/// Material parameters to use in a shader.
///
/// # Examples
///
/// ```no_run
/// # use duku::Duku;
/// # let (mut duku, _) = Duku::windowed(1, 1).unwrap();
/// let material = duku.create_material_pbr().unwrap();
/// material.write().roughness(0.5);
///
/// # duku.draw(None, |t| {
/// // when drawing
/// t.material(&material);
/// t.cube([1.0, 1.0, 1.0]);
/// # });
/// ```
pub struct Material {
    /// parameter A
    pub a: Vec4,
    /// parameter B
    pub b: Vec4,
    /// parameter C
    pub c: Vec4,
    /// parameter D
    pub d: Vec4,
    /// parameter E
    pub e: Vec4,
    /// parameter F
    pub f: Vec4,
    /// parameter G
    pub g: Vec4,
    /// parameter H
    pub h: Vec4,
    /// texture storage for that are used
    /// in the material
    pub textures: Vec<Handle<Texture>>,

    descriptor: Descriptor,
    buffer: Buffer<ShaderMaterial>,
}

impl Material {
    pub(crate) fn new(device: &Device, uniforms: &mut Uniforms) -> Result<Self> {
        let buffer = Buffer::dynamic(device, BufferUsage::Uniform, 1);
        let descriptor = uniforms.material_set(device, &buffer)?;

        Ok(Self {
            a: Vec4::default(),
            b: Vec4::default(),
            c: Vec4::default(),
            d: Vec4::default(),
            e: Vec4::default(),
            f: Vec4::default(),
            g: Vec4::default(),
            h: Vec4::default(),
            textures: vec![],
            buffer,
            descriptor,
        })
    }

    /// Set albedo color for the PBR and other various shaders
    pub fn albedo_color(&mut self, color: impl Into<Rgbf>) {
        let temp = self.a[3];
        self.a = color.into().into();
        self.a[3] = temp;
    }

    /// Set albedo texture for the PBR and other various shaders
    pub fn albedo_texture(&mut self, texture: Handle<Texture>) {
        self.a[3] = texture.read().shader_index() as f32;
        self.textures.push(texture);
    }

    /// Set albedo canvas for the PBR and other various shaders
    pub fn albedo_canvas(&mut self, f: &Handle<Canvas>) {
        self.a[3] = f.read().shader_index() as f32;
    }

    /// Set metalness factor for the PBR shader
    pub fn metalness(&mut self, value: f32) {
        self.b[0] = value;
    }

    /// Set roughness factor for the PBR shader
    pub fn roughness(&mut self, value: f32) {
        self.b[1] = value;
    }

    /// Set emissive color for the PBR shader
    pub fn emissive(&mut self, color: impl Into<Rgbf>) {
        let temp = self.d[3];
        self.d = color.into().into();
        self.d[3] = temp;
    }

    /// Set metalness-roughness texture for the PBR shader
    pub fn metalness_roughness_texture(&mut self, texture: Handle<Texture>) {
        self.b[2] = texture.read().shader_index() as f32;
        self.textures.push(texture);
    }

    /// Set ambient occlusion texture for the PBR shader
    pub fn ambient_occlusion_texture(&mut self, texture: Handle<Texture>) {
        self.b[3] = texture.read().shader_index() as f32;
        self.textures.push(texture);
    }

    /// Set normal texture for the PBR shader
    pub fn normal_texture(&mut self, texture: Handle<Texture>) {
        self.c[0] = texture.read().shader_index() as f32;
        self.textures.push(texture);
    }

    /// Set emissive texture for the PBR shader
    pub fn emissive_texture(&mut self, texture: Handle<Texture>) {
        self.c[1] = texture.read().shader_index() as f32;
        self.textures.push(texture);
    }

    pub(crate) fn update(&mut self) {
        self.buffer.copy_from_data(&[ShaderMaterial {
            a: self.a,
            b: self.b,
            c: self.c,
            d: self.d,
            e: self.e,
            f: self.f,
            g: self.g,
            h: self.h,
        }]);
    }

    pub(crate) const fn descriptor(&self) -> Descriptor {
        self.descriptor
    }

    pub(crate) fn destroy(&self, device: &Device) {
        self.buffer.destroy(device);
    }
}

impl PartialEq for Material {
    fn eq(&self, other: &Self) -> bool {
        self.buffer == other.buffer
    }
}