nannou-org / nannou

A Creative Coding Framework for Rust.
https://nannou.cc/
5.98k stars 304 forks source link

Error after lyon fails to tesselate -> update to lyon 0.15? #409

Closed MacTuitui closed 4 years ago

MacTuitui commented 4 years ago

I have what I think might be a bug in either kow lyon handles weird shapes and how nannou handles lyon's failures.

use nannou::prelude::*;
use nannou::noise::*;

fn main() {
    nannou::sketch(view);
}

fn view(app: &App, frame: &Frame) {
    // Begin drawing
    let draw = app.draw();

    // Clear the background to blue.
    draw.background().color(BLACK);

    let mut noise = Perlin::new();
    noise = noise.set_seed(1);
    let frac = 0.1270903;
    let rotcos = 0.2*(frac*TAU).cos();
    let rotsin = 0.2*(frac*TAU).sin();
    let j = 30;
    let frac_j = (j as f32)/189.0;
    let mut pts = Vec::new();
    for i in 0..200 {
        let frac_i = (i as f32)/199.0;
        let scale = ((frac_i*PI).sin()).powf(3.0);
        let offset = scale*(noise.get([i as f64 *0.03, j as f64 *0.5, rotcos as f64, rotsin as f64]) ) as f32 ;
        pts.push( vec2(-512.0+frac_i*1024.0, 342.0-frac_j*824.0+160.0*offset));
    }
    //draw.polygon().color(WHITE).points(pts);
    draw.polyline().color(WHITE).points(pts);

    draw.to_frame(app, &frame).unwrap();
}

When you draw the polyline it works as expected, but changing it to polygon fails with:

fill tessellation failed: Internal(E01)
thread 'main' panicked at 'no color for vertex', src/libcore/option.rs:1034:5

The same code works when you change the scale to let scale = ((frac_i*TAU).sin()).powf(3.0); and it looks like what I think is the correct answer.

mitchmindtree commented 4 years ago

From mactuitui:

ok I’ve got the best example possible:

let pts = [Vector2 { x: 515.85266, y: -452.88187 }, Vector2 { x: 521.897, y: -435.09717 }, Vector2 { x:     515.93286, y: -452.64264 }, Vector2 { x: 515.85266, y: -452.88187 }];
draw.polygon().points(pts.iter().map(|p| *p*0.6)).color(WHITE);

it crashes nannou directly on my machine

MacTuitui commented 4 years ago

Ok progress here! (I really end up on this bug a lot when dealing with shrinking shapes!!!)

I think that in src/draw/primitive/polygon.rs at line 120, when lyon does the tessellation, you check if you get an error, but here, for my specific example, lyon gives the following: Ok(Count { vertices: 2, indices: 0 }) instead of Ok(Count { vertices: 3, indices: 3 }) when I change the points to something that is meaningfull to draw.

Since you then use this information to extend the mesh, it looks like since the vertices count does not match the indices count at the end, it provokes the no color for vertex error as the lengths do not match.

Would it make sense to maybe ignore the tessellation result when the vertices count does not match the indices count? I'll also look into lyon to see if that's the expected answer.

MacTuitui commented 4 years ago

Lyon on 0.15.6 does produce a nice tessellation result for the same shape, so maybe an update to 0.15 might be the better solution!

mitchmindtree commented 4 years ago

Just tested locally now that we've updated to lyon 0.15 and it seems to be working nicely:

Screenshot from 2020-04-08 00-03-21

Going to close this now, but feel free to re-open if you run into the same issue again!

Closed via #484