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

update to bevy 0.14 #24

Merged
merged 3 commits into from
Jul 4, 2024
Merged
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
8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "bevy_spine"
version = "0.9.0"
version = "0.10.0"
edition = "2021"
description = "Spine plugin for Bevy utilizing rusty_spine"
homepage = "https://github.com/jabuwu/bevy_spine"
Expand All @@ -11,17 +11,17 @@ exclude = ["assets/*"]

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

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

[workspace]
resolver = "2"
Expand Down
2 changes: 1 addition & 1 deletion assets/shaders/custom.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ struct VertexOutput {
fn vertex(vertex: Vertex) -> VertexOutput {
var out: VertexOutput;
out.uv = vertex.uv;
var model = mesh_functions::get_model_matrix(vertex.instance_index);
var model = mesh_functions::get_world_from_local(vertex.instance_index);
out.world_position = mesh_functions::mesh2d_position_local_to_world(
model,
vec4<f32>(vertex.position, 1.0)
Expand Down
3 changes: 3 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# 0.10.0
- Update to Bevy 0.14

# 0.9.0
- Upgrade runtime to Spine 4.2
- Update to `rusty_spine` 0.8
Expand Down
2 changes: 1 addition & 1 deletion examples/3d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ fn setup(
// plane
commands.spawn(PbrBundle {
mesh: meshes.add(Plane3d::default().mesh().size(5.0, 5.0)),
material: materials.add(Color::rgb(0.3, 0.5, 0.3)),
material: materials.add(Color::srgb(0.3, 0.5, 0.3)),
..default()
});

Expand Down
6 changes: 3 additions & 3 deletions examples/custom_material.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use bevy::{
prelude::*,
reflect::TypePath,
render::{
mesh::MeshVertexBufferLayout,
mesh::MeshVertexBufferLayoutRef,
render_resource::{
AsBindGroup, RenderPipelineDescriptor, ShaderRef, SpecializedMeshPipelineError,
},
Expand Down Expand Up @@ -105,7 +105,7 @@ impl Material2d for MyMaterial {

fn specialize(
descriptor: &mut RenderPipelineDescriptor,
layout: &MeshVertexBufferLayout,
layout: &MeshVertexBufferLayoutRef,
_key: Material2dKey<Self>,
) -> Result<(), SpecializedMeshPipelineError> {
let vertex_attributes = vec![
Expand All @@ -115,7 +115,7 @@ impl Material2d for MyMaterial {
Mesh::ATTRIBUTE_COLOR.at_shader_location(4),
DARK_COLOR_ATTRIBUTE.at_shader_location(DARK_COLOR_SHADER_POSITION as u32),
];
let vertex_buffer_layout = layout.get_layout(&vertex_attributes)?;
let vertex_buffer_layout = layout.0.get_layout(&vertex_attributes)?;
descriptor.vertex.buffers = vec![vertex_buffer_layout];
descriptor.primitive.cull_mode = None;
Ok(())
Expand Down
4 changes: 2 additions & 2 deletions examples/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,9 @@ fn footstep_update(
) {
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.a();
let mut alpha = text.sections[0].style.color.alpha();
alpha = (alpha - time.delta_seconds() * 2.).clamp(0., 1.);
text.sections[0].style.color.set_a(alpha);
text.sections[0].style.color.set_alpha(alpha);
if alpha == 0. {
commands.entity(entity).despawn();
}
Expand Down
2 changes: 1 addition & 1 deletion examples/spineboy/bullet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ fn bullet_spawn(mut commands: Commands, mut bullet_spawn_events: EventReader<Bul
commands
.spawn(SpriteBundle {
sprite: Sprite {
color: Color::RED,
color: Srgba::RED.into(),
custom_size: Some(Vec2::ONE * 16.),
..Default::default()
},
Expand Down
30 changes: 8 additions & 22 deletions examples/spineboy/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,30 +175,16 @@ fn player_aim(
camera_query: Query<(Entity, &Camera)>,
time: Res<Time>,
) {
let Some(window) = window_query.get_single().ok() else {
let (camera_entity, camera) = camera_query.single();
let camera_global_transform = global_transform_query.get(camera_entity).unwrap();
let Ok(window) = window_query.get_single() else {
return;
};
let cursor_position = if let Some(cursor_position) = window.cursor_position() {
if let Ok((camera_entity, camera)) = camera_query.get_single() {
if let Ok(camera_transform) = global_transform_query.get(camera_entity) {
let window_size = Vec2::new(
window_query.single().width(),
window_query.single().height(),
);
let ndc = (cursor_position / window_size) * 2.0 - Vec2::ONE;
let ndc_to_world =
camera_transform.compute_matrix() * camera.projection_matrix().inverse();
let world_pos = ndc_to_world.project_point3(ndc.extend(-1.0));
world_pos.truncate() * Vec2::new(1., -1.)
} else {
Vec2::ZERO
}
} else {
Vec2::ZERO
}
} else {
Vec2::ZERO
};
let cursor_position = window
.cursor_position()
.and_then(|cursor| camera.viewport_to_world(camera_global_transform, cursor))
.map(|ray| ray.origin.truncate())
.unwrap_or(Vec2::ZERO);
for (mut spine, player_entity, crosshair, player) in crosshair_query.iter_mut() {
if player.spawned {
if let Ok((crosshair_entity, crosshair_parent)) = bone_query.get(crosshair.bone) {
Expand Down
3 changes: 2 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ bevy_spine = "0.9"

| bevy_spine | rusty_spine | bevy | spine |
| ---------- | ----------- | ---- | ----- |
| main | 0.8 | 0.13 | 4.2 |
| main | 0.8 | 0.14 | 4.2 |
| 0.10 | 0.8 | 0.14 | 4.2 |
| 0.9 | 0.8 | 0.13 | 4.2 |
| 0.8 | 0.7 | 0.13 | 4.1 |
| 0.8 | 0.7 | 0.13 | 4.1 |
Expand Down
67 changes: 30 additions & 37 deletions src/assets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use bevy::{
asset::{io::Reader, AssetLoader, AsyncReadExt, LoadContext},
prelude::*,
reflect::TypePath,
utils::BoxedFuture,
};
use rusty_spine::SpineError;
use thiserror::Error;
Expand Down Expand Up @@ -33,24 +32,22 @@ impl AssetLoader for AtlasLoader {
type Settings = ();
type Error = SpineLoaderError;

fn load<'a>(
async fn load<'a>(
&'a self,
reader: &'a mut Reader,
reader: &'a mut Reader<'_>,
_settings: &'a Self::Settings,
load_context: &'a mut LoadContext,
) -> BoxedFuture<'a, Result<Self::Asset, Self::Error>> {
Box::pin(async move {
let mut bytes = Vec::new();
reader.read_to_end(&mut bytes).await?;
Ok(Atlas {
atlas: Arc::new(rusty_spine::Atlas::new(
&bytes,
load_context
.path()
.parent()
.unwrap_or_else(|| Path::new("")),
)?),
})
load_context: &'a mut LoadContext<'_>,
) -> Result<Self::Asset, Self::Error> {
let mut bytes = Vec::new();
reader.read_to_end(&mut bytes).await?;
Ok(Atlas {
atlas: Arc::new(rusty_spine::Atlas::new(
&bytes,
load_context
.path()
.parent()
.unwrap_or_else(|| Path::new("")),
)?),
})
}

Expand All @@ -75,18 +72,16 @@ impl AssetLoader for SkeletonJsonLoader {
type Settings = ();
type Error = SpineLoaderError;

fn load<'a>(
async fn load<'a>(
&'a self,
reader: &'a mut Reader,
reader: &'a mut Reader<'_>,
_settings: &'a Self::Settings,
_load_context: &'a mut LoadContext,
) -> BoxedFuture<'a, Result<Self::Asset, Self::Error>> {
Box::pin(async move {
let mut bytes = Vec::new();
reader.read_to_end(&mut bytes).await?;
Ok(SkeletonJson {
json: bytes.to_vec(),
})
_load_context: &'a mut LoadContext<'_>,
) -> Result<Self::Asset, Self::Error> {
let mut bytes = Vec::new();
reader.read_to_end(&mut bytes).await?;
Ok(SkeletonJson {
json: bytes.to_vec(),
})
}

Expand All @@ -111,18 +106,16 @@ impl AssetLoader for SkeletonBinaryLoader {
type Settings = ();
type Error = SpineLoaderError;

fn load<'a>(
async fn load<'a>(
&'a self,
reader: &'a mut Reader,
reader: &'a mut Reader<'_>,
_settings: &'a Self::Settings,
_load_context: &'a mut LoadContext,
) -> BoxedFuture<'a, Result<Self::Asset, Self::Error>> {
Box::pin(async move {
let mut bytes = Vec::new();
reader.read_to_end(&mut bytes).await?;
Ok(SkeletonBinary {
binary: bytes.to_vec(),
})
_load_context: &'a mut LoadContext<'_>,
) -> Result<Self::Asset, Self::Error> {
let mut bytes = Vec::new();
reader.read_to_end(&mut bytes).await?;
Ok(SkeletonBinary {
binary: bytes.to_vec(),
})
}

Expand Down
46 changes: 20 additions & 26 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,7 @@ fn spine_load(
premultiplied_alpha,
} = skeleton_data_asset;
if matches!(status, SkeletonDataStatus::Loading) {
let atlas = if let Some(atlas) = atlases.get(atlas_handle.clone()) {
let atlas = if let Some(atlas) = atlases.get(atlas_handle) {
atlas
} else {
continue;
Expand All @@ -549,7 +549,7 @@ fn spine_load(
}
match kind {
SkeletonDataKind::JsonFile(json_handle) => {
let json = if let Some(json) = jsons.get(json_handle.clone()) {
let json = if let Some(json) = jsons.get(json_handle) {
json
} else {
continue;
Expand All @@ -566,7 +566,7 @@ fn spine_load(
}
}
SkeletonDataKind::BinaryFile(binary_handle) => {
let binary = if let Some(binary) = binaries.get(binary_handle.clone()) {
let binary = if let Some(binary) = binaries.get(binary_handle) {
binary
} else {
continue;
Expand Down Expand Up @@ -1051,7 +1051,6 @@ fn adjust_spine_textures(
mut spine_texture_create_events: EventReader<SpineTextureCreateEvent>,
mut images: ResMut<Assets<Image>>,
) {
use bevy::render::color::Color;
for spine_texture_create_event in spine_texture_create_events.read() {
local.handles.push((
spine_texture_create_event.handle.clone(),
Expand Down Expand Up @@ -1093,36 +1092,31 @@ fn adjust_spine_textures(
// multiplied in linear space to render properly in Bevy.
if handle_config.premultiplied_alpha {
for i in 0..(image.data.len() / 4) {
let mut rgba = Color::rgba_u8(
let mut rgba = Srgba::rgba_u8(
image.data[i * 4],
image.data[i * 4 + 1],
image.data[i * 4 + 2],
image.data[i * 4 + 3],
);
if rgba.a() != 0. {
rgba = Color::rgba(
rgba.r() / rgba.a(),
rgba.g() / rgba.a(),
rgba.b() / rgba.a(),
rgba.a(),
if rgba.alpha != 0. {
rgba = Srgba::new(
rgba.red / rgba.alpha,
rgba.green / rgba.alpha,
rgba.blue / rgba.alpha,
rgba.alpha,
);
} else {
rgba = Color::rgba(0., 0., 0., 0.);
}
let mut linear_rgba = rgba.as_linear_rgba_f32();
linear_rgba[0] *= linear_rgba[3];
linear_rgba[1] *= linear_rgba[3];
linear_rgba[2] *= linear_rgba[3];
rgba = Color::rgba_linear(
linear_rgba[0],
linear_rgba[1],
linear_rgba[2],
linear_rgba[3],
)
.as_rgba();
for j in 0..4 {
image.data[i * 4 + j] = (rgba.as_rgba_f32()[j] * 255.) as u8;
rgba = Srgba::new(0., 0., 0., 0.);
}
let mut linear_rgba = LinearRgba::from(rgba);
linear_rgba.red *= linear_rgba.alpha;
linear_rgba.green *= linear_rgba.alpha;
linear_rgba.blue *= linear_rgba.alpha;
rgba = Srgba::from(linear_rgba);
image.data[i * 4] = (rgba.red * 255.) as u8;
image.data[i * 4 + 1] = (rgba.green * 255.) as u8;
image.data[i * 4 + 2] = (rgba.blue * 255.) as u8;
image.data[i * 4 + 3] = (rgba.alpha * 255.) as u8;
}
}
removed_handles.push(handle_index);
Expand Down
9 changes: 6 additions & 3 deletions src/materials.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use bevy::{
prelude::*,
reflect::TypePath,
render::{
mesh::{MeshVertexAttribute, MeshVertexBufferLayout},
mesh::{MeshVertexAttribute, MeshVertexBufferLayoutRef},
render_resource::{
AsBindGroup, BlendComponent, BlendFactor, BlendOperation, BlendState,
RenderPipelineDescriptor, ShaderRef, SpecializedMeshPipelineError, VertexFormat,
Expand Down Expand Up @@ -101,6 +101,9 @@ fn update_materials<T: SpineMaterial>(
*material = new_material;
} else {
materials.remove(handle);
if let Some(mut entity_commands) = commands.get_entity(mesh_entity) {
entity_commands.remove::<Handle<T::Material>>();
}
}
} else if let Some(material) = T::update(None, spine_mesh.spine_entity, data, &params) {
let handle = materials.add(material);
Expand Down Expand Up @@ -155,7 +158,7 @@ macro_rules! material {

fn specialize(
descriptor: &mut RenderPipelineDescriptor,
layout: &MeshVertexBufferLayout,
layout: &MeshVertexBufferLayoutRef,
_key: Material2dKey<Self>,
) -> Result<(), SpecializedMeshPipelineError> {
let vertex_attributes = vec![
Expand All @@ -165,7 +168,7 @@ macro_rules! material {
Mesh::ATTRIBUTE_COLOR.at_shader_location(4),
DARK_COLOR_ATTRIBUTE.at_shader_location(DARK_COLOR_SHADER_POSITION as u32),
];
let vertex_buffer_layout = layout.get_layout(&vertex_attributes)?;
let vertex_buffer_layout = layout.0.get_layout(&vertex_attributes)?;
descriptor.vertex.buffers = vec![vertex_buffer_layout];
if let Some(fragment) = &mut descriptor.fragment {
if let Some(target_state) = &mut fragment.targets[0] {
Expand Down
2 changes: 1 addition & 1 deletion src/spine.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ struct VertexOutput {
fn vertex(vertex: Vertex) -> VertexOutput {
var out: VertexOutput;
out.uv = vertex.uv;
var model = mesh_functions::get_model_matrix(vertex.instance_index);
var model = mesh_functions::get_world_from_local(vertex.instance_index);
out.world_position = mesh_functions::mesh2d_position_local_to_world(
model,
vec4<f32>(vertex.position, 1.0)
Expand Down
Loading