This commit is contained in:
Dongho Kim
2025-12-16 00:42:33 +09:00
parent 45807e3a90
commit 8edb92b25d
6 changed files with 210 additions and 55 deletions

View File

@@ -33,6 +33,8 @@ use tiles::{fetch_cached, get_visible_tiles, get_parent_tile};
use labels::update_labels;
use pipelines::{
Vertex,
ColoredVertex,
create_simple_pipeline,
create_building_pipeline,
create_water_pipeline,
create_water_line_pipeline,
@@ -420,7 +422,7 @@ pub async fn run() {
let mut landuse_residential_vertex_data: Vec<Vertex> = Vec::new();
let mut landuse_sand_vertex_data: Vec<Vertex> = Vec::new();
let mut water_vertex_data: Vec<Vertex> = Vec::new();
let mut railway_vertex_data: Vec<Vertex> = Vec::new();
let mut railway_vertex_data: Vec<ColoredVertex> = Vec::new();
let mut water_line_vertex_data: Vec<Vertex> = Vec::new();
// Process ways (roads)
@@ -518,17 +520,23 @@ pub async fn run() {
}
}
// Process railways
// Process railways with colors
if let Some(railway_list) = state_guard.railways.get(&tile) {
for railway in railway_list {
// Parse color from tags (format: "#RRGGBB" or "#RGB")
let color = railway.tags.get("colour")
.or(railway.tags.get("color"))
.map(|c| parse_hex_color(c))
.unwrap_or([0.0, 0.0, 0.0]); // Default: no color (shader uses grey)
// Parse all points first
let mut rail_points: Vec<Vertex> = Vec::new();
let mut rail_points: Vec<ColoredVertex> = Vec::new();
for chunk in railway.points.chunks(8) {
if chunk.len() < 8 { break; }
let lat = f32::from_le_bytes(chunk[0..4].try_into().unwrap_or([0u8; 4]));
let lon = f32::from_le_bytes(chunk[4..8].try_into().unwrap_or([0u8; 4]));
let (x, y) = project(lat as f64, lon as f64);
rail_points.push(Vertex { position: [x, y] });
rail_points.push(ColoredVertex { position: [x, y], color });
}
// For LineList: push pairs of vertices for each segment
@@ -897,3 +905,25 @@ pub async fn run() {
}
}).unwrap();
}
/// Parse a hex color string (e.g., "#FF0000" or "#F00") into RGB floats [0.0-1.0]
fn parse_hex_color(hex: &str) -> [f32; 3] {
let hex = hex.trim_start_matches('#');
if hex.len() == 6 {
// #RRGGBB format
let r = u8::from_str_radix(&hex[0..2], 16).unwrap_or(0) as f32 / 255.0;
let g = u8::from_str_radix(&hex[2..4], 16).unwrap_or(0) as f32 / 255.0;
let b = u8::from_str_radix(&hex[4..6], 16).unwrap_or(0) as f32 / 255.0;
[r, g, b]
} else if hex.len() == 3 {
// #RGB format (shorthand)
let r = u8::from_str_radix(&hex[0..1], 16).unwrap_or(0) as f32 / 15.0;
let g = u8::from_str_radix(&hex[1..2], 16).unwrap_or(0) as f32 / 15.0;
let b = u8::from_str_radix(&hex[2..3], 16).unwrap_or(0) as f32 / 15.0;
[r, g, b]
} else {
// Invalid format, return black (will trigger grey fallback in shader)
[0.0, 0.0, 0.0]
}
}

View File

@@ -23,6 +23,35 @@ impl Vertex {
}
}
/// GPU vertex with 2D position and RGB color (for railways with line colors)
#[repr(C)]
#[derive(Copy, Clone, Debug, bytemuck::Pod, bytemuck::Zeroable)]
pub struct ColoredVertex {
pub position: [f32; 2],
pub color: [f32; 3],
}
impl ColoredVertex {
pub fn desc() -> wgpu::VertexBufferLayout<'static> {
wgpu::VertexBufferLayout {
array_stride: std::mem::size_of::<ColoredVertex>() as wgpu::BufferAddress,
step_mode: wgpu::VertexStepMode::Vertex,
attributes: &[
wgpu::VertexAttribute {
offset: 0,
shader_location: 0,
format: wgpu::VertexFormat::Float32x2,
},
wgpu::VertexAttribute {
offset: std::mem::size_of::<[f32; 2]>() as wgpu::BufferAddress,
shader_location: 1,
format: wgpu::VertexFormat::Float32x3,
}
]
}
}
}
/// Create a simple render pipeline with standard configuration
pub fn create_simple_pipeline(
device: &wgpu::Device,

View File

@@ -7,7 +7,7 @@ pub mod roads;
pub mod landuse;
pub mod railway;
pub use common::{Vertex, create_simple_pipeline};
pub use common::{Vertex, ColoredVertex, create_simple_pipeline};
pub use building::create_building_pipeline;
pub use water::{create_water_pipeline, create_water_line_pipeline};
pub use roads::{

View File

@@ -1,6 +1,6 @@
//! Railway render pipeline
use super::common::Vertex;
use super::common::ColoredVertex;
pub fn create_railway_pipeline(
device: &wgpu::Device,
@@ -19,10 +19,12 @@ pub fn create_railway_pipeline(
struct VertexInput {
@location(0) position: vec2<f32>,
@location(1) color: vec3<f32>,
};
struct VertexOutput {
@builtin(position) clip_position: vec4<f32>,
@location(0) color: vec3<f32>,
};
@vertex
@@ -35,21 +37,25 @@ pub fn create_railway_pipeline(
let x = world_pos.x * camera.params.x + camera.params.z;
let y = world_pos.y * camera.params.y + camera.params.w;
// Globe Effect: Spherize
// let r2 = x*x + y*y;
// let w = 1.0 + r2 * 0.5;
out.clip_position = vec4<f32>(x, y, 0.0, 1.0);
out.color = model.color;
return out;
}
@fragment
fn fs_main(in: VertexOutput) -> @location(0) vec4<f32> {
let is_dark = camera.theme.x;
// Light: #808080 (grey), Dark: #5a5a5a (darker grey)
let color = mix(vec3<f32>(0.5, 0.5, 0.5), vec3<f32>(0.35, 0.35, 0.35), is_dark);
return vec4<f32>(color, 1.0);
// Use vertex color if it has any value, otherwise use default grey
let has_color = in.color.r > 0.01 || in.color.g > 0.01 || in.color.b > 0.01;
if (has_color) {
return vec4<f32>(in.color, 1.0);
} else {
// Fallback: Light: #808080 (grey), Dark: #5a5a5a (darker grey)
let is_dark = camera.theme.x;
let color = mix(vec3<f32>(0.5, 0.5, 0.5), vec3<f32>(0.35, 0.35, 0.35), is_dark);
return vec4<f32>(color, 1.0);
}
}
"#)),
});
@@ -67,7 +73,7 @@ pub fn create_railway_pipeline(
module: &shader,
entry_point: "vs_main",
buffers: &[
Vertex::desc(),
ColoredVertex::desc(),
],
},
fragment: Some(wgpu::FragmentState {