Weasy666 / bevy_svg

A simple and incomplete SVG drawer for the Bevy engine.
Apache License 2.0
108 stars 26 forks source link

SVG Not Rendered Properly #37

Closed shakesbeare closed 3 months ago

shakesbeare commented 10 months ago

Possibly related to #34 ?

Some SVGs seem to not be rendered properly. I've worked with these files in resvg directly before and they didn't have problems, so I'm guessing it's something gone wrong here. Chrome renders the same file properly. Unfortunately, I don't know enough about SVGs to guess at the issue, but hopefully it isn't too hard of a problem.

Actual Result:

image

Expected Result:

image

File: white_knight

Relevant Code:

       let svg = asset_server.load(filename);
        commands.spawn(Svg2dBundle {
            svg,
            origin: Origin::Center,
            transform: Transform::from_translation(Vec3::new(
                offset + file * draw_info.square_size + (draw_info.square_size / 2.),
                offset + rank * draw_info.square_size + (draw_info.square_size / 2.),
                1.0,
            ))
            .with_scale(Vec3::new(
                draw_info.square_size / SPRITE_SIZE,
                draw_info.square_size / SPRITE_SIZE,
                1.0,
            )),
            ..default()
        });

Thanks for your help and the plugin!

StrikeForceZero commented 8 months ago

I was able to fix this knight chess piece svg using these changes to the library

image

Index: src/render/tessellation.rs
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/src/render/tessellation.rs b/src/render/tessellation.rs
--- a/src/render/tessellation.rs    (revision HEAD)
+++ b/src/render/tessellation.rs    (revision Staged)
@@ -17,7 +17,6 @@
 ) -> VertexBuffers {
     debug!("Tessellating SVG: {}", svg.name);

-    let flip_y = Transform::from_scale(Vec3::new(1.0, -1.0, 1.0));
     let mut buffers = VertexBuffers::new();

     let mut color = None;
@@ -28,8 +27,7 @@
             color = Some(path.color);
         }

-        // Bevy has a different y-axis origin, so we need to flip that axis
-        let transform = flip_y * path.abs_transform;
+        let transform = path.abs_transform;
         match path.draw_type {
             DrawType::Fill => {
                 if let Err(e) = fill_tess.tessellate(
Index: src/render/vertex_buffer.rs
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/src/render/vertex_buffer.rs b/src/render/vertex_buffer.rs
--- a/src/render/vertex_buffer.rs   (revision HEAD)
+++ b/src/render/vertex_buffer.rs   (revision Staged)
@@ -7,6 +7,7 @@
     },
     transform::components::Transform,
 };
+use bevy::render::mesh::VertexAttributeValues;
 use copyless::VecHelper;
 use lyon_tessellation::{
     self, FillVertex, FillVertexConstructor, StrokeVertex, StrokeVertexConstructor,
@@ -28,6 +29,15 @@
 /// Lyon's [`VertexBuffers`] generic data type defined for [`Vertex`].
 pub(crate) type VertexBuffers = lyon_tessellation::VertexBuffers<Vertex, IndexType>;

+fn flip_mesh_vertically(mesh: &mut Mesh) {
+    if let Some(VertexAttributeValues::Float32x3(positions)) = mesh.attribute_mut(Mesh::ATTRIBUTE_POSITION) {
+        for position in positions.iter_mut() {
+            // Invert the y-coordinate to flip the mesh vertically
+            position[1] = -position[1];
+        }
+    }
+}
+
 impl Convert<Mesh> for VertexBuffers {
     fn convert(self) -> Mesh {
         let mut positions = Vec::with_capacity(self.vertices.len());
@@ -43,6 +53,9 @@
         mesh.insert_attribute(Mesh::ATTRIBUTE_COLOR, colors);
         mesh.set_indices(Some(Indices::U32(self.indices)));

+        // Bevy has a different y-axis origin, so we need to flip that axis
+        flip_mesh_vertically(&mut mesh);
+
         mesh
     }
 }

but its not a direct fix for more complicated svgs... so leaving this here as a hope to inspire someone that knows more to go deeper down the rabbit hole.

Weasy666 commented 3 months ago

Closed by #41