radar-g/gi/src/shaders/proj.rs
2024-09-25 15:57:15 +08:00

48 lines
1.3 KiB
Rust

use super::CodePiece;
use crate::impl_code_piece;
use glsl::parser::Parse;
use glsl::syntax::ShaderStage;
use glsl::syntax::TranslationUnit;
use glsl::transpiler::glsl::show_translation_unit;
pub struct Mercator(pub ShaderStage);
impl Mercator {
pub fn new() -> Self {
let raw = {
"
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(ShaderStage::parse(raw).unwrap())
}
}
impl_code_piece!(Mercator, 0);