comfy_core/
fast_sprite.rs

1use crate::*;
2
3pub fn draw_sprite_rot(
4    texture: TextureHandle,
5    position: Vec2,
6    tint: Color,
7    z_index: i32,
8    rotation: f32,
9    dest_size: Vec2,
10) {
11    let _span = span!("draw_sprite_rot");
12
13    let vertices = if rotation != 0.0 {
14        simple_rotated_rect(
15            position.extend(z_index as f32),
16            tint,
17            dest_size,
18            rotation,
19        )
20    } else {
21        simple_rect(position.extend(z_index as f32), tint, dest_size)
22    };
23
24    const QUAD_INDICES_U32: &[u32] = &[0, 2, 1, 0, 3, 2];
25
26    let mesh = Mesh {
27        origin: position.extend(z_index as f32),
28        vertices: vertices.into(),
29        indices: QUAD_INDICES_U32.into(),
30        z_index,
31        texture: Some(texture),
32        y_sort_offset: 0.0,
33    };
34
35    draw_mesh_ex(mesh, BlendMode::None);
36}
37
38pub fn simple_rotated_rect(
39    position: Vec3,
40    color: Color,
41    dest_size: Vec2,
42    rotation: f32,
43) -> [SpriteVertex; 4] {
44    let x = position.x;
45    let y = position.y;
46
47    let (w, h) = (dest_size.x, dest_size.y);
48
49    let pivot = vec2(x + w / 2.0, y + h / 2.0);
50    let m = pivot - vec2(w / 2.0, h / 2.0);
51
52    let r = rotation;
53
54    let p = [
55        vec2(x, y) - pivot,
56        vec2(x + w, y) - pivot,
57        vec2(x + w, y + h) - pivot,
58        vec2(x, y + h) - pivot,
59    ];
60
61    let p = [
62        vec2(
63            p[0].x * r.cos() - p[0].y * r.sin(),
64            p[0].x * r.sin() + p[0].y * r.cos(),
65        ) + m,
66        vec2(
67            p[1].x * r.cos() - p[1].y * r.sin(),
68            p[1].x * r.sin() + p[1].y * r.cos(),
69        ) + m,
70        vec2(
71            p[2].x * r.cos() - p[2].y * r.sin(),
72            p[2].x * r.sin() + p[2].y * r.cos(),
73        ) + m,
74        vec2(
75            p[3].x * r.cos() - p[3].y * r.sin(),
76            p[3].x * r.sin() + p[3].y * r.cos(),
77        ) + m,
78    ];
79
80    [
81        SpriteVertex::new(
82            vec3(p[0].x, p[0].y, position.z),
83            vec2(0.0, 0.0),
84            color,
85        ),
86        SpriteVertex::new(
87            vec3(p[1].x, p[1].y, position.z),
88            vec2(1.0, 0.0),
89            color,
90        ),
91        SpriteVertex::new(
92            vec3(p[2].x, p[2].y, position.z),
93            vec2(1.0, 1.0),
94            color,
95        ),
96        SpriteVertex::new(
97            vec3(p[3].x, p[3].y, position.z),
98            vec2(0.0, 1.0),
99            color,
100        ),
101    ]
102}
103
104pub fn simple_rect(
105    position: Vec3,
106    color: Color,
107    dest_size: Vec2,
108) -> [SpriteVertex; 4] {
109    let x = position.x;
110    let y = position.y;
111
112    let (w, h) = (dest_size.x, dest_size.y);
113
114    let m = vec2(w / 2.0, h / 2.0);
115
116    let p = [
117        vec2(x, y) - m,
118        vec2(x + w, y) - m,
119        vec2(x + w, y + h) - m,
120        vec2(x, y + h) - m,
121    ];
122
123    [
124        SpriteVertex::new(
125            vec3(p[0].x, p[0].y, position.z),
126            vec2(0.0, 0.0),
127            color,
128        ),
129        SpriteVertex::new(
130            vec3(p[1].x, p[1].y, position.z),
131            vec2(1.0, 0.0),
132            color,
133        ),
134        SpriteVertex::new(
135            vec3(p[2].x, p[2].y, position.z),
136            vec2(1.0, 1.0),
137            color,
138        ),
139        SpriteVertex::new(
140            vec3(p[3].x, p[3].y, position.z),
141            vec2(0.0, 1.0),
142            color,
143        ),
144    ]
145}