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
use minterpolate::InterpolationPrimitive;
use serde::{Deserialize, Serialize};
use amethyst_assets::Handle;
use amethyst_rendy::{
mtl::{Material, TextureOffset},
sprite::Sprite,
types::Texture,
};
use crate::{AnimationSampling, ApplyData, BlendMethod};
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
#[serde(untagged)]
pub enum MaterialPrimitive {
#[serde(skip)]
Texture(Handle<Texture>),
Offset((f32, f32), (f32, f32)),
}
impl InterpolationPrimitive for MaterialPrimitive {
fn add(&self, _: &Self) -> Self {
panic!("Cannot add MaterialPrimitive")
}
fn sub(&self, _: &Self) -> Self {
panic!("Cannot sub MaterialPrimitive")
}
fn mul(&self, _: f32) -> Self {
panic!("Cannot mul MaterialPrimitive")
}
fn dot(&self, _: &Self) -> f32 {
panic!("Cannot dot MaterialPrimitive")
}
fn magnitude2(&self) -> f32 {
panic!("Cannot magnitude2 MaterialPrimitive")
}
fn magnitude(&self) -> f32 {
panic!("Cannot magnitude MaterialPrimitive")
}
fn normalize(&self) -> Self {
panic!("Cannot normalize MaterialPrimitive")
}
}
impl From<Sprite> for MaterialPrimitive {
fn from(sprite: Sprite) -> Self {
let tex_coords = &sprite.tex_coords;
MaterialPrimitive::Offset(
(tex_coords.left, tex_coords.right),
(tex_coords.top, tex_coords.bottom),
)
}
}
impl<'a> From<&'a Sprite> for MaterialPrimitive {
fn from(sprite: &'a Sprite) -> Self {
let tex_coords = &sprite.tex_coords;
MaterialPrimitive::Offset(
(tex_coords.left, tex_coords.right),
(tex_coords.top, tex_coords.bottom),
)
}
}
#[derive(Debug, Clone, Copy, Hash, Eq, PartialEq, Serialize, Deserialize)]
pub enum MaterialChannel {
AlbedoTexture,
EmissionTexture,
NormalTexture,
MetallicRoughnessTexture,
AmbientOcclusionTexture,
CavityTexture,
UvOffset,
}
impl<'a> ApplyData<'a> for Material {
type ApplyData = ();
}
fn offset(offset: &TextureOffset) -> MaterialPrimitive {
MaterialPrimitive::Offset(offset.u, offset.v)
}
fn texture_offset(u: (f32, f32), v: (f32, f32)) -> TextureOffset {
TextureOffset { u, v }
}
impl AnimationSampling for Material {
type Primitive = MaterialPrimitive;
type Channel = MaterialChannel;
fn apply_sample(&mut self, channel: &Self::Channel, data: &Self::Primitive, _: &()) {
match (channel, data) {
(MaterialChannel::AlbedoTexture, MaterialPrimitive::Texture(i)) => {
self.albedo = i.clone();
}
(MaterialChannel::EmissionTexture, MaterialPrimitive::Texture(i)) => {
self.emission = i.clone();
}
(MaterialChannel::NormalTexture, MaterialPrimitive::Texture(i)) => {
self.normal = i.clone();
}
(MaterialChannel::MetallicRoughnessTexture, MaterialPrimitive::Texture(i)) => {
self.metallic_roughness = i.clone();
}
(MaterialChannel::AmbientOcclusionTexture, MaterialPrimitive::Texture(i)) => {
self.ambient_occlusion = i.clone();
}
(MaterialChannel::CavityTexture, MaterialPrimitive::Texture(i)) => {
self.cavity = i.clone();
}
(MaterialChannel::UvOffset, MaterialPrimitive::Offset(u, v)) => {
self.uv_offset = texture_offset(*u, *v)
}
_ => panic!("Bad combination of data in Material animation"),
}
}
fn current_sample(&self, channel: &Self::Channel, _: &()) -> Self::Primitive {
match *channel {
MaterialChannel::AlbedoTexture => MaterialPrimitive::Texture(self.albedo.clone()),
MaterialChannel::EmissionTexture => MaterialPrimitive::Texture(self.emission.clone()),
MaterialChannel::NormalTexture => MaterialPrimitive::Texture(self.normal.clone()),
MaterialChannel::MetallicRoughnessTexture => {
MaterialPrimitive::Texture(self.metallic_roughness.clone())
}
MaterialChannel::AmbientOcclusionTexture => {
MaterialPrimitive::Texture(self.ambient_occlusion.clone())
}
MaterialChannel::CavityTexture => MaterialPrimitive::Texture(self.cavity.clone()),
MaterialChannel::UvOffset => offset(&self.uv_offset),
}
}
fn default_primitive(_: &Self::Channel) -> Self::Primitive {
panic!("Blending is not applicable to Material animation")
}
fn blend_method(&self, _: &Self::Channel) -> Option<BlendMethod> {
None
}
}