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

Change material api #328

Merged
merged 5 commits into from
Aug 20, 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
10 changes: 5 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ edition = "2021"
version = "0.1.0"
license = "MIT OR Apache-2.0"
repository = "https://github.com/modor-engine/modor"
rust-version = "1.79.0"
rust-version = "1.80.1"

[workspace.dependencies]
ab_glyph = "0.2"
Expand Down Expand Up @@ -71,10 +71,10 @@ unused_import_braces = "warn"
unused_qualifications = "warn"

[workspace.lints.clippy]
all = "warn"
pedantic = "warn"
nursery = "warn"
cargo = "warn"
all = { level = "warn", priority = -1 }
pedantic = { level = "warn", priority = -1 }
nursery = { level = "warn", priority = -1 }
cargo = { level = "warn", priority = -1 }
dbg_macro = "warn"
decimal_literal_representation = "warn"
filetype_is_file = "warn"
Expand Down
2 changes: 1 addition & 1 deletion crates/modor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ pub use modor_derive::main;
/// # Platform-specific
///
/// - Web: function is annotated with `#[wasm_bindgen_test::wasm_bindgen_test]` instead of
/// `#[test]`.
/// `#[test]`.
///
/// # Examples
///
Expand Down
16 changes: 14 additions & 2 deletions crates/modor_derive/src/updater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use proc_macro2::Span;
use proc_macro2::{Ident, TokenStream};
use quote::{format_ident, quote, quote_spanned};
use syn::spanned::Spanned;
use syn::{parse_quote, Attribute, DeriveInput, GenericParam, Generics, Type};
use syn::{parse_quote, Attribute, DeriveInput, GenericParam, Generics, Type, Visibility};

pub(crate) fn impl_block(input: &DeriveInput) -> Result<TokenStream, TokenStream> {
let crate_ident = utils::crate_ident();
Expand All @@ -22,14 +22,15 @@ pub(crate) fn impl_block(input: &DeriveInput) -> Result<TokenStream, TokenStream
.try_into()?;
let field_idents = field_idents(&parsed.fields);
let field_types = field_types(&parsed.fields);
let field_visibilities = field_visibilities(&parsed.fields);
let updater_fns = all_field_fns(input, &crate_ident, &parsed.fields);
let updater_ident = format_ident!("{}Updater", ident);
let updater_doc = format!("An updater for [`{ident}`].");
Ok(quote! {
#[doc = #updater_doc]
#[must_use]
#vis struct #updater_ident #updater_generics {
#(#field_idents: ::#crate_ident::Update<'closures, #field_types>,)*
#(#field_visibilities #field_idents: ::#crate_ident::Update<'closures, #field_types>,)*
phantom: #phantom_type,
}

Expand Down Expand Up @@ -78,6 +79,14 @@ fn field_types(fields: &[ParsedUpdaterField]) -> Vec<&Type> {
.collect()
}

fn field_visibilities(fields: &[ParsedUpdaterField]) -> Vec<&Visibility> {
fields
.iter()
.filter(|field| field.is_field_method_generated || field.is_for_field_method_generated)
.map(|field| &field.vis)
.collect()
}

fn all_field_fns(
input: &DeriveInput,
crate_ident: &Ident,
Expand Down Expand Up @@ -144,6 +153,7 @@ struct UpdaterStruct {
struct UpdaterField {
ident: Option<Ident>,
ty: Type,
vis: Visibility,
attrs: Vec<Attribute>,
#[darling(default)]
inner_type: bool,
Expand Down Expand Up @@ -179,6 +189,7 @@ impl TryFrom<UpdaterStruct> for ParsedUpdaterStruct {
struct ParsedUpdaterField {
ident: Ident,
type_: Type,
vis: Visibility,
doc_attrs: Vec<Attribute>,
is_field_method_generated: bool,
is_for_field_method_generated: bool,
Expand All @@ -192,6 +203,7 @@ impl TryFrom<UpdaterField> for ParsedUpdaterField {
Ok(Self {
ident,
type_: Self::parse_type(field.inner_type, field.ty)?,
vis: field.vis,
doc_attrs: Self::parse_doc_attributes(field.attrs),
is_field_method_generated: field.field,
is_for_field_method_generated: field.for_field,
Expand Down
12 changes: 8 additions & 4 deletions crates/modor_graphics/src/animation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,10 @@ use std::time::Duration;
/// TexturePart::new(2, 1),
/// ];
/// Self {
/// sprite: Sprite2D::new(app)
/// .with_material(|m| m.texture = texture.to_ref()),
/// sprite: Sprite2D::from_app(app)
/// .with_material(|m| DefaultMaterial2DUpdater::default()
/// .texture(texture.to_ref())
/// .apply(app, m)),
/// animation: TextureAnimation::new(3, 2)
/// .with_fps(5)
/// .with_parts(|p| *p = animation_parts),
Expand All @@ -50,8 +52,10 @@ use std::time::Duration;
/// }
///
/// fn update(&mut self, app: &mut App) {
/// self.sprite.material.texture_size = self.animation.part_size();
/// self.sprite.material.texture_position = self.animation.part_position();
/// DefaultMaterial2DUpdater::default()
/// .texture_size(self.animation.part_size())
/// .texture_position(self.animation.part_position())
/// .apply(app, &self.sprite.material);
/// self.sprite.update(app);
/// self.animation.update(app);
/// }
Expand Down
22 changes: 11 additions & 11 deletions crates/modor_graphics/src/camera.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::buffer::{Buffer, BufferBindGroup};
use crate::gpu::{Gpu, GpuManager};
use crate::{Size, TargetGlob};
use crate::{Size, Target};
use fxhash::FxHashMap;
use modor::{App, Builder, FromApp, Glob, GlobRef, Global};
use modor_physics::modor_math::{Mat4, Quat, Vec2, Vec3};
Expand Down Expand Up @@ -29,7 +29,7 @@ use wgpu::{BindGroup, BufferUsages};
/// fn new(app: &mut App) -> Self {
/// let camera = app.get_mut::<MovingCamera>().camera.glob().to_ref();
/// Self {
/// sprite: Sprite2D::new(app)
/// sprite: Sprite2D::from_app(app)
/// .with_model(|m| m.size = Vec2::ONE * 0.2)
/// .with_model(|m| m.camera = camera)
/// }
Expand All @@ -46,7 +46,7 @@ use wgpu::{BindGroup, BufferUsages};
///
/// impl FromApp for MovingCamera {
/// fn from_app(app: &mut App) -> Self {
/// let target = app.get_mut::<Window>().target.glob().to_ref();
/// let target = app.get_mut::<Window>().target.to_ref();
/// Self {
/// camera: Camera2D::new(app, vec![target])
/// .with_size(Vec2::ONE * 0.5) // zoom x2
Expand Down Expand Up @@ -77,13 +77,13 @@ pub struct Camera2D {
/// If a camera is linked to a target, then all models linked to the camera are rendered in the
/// target.
#[builder(form(closure))]
pub targets: Vec<GlobRef<TargetGlob>>,
pub targets: Vec<GlobRef<Target>>,
glob: Glob<Camera2DGlob>,
}

impl Camera2D {
/// Creates a new camera.
pub fn new(app: &mut App, targets: Vec<GlobRef<TargetGlob>>) -> Self {
pub fn new(app: &mut App, targets: Vec<GlobRef<Target>>) -> Self {
Self {
position: Vec2::ZERO,
size: Vec2::ONE,
Expand All @@ -94,7 +94,7 @@ impl Camera2D {
}

/// Updates the camera.
pub fn update(&mut self, app: &mut App) {
pub fn update(&self, app: &mut App) {
let target_sizes = self.target_sizes(app);
let gpu = app.get_mut::<GpuManager>().get_or_init().clone();
let glob = self.glob.get_mut(app);
Expand Down Expand Up @@ -126,7 +126,7 @@ impl Camera2D {
fn target_sizes(&self, app: &App) -> Vec<(usize, Size)> {
self.targets
.iter()
.map(|target| (target.index(), target.get(app).size))
.map(|target| (target.index(), target.get(app).size()))
.collect()
}
}
Expand All @@ -137,7 +137,7 @@ pub struct Camera2DGlob {
pub(crate) position: Vec2,
pub(crate) size: Vec2,
pub(crate) rotation: f32,
pub(crate) targets: Vec<GlobRef<TargetGlob>>,
pub(crate) targets: Vec<GlobRef<Target>>,
target_uniforms: FxHashMap<usize, CameraUniform>,
}

Expand Down Expand Up @@ -168,13 +168,13 @@ impl Camera2DGlob {
)
}

pub(crate) fn bind_group(&self, target: &Glob<TargetGlob>) -> Option<&BindGroup> {
pub(crate) fn bind_group(&self, target_index: usize) -> Option<&BindGroup> {
self.target_uniforms
.get(&target.index())
.get(&target_index)
.map(|uniform| &uniform.bind_group.inner)
}

fn register_targets(&mut self, targets: &[GlobRef<TargetGlob>]) {
fn register_targets(&mut self, targets: &[GlobRef<Target>]) {
let target_indexes: Vec<_> = targets.iter().map(|target| target.index()).collect();
self.target_uniforms
.retain(|target_index, _| target_indexes.contains(target_index));
Expand Down
6 changes: 6 additions & 0 deletions crates/modor_graphics/src/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ impl From<Color> for [f32; 4] {
}
}

impl From<[f32; 4]> for Color {
fn from(color: [f32; 4]) -> Self {
Self::rgba(color[0], color[1], color[2], color[3])
}
}

impl Color {
/// <span style="color:black">█</span>
pub const BLACK: Self = Self::rgb(0., 0., 0.);
Expand Down
Loading
Loading