-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathmaterial.rs
More file actions
446 lines (403 loc) · 14.7 KB
/
Copy pathmaterial.rs
File metadata and controls
446 lines (403 loc) · 14.7 KB
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
use crate::content::content_mapping::ContentMappingAndUrlRef;
use super::{
proto_components::{
common::{
texture_union::Tex, Color3, Color4, TextureFilterMode, TextureUnion, TextureWrapMode,
Vector2,
},
sdk::components::{pb_material, MaterialTransparencyMode},
},
SceneEntityId,
};
#[derive(Clone)]
pub struct RoundedFloat(pub f32);
#[derive(Clone)]
pub struct RoundedColor3(pub Color3);
#[derive(Clone)]
pub struct RoundedColor4(pub Color4);
impl From<RoundedFloat> for f32 {
fn from(val: RoundedFloat) -> Self {
val.0
}
}
impl From<&f32> for RoundedFloat {
fn from(value: &f32) -> Self {
Self(*value)
}
}
impl std::hash::Hash for RoundedFloat {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
let value = (100000.0 * self.0) as i64;
value.hash(state);
}
}
impl PartialEq for RoundedFloat {
fn eq(&self, other: &Self) -> bool {
let value = (100000.0 * self.0) as i64;
let other_value = (100000.0 * other.0) as i64;
value == other_value
}
}
impl Eq for RoundedFloat {
fn assert_receiver_is_total_eq(&self) {}
}
impl std::hash::Hash for RoundedColor3 {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
RoundedFloat(self.0.r).hash(state);
RoundedFloat(self.0.g).hash(state);
RoundedFloat(self.0.b).hash(state);
}
}
impl PartialEq for RoundedColor3 {
fn eq(&self, other: &Self) -> bool {
RoundedFloat(self.0.r) == RoundedFloat(other.0.r)
&& RoundedFloat(self.0.g) == RoundedFloat(other.0.g)
&& RoundedFloat(self.0.b) == RoundedFloat(other.0.b)
}
}
impl std::hash::Hash for RoundedColor4 {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
RoundedFloat(self.0.a).hash(state);
RoundedFloat(self.0.r).hash(state);
RoundedFloat(self.0.g).hash(state);
RoundedFloat(self.0.b).hash(state);
}
}
impl PartialEq for RoundedColor4 {
fn eq(&self, other: &Self) -> bool {
RoundedFloat(self.0.r) == RoundedFloat(other.0.r)
&& RoundedFloat(self.0.g) == RoundedFloat(other.0.g)
&& RoundedFloat(self.0.b) == RoundedFloat(other.0.b)
&& RoundedFloat(self.0.a) == RoundedFloat(other.0.a)
}
}
impl Eq for RoundedColor4 {
fn assert_receiver_is_total_eq(&self) {}
}
impl Eq for RoundedColor3 {
fn assert_receiver_is_total_eq(&self) {}
}
#[derive(Clone)]
pub struct RoundedVector2(pub Vector2);
impl std::hash::Hash for RoundedVector2 {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
RoundedFloat(self.0.x).hash(state);
RoundedFloat(self.0.y).hash(state);
}
}
impl PartialEq for RoundedVector2 {
fn eq(&self, other: &Self) -> bool {
RoundedFloat(self.0.x) == RoundedFloat(other.0.x)
&& RoundedFloat(self.0.y) == RoundedFloat(other.0.y)
}
}
impl Eq for RoundedVector2 {
fn assert_receiver_is_total_eq(&self) {}
}
impl Default for RoundedVector2 {
fn default() -> Self {
Self(Vector2 { x: 0.0, y: 0.0 })
}
}
#[derive(Clone, Hash, PartialEq, Eq)]
pub enum DclSourceTex {
Texture(String),
AvatarTexture(String),
VideoTexture(SceneEntityId),
}
impl Default for DclSourceTex {
fn default() -> DclSourceTex {
DclSourceTex::Texture("".to_string())
}
}
#[derive(Clone, Hash, PartialEq, Eq)]
pub struct DclTexture {
pub wrap_mode: TextureWrapMode,
pub filter_mode: TextureFilterMode,
pub source: DclSourceTex,
/// UV offset, default = (0, 0). Only applies to main texture in PbrMaterial/UnlitMaterial.
pub offset: RoundedVector2,
/// UV tiling/scale, default = (1, 1). Only applies to main texture in PbrMaterial/UnlitMaterial.
pub tiling: RoundedVector2,
}
impl Default for DclTexture {
fn default() -> Self {
Self {
wrap_mode: TextureWrapMode::TwmClamp,
filter_mode: TextureFilterMode::TfmBilinear,
source: DclSourceTex::default(),
offset: RoundedVector2(Vector2 { x: 0.0, y: 0.0 }),
tiling: RoundedVector2(Vector2 { x: 1.0, y: 1.0 }),
}
}
}
impl From<&TextureUnion> for Option<DclTexture> {
fn from(value: &TextureUnion) -> Option<DclTexture> {
let texture = value.tex.as_ref()?;
let wrap_mode = match texture {
Tex::Texture(texture) => *texture
.wrap_mode
.as_ref()
.unwrap_or(&TextureWrapMode::TwmClamp.into()),
Tex::AvatarTexture(avatar_texture) => *avatar_texture
.wrap_mode
.as_ref()
.unwrap_or(&TextureWrapMode::TwmClamp.into()),
Tex::VideoTexture(video_texture) => *video_texture
.wrap_mode
.as_ref()
.unwrap_or(&TextureWrapMode::TwmClamp.into()),
Tex::UiTexture(_) => todo!("UI Texture not implemented"),
};
let filter_mode = match texture {
Tex::Texture(texture) => *texture
.filter_mode
.as_ref()
.unwrap_or(&TextureFilterMode::TfmBilinear.into()),
Tex::AvatarTexture(avatar_texture) => *avatar_texture
.filter_mode
.as_ref()
.unwrap_or(&TextureFilterMode::TfmBilinear.into()),
Tex::VideoTexture(video_texture) => *video_texture
.filter_mode
.as_ref()
.unwrap_or(&TextureFilterMode::TfmBilinear.into()),
Tex::UiTexture(_) => todo!("UI Texture not implemented"),
};
let wrap_mode = TextureWrapMode::from_i32(wrap_mode).unwrap_or(TextureWrapMode::TwmClamp);
let filter_mode =
TextureFilterMode::from_i32(filter_mode).unwrap_or(TextureFilterMode::TfmBilinear);
match value.tex.as_ref()? {
Tex::Texture(texture) => {
// Only Texture type has offset and tiling fields
let offset = texture
.offset
.as_ref()
.map(|v| RoundedVector2(v.clone()))
.unwrap_or_else(|| RoundedVector2(Vector2 { x: 0.0, y: 0.0 }));
let tiling = texture
.tiling
.as_ref()
.map(|v| RoundedVector2(v.clone()))
.unwrap_or_else(|| RoundedVector2(Vector2 { x: 1.0, y: 1.0 }));
Some(DclTexture {
wrap_mode,
filter_mode,
source: DclSourceTex::Texture(texture.src.clone()),
offset,
tiling,
})
}
Tex::AvatarTexture(avatar_texture) => Some(DclTexture {
wrap_mode,
filter_mode,
source: DclSourceTex::AvatarTexture(avatar_texture.user_id.clone()),
offset: RoundedVector2(Vector2 { x: 0.0, y: 0.0 }),
tiling: RoundedVector2(Vector2 { x: 1.0, y: 1.0 }),
}),
Tex::VideoTexture(video_texture) => Some(DclTexture {
wrap_mode,
filter_mode,
source: DclSourceTex::VideoTexture(SceneEntityId::from_i32(
video_texture.video_player_entity as i32,
)),
offset: RoundedVector2(Vector2 { x: 0.0, y: 0.0 }),
tiling: RoundedVector2(Vector2 { x: 1.0, y: 1.0 }),
}),
Tex::UiTexture(_) => todo!("UI Texture not implemented"),
}
}
}
impl DclTexture {
fn with_hash(&mut self, content_mapping_files: &ContentMappingAndUrlRef) {
if let DclSourceTex::Texture(file_path) = &mut self.source {
let content_hash = content_mapping_files.get_hash(file_path.as_str());
if content_hash.is_none() {
return;
}
if let Some(content_hash) = content_hash {
*file_path = content_hash.to_string();
}
}
}
pub fn from_proto_with_hash(
texture: &Option<TextureUnion>,
content_mapping_files: &ContentMappingAndUrlRef,
) -> Option<Self> {
let value: Option<DclTexture> = texture.as_ref()?.into();
if let Some(mut value) = value {
value.with_hash(content_mapping_files);
Some(value)
} else {
None
}
}
}
#[derive(Clone, Hash, PartialEq, Eq)]
pub struct DclUnlitMaterial {
pub texture: Option<DclTexture>,
pub alpha_test: RoundedFloat,
pub cast_shadows: bool,
pub diffuse_color: RoundedColor4,
}
#[derive(Clone, Hash, PartialEq, Eq)]
pub struct DclPbrMaterial {
pub texture: Option<DclTexture>,
pub alpha_test: RoundedFloat,
pub cast_shadows: bool,
pub alpha_texture: Option<DclTexture>,
pub emissive_texture: Option<DclTexture>,
pub bump_texture: Option<DclTexture>,
pub albedo_color: RoundedColor4,
pub emissive_color: RoundedColor3,
pub reflectivity_color: RoundedColor3,
pub transparency_mode: MaterialTransparencyMode,
pub metallic: RoundedFloat,
pub roughness: RoundedFloat,
pub specular_intensity: RoundedFloat,
pub emissive_intensity: RoundedFloat,
pub direct_intensity: RoundedFloat,
}
#[derive(Clone, Hash, PartialEq, Eq)]
#[allow(clippy::large_enum_variant)]
pub enum DclMaterial {
Unlit(DclUnlitMaterial),
Pbr(DclPbrMaterial),
}
impl DclMaterial {
pub fn get_textures(&self) -> Vec<&Option<DclTexture>> {
match self {
DclMaterial::Unlit(unlit_material) => {
vec![&unlit_material.texture]
}
DclMaterial::Pbr(pbr) => {
vec![
&pbr.texture,
&pbr.alpha_texture,
&pbr.emissive_texture,
&pbr.bump_texture,
]
}
}
}
}
// Default from .proto comments
impl Default for DclPbrMaterial {
fn default() -> Self {
Self {
texture: None,
alpha_test: RoundedFloat(0.5),
cast_shadows: true,
alpha_texture: None,
emissive_texture: None,
bump_texture: None,
albedo_color: RoundedColor4(Color4::white()),
emissive_color: RoundedColor3(Color3::black()),
reflectivity_color: RoundedColor3(Color3::white()),
transparency_mode: MaterialTransparencyMode::MtmAuto,
metallic: RoundedFloat(0.5),
roughness: RoundedFloat(0.5),
specular_intensity: RoundedFloat(1.0),
emissive_intensity: RoundedFloat(2.0),
direct_intensity: RoundedFloat(1.0),
}
}
}
// Default from .proto comments
impl Default for DclUnlitMaterial {
fn default() -> Self {
Self {
texture: None,
alpha_test: RoundedFloat(0.5),
cast_shadows: true,
diffuse_color: RoundedColor4(Color4::white()),
}
}
}
impl DclMaterial {
pub fn from_proto(
source: &pb_material::Material,
content_mapping_files: &ContentMappingAndUrlRef,
) -> Self {
match source {
pb_material::Material::Pbr(pbr) => {
let mut value: DclPbrMaterial = DclPbrMaterial {
// Fill defined values from pbr
texture: DclTexture::from_proto_with_hash(&pbr.texture, content_mapping_files),
alpha_texture: DclTexture::from_proto_with_hash(
&pbr.alpha_texture,
content_mapping_files,
),
bump_texture: DclTexture::from_proto_with_hash(
&pbr.bump_texture,
content_mapping_files,
),
emissive_texture: DclTexture::from_proto_with_hash(
&pbr.emissive_texture,
content_mapping_files,
),
..Default::default()
};
if pbr.alpha_test.is_some() {
value.alpha_test = pbr.alpha_test.as_ref().unwrap().into();
}
if pbr.cast_shadows.is_some() {
value.cast_shadows = *pbr.cast_shadows.as_ref().unwrap();
}
if pbr.albedo_color.is_some() {
value.albedo_color = RoundedColor4(pbr.albedo_color.as_ref().unwrap().clone());
}
if pbr.emissive_color.is_some() {
value.emissive_color =
RoundedColor3(pbr.emissive_color.as_ref().unwrap().clone());
}
if pbr.reflectivity_color.is_some() {
value.reflectivity_color =
RoundedColor3(pbr.reflectivity_color.as_ref().unwrap().clone());
}
if pbr.transparency_mode.is_some() {
value.transparency_mode = MaterialTransparencyMode::from_i32(
*pbr.transparency_mode.as_ref().unwrap(),
)
.unwrap_or_default();
}
if pbr.metallic.is_some() {
value.metallic = pbr.metallic.as_ref().unwrap().into();
}
if pbr.roughness.is_some() {
value.roughness = pbr.roughness.as_ref().unwrap().into();
}
if pbr.specular_intensity.is_some() {
value.specular_intensity = pbr.specular_intensity.as_ref().unwrap().into();
}
if pbr.emissive_intensity.is_some() {
value.emissive_intensity = pbr.emissive_intensity.as_ref().unwrap().into();
}
if pbr.direct_intensity.is_some() {
value.direct_intensity = pbr.direct_intensity.as_ref().unwrap().into();
}
DclMaterial::Pbr(value)
}
pb_material::Material::Unlit(unlit) => {
let mut value = DclUnlitMaterial {
texture: DclTexture::from_proto_with_hash(
&unlit.texture,
content_mapping_files,
),
..Default::default()
};
if unlit.alpha_test.is_some() {
value.alpha_test = unlit.alpha_test.as_ref().unwrap().into();
}
if unlit.cast_shadows.is_some() {
value.cast_shadows = *unlit.cast_shadows.as_ref().unwrap();
}
if unlit.diffuse_color.is_some() {
value.diffuse_color =
RoundedColor4(unlit.diffuse_color.as_ref().unwrap().clone());
}
DclMaterial::Unlit(value)
}
}
}
}