Skip to content

Commit

Permalink
wip, simple example compiling
Browse files Browse the repository at this point in the history
  • Loading branch information
jabuwu committed Nov 30, 2024
1 parent e7fa383 commit 63eae2d
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 76 deletions.
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
9 changes: 6 additions & 3 deletions examples/simple.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use bevy::prelude::*;
use bevy_spine::{
SkeletonController, SkeletonData, Spine, SpineBundle, SpinePlugin, SpineReadyEvent, SpineSet,
SkeletonController, SkeletonData, Spine, SpineBundle, SpineLoader, SpinePlugin, SpineReadyEvent, SpineSet
};

fn main() {
Expand All @@ -16,7 +16,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 @@ -25,7 +25,10 @@ fn setup(
let skeleton_handle = skeletons.add(skeleton);

commands.spawn(SpineBundle {
skeleton: skeleton_handle.clone(),
loader: SpineLoader {
skeleton: skeleton_handle.clone(),
with_children: true,
},
transform: Transform::from_xyz(0., -200., 0.),
..Default::default()
});
Expand Down
4 changes: 4 additions & 0 deletions rust-toolchain
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[toolchain]
channel = "1.83.0"
components = ["rustfmt", "clippy"]
targets = ["wasm32-unknown-unknown"]
30 changes: 15 additions & 15 deletions src/assets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ impl AssetLoader for AtlasLoader {
type Settings = ();
type Error = SpineLoaderError;

async fn load<'a>(
&'a self,
reader: &'a mut Reader<'_>,
_settings: &'a Self::Settings,
load_context: &'a mut LoadContext<'_>,
async fn load(
&self,
reader: &mut dyn Reader,
_settings: &Self::Settings,
load_context: &mut LoadContext<'_>,
) -> Result<Self::Asset, Self::Error> {
let mut bytes = Vec::new();
reader.read_to_end(&mut bytes).await?;
Expand Down Expand Up @@ -72,11 +72,11 @@ impl AssetLoader for SkeletonJsonLoader {
type Settings = ();
type Error = SpineLoaderError;

async fn load<'a>(
&'a self,
reader: &'a mut Reader<'_>,
_settings: &'a Self::Settings,
_load_context: &'a mut LoadContext<'_>,
async fn load(
&self,
reader: &mut dyn Reader,
_settings: &Self::Settings,
_load_context: &mut LoadContext<'_>,
) -> Result<Self::Asset, Self::Error> {
let mut bytes = Vec::new();
reader.read_to_end(&mut bytes).await?;
Expand Down Expand Up @@ -106,11 +106,11 @@ impl AssetLoader for SkeletonBinaryLoader {
type Settings = ();
type Error = SpineLoaderError;

async fn load<'a>(
&'a self,
reader: &'a mut Reader<'_>,
_settings: &'a Self::Settings,
_load_context: &'a mut LoadContext<'_>,
async fn load(
&self,
reader: &mut dyn Reader,
_settings: &Self::Settings,
_load_context: &mut LoadContext<'_>,
) -> Result<Self::Asset, Self::Error> {
let mut bytes = Vec::new();
reader.read_to_end(&mut bytes).await?;
Expand Down
85 changes: 35 additions & 50 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,11 @@ use std::{
};

use bevy::{
asset::load_internal_binary_asset,
prelude::*,
render::{
asset::load_internal_binary_asset, image::{ImageAddressMode, ImageFilterMode, ImageSampler, ImageSamplerDescriptor}, prelude::*, render::{
mesh::{Indices, MeshVertexAttribute},
render_asset::RenderAssetUsages,
render_resource::{PrimitiveTopology, VertexFormat},
texture::{ImageAddressMode, ImageFilterMode, ImageSampler, ImageSamplerDescriptor},
},
sprite::{Material2dPlugin, Mesh2dHandle},
}, sprite::Material2dPlugin
};
use materials::{
SpineAdditiveMaterial, SpineAdditivePmaMaterial, SpineMaterialInfo, SpineMultiplyMaterial,
Expand Down Expand Up @@ -258,36 +254,16 @@ impl core::ops::DerefMut for Spine {
/// entities representing the bones of a skeleton (see [`SpineBone`]). These bones are not
/// synchronized (see [`SpineSync`]), and can be disabled entirely using
/// [`SpineLoader::without_children`].
#[derive(Component, Debug)]
pub enum SpineLoader {
/// The spine rig is still loading.
Loading {
/// If true, will spawn child entities for each bone in the skeleton (see [`SpineBone`]).
with_children: bool,
},
/// The spine rig is ready.
Ready,
/// The spine rig failed to load.
Failed,
}

impl Default for SpineLoader {
fn default() -> Self {
Self::new()
}
#[derive(Default, Component, Debug)]
pub struct SpineLoader {
pub skeleton: Handle<SkeletonData>,
/// If true, will spawn child entities for each bone in the skeleton (see [`SpineBone`]).
pub with_children: bool,
}

impl SpineLoader {
pub fn new() -> Self {
Self::with_children()
}

pub fn with_children() -> Self {
Self::Loading {
with_children: true,
}
}

// 0.15 TODO
/*
/// Load a [`Spine`] entity without child entities containing [`SpineBone`] components.
///
/// Renderable mesh child entities are still created.
Expand All @@ -304,10 +280,19 @@ impl SpineLoader {
/// # }
/// ```
pub fn without_children() -> Self {
Self::Loading {
Self {
with_children: false,
state: SpineLoadState::Loading,
}
}
}*/
}

#[derive(Component, Default, Debug, Clone, Copy, PartialEq, Eq)]
pub enum SpineLoadState {
#[default]
Loading,
Ready,
Failed,
}

/// Settings for how this Spine updates and renders.
Expand Down Expand Up @@ -430,8 +415,8 @@ impl Default for SpineSettings {
#[derive(Default, Bundle)]
pub struct SpineBundle {
pub loader: SpineLoader,
pub load_state: SpineLoadState,
pub settings: SpineSettings,
pub skeleton: Handle<SkeletonData>,
pub crossfades: Crossfades,
pub transform: Transform,
pub global_transform: GlobalTransform,
Expand Down Expand Up @@ -600,8 +585,8 @@ fn spine_load(
fn spine_spawn(
mut skeleton_query: Query<(
&mut SpineLoader,
&mut SpineLoadState,
Entity,
&Handle<SkeletonData>,
Option<&Crossfades>,
)>,
mut commands: Commands,
Expand All @@ -610,10 +595,10 @@ fn spine_spawn(
mut skeleton_data_assets: ResMut<Assets<SkeletonData>>,
spine_event_queue: Res<SpineEventQueue>,
) {
for (mut spine_loader, spine_entity, data_handle, crossfades) in skeleton_query.iter_mut() {
if let SpineLoader::Loading { with_children } = spine_loader.as_ref() {
for (mut spine_loader, mut spine_load_state, spine_entity, crossfades) in skeleton_query.iter_mut() {
if matches!(spine_load_state.as_ref(), SpineLoadState::Loading) {
let skeleton_data_asset =
if let Some(skeleton_data_asset) = skeleton_data_assets.get_mut(data_handle) {
if let Some(skeleton_data_asset) = skeleton_data_assets.get_mut(&spine_loader.skeleton) {
skeleton_data_asset
} else {
continue;
Expand Down Expand Up @@ -738,7 +723,7 @@ fn spine_spawn(
z += 0.001;
}
});
if *with_children {
if spine_loader.with_children {
spawn_bones(
spine_entity,
None,
Expand All @@ -751,15 +736,15 @@ fn spine_spawn(
})
.insert(Spine(controller));
}
*spine_loader = SpineLoader::Ready;
*spine_load_state = SpineLoadState::Ready;
ready_events.0.push(SpineReadyEvent {
entity: spine_entity,
bones,
});
}
SkeletonDataStatus::Loading => {}
SkeletonDataStatus::Failed => {
*spine_loader = SpineLoader::Failed;
*spine_load_state = SpineLoadState::Failed;
}
}
}
Expand Down Expand Up @@ -833,7 +818,7 @@ fn spine_update_animation(
spine_event_queue: Res<SpineEventQueue>,
) {
for (_, mut spine) in spine_query.iter_mut() {
spine.update(time.delta_seconds(), Physics::Update);
spine.update(time.delta_secs(), Physics::Update);
}
{
let mut events = spine_event_queue.0.lock().unwrap();
Expand All @@ -856,8 +841,8 @@ fn spine_update_meshes(
Entity,
&mut SpineMesh,
&mut Transform,
Option<&Mesh2dHandle>,
Option<&Handle<Mesh>>,
Option<&Mesh2d>,
Option<&Mesh3d>,
)>,
mut commands: Commands,
meshes_query: Query<(&Parent, &Children), With<SpineMeshes>>,
Expand Down Expand Up @@ -908,14 +893,14 @@ fn spine_update_meshes(
apply_mesh!(
spine_2d_mesh,
mesh_type == SpineMeshType::Mesh2D,
Mesh2dHandle(spine_mesh.handle.clone()),
Mesh2dHandle
Mesh2d(spine_mesh.handle.clone()),
Mesh2d
);
apply_mesh!(
spine_3d_mesh,
mesh_type == SpineMeshType::Mesh3D,
spine_mesh.handle.clone(),
Handle<Mesh>
Mesh3d(spine_mesh.handle.clone()),
Mesh3d
);
let Some(mesh) = meshes.get_mut(&spine_mesh.handle) else {
continue;
Expand Down
10 changes: 5 additions & 5 deletions src/materials.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use crate::{SpineMesh, SpineMeshState, SpineSettings, SpineSystem};
/// Implement the trait and add it with [`SpineMaterialPlugin`].
pub trait SpineMaterial: Sized {
/// The material type to apply to [`SpineMesh`]. Usually is `Self`.
type Material: Asset + Clone;
type Material: Material2d;
/// System parameters to query when updating this material.
type Params<'w, 's>: SystemParam;

Expand Down Expand Up @@ -82,7 +82,7 @@ pub struct SpineMaterialInfo {
fn update_materials<T: SpineMaterial>(
mut commands: Commands,
mut materials: ResMut<Assets<T::Material>>,
mesh_query: Query<(Entity, &SpineMesh, Option<&Handle<T::Material>>)>,
mesh_query: Query<(Entity, &SpineMesh, Option<&MeshMaterial2d<T::Material>>)>,
params: StaticSystemParam<T::Params<'_, '_>>,
) {
for (mesh_entity, spine_mesh, material_handle) in mesh_query.iter() {
Expand All @@ -102,19 +102,19 @@ fn update_materials<T: SpineMaterial>(
} else {
materials.remove(handle);
if let Some(mut entity_commands) = commands.get_entity(mesh_entity) {
entity_commands.remove::<Handle<T::Material>>();
entity_commands.remove::<MeshMaterial2d<T::Material>>();
}
}
} else if let Some(material) = T::update(None, spine_mesh.spine_entity, data, &params) {
let handle = materials.add(material);
if let Some(mut entity_commands) = commands.get_entity(mesh_entity) {
entity_commands.insert(handle.clone());
entity_commands.insert(MeshMaterial2d(handle.clone()));
}
};
}
}

pub const DARK_COLOR_SHADER_POSITION: usize = 10;
pub const DARK_COLOR_SHADER_POSITION: u64 = 10;
pub const DARK_COLOR_ATTRIBUTE: MeshVertexAttribute = MeshVertexAttribute::new(
"Vertex_DarkColor",
DARK_COLOR_SHADER_POSITION,
Expand Down

0 comments on commit 63eae2d

Please sign in to comment.