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
mod pipeline;
mod render;
mod vertex;
pub use pipeline::*;
pub use render::*;
pub use vertex::*;
use crate::{
AtlasSet, Bounds, CameraView, Color, DrawOrder, FlipStyle, GpuRenderer,
Index, Vec2, Vec3, Vec4, instance_buffer::OrderedIndex,
};
/// Basic and Fast AnimImage Rendering Type. Best used for Sprites and Objects in the world.
///
#[derive(Clone, Debug, PartialEq)]
pub struct AnimImage {
/// Position of the object
pub pos: Vec3,
/// Height and Width
pub size: Vec2,
/// Static texture offsets or animation frame positions
pub uv: Vec4,
/// Color.
pub color: Color,
/// frames, frames_per_row: this will cycle thru
/// frames per row at the uv start.
pub frames: Vec2,
/// in millsecs 1000 = 1sec
pub switch_time: u32,
/// turn on animation if set.
pub animate: bool,
/// Global Camera the Shader will use to render the object with
pub camera_view: CameraView,
/// Texture area location in Atlas.
pub texture: Option<usize>,
/// Buffer's store Index.
pub store_id: Index,
/// Ordering Type, used to order the Stores in the buffers.
pub order: DrawOrder,
/// Clip bounds if enabled in the renderer.
pub bounds: Option<Bounds>,
/// Directional Flip.
pub flip_style: FlipStyle,
/// direct angle of rotation from the center Axis.
pub rotation_angle: f32,
/// When true tells system to update the buffers.
pub changed: bool,
}
impl AnimImage {
/// Creates a new [`AnimImage`] with rendering order layer, pos, size and uv.
///
/// order_layer: Rendering Order Layer used in DrawOrder.
pub fn new(
texture: Option<usize>,
renderer: &mut GpuRenderer,
pos: Vec3,
size: Vec2,
uv: Vec4,
order_layer: u32,
) -> Self {
Self {
pos,
size,
uv,
frames: Vec2::default(),
switch_time: 0,
animate: false,
camera_view: CameraView::default(),
color: Color::rgba(255, 255, 255, 255),
texture,
store_id: renderer.new_ibo_store(
bytemuck::bytes_of(&AnimImageVertex::default()).len(),
),
order: DrawOrder::new(false, Vec3::default(), order_layer),
bounds: None,
flip_style: FlipStyle::None,
rotation_angle: 0.0,
changed: true,
}
}
/// Unloads the [`AnimImage`] from the Instance Buffers Store.
///
pub fn unload(self, renderer: &mut GpuRenderer) {
renderer.remove_ibo_store(self.store_id);
}
/// Updates the [`AnimImage`]'s order_override.
/// Use this after calls to set_position to set it to a specific rendering order.
///
pub fn set_order_override(&mut self, order_override: Vec3) -> &mut Self {
self.order.set_pos(order_override);
self
}
/// Updates the [`AnimImage`]'s Optional Clipping Bounds.
///
pub fn update_bounds(&mut self, bounds: Option<Bounds>) -> &mut Self {
self.bounds = bounds;
self
}
/// Updates the [`AnimImage`]'s [`FlipStyle`].
///
pub fn set_flip_style(&mut self, flip_style: FlipStyle) -> &mut Self {
self.changed = true;
self.flip_style = flip_style;
self
}
/// Updates the [`AnimImage`]'s rotation.
///
pub fn set_rotation_angle(&mut self, rotation_angle: f32) -> &mut Self {
self.changed = true;
self.rotation_angle = rotation_angle;
self
}
/// Updates the [`AnimImage`]'s position.
///
pub fn set_pos(&mut self, pos: Vec3) -> &mut Self {
self.changed = true;
self.pos = pos;
self.order.set_pos(pos);
self
}
/// Updates the [`AnimImage`]'s width and height.
///
pub fn set_size(&mut self, size: Vec2) -> &mut Self {
self.changed = true;
self.size = size;
self
}
/// Updates the [`AnimImage`]'s animation frames.
///
pub fn set_frames(&mut self, frames: Vec2) -> &mut Self {
self.changed = true;
self.frames = frames;
self
}
/// Updates the [`AnimImage`]'s animate boolean.
///
pub fn set_animate(&mut self, animate: bool) -> &mut Self {
self.changed = true;
self.animate = animate;
self
}
/// Updates the [`AnimImage`]'s texture x, y, w, h
///
pub fn set_uv(&mut self, uv: Vec4) -> &mut Self {
self.changed = true;
self.uv = uv;
self
}
/// Updates the [`AnimImage`]'s rendering layer.
///
pub fn set_order_layer(&mut self, order_layer: u32) -> &mut Self {
self.order.order_layer = order_layer;
self
}
/// Updates the [`AnimImage`]'s Texture ID.
///
pub fn set_texture(&mut self, texture: Option<usize>) -> &mut Self {
self.changed = true;
self.texture = texture;
self
}
/// Updates the [`AnimImage`]'s [`Color`].
///
pub fn set_color(&mut self, color: Color) -> &mut Self {
self.changed = true;
self.order.alpha = color.a() < 255;
self.color = color;
self
}
/// Updates the [`AnimImage`]'s [`DrawOrder`]'s is Alpha.
/// Use this after set_color to overide the alpha sorting.
///
pub fn set_order_alpha(&mut self, alpha: bool) -> &mut Self {
self.order.alpha = alpha;
self
}
/// Updates the [`AnimImage`]'s [`CameraView`].
///
pub fn set_camera_view(&mut self, camera_view: CameraView) -> &mut Self {
self.changed = true;
self.camera_view = camera_view;
self
}
/// Updates the [`AnimImage`]'s animation switch time.
///
pub fn set_switch_time(&mut self, switch_time: u32) -> &mut Self {
self.changed = true;
self.switch_time = switch_time;
self
}
/// Updates the [`AnimImage`]'s Buffers to prepare them for rendering.
///
fn create_quad(
&mut self,
renderer: &mut GpuRenderer,
atlas: &mut AtlasSet,
) {
let allocation = match &self.texture {
Some(id) => {
if let Some(allocation) = atlas.get(*id) {
allocation
} else {
return;
}
}
None => return,
};
let (u, v, width, height) = allocation.rect();
let tex_data = (
self.uv.x + u as f32,
self.uv.y + v as f32,
self.uv.z.min(width as f32),
self.uv.w.min(height as f32),
);
let instance = AnimImageVertex {
pos: self.pos.to_array(),
size: self.size.to_array(),
tex_data: tex_data.into(),
color: self.color.0,
frames: self.frames.to_array(),
animate: u32::from(self.animate),
camera_view: self.camera_view as u32,
time: self.switch_time,
layer: allocation.layer as i32,
flip_style: self.flip_style as u32,
angle: self.rotation_angle,
};
if let Some(store) = renderer.get_ibo_store_mut(self.store_id) {
let bytes = bytemuck::bytes_of(&instance);
if bytes.len() != store.store.len() {
store.store.resize_with(bytes.len(), || 0);
}
store.store.copy_from_slice(bytes);
store.changed = true;
}
}
/// Used to check and update the vertex array.
/// Returns a [`OrderedIndex`] used in Rendering.
///
pub fn update(
&mut self,
renderer: &mut GpuRenderer,
atlas: &mut AtlasSet,
) -> OrderedIndex {
if self.changed {
self.create_quad(renderer, atlas);
self.changed = false;
}
OrderedIndex::new(self.order, self.store_id)
}
}