Giter Site home page Giter Site logo

Can I capture render result? about notan HOT 6 CLOSED

nazariglez avatar nazariglez commented on May 17, 2024
Can I capture render result?

from notan.

Comments (6)

Nazariglez avatar Nazariglez commented on May 17, 2024

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!

from notan.

zimond avatar zimond commented on May 17, 2024

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.

from notan.

Nazariglez avatar Nazariglez commented on May 17, 2024

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.

from notan.

zimond avatar zimond commented on May 17, 2024

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!

from notan.

Nazariglez avatar Nazariglez commented on May 17, 2024

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.

from notan.

zimond avatar zimond commented on May 17, 2024

Thanks!

from notan.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.