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
/*!
 * Materials in mutiny. This module is renderer independent and the Components in it only
 * specify materials data and some utilities. Each specific renderer should read this components in
 * the scene and implement their behaivour
 */

use densevec::Key;
use rin_math::Rect;
use rinecs::OneToNComponent;
use serde_derive::{Serialize, Deserialize};

pub use self::pbr_material::*;
pub use self::basic_material::{BasicMaterial, BasicMaterialBuilder};
pub use self::parameter::{Parameter, UniformValueRef, ParameterAny, ParameterMutAny, UniformRef};
pub use self::outline_material::{OutlineMaterial, OutlineMaterialBuilder};

#[macro_use] pub mod parameter;
pub mod texture;
mod pbr_material;
mod basic_material;
mod outline_material;

pub mod metals{
    use color::{Rgb, rgb_linear, color_space::LinearRgb};

    pub const IRON: Rgb<f32, LinearRgb>     = rgb_linear!(0.560, 0.570, 0.580);
    pub const SILVER: Rgb<f32, LinearRgb>   = rgb_linear!(0.972, 0.960, 0.915);
    pub const ALUMINUM: Rgb<f32, LinearRgb> = rgb_linear!(0.913, 0.921, 0.925);
    pub const GOLD: Rgb<f32, LinearRgb>     = rgb_linear!(1.000, 0.766, 0.336);
    pub const COPPER: Rgb<f32, LinearRgb>   = rgb_linear!(0.955, 0.637, 0.538);
    pub const CHROMIUM: Rgb<f32, LinearRgb> = rgb_linear!(0.550, 0.556, 0.554);
    pub const NICKEL: Rgb<f32, LinearRgb>   = rgb_linear!(0.660, 0.609, 0.526);
    pub const TITANIUM: Rgb<f32, LinearRgb> = rgb_linear!(0.542, 0.497, 0.449);
    pub const COBALT: Rgb<f32, LinearRgb>   = rgb_linear!(0.662, 0.655, 0.634);
    pub const PLATINUM: Rgb<f32, LinearRgb> = rgb_linear!(0.672, 0.637, 0.585);
}

#[derive(Debug,Clone,Copy,Eq,PartialEq,Hash)]
pub enum BlendFactor{
    Zero,
    One,
    SrcColor,
    OneMinusSrcColor,
    SrcAlpha,
    OneMinusSrcAlpha,
    DstColor,
    OneMinusDstColor,
    DstAlpha,
    OneMinusDstAlpha,
    // TODO: have floats can't be hashed, use int colors? hidden per bits hash?
    // ConstantColor(Rgb<f32>),
    // OneMinusConstantColor(Rgb<f32>),
    // ConstantAlpha(f32),
    // OneMinusConstantAlpha(f32),
}

#[derive(Debug,Clone,Copy,Eq,PartialEq,Hash)]
pub struct ColorBlendFactor{
    pub src: BlendFactor,
    pub dst: BlendFactor,
}

#[derive(Debug,Clone,Copy,Eq,PartialEq,Hash)]
pub struct AlphaBlendFactor{
    pub src: BlendFactor,
    pub dst: BlendFactor,
}

#[derive(Debug,Clone,Copy,Eq,PartialEq,Hash)]
pub enum AlphaType{
    None,
    Blending,
    BlendingFactors(ColorBlendFactor, AlphaBlendFactor),
    AlphaToCoverage,
    ScreenDoor,
    Threshold,
}

impl Default for AlphaType{
    fn default() -> AlphaType {
        AlphaType::None
    }
}

impl ToString for AlphaType{
    fn to_string(&self) -> String{
        match self{
            AlphaType::None => "ALPHA_NONE",
            AlphaType::Blending
                | AlphaType::BlendingFactors(_,_) => "ALPHA_BLENDING",
            AlphaType::AlphaToCoverage => "ALPHA_TO_COVERAGE",
            AlphaType::ScreenDoor => "ALPHA_SCREEN_DOOR",
            AlphaType::Threshold => "ALPHA_THRESHOLD",
        }.to_owned()
    }
}

#[derive(OneToNComponent, Clone, Copy, Eq, PartialEq, Debug, Ord, PartialOrd, Serialize, Deserialize, Hash)]
#[autochanges]
pub struct MaterialRef{id: usize}

impl Key for MaterialRef{
    fn to_usize(self) -> usize{
        self.id
    }

    fn from_usize(id: usize) -> MaterialRef{
        MaterialRef{id}
    }
}

impl rinecs::Changes for MaterialRef{
    fn has_changed(&self) -> bool {
        false
    }

    fn reset_changed(&mut self){

    }
}

#[derive(OneToNComponent, Clone, Eq, PartialEq, Debug, Ord, PartialOrd, Serialize, Deserialize)]
#[autochanges]
pub struct MaterialMultiRef(Vec<MaterialRef>);

impl MaterialMultiRef{
    pub fn new(mats: Vec<MaterialRef>) -> MaterialMultiRef{
        MaterialMultiRef(mats)
    }
}

impl rinecs::Changes for MaterialMultiRef{
    fn has_changed(&self) -> bool {
        false
    }

    fn reset_changed(&mut self){

    }
}

#[derive(OneToNComponent, Clone, Copy, Eq, PartialEq, Debug, Ord, PartialOrd, Serialize, Deserialize)]
#[autochanges]
pub struct ShadowMaterialRef{id: usize}

impl Key for ShadowMaterialRef{
    fn to_usize(self) -> usize{
        self.id
    }

    fn from_usize(id: usize) -> ShadowMaterialRef{
        ShadowMaterialRef{id}
    }
}

impl rinecs::Changes for ShadowMaterialRef{
    fn has_changed(&self) -> bool {
        false
    }

    fn reset_changed(&mut self){

    }
}

#[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub enum Face{
    Front,
    Back,
}

#[derive(Clone, Debug, PartialEq)]
pub enum Property{
    ClipDistance(Vec<u32>),
	CullFace(Option<Face>),
    FrontFace(Face),
    DepthClamp(bool),
	DepthTest(bool),
	Dither(bool),
	LineSmooth(bool),
	Multisample(bool),
	PolygonOffsetFill(Option<(f32, f32)>),
	PolygonOffsetLine(Option<(f32, f32)>),
	PolygonOffsetPoint(Option<(f32, f32)>),
	PolygonSmooth(bool),
	RasterizerDiscard(bool),
    // SampleAlphaToCoverage(bool),
    // SampleAlphaToOne(bool),
    // SampleCoverage(Option<(f32, bool)>),
    StencilMask(u32),
	Scissor(Option<Rect<u32>>),
	StencilTest(bool),
    // StencilFunc(GLenum, GLint, GLuint),
    // StencilOp(GLenum,GLenum,GLenum),
    TextureCubemapSeamless(bool),
	// BlendEquation(GLenum),
	// BlendFunc(GLenum,GLenum),
	// BlendColor([GLclampf;4]),
    // Blend(bool),
    // DepthFunc(GLenum),
    ProgramPointSize(bool),
	DepthMask(bool),
    ColorMask(bool, bool, bool, bool),
	DepthRange(f32, f32),
	LineWidth(f32),
    PointSize(f32),
}

pub trait Material{
    fn type_name(&self) -> &str;
    fn parameter_names(&self) -> Vec<&str>;
    fn parameters(&self) -> Vec<ParameterAny>;
    fn parameters_mut(&mut self) -> Vec<ParameterMutAny>;
    fn parameter(&self, name: &str) -> Option<ParameterAny>;
    fn parameter_mut(&mut self, name: &str) -> Option<ParameterMutAny>;
    fn parameter_type_name(&self, name: &str) -> Option<&str>;
    fn reset_changed(&mut self);
}

pub trait MaterialParameterTypes{
    fn uniform_parameters(&self) -> Vec<UniformRef>;
    // TODO: refactor to Vec<Parameter<(&str, &TextureSampler>)>? options that are Some would be mapped
    fn texture_parameters(&self) -> Vec<UniformRef>;
    fn cubemap_parameters(&self) -> Vec<UniformRef>;
    fn properties_parameter(&self) -> Option<&Parameter<Vec<Property>>>;
    fn alpha_type_parameter(&self) -> Option<&Parameter<AlphaType>>;
    fn any_uniform_changed(&self) -> bool;
    fn any_texture_changed(&self) -> bool;
    fn any_cubemap_changed(&self) -> bool;
    fn any_data_changed(&self) -> bool;
    fn any_uniform_option_changed(&self) -> bool;
    fn any_texture_option_changed(&self) -> bool;
    fn any_cubemap_option_changed(&self) -> bool;
    fn any_data_option_changed(&self) -> bool;
    fn has_data(&self) -> bool;
    fn has_uniforms(&self) -> bool;
    fn has_textures(&self) -> bool;
    fn has_cubemaps(&self) -> bool;
}

#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash, Serialize, Deserialize)]
pub enum ShaderPrecision{
    Low,
    Medium,
    High,
}

impl Default for ShaderPrecision{
    fn default() -> Self {
        ShaderPrecision::High
    }
}