This commit is contained in:
not-elm
2025-08-10 21:28:45 +09:00
commit 23bdc65da3
91 changed files with 20122 additions and 0 deletions

BIN
examples/.DS_Store vendored Normal file

Binary file not shown.

88
examples/brp.rs Normal file
View File

@@ -0,0 +1,88 @@
//! Shows how to use Bevy Remote Protocol (BRP) with the webview.
//!
//! Please see [here](https://gist.github.com/coreh/1baf6f255d7e86e4be29874d00137d1d) for more about BRP.
use bevy::prelude::*;
use bevy::time::common_conditions::on_timer;
use bevy::window::PrimaryWindow;
use bevy_cef::prelude::*;
use bevy_remote::{BrpResult, RemotePlugin};
use cef::Window;
use serde::Deserialize;
use std::time::Duration;
fn main() {
App::new()
.add_plugins((
DefaultPlugins,
RemotePlugin::default().with_method("greet", greet),
CefPlugin,
))
.add_systems(
Startup,
(ime, spawn_camera, spawn_directional_light, spawn_webview),
)
.add_systems(
Update,
show_devtool.run_if(on_timer(Duration::from_secs(1))),
)
.run();
}
fn greet(In(name): In<Option<serde_json::Value>>) -> BrpResult {
let name = name.unwrap_or_default();
Ok(serde_json::Value::String(format!("Hello, {name}!")))
}
fn spawn_camera(mut commands: Commands) {
commands.spawn((
Camera3d::default(),
Transform::from_translation(Vec3::new(0., 0., 3.)).looking_at(Vec3::ZERO, Vec3::Y),
));
}
fn spawn_directional_light(mut commands: Commands) {
commands.spawn((
DirectionalLight::default(),
Transform::from_translation(Vec3::new(1., 1., 1.)).looking_at(Vec3::ZERO, Vec3::Y),
));
}
fn spawn_webview(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<WebviewExtendStandardMaterial>>,
) {
commands.spawn((
CefWebviewUri::local("brp.html"),
Mesh3d(meshes.add(Plane3d::new(Vec3::Z, Vec2::ONE))),
MeshMaterial3d(materials.add(WebviewExtendStandardMaterial {
base: StandardMaterial {
unlit: true,
emissive: Color::WHITE.into(),
..default()
},
..default()
})),
));
}
fn ime(mut windows: Query<&mut bevy::prelude::Window>) {
for mut window in windows.iter_mut() {
window.ime_enabled = true;
}
}
fn show_devtool(
mut commands: Commands,
webviews: Query<Entity, With<CefWebviewUri>>,
mut initialized: Local<bool>,
) {
if *initialized {
return;
}
*initialized = true;
for webview in webviews.iter() {
commands.entity(webview).trigger(RequestShowDevTool);
}
}

View File

@@ -0,0 +1,57 @@
//! You can create a custom material based on [`WebviewMaterial`].
//!
//! This example creates a custom material that blends an image.
use bevy::pbr::MaterialExtension;
use bevy::prelude::*;
use bevy::render::render_resource::{AsBindGroup, ShaderRef};
use bevy_cef::prelude::*;
fn main() {
App::new()
.add_plugins((
DefaultPlugins,
CefPlugin,
WebviewExtendMaterialPlugin::<CustomExtension>::default(),
))
.add_systems(Startup, (spawn_camera, spawn_webview))
.run();
}
fn spawn_camera(mut commands: Commands) {
commands.spawn((
Camera3d::default(),
Transform::from_translation(Vec3::new(0., 0., 3.)).looking_at(Vec3::ZERO, Vec3::Y),
));
}
fn spawn_webview(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<WebviewExtendedMaterial<CustomExtension>>>,
asset_server: Res<AssetServer>,
) {
commands.spawn((
CefWebviewUri("https://bevy.org/".to_string()),
Mesh3d(meshes.add(Plane3d::new(Vec3::Z, Vec2::ONE))),
MeshMaterial3d(materials.add(WebviewExtendedMaterial {
extension: CustomExtension {
mask: asset_server.load("images/rustacean-flat-gesture.png"),
},
..default()
})),
));
}
#[derive(Asset, Reflect, Clone, Debug, AsBindGroup, Default)]
struct CustomExtension {
#[texture(0)]
#[sampler(1)]
mask: Handle<Image>,
}
impl MaterialExtension for CustomExtension {
fn fragment_shader() -> ShaderRef {
"shaders/custom_material.wgsl".into()
}
}

5937
examples/demo/Cargo.lock generated Normal file

File diff suppressed because it is too large Load Diff

14
examples/demo/Cargo.toml Normal file
View File

@@ -0,0 +1,14 @@
[package]
name = "demo"
version.workspace = true
edition.workspace = true
license.workspace = true
authors.workspace = true
repository.workspace = true
publish = false
[dependencies]
bevy = { workspace = true, features = ["file_watcher"]}
bevy_remote = { workspace = true }
bevy_cef = { workspace = true, features = ["debug"] }

126
examples/demo/src/main.rs Normal file
View File

@@ -0,0 +1,126 @@
use bevy::input::common_conditions::input_pressed;
use bevy::prelude::*;
use bevy::window::PrimaryWindow;
use bevy_cef::prelude::*;
fn main() {
App::new()
.add_plugins((DefaultPlugins, CefPlugin))
.add_systems(
Startup,
(
spawn_camera,
spawn_directional_light,
spawn_github_webview,
spawn_google_search_webview,
spawn_ground,
enable_ime,
),
)
.insert_resource(AmbientLight::default())
.add_systems(
Update,
(
walk::<1, 0>.run_if(input_pressed(KeyCode::ArrowRight)),
walk::<-1, 0>.run_if(input_pressed(KeyCode::ArrowLeft)),
walk::<0, 1>.run_if(input_pressed(KeyCode::ArrowUp)),
walk::<0, -1>.run_if(input_pressed(KeyCode::ArrowDown)),
rotate_camera::<1>.run_if(input_pressed(KeyCode::Digit1)),
rotate_camera::<-1>.run_if(input_pressed(KeyCode::Digit2)),
),
)
.run();
}
fn spawn_camera(mut commands: Commands) {
commands.spawn(Camera3d::default());
}
fn spawn_directional_light(mut commands: Commands) {
commands.spawn((
DirectionalLight {
shadows_enabled: true,
..default()
},
Transform::from_translation(Vec3::new(1., 1., 1.)).looking_at(Vec3::ZERO, Vec3::Y),
));
}
fn enable_ime(mut primary_window: Query<&mut Window, With<PrimaryWindow>>) {
primary_window.single_mut().unwrap().ime_enabled = true;
}
fn spawn_github_webview(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<WebviewExtendStandardMaterial>>,
) {
commands.spawn((
CefWebviewUri("https://github.com/not-elm".to_string()),
Mesh3d(meshes.add(Plane3d::new(Vec3::Z, Vec2::ONE))),
WebviewSize(Vec2::splat(800.0)),
MeshMaterial3d(materials.add(WebviewExtendStandardMaterial::default())),
Transform::from_translation(Vec3::new(1.5, 0., -4.0)),
));
}
fn spawn_google_search_webview(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<WebviewExtendStandardMaterial>>,
) {
commands.spawn((
CefWebviewUri("https://www.youtube.com/".to_string()),
WebviewSize(Vec2::splat(800.0)),
Mesh3d(meshes.add(Plane3d::new(Vec3::Z, Vec2::ONE))),
MeshMaterial3d(materials.add(WebviewExtendStandardMaterial::default())),
Transform::from_translation(Vec3::new(-1.5, 0., -4.0)), // .with_rotation(Quat::from_rotation_y(-90f32.to_radians())),
));
}
fn spawn_ground(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
) {
commands.spawn((
Mesh3d(meshes.add(Plane3d::new(Vec3::Y, Vec2::new(10., 10.)))),
MeshMaterial3d(materials.add(StandardMaterial {
base_color: Color::srgba(0.8, 0.8, 0.8, 1.0),
..default()
})),
Transform::from_translation(Vec3::new(0., -2., 0.)),
));
}
fn walk<const X: isize, const Z: isize>(
mut q: Query<&mut Transform, With<Camera3d>>,
time: Res<Time>,
) {
for mut t in &mut q {
const SPEED: f32 = 1.5; // 調整可
let dt = time.delta_secs();
let up = Vec3::Y;
let mut f = t.forward().as_vec3();
f = (f - up * f.dot(up)).normalize_or_zero();
let r = f.cross(up).normalize_or_zero();
let input = Vec2::new(X as f32, Z as f32);
if input.length_squared() > 0.0 {
let dir = (r * input.x + f * input.y).normalize_or_zero();
t.translation += dir * SPEED * dt;
}
}
}
fn rotate_camera<const X: isize>(
mut transforms: Query<&mut Transform, With<Camera3d>>,
time: Res<Time>,
) {
for mut transform in transforms.iter_mut() {
const SPEED: f32 = 1.0;
let rotation = Quat::from_rotation_y(SPEED * time.delta_secs() * X as f32);
transform.rotation *= rotation;
}
}

75
examples/devtool.rs Normal file
View File

@@ -0,0 +1,75 @@
//! Shows how to use devtools.
//!
//! ## Key Bindings
//! - `Q`: Show DevTool
//! - `E`: Close DevTool
use bevy::input::common_conditions::input_just_pressed;
use bevy::prelude::*;
use bevy_cef::prelude::*;
fn main() {
App::new()
.add_plugins((DefaultPlugins, CefPlugin))
.add_systems(
Startup,
(spawn_camera, spawn_directional_light, spawn_webview),
)
.add_systems(
Update,
(
show_devtool.run_if(input_just_pressed(KeyCode::KeyQ)),
close_devtool.run_if(input_just_pressed(KeyCode::KeyE)),
),
)
.run();
}
#[derive(Component)]
struct DebugWebview;
fn spawn_camera(mut commands: Commands) {
commands.spawn((
Camera3d::default(),
Transform::from_translation(Vec3::new(0., 0., 3.)).looking_at(Vec3::ZERO, Vec3::Y),
));
}
fn spawn_directional_light(mut commands: Commands) {
commands.spawn((
DirectionalLight::default(),
Transform::from_translation(Vec3::new(1., 1., 1.)).looking_at(Vec3::ZERO, Vec3::Y),
));
}
fn spawn_webview(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<WebviewExtendStandardMaterial>>,
) {
commands.spawn((
DebugWebview,
CefWebviewUri("https://bevy.org/".to_string()),
Mesh3d(meshes.add(Plane3d::new(Vec3::Z, Vec2::ONE))),
MeshMaterial3d(materials.add(WebviewExtendStandardMaterial {
base: StandardMaterial {
unlit: true,
emissive: LinearRgba::WHITE,
..default()
},
..default()
})),
));
}
fn show_devtool(mut commands: Commands, webviews: Query<Entity, With<DebugWebview>>) {
commands
.entity(webviews.single().unwrap())
.trigger(RequestShowDevTool);
}
fn close_devtool(mut commands: Commands, webviews: Query<Entity, With<DebugWebview>>) {
commands
.entity(webviews.single().unwrap())
.trigger(RequestCloseDevtool);
}

58
examples/host_emit.rs Normal file
View File

@@ -0,0 +1,58 @@
//! Shows how to emit an event from the host to the webview.
use bevy::prelude::*;
use bevy::time::common_conditions::on_timer;
use bevy_cef::prelude::*;
use std::time::Duration;
fn main() {
App::new()
.add_plugins((DefaultPlugins, CefPlugin))
.add_systems(
Startup,
(spawn_camera, spawn_directional_light, spawn_webview),
)
.add_systems(Update, emit_count.run_if(on_timer(Duration::from_secs(1))))
.run();
}
#[derive(Component)]
struct DebugWebview;
fn spawn_camera(mut commands: Commands) {
commands.spawn((
Camera3d::default(),
Transform::from_translation(Vec3::new(0., 0., 3.)).looking_at(Vec3::ZERO, Vec3::Y),
));
}
fn spawn_directional_light(mut commands: Commands) {
commands.spawn((
DirectionalLight::default(),
Transform::from_translation(Vec3::new(1., 1., 1.)).looking_at(Vec3::ZERO, Vec3::Y),
));
}
fn spawn_webview(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<WebviewExtendStandardMaterial>>,
) {
commands.spawn((
DebugWebview,
CefWebviewUri::local("host_emit.html"),
Mesh3d(meshes.add(Plane3d::new(Vec3::Z, Vec2::ONE))),
MeshMaterial3d(materials.add(WebviewExtendStandardMaterial::default())),
));
}
fn emit_count(
mut commands: Commands,
mut count: Local<usize>,
webviews: Query<Entity, With<DebugWebview>>,
) {
*count += 1;
commands
.entity(webviews.single().unwrap())
.trigger(HostEmitEvent::new("count", &*count));
}

55
examples/js_emit.rs Normal file
View File

@@ -0,0 +1,55 @@
//! Shows how to emit a message from the webview to the application.
use bevy::prelude::*;
use bevy_cef::prelude::*;
use serde::Deserialize;
fn main() {
App::new()
.add_plugins((
DefaultPlugins,
CefPlugin,
JsEmitEventPlugin::<Message>::default(),
))
.add_systems(
Startup,
(spawn_camera, spawn_directional_light, spawn_webview),
)
.add_observer(apply_receive_message)
.run();
}
#[derive(Event, Deserialize)]
struct Message {
count: u32,
}
fn apply_receive_message(trigger: Trigger<Message>) {
info!("Received: {:?}", trigger.count);
}
fn spawn_camera(mut commands: Commands) {
commands.spawn((
Camera3d::default(),
Transform::from_translation(Vec3::new(0., 0., 3.)).looking_at(Vec3::ZERO, Vec3::Y),
));
}
fn spawn_directional_light(mut commands: Commands) {
commands.spawn((
DirectionalLight::default(),
Transform::from_translation(Vec3::new(1., 1., 1.)).looking_at(Vec3::ZERO, Vec3::Y),
));
}
fn spawn_webview(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<WebviewExtendStandardMaterial>>,
) {
commands.spawn((
CefWebviewUri::local("js_emit.html"),
Mesh3d(meshes.add(Plane3d::new(Vec3::Z, Vec2::ONE))),
MeshMaterial3d(materials.add(WebviewExtendStandardMaterial::default())),
));
}

69
examples/navigation.rs Normal file
View File

@@ -0,0 +1,69 @@
//! Shows how to navigate a webview using keyboard input.
//!
//! ## Keyboard Controls
//!
//! - Press `Z` to go back in history.
//! - Press `X` to go forward in history.
use bevy::input::common_conditions::input_just_pressed;
use bevy::prelude::*;
use bevy_cef::prelude::*;
fn main() {
App::new()
.add_plugins((DefaultPlugins, CefPlugin))
.add_systems(
Startup,
(spawn_camera, spawn_directional_light, spawn_webview),
)
.add_systems(
Update,
(
request_go_back.run_if(input_just_pressed(KeyCode::KeyZ)),
request_go_forward.run_if(input_just_pressed(KeyCode::KeyX)),
),
)
.run();
}
#[derive(Component)]
struct DebugWebview;
fn spawn_camera(mut commands: Commands) {
commands.spawn((
Camera3d::default(),
Transform::from_translation(Vec3::new(0., 0., 3.)).looking_at(Vec3::ZERO, Vec3::Y),
));
}
fn spawn_directional_light(mut commands: Commands) {
commands.spawn((
DirectionalLight::default(),
Transform::from_translation(Vec3::new(1., 1., 1.)).looking_at(Vec3::ZERO, Vec3::Y),
));
}
fn spawn_webview(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<WebviewExtendStandardMaterial>>,
) {
commands.spawn((
DebugWebview,
CefWebviewUri("https://bevy.org/".to_string()),
Mesh3d(meshes.add(Plane3d::new(Vec3::Z, Vec2::ONE))),
MeshMaterial3d(materials.add(WebviewExtendStandardMaterial::default())),
));
}
fn request_go_back(mut commands: Commands, webviews: Query<Entity, With<DebugWebview>>) {
for webview in webviews.iter() {
commands.entity(webview).trigger(RequestGoBack);
}
}
fn request_go_forward(mut commands: Commands, webviews: Query<Entity, With<DebugWebview>>) {
for webview in webviews.iter() {
commands.entity(webview).trigger(RequestGoForward);
}
}

40
examples/simple.rs Normal file
View File

@@ -0,0 +1,40 @@
//! A simple example that shows how to spawn a webview in world space.
use bevy::prelude::*;
use bevy_cef::prelude::*;
fn main() {
App::new()
.add_plugins((DefaultPlugins, CefPlugin))
.add_systems(
Startup,
(spawn_camera, spawn_directional_light, spawn_webview),
)
.run();
}
fn spawn_camera(mut commands: Commands) {
commands.spawn((
Camera3d::default(),
Transform::from_translation(Vec3::new(0., 0., 3.)).looking_at(Vec3::ZERO, Vec3::Y),
));
}
fn spawn_directional_light(mut commands: Commands) {
commands.spawn((
DirectionalLight::default(),
Transform::from_translation(Vec3::new(1., 1., 1.)).looking_at(Vec3::ZERO, Vec3::Y),
));
}
fn spawn_webview(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<WebviewExtendStandardMaterial>>,
) {
commands.spawn((
CefWebviewUri("https://bevy.org/".to_string()),
Mesh3d(meshes.add(Plane3d::new(Vec3::Z, Vec2::ONE))),
MeshMaterial3d(materials.add(WebviewExtendStandardMaterial::default())),
));
}

27
examples/sprite.rs Normal file
View File

@@ -0,0 +1,27 @@
//! You can create a webview as a sprite in your scene.
use bevy::prelude::*;
use bevy_cef::prelude::*;
fn main() {
App::new()
.add_plugins((DefaultPlugins, CefPlugin))
.add_systems(Startup, (spawn_camera_2d, spawn_sprite_webview))
.run();
}
fn spawn_camera_2d(mut commands: Commands) {
commands.spawn(Camera2d);
}
fn spawn_sprite_webview(mut commands: Commands, mut images: ResMut<Assets<Image>>) {
commands.spawn((
CefWebviewUri::new("https://bevyengine.org/"),
Pickable::default(),
Sprite {
image: images.add(Image::default()),
custom_size: Some(Vec2::splat(500.0)),
..default()
},
));
}

54
examples/zoom_level.rs Normal file
View File

@@ -0,0 +1,54 @@
//! Shows how to change the zoom level of a webview.
use bevy::input::mouse::MouseWheel;
use bevy::prelude::*;
use bevy_cef::prelude::*;
fn main() {
App::new()
.add_plugins((DefaultPlugins, CefPlugin))
.add_systems(
Startup,
(spawn_camera, spawn_directional_light, spawn_webview),
)
.add_systems(Update, change_zoom_level)
.run();
}
fn spawn_camera(mut commands: Commands) {
commands.spawn((
Camera3d::default(),
Transform::from_translation(Vec3::new(0., 0., 3.)).looking_at(Vec3::ZERO, Vec3::Y),
));
}
fn spawn_directional_light(mut commands: Commands) {
commands.spawn((
DirectionalLight::default(),
Transform::from_translation(Vec3::new(1., 1., 1.)).looking_at(Vec3::ZERO, Vec3::Y),
));
}
fn spawn_webview(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<WebviewExtendStandardMaterial>>,
) {
commands.spawn((
CefWebviewUri("https://bevy.org/".to_string()),
Mesh3d(meshes.add(Plane3d::new(Vec3::Z, Vec2::ONE))),
MeshMaterial3d(materials.add(WebviewExtendStandardMaterial::default())),
));
}
fn change_zoom_level(mut er: EventReader<MouseWheel>, mut webviews: Query<&mut ZoomLevel>) {
for event in er.read() {
webviews.par_iter_mut().for_each(|mut level| {
if event.y > 0.0 {
level.0 += 0.1; // Zoom in
} else if event.y < 0.0 {
level.0 -= 0.1; // Zoom out
}
});
}
}