| 134 | /// - `material_id`: 材质 ID | Material ID |
| 135 | #[allow(clippy::too_many_arguments)] |
| 136 | pub fn add_sprite( |
| 137 | &mut self, |
| 138 | x: f32, |
| 139 | y: f32, |
| 140 | width: f32, |
| 141 | height: f32, |
| 142 | rotation: f32, |
| 143 | origin_x: f32, |
| 144 | origin_y: f32, |
| 145 | u0: f32, |
| 146 | v0: f32, |
| 147 | u1: f32, |
| 148 | v1: f32, |
| 149 | color: u32, |
| 150 | texture_id: u32, |
| 151 | material_id: u32, |
| 152 | ) -> bool { |
| 153 | if self.sprite_count >= self.max_sprites { |
| 154 | return false; |
| 155 | } |
| 156 | |
| 157 | // 解包颜色 |
| 158 | let r = ((color >> 24) & 0xFF) as f32 / 255.0; |
| 159 | let g = ((color >> 16) & 0xFF) as f32 / 255.0; |
| 160 | let b = ((color >> 8) & 0xFF) as f32 / 255.0; |
| 161 | let a = (color & 0xFF) as f32 / 255.0; |
| 162 | let color_arr = [r, g, b, a]; |
| 163 | |
| 164 | // 计算宽高比 |
| 165 | let aspect = if height != 0.0 { width / height } else { 1.0 }; |
| 166 | |
| 167 | // 计算顶点位置(考虑原点和旋转) |
| 168 | let ox = origin_x * width; |
| 169 | let oy = origin_y * height; |
| 170 | |
| 171 | let cos_r = rotation.cos(); |
| 172 | let sin_r = rotation.sin(); |
| 173 | |
| 174 | // 四个角的局部坐标 |
| 175 | let corners = [ |
| 176 | (-ox, -oy), // 左上 |
| 177 | (width - ox, -oy), // 右上 |
| 178 | (width - ox, height - oy), // 右下 |
| 179 | (-ox, height - oy), // 左下 |
| 180 | ]; |
| 181 | |
| 182 | // UV 坐标 |
| 183 | let uvs = [ |
| 184 | [u0, v0], // 左上 |
| 185 | [u1, v0], // 右上 |
| 186 | [u1, v1], // 右下 |
| 187 | [u0, v1], // 左下 |
| 188 | ]; |
| 189 | |
| 190 | // 添加四个顶点 |
| 191 | for i in 0..4 { |
| 192 | let (lx, ly) = corners[i]; |
| 193 | let rx = lx * cos_r - ly * sin_r + x; |