Created
January 19, 2023 23:38
-
-
Save troykinsella/7cecadc164aa95e943c9d0e26f8915cc to your computer and use it in GitHub Desktop.
Bevy Mesh to Geo Polygon conversion
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This is surely incorrect, and possibly terrible. Do not use! | |
``` | |
use bevy::prelude::Mesh; | |
use geo_types::{Coordinate, LineString, Polygon}; | |
pub fn mesh_to_clipper_polygon(mesh: &Mesh) -> Option<Polygon> { | |
let vertex_count = mesh.count_vertices(); | |
if let Some(positions) = mesh.attribute(Mesh::ATTRIBUTE_POSITION) { | |
let mut path: Vec<Coordinate> = Vec::with_capacity(vertex_count); | |
if let Some(fl3s) = positions.as_float3() { | |
for fl3 in fl3s { | |
path.push(Coordinate { | |
x: fl3[0] as f64, | |
y: fl3[1] as f64, | |
}); | |
} | |
} | |
let poly = Polygon::new(LineString(path), vec![]); | |
Some(poly) | |
} else { | |
None | |
} | |
} | |
pub fn clipper_polygon_to_mesh(polygon: &Polygon) -> Mesh { | |
let coords = &polygon.exterior().0; | |
let mut mesh = Mesh::new(bevy::render::mesh::PrimitiveTopology::LineStrip); | |
let mut positions = Vec::with_capacity(coords.len()); | |
let mut normals = Vec::with_capacity(coords.len()); | |
for coord in coords { | |
positions.push([coord.x as f32, coord.y as f32, 0.]); | |
normals.push([0.0, 0.0, 1.0]); | |
} | |
mesh.insert_attribute(Mesh::ATTRIBUTE_POSITION, positions); | |
mesh.insert_attribute(Mesh::ATTRIBUTE_NORMAL, normals); | |
mesh | |
} | |
``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment