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
use std::rc::Rc;
use std::sync::Arc;
use std::cell::RefCell;
use std::collections::hash_map::HashMap;

use crate::core::format::*;
use crate::core::variant::*;
use crate::core::resource::*;
use crate::core::material::*;

use math::*;
use math::Zero;
use math::type_size::*;
use util::uuid::OsRandNewV4;

pub static SHADER_VERTEX:&'static str = r#"
Result main_vs(Args args)
{
	Result result;
	result.position = transform(args.position);
	result.normal = args.normal;
	result.coord = args.coord;
	return result;
}
"#;

pub static SHADER_FRAGMENT:&'static str = r#"
uniform float3 albedo;
uniform float3 specular;
uniform float3 emissive;
uniform float3 emissiveIntensity;
uniform float smoothness;
uniform float metalness;

uniform sampler2D albedo_map;
uniform sampler2D normal_map;
uniform sampler2D emissive_map;

uniform bool albedo_map_enable;
uniform bool normal_map_enable;
uniform bool emissive_map_enable;
uniform bool smoothness_map_enable;
uniform bool metalness_map_enable;
uniform bool occlusion_map_enable;

Gbuffer main_fs(Args args)
{
	Gbuffer buffer;
	buffer.albedo = albedo;
	buffer.specular = specular;
	buffer.emissive = emissive;
	buffer.smoothness = smoothness;
	buffer.metalness = metalness;
	buffer.normal = normalize(args.normal);

	if (emissive_map_enable)
	{ 
		buffer.emissive *= pow(texture2D(emissive_map, ComputeSphereCoord(buffer.normal)).xyz, float3(2.2));
	}

	if (albedo_map_enable)
	{
		buffer.emissive += albedo * pow(texture2D(albedo_map, ComputeSphereCoord(buffer.normal)).xyz, float3(2.2));
	}

	return buffer;
}"#;


#[derive(Debug)]
pub struct StandardMaterial
{
	pub uuid: uuid::Uuid,
	pub attribs:Vec<VertexAttrib>,
	pub uniforms:HashMap<String, Variant>
}

impl StandardMaterial
{
	pub fn new() -> Self
	{
		let mut params = HashMap::new();
		params.insert("albedo".to_string(), float3::one().into());
		params.insert("specular".to_string(), float3::new(0.5,0.5,0.5).into());
		params.insert("emissive".to_string(), float3::zero().into());
		params.insert("emissive_intensity".to_string(), float1::zero().into());
		params.insert("smoothness".to_string(), float1::zero().into());
		params.insert("metalness".to_string(), float1::zero().into());
		params.insert("occlusion".to_string(), float1::one().into());
		params.insert("albedo_map".to_string(), None.into());
		params.insert("normal_map".to_string(), None.into());
		params.insert("emissive_map".to_string(), None.into());
		params.insert("smoothness_map".to_string(), None.into());
		params.insert("metalness_map".to_string(), None.into());
		params.insert("occlusion_map".to_string(), None.into());
		params.insert("albedo_map_enable".to_string(), float1::zero().into());
		params.insert("normal_map_enable".to_string(), float1::zero().into());
		params.insert("emissive_map_enable".to_string(), float1::zero().into());
		params.insert("smoothness_map_enable".to_string(), float1::zero().into());
		params.insert("metalness_map_enable".to_string(), float1::zero().into());
		params.insert("occlusion_map_enable".to_string(), float1::zero().into());

		let mut attribs = Vec::new();
		let stride = (float3::type_size() + float3::type_size() + float2::type_size()) as u8;
		attribs.push(VertexAttrib::new(Format::RGBSFloat(8,8,8), 0, 3, float3::type_size() as _, stride, 0));
		attribs.push(VertexAttrib::new(Format::RGBSFloat(8,8,8), 1, 3, float3::type_size() as _, stride, float3::type_size() as _));
		attribs.push(VertexAttrib::new(Format::RGBSFloat(8,8,8), 2, 2, float2::type_size() as _, stride, (float3::type_size() * 2) as _));

		Self
		{
			uuid:uuid::Uuid::new_v4_osrng(),
			uniforms:params,
			attribs:attribs
		}
	}
}

impl Material for StandardMaterial 
{
	fn input_layout(&self) -> &[VertexAttrib]
	{
		&self.attribs
	}

	fn uniforms(&self) -> &HashMap<String, Variant>
	{
		&self.uniforms
	}

	fn set_uniform(&mut self, name:&str, value:Variant)
	{
		if let Some(x) = self.uniforms.get_mut(name) {
			*x = value;
		}
	}

	fn vs(&self) -> &str
	{
		SHADER_VERTEX
	}

	fn fs(&self) -> &str
	{
		SHADER_FRAGMENT		
	}
}

impl Resource for StandardMaterial
{
	#[inline]
	fn uuid(&self) -> &uuid::Uuid
	{
		&self.uuid
	}
}

impl From<StandardMaterial> for Rc<Material + 'static>
{
	fn from(material:StandardMaterial) -> Self
	{
		Rc::new(material)
	}
}

impl From<StandardMaterial> for Arc<Material + 'static>
{
	fn from(material:StandardMaterial) -> Self
	{
		Arc::new(material)
	}
}

impl From<StandardMaterial> for Rc<RefCell<Material + 'static>>
{
	fn from(material:StandardMaterial) -> Self
	{
		Rc::new(RefCell::new(material))
	}
}

impl From<StandardMaterial> for Arc<RefCell<Material + 'static>>
{
	fn from(material:StandardMaterial) -> Self
	{
		Arc::new(RefCell::new(material))
	}
}