use glam::{DMat4, DVec2, DVec3, Vec2};
use mln_gfx::shaders::layer_ubo::ProjectionUBO;
use mln_style::constants::EXTENT;
use mln_style::types::TranslateAnchorType;
pub use crate::camera::ProjectionData;
use crate::camera::View;
use crate::draw::draws::LayerFrame;
use crate::geo::constants::M2PI;
use crate::render::paint_parameters::TransformParameters;
use crate::render::tile::RenderTile;
use crate::tile::UnwrappedTileID;
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct TileTransform {
pub translate: [f32; 2],
pub anchor: TranslateAnchorType,
pub near_clipped: bool,
pub pixel_aligned: bool,
}
impl TileTransform {
pub const NONE: TileTransform = TileTransform {
translate: [0.0, 0.0],
anchor: TranslateAnchorType::Viewport,
near_clipped: false,
pixel_aligned: false,
};
}
impl Default for TileTransform {
fn default() -> Self {
TileTransform::NONE
}
}
#[must_use]
pub fn tile_projection(tile_id: &UnwrappedTileID, frame: &LayerFrame<'_>, transform: TileTransform) -> ProjectionData {
let matrix = tile_matrix(tile_id, frame.view, frame.transform_params, &transform);
let mut data = frame.view.projection_data_for_matrix(tile_id, &matrix);
let translate = RenderTile::tile_unit_translation(tile_id, transform.translate, transform.anchor, frame.view);
data.translate = DVec2::new(f64::from(translate[0]), f64::from(translate[1]));
data
}
impl ProjectionData {
#[must_use]
pub fn ubo(&self) -> ProjectionUBO {
let anchor = self.sphere_anchor;
let plane = self.clipping_plane;
ProjectionUBO {
matrix: cast_matrix(&(self.main_matrix * DMat4::from_translation(anchor))),
fallback_matrix: cast_matrix(&self.fallback_matrix),
tile_mercator_coords: self.tile_mercator_coords.as_vec4().to_array(),
clipping_plane: self.clipping_plane.as_vec4().to_array(),
projection_transition: self.projection_transition as f32,
depth_offset: self.depth_offset as f32,
translate: self.translate.as_vec2().to_array(),
anchor: anchor.extend(anchor.dot(plane.truncate()) + plane.w).as_vec4().to_array(),
anchor_angles: self.sphere_anchor_angles.as_vec4().to_array(),
}
}
}
#[must_use]
pub fn globe_extrude_scale(view: &View, tile_id: &UnwrappedTileID) -> f32 {
(f64::from(tile_id.pixels_to_tile_units(1.0, view.zoom() as f32))
/ (f64::from(EXTENT) * (1u64 << tile_id.canonical.z) as f64)
* M2PI
* view.projection().map_aligned_scale(view)) as f32
}
pub fn zero_to_one_clip_z(matrix: &mut DMat4) {
matrix.set_row(2, 2.0 * matrix.row(2) - matrix.row(3));
}
fn tile_matrix(
tile_id: &UnwrappedTileID,
view: &View,
transform_params: &TransformParameters,
transform: &TileTransform,
) -> DMat4 {
let projection = if transform.pixel_aligned {
&transform_params.aligned_proj_matrix
} else if transform.near_clipped {
&transform_params.near_clipped_proj_matrix
} else {
&transform_params.proj_matrix
};
let matrix = *projection * view.tile_matrix(tile_id);
let [x, y] = transform.translate;
if x == 0.0 && y == 0.0 {
return matrix;
}
let angle = if transform.anchor == TranslateAnchorType::Viewport { (-view.angle()) as f32 } else { 0.0 };
let translate = Vec2::from_angle(angle).rotate(Vec2::new(x, y));
let zoom = view.zoom() as f32;
let offset = DVec3::new(
f64::from(tile_id.pixels_to_tile_units(translate.x, zoom)),
f64::from(tile_id.pixels_to_tile_units(translate.y, zoom)),
0.0,
);
matrix * DMat4::from_translation(offset)
}
fn cast_matrix(m: &DMat4) -> [f32; 16] {
m.as_mat4().to_cols_array()
}