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

Rename SpineMaterial to SpineMaterial2d and add SpineMaterial3d #31

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@ exclude = ["assets/*"]

[dependencies]
rusty_spine = "0.8"
bevy = { version = "0.14", default-features = false, features = [
bevy = { version = "0.15", default-features = false, features = [
"bevy_render",
"bevy_asset",
"bevy_sprite",
] }
glam = { version = "0.27", features = ["mint"] }
glam = { version = "0.29", features = ["mint"] }
thiserror = "1.0.50"

[dev-dependencies]
lerp = "0.5"
bevy = { version = "0.14", default-features = true }
bevy = { version = "0.15", default-features = true }

[workspace]
resolver = "2"
Expand Down
96 changes: 48 additions & 48 deletions examples/3d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use bevy::{
window::{CursorGrabMode, PrimaryWindow},
};
use bevy_spine::{
materials::{SpineMaterial, SpineMaterialInfo, SpineMaterialPlugin, SpineSettingsQuery},
materials::{Spine3dSettingsQuery, SpineMaterial3d, SpineMaterialInfo, SpineMaterialPlugin3d},
prelude::*,
SpineMeshType,
};
Expand All @@ -30,7 +30,7 @@ fn main() {
.add_plugins((
DefaultPlugins,
SpinePlugin,
SpineMaterialPlugin::<Spine3DMaterial>::default(),
SpineMaterialPlugin3d::<Spine3DMaterial>::default(),
))
.add_systems(Startup, setup)
.add_systems(Update, (on_spawn.in_set(SpineSet::OnReady), controls))
Expand All @@ -45,29 +45,27 @@ fn setup(
mut materials: ResMut<Assets<StandardMaterial>>,
) {
// plane
commands.spawn(PbrBundle {
mesh: meshes.add(Plane3d::default().mesh().size(5.0, 5.0)),
material: materials.add(Color::srgb(0.3, 0.5, 0.3)),
..default()
});
commands.spawn((
Mesh3d(meshes.add(Plane3d::default().mesh().size(5.0, 5.0))),
MeshMaterial3d(materials.add(Color::srgb(0.3, 0.5, 0.3))),
));

// light
commands.spawn(PointLightBundle {
point_light: PointLight {
intensity: 1500.0,
commands.spawn((
DirectionalLight {
shadows_enabled: true,
shadow_depth_bias: 0.05,
shadow_normal_bias: 0.05,
illuminance: 5_800.0,
..default()
},
transform: Transform::from_xyz(4.0, 8.0, 4.0),
..default()
});
Transform::from_xyz(0.0, 5.0, 5.0).looking_at(Vec3::ZERO, Vec3::Y),
));

// camera
commands.spawn((
Camera3dBundle {
transform: Transform::from_xyz(-2.0, 2.5, 5.0).looking_at(Vec3::ZERO, Vec3::Y),
..default()
},
Camera3d::default(),
Transform::from_xyz(-2.0, 2.5, 5.0).looking_at(Vec3::ZERO, Vec3::Y),
Orbit::default(),
));

Expand All @@ -77,16 +75,19 @@ fn setup(
asset_server.load("spineboy/export/spineboy-pma.atlas"),
);
let skeleton_handle = skeletons.add(skeleton);
commands.spawn(SpineBundle {
skeleton: skeleton_handle.clone(),
commands.spawn((SpineBundle {
loader: SpineLoader {
skeleton: skeleton_handle.clone(),
..Default::default()
},
transform: Transform::from_xyz(0., 0., 0.).with_scale(Vec3::ONE * 0.005),
settings: SpineSettings {
default_materials: false,
mesh_type: SpineMeshType::Mesh3D,
..Default::default()
},
..Default::default()
});
},));
}

fn on_spawn(
Expand All @@ -110,37 +111,39 @@ fn controls(
mouse_buttons: Res<ButtonInput<MouseButton>>,
keys: Res<ButtonInput<KeyCode>>,
) {
let mut window = window_query.single_mut();
if mouse_buttons.just_pressed(MouseButton::Left) {
window.cursor.grab_mode = CursorGrabMode::Locked;
window.cursor.visible = false;
}
if keys.just_pressed(KeyCode::Escape) {
window.cursor.grab_mode = CursorGrabMode::None;
window.cursor.visible = true;
}
let window = window_query.get_single_mut();
if let Ok(mut window) = window {
if mouse_buttons.just_pressed(MouseButton::Left) {
window.cursor_options.grab_mode = CursorGrabMode::Locked;
window.cursor_options.visible = false;
}
if keys.just_pressed(KeyCode::Escape) {
window.cursor_options.grab_mode = CursorGrabMode::None;
window.cursor_options.visible = true;
}

let mut mouse_movement = Vec2::ZERO;
for mouse_motion_event in mouse_motion_events.read() {
if window.cursor.grab_mode == CursorGrabMode::Locked {
mouse_movement += mouse_motion_event.delta;
let mut mouse_movement = Vec2::ZERO;
for mouse_motion_event in mouse_motion_events.read() {
if window.cursor_options.grab_mode == CursorGrabMode::Locked {
mouse_movement += mouse_motion_event.delta;
}
}
for (mut orbit, mut orbit_transform) in orbit_query.iter_mut() {
orbit.angle = (orbit.angle + mouse_movement.x * 0.001).clamp(0.14159, 3.);
orbit.pitch = (orbit.pitch + mouse_movement.y * 0.001).clamp(0.1, 1.5);
orbit_transform.translation =
Vec3::new(orbit.angle.cos(), orbit.pitch.tan(), orbit.angle.sin()).normalize() * 7.;
orbit_transform.look_at(Vec3::new(0., 1.5, 0.), Vec3::Y);
}
}
for (mut orbit, mut orbit_transform) in orbit_query.iter_mut() {
orbit.angle = (orbit.angle + mouse_movement.x * 0.001).clamp(0.14159, 3.);
orbit.pitch = (orbit.pitch + mouse_movement.y * 0.001).clamp(0.1, 1.5);
orbit_transform.translation =
Vec3::new(orbit.angle.cos(), orbit.pitch.tan(), orbit.angle.sin()).normalize() * 7.;
orbit_transform.look_at(Vec3::new(0., 1.5, 0.), Vec3::Y);
}
}

#[derive(Component)]
pub struct Spine3DMaterial;

impl SpineMaterial for Spine3DMaterial {
impl SpineMaterial3d for Spine3DMaterial {
type Material = StandardMaterial;
type Params<'w, 's> = SpineSettingsQuery<'w, 's>;
type Params<'w, 's> = Spine3dSettingsQuery<'w, 's>;

fn update(
material: Option<Self::Material>,
Expand All @@ -155,15 +158,12 @@ impl SpineMaterial for Spine3DMaterial {
.unwrap_or(SpineSettings::default());
if spine_settings.mesh_type == SpineMeshType::Mesh3D {
let mut material = material.unwrap_or_else(|| Self::Material {
unlit: true,
alpha_mode: if renderable_data.premultiplied_alpha {
AlphaMode::Premultiplied
} else {
AlphaMode::Blend
},
..Self::Material::default()
});
material.base_color = Color::srgba(1.0, 1.0, 1.0, 1.0);
material.base_color_texture = Some(renderable_data.texture);
material.alpha_mode = AlphaMode::Blend;
material.unlit = true;
Some(material)
} else {
None
Expand Down
18 changes: 9 additions & 9 deletions examples/crossfades.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
use bevy::prelude::*;
use bevy_spine::{
Crossfades, SkeletonController, SkeletonData, Spine, SpineBundle, SpinePlugin, SpineReadyEvent,
SpineSet, SpineSystem,
};
use bevy_spine::prelude::*;

fn main() {
App::new()
Expand All @@ -23,7 +20,7 @@ fn setup(
mut commands: Commands,
mut skeletons: ResMut<Assets<SkeletonData>>,
) {
commands.spawn(Camera2dBundle::default());
commands.spawn(Camera2d);

let skeleton = SkeletonData::new_from_json(
asset_server.load("spineboy/export/spineboy-pro.json"),
Expand All @@ -35,14 +32,17 @@ fn setup(
crossfades.add("idle", "walk", 0.5);
crossfades.add("walk", "idle", 0.5);

commands.spawn(SpineBundle {
skeleton: skeleton_handle.clone(),
commands.spawn((SpineBundle {
loader: SpineLoader {
skeleton: skeleton_handle.clone(),
..Default::default()
},
crossfades,
transform: Transform::default()
.with_translation(Vec3::new(0., -200., 0.))
.with_scale(Vec3::ONE * 0.5),
..Default::default()
});
},));
}

fn on_spawn(
Expand All @@ -68,7 +68,7 @@ fn crossfades(mut spine_query: Query<&mut Spine>, time: Res<Time>) {
.animation()
.name()
.to_owned();
if time.elapsed_seconds() % 2. > 1. {
if time.elapsed_secs() % 2. > 1. {
if current_animation != "walk" {
let _ = spine.animation_state.set_animation_by_name(0, "walk", true);
}
Expand Down
22 changes: 14 additions & 8 deletions examples/custom_material.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ use bevy::{
};
use bevy_spine::{
materials::{
SpineMaterial, SpineMaterialInfo, SpineMaterialPlugin, DARK_COLOR_ATTRIBUTE,
SpineMaterial2d, SpineMaterialInfo, SpineMaterialPlugin, DARK_COLOR_ATTRIBUTE,
DARK_COLOR_SHADER_POSITION,
},
SkeletonController, SkeletonData, Spine, SpineBundle, SpineDrawer, SpinePlugin,
SpineReadyEvent, SpineSet, SpineSettings,
prelude::*,
SpineDrawer,
};

fn main() {
Expand All @@ -37,7 +37,7 @@ fn setup(
mut commands: Commands,
mut skeletons: ResMut<Assets<SkeletonData>>,
) {
commands.spawn(Camera2dBundle::default());
commands.spawn(Camera2d);

let skeleton = SkeletonData::new_from_json(
asset_server.load("spineboy/export/spineboy-pro.json"),
Expand All @@ -47,15 +47,21 @@ fn setup(

// Spine with no custom materials
commands.spawn((SpineBundle {
skeleton: skeleton_handle.clone(),
loader: SpineLoader {
skeleton: skeleton_handle.clone(),
..Default::default()
},
transform: Transform::from_xyz(-230., -130., 0.).with_scale(Vec3::ONE * 0.375),
..Default::default()
},));

// Spine with custom materials
commands.spawn((
SpineBundle {
skeleton: skeleton_handle.clone(),
loader: SpineLoader {
skeleton: skeleton_handle.clone(),
..Default::default()
},
transform: Transform::from_xyz(230., -130., 0.).with_scale(Vec3::ONE * 0.375),
settings: SpineSettings {
default_materials: false,
Expand Down Expand Up @@ -128,7 +134,7 @@ pub struct MyMaterialParam<'w, 's> {
time: Res<'w, Time>,
}

impl SpineMaterial for MyMaterial {
impl SpineMaterial2d for MyMaterial {
type Material = Self;
type Params<'w, 's> = MyMaterialParam<'w, 's>;

Expand All @@ -141,7 +147,7 @@ impl SpineMaterial for MyMaterial {
if let Ok(spine) = params.my_spine_query.get(entity) {
let mut material = material.unwrap_or_default();
material.image = renderable_data.texture;
material.time = params.time.elapsed_seconds();
material.time = params.time.elapsed_secs();
if let Some(slot) = spine
.skeleton
.slot_at_index(renderable_data.slot_index.unwrap_or(9999))
Expand Down
52 changes: 25 additions & 27 deletions examples/events.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
use bevy::prelude::*;
use bevy_spine::{
SkeletonController, SkeletonData, Spine, SpineBundle, SpineEvent, SpinePlugin, SpineReadyEvent,
SpineSet,
};
use bevy_spine::prelude::*;

fn main() {
App::new()
Expand All @@ -24,19 +21,22 @@ fn setup(
mut commands: Commands,
mut skeletons: ResMut<Assets<SkeletonData>>,
) {
commands.spawn(Camera2dBundle::default());
commands.spawn(Camera2d);

let skeleton = SkeletonData::new_from_json(
asset_server.load("spineboy/export/spineboy-pro.json"),
asset_server.load("spineboy/export/spineboy-pma.atlas"),
);
let skeleton_handle = skeletons.add(skeleton);

commands.spawn(SpineBundle {
skeleton: skeleton_handle.clone(),
commands.spawn((SpineBundle {
loader: SpineLoader {
skeleton: skeleton_handle.clone(),
..Default::default()
},
transform: Transform::from_xyz(0., -200., 0.).with_scale(Vec3::ONE * 0.5),
..Default::default()
});
},));
}

fn on_spawn(
Expand All @@ -61,19 +61,17 @@ fn on_spine_event(
for event in spine_events.read() {
if let SpineEvent::Event { name, .. } = event {
commands
.spawn(Text2dBundle {
text: Text::from_section(
name.as_str(),
TextStyle {
font: asset_server.load("FiraMono-Medium.ttf"),
font_size: 22.0,
color: Color::WHITE,
},
)
.with_justify(JustifyText::Center),
transform: Transform::from_xyz(0., -200., 1.),
..Default::default()
})
.spawn((
Text2d(name.clone()),
TextFont {
font: asset_server.load("FiraMono-Medium.ttf"),
font_size: 22.0,
..Default::default()
},
TextColor(Color::WHITE),
Transform::from_xyz(0., -200., 1.),
TextLayout::new_with_justify(JustifyText::Center),
))
.insert(Footstep);
}
}
Expand All @@ -83,15 +81,15 @@ fn on_spine_event(
struct Footstep;

fn footstep_update(
mut footstep_query: Query<(&mut Transform, &mut Text, Entity), With<Footstep>>,
mut footstep_query: Query<(&mut Transform, &mut TextColor, Entity), With<Footstep>>,
mut commands: Commands,
time: Res<Time>,
) {
for (mut transform, mut text, entity) in footstep_query.iter_mut() {
transform.translation.y += time.delta_seconds() * 70.;
let mut alpha = text.sections[0].style.color.alpha();
alpha = (alpha - time.delta_seconds() * 2.).clamp(0., 1.);
text.sections[0].style.color.set_alpha(alpha);
for (mut transform, mut text_color, entity) in footstep_query.iter_mut() {
transform.translation.y += time.delta_secs() * 70.;
let mut alpha = text_color.0.alpha();
alpha = (alpha - time.delta_secs() * 2.).clamp(0., 1.);
text_color.0.set_alpha(alpha);
if alpha == 0. {
commands.entity(entity).despawn();
}
Expand Down
Loading