use super::CodePiece; use crate::impl_code_piece; use glsl::syntax::ShaderStage; use glsl::syntax::TranslationUnit; use glsl::transpiler::glsl::show_translation_unit; use glsl_quasiquote::glsl; pub struct Mercator(pub ShaderStage); impl Mercator { pub fn new() -> Self { let raw = glsl! { uniform float R; vec2 mercatorProject(vec2 loc) { // 将经纬度从度转换为弧度 float lonRad = radians(loc.x); float latRad = radians(loc.y); // X轴投影 (线性映射经度) float x = R * lonRad; // Y轴投影 (非线性映射纬度) float y = R * log(tan(latRad / 2.0 + radians(45.0))); return vec2(x, y); } vec2 mercatorInverse(vec2 loc) { // 计算经度 (lon),线性反映射 float lon = degrees(loc.x / R); // 将弧度转换为度数 // 计算纬度 (lat),非线性反映射 float lat = degrees(2.0 * atan(exp(loc.y / R)) - radians(90.0)); return vec2(lon, lat); // 返回经纬度 (lon, lat) } }; Self(raw) } } impl_code_piece!(Mercator, 0);