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
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 color;
uniform sampler2D texture;

uniform bool texture_enable;

Gbuffer main_fs(Args args)
{
	Gbuffer buffer;
	buffer.albedo = float3(1e-16,1e-16,1e-16);
	buffer.specular = float3(0.0,0.0,0.0);
	buffer.emissive = color;
	buffer.smoothness = 0.0;
	buffer.metalness = 0.0;
	buffer.normal = normalize(args.normal);

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

	return buffer;
}"#;


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

impl SkyboxMaterial
{
	pub fn new() -> Self
	{
		let mut params = HashMap::new();
		params.insert("color".to_string(), float3::zero().into());
		params.insert("texture".to_string(), None.into());
		params.insert("texture_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 SkyboxMaterial 
{
	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		
	}

	fn cull_mode(&self) -> CullMode
	{
		return CullMode::None;
	}
}

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

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

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

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

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