#wgsl #creative-coding #wgs #graphics

wgs_runtime_wgpu

Wgs 格式 wgpu 运行时

5 个版本

0.1.2 2023年10月26日
0.1.1 2023年6月21日
0.1.0 2023年6月20日
0.0.2 2023年6月7日
0.0.1 2023年6月7日

#586 in 图形 API

每月42次下载

MIT 许可证

53KB
1K SLoC

wgs_runtime_wgpu

Latest version Documentation MIT

wgpu 驱动的 wgs 运行时。


lib.rs:

wgpu 驱动的 wgs 运行时。

可在原生和 Web 上运行。

示例

与 Winit 集成

use wgs_core::WgsData;
use wgs_runtime_wgpu::{Runtime, RuntimeExt};
use winit::{
    event::{Event, WindowEvent},
    event_loop::{ControlFlow, EventLoop},
    window::Window,
};

fn main() {
    let event_loop = EventLoop::new();

    let window = Window::new(&event_loop).unwrap();

    let mut runtime =
        futures::executor::block_on(Runtime::new(&window, WgsData::default(), None)).unwrap();

    let size = window.inner_size();

    /// Needs to set the width and height of the runtime before rendering.
    runtime.resize(size.width as f32, size.height as f32);

    event_loop.run(move |event, _, control_flow| {
        *control_flow = ControlFlow::Wait;

        match event {
            Event::RedrawEventsCleared => window.request_redraw(),
            Event::WindowEvent {
                event: WindowEvent::Resized(size),
                ..
            } => {
                runtime.resize(size.width as f32, size.height as f32);

                window.request_redraw();
            }
            Event::RedrawRequested(_) => {
                /// Starts a new frame before doing the actual rendering.
                runtime.frame_start().unwrap();

                /// The actual rendering for wgs.
                runtime.render().unwrap();

                /// To render other stuff on the target surface besides the wgs content.
                // runtime.render_with(|_device, _queue, _view| {
                //     // Other rendering like ui etc.
                // }).unwrap();

                /// Remember to finish the current working frame.
                runtime.frame_finish().unwrap();

                window.request_redraw();
            }
            Event::WindowEvent {
                event: WindowEvent::CloseRequested,
                ..
            } => *control_flow = ControlFlow::Exit,
            _ => {}
        }
    });
}

与 Web 集成

wgs_runtime_wgpu 还可以编译为 Wasm32 并在 Web 上运行。

您可以从 npm 安装它或使用高级库 wgs-player

依赖项

~4–39MB
~549K SLoC