Add z index for layers

This commit is contained in:
Maximilian Ammann 2022-03-25 15:28:18 +01:00
parent 3fe6865038
commit ebaacd7da3
5 changed files with 34 additions and 6 deletions

View File

@ -39,6 +39,8 @@ pub enum LayerPaint {
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct StyleLayer {
#[serde(skip)]
pub index: u32,
pub id: String,
#[serde(rename = "type")]
pub typ: String,
@ -62,6 +64,7 @@ pub struct StyleLayer {
impl Default for StyleLayer {
fn default() -> Self {
Self {
index: 0,
id: "id".to_string(),
typ: "fill".to_string(),
maxzoom: None,

View File

@ -23,6 +23,7 @@ impl Default for Style {
sources: Default::default(),
layers: vec![
StyleLayer {
index: 0,
id: "park".to_string(),
typ: "fill".to_string(),
maxzoom: None,
@ -35,6 +36,7 @@ impl Default for Style {
source_layer: Some("park".to_string()),
},
StyleLayer {
index: 1,
id: "landuse".to_string(),
typ: "fill".to_string(),
maxzoom: None,
@ -47,6 +49,7 @@ impl Default for Style {
source_layer: Some("landuse".to_string()),
},
StyleLayer {
index: 2,
id: "landcover".to_string(),
typ: "fill".to_string(),
maxzoom: None,
@ -59,6 +62,7 @@ impl Default for Style {
source_layer: Some("landcover".to_string()),
},
StyleLayer {
index: 3,
id: "1transportation".to_string(),
typ: "line".to_string(),
maxzoom: None,
@ -71,6 +75,7 @@ impl Default for Style {
source_layer: Some("transportation".to_string()),
},
StyleLayer {
index: 4,
id: "building".to_string(),
typ: "fill".to_string(),
maxzoom: None,
@ -83,6 +88,7 @@ impl Default for Style {
source_layer: Some("building".to_string()),
},
StyleLayer {
index: 4,
id: "water".to_string(),
typ: "fill".to_string(),
maxzoom: None,
@ -95,6 +101,7 @@ impl Default for Style {
source_layer: Some("water".to_string()),
},
StyleLayer {
index: 6,
id: "waterway".to_string(),
typ: "fill".to_string(),
maxzoom: None,
@ -107,6 +114,7 @@ impl Default for Style {
source_layer: Some("waterway".to_string()),
},
StyleLayer {
index: 7,
id: "boundary".to_string(),
typ: "line".to_string(),
maxzoom: None,

View File

@ -267,8 +267,8 @@ impl RenderState {
surface_config.width,
surface_config.height,
cgmath::Deg(110.0),
10.0,
600.0,
100.0,
2000.0,
);
Self {
@ -400,7 +400,11 @@ impl RenderState {
self.buffer_pool.update_tile_metadata(
&self.queue,
entry,
ShaderTileMetadata::new(transform.into(), zoom_factor),
ShaderTileMetadata::new(
transform.into(),
zoom_factor,
entry.style_layer.index as f32,
),
);
}
}
@ -470,7 +474,11 @@ impl RenderState {
*coords,
style_layer.clone(),
buffer,
ShaderTileMetadata::new(transform.into(), zoom_factor),
ShaderTileMetadata::new(
transform.into(),
zoom_factor,
style_layer.index as f32,
),
&feature_metadata,
);
}

View File

@ -142,6 +142,12 @@ pub mod tile {
format: wgpu::VertexFormat::Float32,
shader_location: 9,
},
wgpu::VertexAttribute {
offset: 4 * wgpu::VertexFormat::Float32x4.size()
+ wgpu::VertexFormat::Float32.size(),
format: wgpu::VertexFormat::Float32,
shader_location: 10,
},
],
},
// vertex style
@ -308,13 +314,15 @@ pub struct ShaderFeatureStyle {
pub struct ShaderTileMetadata {
pub transform: Mat4x4f32,
pub zoom_factor: f32,
pub z_index: f32,
}
impl ShaderTileMetadata {
pub fn new(transform: Mat4x4f32, zoom_factor: f32) -> Self {
pub fn new(transform: Mat4x4f32, zoom_factor: f32, z_index: f32) -> Self {
Self {
transform,
zoom_factor,
z_index,
}
}
}

View File

@ -24,6 +24,7 @@ fn main(
[[location(7)]] translate4: vec4<f32>,
[[location(8)]] color: vec4<f32>,
[[location(9)]] zoom_factor: f32,
[[location(10)]] z_index: f32,
[[builtin(instance_index)]] instance_idx: u32 // instance_index is used when we have multiple instances of the same "object"
) -> VertexOutput {
let z = 0.0;
@ -36,7 +37,7 @@ fn main(
var position = mat4x4<f32>(translate1, translate2, translate3, translate4) * vec4<f32>(position + normal * width, z, 1.0);
// FIXME: how to fix z-fighting?
position.z = 1.0;
position.z = z_index;
return VertexOutput(color, position);
}