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

Can I capture render result? #106

Closed
zimond opened this issue Jun 22, 2022 · 6 comments
Closed

Can I capture render result? #106

zimond opened this issue Jun 22, 2022 · 6 comments
Labels
question Further information is requested

Comments

@zimond
Copy link

zimond commented Jun 22, 2022

Can I create a CPU buffer and render to it, So I can save the render result to an image? I'm not familiar with notan, and sorry if this is noob question.

@Nazariglez
Copy link
Owner

Nazariglez commented Jun 22, 2022

Yes! You can use the image crate to export the content of a texture to the filedisk. Check this example:

use notan::draw::*;
use notan::prelude::*;

#[notan_main]
fn main() -> Result<(), String> {
    notan::init_with(setup)
        .add_config(DrawConfig) // Simple way to add the draw extension
        .build()
}

fn setup(app: &mut App, gfx: &mut Graphics) {
    // create a render texture using the screen's size
    let (width, height) = gfx.size();
    let mut rt = gfx.create_render_texture(width, height).build().unwrap();

    // draw a triangle
    let mut draw = gfx.create_draw();
    draw.clear(Color::BLACK);
    draw.triangle((400.0, 100.0), (100.0, 500.0), (700.0, 500.0))
        .color_vertex(Color::RED, Color::BLUE, Color::GREEN);

    // render the triangle into the RenderTexture
    gfx.render_to(&mut rt, &draw);

    // read the pixels from the RenderTexture
    let mut pixels = vec![0; (width * height * 4) as usize];
    gfx.read_pixels(&rt).read_to(&mut pixels).unwrap();

    // use the image crate to export the buffer to the filedisk
    image::save_buffer("triangle.png", &pixels, width as _, height as _, image::ColorType::Rgba8).unwrap();

    // close the app
    app.exit();
}

This will create a png with this:
triangle

This solution will not work on the web, if you need to do this on the web let me know, maybe we can create some custom solution integrated in notan to do it.

Feel free to close this if it resolve your question, have a nice day!

@Nazariglez Nazariglez added the question Further information is requested label Jun 22, 2022
@zimond
Copy link
Author

zimond commented Jun 22, 2022

Thanks for the quick response! Can you explain a little more about why webgl backend doesn't support image exporting? I think image crate works in wasm32 target.

@Nazariglez
Copy link
Owner

Just to be sure, I may be wrong with your question, but I am talking about render to GPU and then move it to the CPU and exporting it to a file. Is that your use case? If you don't have a GPU notan will not work for you because all the rendering happens in the GPU.

Anyway, going back to the question.

The problem is not the image crate but the filesystem and how the browsers work. Save a file on browsers is "hard" and saving an image can be tricky because of encoding.

So, there are two options:

  1. We can render the textures to an offscreen canvas and then use the canvas to create an url object and force the download. The heavy lifting of encoding will be done by the browser here, but we need at least a secondary (hidden) canvas to do it.
  2. We can use image crate, do the encoding with it, and then create a js Blob object, the blob to an url object and force the download. This seems cleaner (though maybe image crate is a little slower than the encoding systems of the browser itself.)

This can be a nice feature behind a feature flag. What do you think? If this is useful for you I can try to do it for the develop branch with a simple API that works across platforms.

@zimond
Copy link
Author

zimond commented Jun 23, 2022

I see why you are feeling confused. I'm actually trying to export image buffer for other jobs like image analyzing and more. I think this is quite straightforward (after I know how to do it LOL) so I won't bother you to add a new API. Let's just close this issue and thanks for your explanation!

@zimond zimond closed this as completed Jun 23, 2022
@Nazariglez
Copy link
Owner

Hey @zimond I thought that this was a cool feature and I added it behind the texture_to_file feature in the develop branch.

The same example now will be:

use notan::draw::*;
use notan::prelude::*;

#[notan_main]
fn main() -> Result<(), String> {
    notan::init_with(setup)
        .add_config(DrawConfig) // Simple way to add the draw extension
        .build()
}

fn setup(app: &mut App, gfx: &mut Graphics) {
    // create a render texture using the screen's size
    let (width, height) = gfx.size();
    let mut rt = gfx.create_render_texture(width, height).build().unwrap();

    // draw a triangle
    let mut draw = gfx.create_draw();
    draw.clear(Color::BLACK);
    draw.triangle((400.0, 100.0), (100.0, 500.0), (700.0, 500.0))
        .color_vertex(Color::RED, Color::BLUE, Color::GREEN);

    // render the triangle into the RenderTexture
    gfx.render_to(&mut rt, &draw);

    // export texture content to a file 
    rt.to_file(gfx, "triangle.png").unwrap();

    // close the app
    app.exit();
}

It will take care of flipping the output for the RenderTexture and It works on browsers and desktop. I hope it helps.

@zimond
Copy link
Author

zimond commented Jun 26, 2022

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants