Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug when using multiple textures #611

Open
chrisburnor opened this issue May 25, 2020 · 2 comments
Open

Bug when using multiple textures #611

chrisburnor opened this issue May 25, 2020 · 2 comments

Comments

@chrisburnor
Copy link

Ran into a bug today when trying to set up two meshes with different textures. Essentially, which ever texture I use last, becomes the texture used by the other mesh:

use std::path::PathBuf;

use nannou::image::GenericImageView;
use nannou::prelude::*;
use palette;

const UPDATE_FPS_COUNT: u64 = 100;

pub fn run() {
    nannou::app(model)
        .update(update)
        .simple_window(view)
        .run();
}

struct ImageTexture {
    texture: wgpu::Texture,
    image: nannou::image::DynamicImage,
}

impl ImageTexture {
    pub fn new(app: &App, img_path: PathBuf) -> Self {
        let image = nannou::image::open(img_path).unwrap();
        let texture = wgpu::Texture::from_image(app, &image);

        Self {
            texture,
            image,
        }
    }
}

struct Model {
    bg_color: Hsl,
    island_image_texture: ImageTexture,
    tree_image_texture: ImageTexture,
}

fn model(app: &App) -> Model {
    let bg_color = Hsl::new(0.0, 0.0, 0.0);

    let assets = app.assets_path().unwrap();
    let island_img_path = assets.join("images").join("island.png");
    let island_image_texture = ImageTexture::new(app, island_img_path);

    let tree_img_path = assets.join("images").join("tree.png");
    let tree_image_texture = ImageTexture::new(app, tree_img_path);

    Model {
        bg_color,
        island_image_texture,
        tree_image_texture,
    }
}

fn update(app: &App, model: &mut Model, update: Update) {
    let elapsed_frames = app.elapsed_frames();
    if elapsed_frames % UPDATE_FPS_COUNT == 0 {
        let fps = app.fps();
        log::info!("{} fps at {}", fps, elapsed_frames);
    }
}

fn view(app: &App, model: &Model, frame: Frame) {
    let draw = app.draw();
    draw.background().color(palette::named::BLACK);

    {
        let square = geom::Rect::from_w_h(100.0, 100.0);
        let points = square
            .triangles_iter()
            .flat_map(geom::Tri::vertices)
            .map(|point| {
                let tex_coords = [
                    point.x / square.w() + 0.5,
                    (point.y / square.h() * -1.0) + 0.5,
                ];
                (point, tex_coords)
            });
        draw.mesh()
            .points_textured(&model.island_image_texture.texture, points)
            .y_radians(app.time * 0.33);
    }

    {
        let square = geom::Rect::from_w_h(100.0, 100.0);
        let points = square
            .triangles_iter()
            .flat_map(geom::Tri::vertices)
            .map(|point| {
                let tex_coords = [
                    point.x / square.w() + 0.5,
                    (point.y / square.h() * -1.0) + 0.5,
                ];
                (point, tex_coords)
            });
        draw.mesh()
            .points_textured(&model.tree_image_texture.texture, points);
    }

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

In this code, both meshes display the 'tree_image' whereas if I swap the order, both show the 'island' image.

@mitchmindtree
Copy link
Member

mitchmindtree commented May 25, 2020

Thanks for the issue @chrisburnor!

I think perhaps this has been fixed on master by #594 which overhauled the way the draw::Renderer creates commands for the render pass which I believe might have addressed this issue.

Sure enough, if I run your snippet above on master (with some small modifications to get it compiling/running locally with local images) it seems to be working with the two textures displaying one on top of the other:

Screenshot from 2020-05-25 13-54-26

In case you're interested, you can see what other changes are yet to be published to crates.io under the "Unreleased" section of the changelog here.

Perhaps we should publish 0.15 soon as there are quite a few changes pending there now!

@chrisburnor
Copy link
Author

Oh excellent. Switching to master fixed it for me too.

Looking forward to the 0.15 bump -- looks like a lot of good stuff around textures. I'll leave this ticket open until that gets bumped in case others run into the same issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants