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

Add AssetSchematic #57

Merged
merged 6 commits into from
Sep 17, 2023
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
15 changes: 15 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,11 @@ bevy = "0.11.0"
bevy_prototype_lyon = "0.9.0"
trybuild = "1.0.71"

[[example]]
name = "asset_schematic"
path = "examples/asset_schematic.rs"
required-features = ["ron", "auto_name", "custom_schematics", "bevy_sprite"]

[[example]]
name = "basic_schematic"
path = "examples/basic_schematic.rs"
Expand Down Expand Up @@ -165,6 +170,16 @@ name = "templates"
path = "examples/templates.rs"
required-features = ["ron", "auto_name", "custom_schematics", "bevy_sprite", "yaml"]

[[example]]
name = "bevy_asset_loading"
path = "examples/bevy/asset_loading.rs"
required-features = ["ron", "auto_name", "custom_schematics"]

[[example]]
name = "bevy_sprite_sheet"
path = "examples/bevy/sprite_sheet.rs"
required-features = ["ron", "auto_name", "custom_schematics", "bevy_sprite"]

[[example]]
name = "bevy_ui"
path = "examples/bevy/ui.rs"
Expand Down
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,15 @@ This crate can be used for:
> (
> name: "Puppy",
> schematics: {
> "game::image::GameImage": (
> handle: AssetPath("textures/puppy.png"),
> "game::level::Level": (
> // Load by path:
> background: AssetPath("textures/bg.png"),
> // Or define assets inline:
> map: Asset((
> name: "Level 1",
> size: (20, 20),
> // ...
> ))
> ),
> },
> )
Expand Down
14 changes: 14 additions & 0 deletions assets/examples/asset_schematic/Level1.prototype.ron
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
(
name: "Level1",
schematics: {
"asset_schematic::CurrentLevel": (
// The `InlinableProtoAsset::Asset` variant allows us to define our asset inline.
level: Asset((
name: "Tutorial",
// Both `InlinableProtoAsset` and the standard `ProtoAsset` contain an `AssetPath`
// variant for cases where we want to reference an asset defined in a separate file.
player: AssetPath("textures/platformer/player/p1_front.png")
))
),
}
)
15 changes: 15 additions & 0 deletions assets/examples/bevy/asset_loading/Camera.prototype.ron
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
(
name: "Camera",
schematics: {
"bevy_proto::custom::Camera3dBundle": (
transform: (
translation: (
x: 0,
y: 3.0,
z: 10.0
),
rotation: (-0.14521314, -0.0, -0.0, 0.9894004),
)
)
}
)
29 changes: 29 additions & 0 deletions assets/examples/bevy/asset_loading/Cube.prototype.ron
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
(
name: "Cube",
schematics: {
"bevy_proto::custom::MaterialMeshBundle<bevy_pbr::pbr_material::StandardMaterial>": (
// Mesh is an `AssetSchematic` which has a dedicated `MeshInput` type used for
// defining the mesh within a prototype file.
// The `MeshInput` type is an enum with various variants for primitive shapes.
// Here, we're using the `Cube` variant:
mesh: Asset(Cube((
size: 2.0,
))),
material: Asset((
base_color: Rgba(
red: 0.8,
green: 0.7,
blue: 0.6,
alpha: 1.0
),
)),
transform: (
translation: (
x: 0.0,
y: 0.0,
z: 0.0
)
),
)
},
)
14 changes: 14 additions & 0 deletions assets/examples/bevy/asset_loading/Light.prototype.ron
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
(
name: "Light",
schematics: {
"bevy_proto::custom::PointLightBundle": (
transform: (
translation: (
x: 4.0,
y: 5.0,
z: 4.0
),
)
)
}
)
23 changes: 23 additions & 0 deletions assets/examples/bevy/asset_loading/Monkey.prototype.ron
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
(
name: "Monkey",
schematics: {
"bevy_proto::custom::MaterialMeshBundle<bevy_pbr::pbr_material::StandardMaterial>": (
mesh: AssetPath("examples/bevy/asset_loading/monkey/Monkey.gltf#Mesh0/Primitive0"),
material: Asset((
base_color: Rgba(
red: 0.8,
green: 0.7,
blue: 0.6,
alpha: 1.0
),
)),
transform: (
translation: (
x: -3.0,
y: 0.0,
z: 0.0
)
),
)
}
)
29 changes: 29 additions & 0 deletions assets/examples/bevy/asset_loading/Sphere.prototype.ron
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
(
name: "Sphere",
schematics: {
"bevy_proto::custom::MaterialMeshBundle<bevy_pbr::pbr_material::StandardMaterial>": (
// Mesh is an `AssetSchematic` which has a dedicated `MeshInput` type used for
// defining the mesh within a prototype file.
// The `MeshInput` type is an enum with various variants for primitive shapes.
// Here, we're using the `Cube` variant:
mesh: Asset(UvSphere((
radius: 1.0,
))),
material: Asset((
base_color: Rgba(
red: 0.8,
green: 0.7,
blue: 0.6,
alpha: 1.0
),
)),
transform: (
translation: (
x: 3.0,
y: 0.0,
z: 0.0
)
),
)
}
)
122 changes: 122 additions & 0 deletions assets/examples/bevy/asset_loading/monkey/Monkey.gltf

Large diffs are not rendered by default.

44 changes: 44 additions & 0 deletions assets/examples/bevy/sprite_sheet/Player.prototype.ron
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
(
name: "Player",
schematics: {
"bevy_proto::custom::SpriteSheetBundle": (
// The schematic for `TextureAtlas` defines an enum input with a `Grid` variant.
// The fields of this variant map to the parameters of `TextureAtlas::from_grid`.
texture_atlas: Asset(Grid(
texture: AssetPath("textures/rpg/chars/gabe/gabe-idle-run.png"),
tile_size: (
x: 24.0,
y: 24.0
),
columns: 7,
rows: 1,
padding: None,
offset: None,
)),
sprite: (
// Start our sprite at the first frame of the run animation
index: 1
),
transform: (
scale: (
x: 6.0,
y: 6.0,
z: 6.0,
)
),
),
"bevy_sprite_sheet::AnimationIndices": (
// The run animation actually starts on the second frame (index 1)
first: 1,
last: 6
),
"bevy_sprite_sheet::AnimationTimer": ((
duration: (
secs: 0,
// 1e8 nanoseconds == 0.1 seconds
nanos: 100000000
),
mode: Repeating
))
}
)
Binary file added assets/textures/rpg/chars/gabe/gabe-idle-run.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 23 additions & 0 deletions assets/textures/rpg/release.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
EST�DIO VACA ROXA

Ajude a iniciativa Vaca Roxa:
https://apoia.se/vacaroxa
https://patreon.com/bakudas

Social:
https://twitter.com/bakudas
https://twitter.com/estudiovacaroxa/
https://www.fb.com/estudiovacaroxa
https://www.youtube.com/estudiovacaroxa

Artwork by Bakudas and Gabe Fern
Vers�o ALPHA release v0.4

v0.4:
- Readme update licence to cc0 as itch page.

Licence:
CC0 1.0 Universal (CC0 1.0)
Public Domain Dedication

You can copy, modify, distribute and perform the work, even for commercial purposes, all without asking permission.
31 changes: 31 additions & 0 deletions bevy_proto_backend/src/assets/extension.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use crate::assets::{AssetSchematic, InlinableProtoAsset, ProtoAsset};
use bevy::app::App;
use bevy::asset::{AddAsset, Handle};
use bevy::reflect::TypePath;

/// [`App`] extension trait for working with [`AssetSchematics`].
///
/// [`AssetSchematics`]: AssetSchematic
pub trait AssetSchematicAppExt {
/// Registers an [`AssetSchematic`].
///
/// This is a convenience method for the following registrations:
/// - `Handle<T::Output>`
/// - `ProtoAsset<T::Output>`
/// - `InlinableProtoAsset<T>`
/// - `T::Input`
fn register_asset_schematic<T: AssetSchematic + TypePath>(&mut self) -> &mut Self;
}

impl AssetSchematicAppExt for App {
fn register_asset_schematic<T: AssetSchematic + TypePath>(&mut self) -> &mut Self {
self.add_asset::<T::Output>()
.register_type::<Handle<T::Output>>()
.register_type::<Option<Handle<T::Output>>>()
.register_type::<ProtoAsset<T::Output>>()
.register_type::<Option<ProtoAsset<T::Output>>>()
.register_type::<InlinableProtoAsset<T>>()
.register_type::<Option<InlinableProtoAsset<T>>>()
.register_type::<T::Input>()
}
}
110 changes: 110 additions & 0 deletions bevy_proto_backend/src/assets/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
//! Schematic types used to build [assets].
//!
//! # Handling Assets
//!
//! Bevy uses a [`Handle`] type to reference assets.
//! Unfortunately, this type isn't very useful serialized as it doesn't directly
//! track the asset's path.
//!
//! In order to reference assets in a serialized format,
//! this crate defines a special [`ProtoAsset`] type which does keep track of the path.
//! This can then be used in the input type for a [`Schematic`] to load assets.
//! And, in fact, when deriving `Schematic`, a `Handle`-containing field
//! can be marked with the `#[schematic(asset)]` attribute to automatically handle this.
//!
//! ```
//! # use bevy::prelude::*;
//! # use bevy_proto_derive::Schematic;
//! # use bevy_proto_backend::schematics::ReflectSchematic;
//! #[derive(Component, Reflect, Schematic)]
//! #[reflect(Schematic)]
//! struct Player {
//! #[schematic(asset)]
//! sprite: Handle<Image>
//! }
//! ```
//!
//! This will generate a schematic input type with the appropriate `ProtoAsset` field:
//!
//! ```
//! # use bevy::prelude::*;
//! # use bevy_proto_backend::assets::ProtoAsset;
//! #[derive(Reflect)]
//! struct PlayerInput {
//! sprite: ProtoAsset<Image>
//! }
//! ```
//!
//! See the [derive macro documentation](bevy_proto_derive::Schematic) for more details
//! on this attribute and its various arguments.
//!
//! # Asset Schematics
//!
//! While referencing an asset by path is good enough for most cases,
//! sometimes it would be better to just define an asset as part of a prototype,
//! without needing to create a completely separate asset file.
//!
//! This is where [`AssetSchematic`] comes in.
//! This trait defines an [`Input`] data type and an [`Output`] asset type,
//! which can be used to create assets inline with a given schematic.
//! Because the implementor does not need to be the asset type itself,
//! any number of schematics can be defined for a single asset type.
//!
//! An asset schematic is used much like normal assets,
//! except, instead of using `ProtoAsset`, [`InlinableProtoAsset`] is used.
//!
//! The `AssetSchematic` trait can be derived and can even reference other assets.
//! It can then be referenced in a schematic using the `#[schematic(asset)]` attribute
//! with the `inline` argument.
//!
//! ```
//! # use bevy::prelude::*;
//! # use bevy::reflect::{TypePath, TypeUuid};
//! # use bevy_proto_derive::{AssetSchematic, Schematic};
//! # use bevy_proto_backend::schematics::ReflectSchematic;
//! #[derive(AssetSchematic, TypeUuid, TypePath)]
//! #[uuid = "a3de79e1-0364-4fdf-9a82-9fae288e0aed"]
//! struct Level {
//! name: String,
//! #[asset_schematic(asset)]
//! background: Handle<Image>
//! }
//!
//! #[derive(Component, Reflect, Schematic)]
//! #[reflect(Schematic)]
//! struct CurrentLevel {
//! #[schematic(asset(inline))]
//! sprite: Handle<Level>
//! }
//! ```
//!
//! Asset schematics have a lot of moving parts that need to be registered in the app.
//! To make things easier, this crate comes with an [extension trait] which can be used
//! to automatically register all of the necessary types.
//!
//! ```ignore
//! app.register_asset_schematic::<MyAssetSchematic>();
//! ```
//!
//! See the [derive macro documentation](bevy_proto_derive::AssetSchematic) for more details.
//!
//! [assets]: bevy::asset::Asset
//! [`Handle`]: bevy::asset::Handle
//! [`ProtoAsset`]: ProtoAsset
//! [`Schematic`]: crate::schematics::Schematic
//! [`AssetSchematic`]: AssetSchematic
//! [`Input`]: AssetSchematic::Input
//! [`Output`]: AssetSchematic::Output
//! [`InlinableProtoAsset`]: InlinableProtoAsset
//! [extension trait]: AssetSchematicAppExt

pub use bevy_proto_derive::AssetSchematic;
pub use event::*;
pub use extension::*;
pub use proto::*;
pub use schematic::*;

mod event;
mod extension;
mod proto;
mod schematic;
Loading