Suppress the `clippy::type_complexity` lint (#8313)
# Objective
The clippy lint `type_complexity` is known not to play well with bevy.
It frequently triggers when writing complex queries, and taking the
lint's advice of using a type alias almost always just obfuscates the
code with no benefit. Because of this, this lint is currently ignored in
CI, but unfortunately it still shows up when viewing bevy code in an
IDE.
As someone who's made a fair amount of pull requests to this repo, I
will say that this issue has been a consistent thorn in my side. Since
bevy code is filled with spurious, ignorable warnings, it can be very
difficult to spot the *real* warnings that must be fixed -- most of the
time I just ignore all warnings, only to later find out that one of them
was real after I'm done when CI runs.
## Solution
Suppress this lint in all bevy crates. This was previously attempted in
#7050, but the review process ended up making it more complicated than
it needs to be and landed on a subpar solution.
The discussion in https://github.com/rust-lang/rust-clippy/pull/10571
explores some better long-term solutions to this problem. Since there is
no timeline on when these solutions may land, we should resolve this
issue in the meantime by locally suppressing these lints.
### Unresolved issues
Currently, these lints are not suppressed in our examples, since that
would require suppressing the lint in every single source file. They are
still ignored in CI.
2023-04-06 21:27:36 +00:00
|
|
|
#![allow(clippy::type_complexity)]
|
|
|
|
|
2022-04-02 22:36:02 +00:00
|
|
|
#[cfg(feature = "bevy_animation")]
|
|
|
|
use bevy_animation::AnimationClip;
|
2021-12-14 03:58:23 +00:00
|
|
|
use bevy_utils::HashMap;
|
2020-12-31 20:57:15 +00:00
|
|
|
|
2020-05-15 23:55:44 +00:00
|
|
|
mod loader;
|
2023-04-24 14:20:13 +00:00
|
|
|
mod vertex_attributes;
|
2020-05-15 23:55:44 +00:00
|
|
|
pub use loader::*;
|
2020-04-19 17:08:47 +00:00
|
|
|
|
2020-07-17 01:47:51 +00:00
|
|
|
use bevy_app::prelude::*;
|
2020-12-31 20:57:15 +00:00
|
|
|
use bevy_asset::{AddAsset, Handle};
|
2022-04-07 21:30:52 +00:00
|
|
|
use bevy_ecs::{prelude::Component, reflect::ReflectComponent};
|
2021-12-14 03:58:23 +00:00
|
|
|
use bevy_pbr::StandardMaterial;
|
bevy_reflect: `FromReflect` Ergonomics Implementation (#6056)
# Objective
**This implementation is based on
https://github.com/bevyengine/rfcs/pull/59.**
---
Resolves #4597
Full details and motivation can be found in the RFC, but here's a brief
summary.
`FromReflect` is a very powerful and important trait within the
reflection API. It allows Dynamic types (e.g., `DynamicList`, etc.) to
be formed into Real ones (e.g., `Vec<i32>`, etc.).
This mainly comes into play concerning deserialization, where the
reflection deserializers both return a `Box<dyn Reflect>` that almost
always contain one of these Dynamic representations of a Real type. To
convert this to our Real type, we need to use `FromReflect`.
It also sneaks up in other ways. For example, it's a required bound for
`T` in `Vec<T>` so that `Vec<T>` as a whole can be made `FromReflect`.
It's also required by all fields of an enum as it's used as part of the
`Reflect::apply` implementation.
So in other words, much like `GetTypeRegistration` and `Typed`, it is
very much a core reflection trait.
The problem is that it is not currently treated like a core trait and is
not automatically derived alongside `Reflect`. This makes using it a bit
cumbersome and easy to forget.
## Solution
Automatically derive `FromReflect` when deriving `Reflect`.
Users can then choose to opt-out if needed using the
`#[reflect(from_reflect = false)]` attribute.
```rust
#[derive(Reflect)]
struct Foo;
#[derive(Reflect)]
#[reflect(from_reflect = false)]
struct Bar;
fn test<T: FromReflect>(value: T) {}
test(Foo); // <-- OK
test(Bar); // <-- Panic! Bar does not implement trait `FromReflect`
```
#### `ReflectFromReflect`
This PR also automatically adds the `ReflectFromReflect` (introduced in
#6245) registration to the derived `GetTypeRegistration` impl— if the
type hasn't opted out of `FromReflect` of course.
<details>
<summary><h4>Improved Deserialization</h4></summary>
> **Warning**
> This section includes changes that have since been descoped from this
PR. They will likely be implemented again in a followup PR. I am mainly
leaving these details in for archival purposes, as well as for reference
when implementing this logic again.
And since we can do all the above, we might as well improve
deserialization. We can now choose to deserialize into a Dynamic type or
automatically convert it using `FromReflect` under the hood.
`[Un]TypedReflectDeserializer::new` will now perform the conversion and
return the `Box`'d Real type.
`[Un]TypedReflectDeserializer::new_dynamic` will work like what we have
now and simply return the `Box`'d Dynamic type.
```rust
// Returns the Real type
let reflect_deserializer = UntypedReflectDeserializer::new(®istry);
let mut deserializer = ron::de::Deserializer::from_str(input)?;
let output: SomeStruct = reflect_deserializer.deserialize(&mut deserializer)?.take()?;
// Returns the Dynamic type
let reflect_deserializer = UntypedReflectDeserializer::new_dynamic(®istry);
let mut deserializer = ron::de::Deserializer::from_str(input)?;
let output: DynamicStruct = reflect_deserializer.deserialize(&mut deserializer)?.take()?;
```
</details>
---
## Changelog
* `FromReflect` is now automatically derived within the `Reflect` derive
macro
* This includes auto-registering `ReflectFromReflect` in the derived
`GetTypeRegistration` impl
* ~~Renamed `TypedReflectDeserializer::new` and
`UntypedReflectDeserializer::new` to
`TypedReflectDeserializer::new_dynamic` and
`UntypedReflectDeserializer::new_dynamic`, respectively~~ **Descoped**
* ~~Changed `TypedReflectDeserializer::new` and
`UntypedReflectDeserializer::new` to automatically convert the
deserialized output using `FromReflect`~~ **Descoped**
## Migration Guide
* `FromReflect` is now automatically derived within the `Reflect` derive
macro. Items with both derives will need to remove the `FromReflect`
one.
```rust
// OLD
#[derive(Reflect, FromReflect)]
struct Foo;
// NEW
#[derive(Reflect)]
struct Foo;
```
If using a manual implementation of `FromReflect` and the `Reflect`
derive, users will need to opt-out of the automatic implementation.
```rust
// OLD
#[derive(Reflect)]
struct Foo;
impl FromReflect for Foo {/* ... */}
// NEW
#[derive(Reflect)]
#[reflect(from_reflect = false)]
struct Foo;
impl FromReflect for Foo {/* ... */}
```
<details>
<summary><h4>Removed Migrations</h4></summary>
> **Warning**
> This section includes changes that have since been descoped from this
PR. They will likely be implemented again in a followup PR. I am mainly
leaving these details in for archival purposes, as well as for reference
when implementing this logic again.
* The reflect deserializers now perform a `FromReflect` conversion
internally. The expected output of `TypedReflectDeserializer::new` and
`UntypedReflectDeserializer::new` is no longer a Dynamic (e.g.,
`DynamicList`), but its Real counterpart (e.g., `Vec<i32>`).
```rust
let reflect_deserializer =
UntypedReflectDeserializer::new_dynamic(®istry);
let mut deserializer = ron::de::Deserializer::from_str(input)?;
// OLD
let output: DynamicStruct = reflect_deserializer.deserialize(&mut
deserializer)?.take()?;
// NEW
let output: SomeStruct = reflect_deserializer.deserialize(&mut
deserializer)?.take()?;
```
Alternatively, if this behavior isn't desired, use the
`TypedReflectDeserializer::new_dynamic` and
`UntypedReflectDeserializer::new_dynamic` methods instead:
```rust
// OLD
let reflect_deserializer = UntypedReflectDeserializer::new(®istry);
// NEW
let reflect_deserializer =
UntypedReflectDeserializer::new_dynamic(®istry);
```
</details>
---------
Co-authored-by: Carter Anderson <mcanders1@gmail.com>
2023-06-29 01:31:34 +00:00
|
|
|
use bevy_reflect::{Reflect, TypePath, TypeUuid};
|
2023-04-24 14:20:13 +00:00
|
|
|
use bevy_render::{
|
|
|
|
mesh::{Mesh, MeshVertexAttribute},
|
|
|
|
renderer::RenderDevice,
|
|
|
|
texture::CompressedImageFormats,
|
|
|
|
};
|
2020-12-31 20:57:15 +00:00
|
|
|
use bevy_scene::Scene;
|
2020-04-19 17:08:47 +00:00
|
|
|
|
2021-12-14 03:58:23 +00:00
|
|
|
/// Adds support for glTF file loading to the app.
|
2020-05-15 23:55:44 +00:00
|
|
|
#[derive(Default)]
|
2023-04-24 14:20:13 +00:00
|
|
|
pub struct GltfPlugin {
|
|
|
|
custom_vertex_attributes: HashMap<String, MeshVertexAttribute>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl GltfPlugin {
|
|
|
|
pub fn add_custom_vertex_attribute(
|
|
|
|
mut self,
|
|
|
|
name: &str,
|
|
|
|
attribute: MeshVertexAttribute,
|
|
|
|
) -> Self {
|
|
|
|
self.custom_vertex_attributes
|
|
|
|
.insert(name.to_string(), attribute);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
2020-04-20 02:29:33 +00:00
|
|
|
|
2020-08-08 03:22:17 +00:00
|
|
|
impl Plugin for GltfPlugin {
|
2021-07-27 20:21:06 +00:00
|
|
|
fn build(&self, app: &mut App) {
|
2023-07-23 01:27:37 +00:00
|
|
|
app.register_type::<GltfExtras>()
|
|
|
|
.add_asset::<Gltf>()
|
|
|
|
.add_asset::<GltfNode>()
|
|
|
|
.add_asset::<GltfPrimitive>()
|
2023-08-14 21:27:51 +00:00
|
|
|
.add_asset::<GltfMesh>()
|
|
|
|
.preregister_asset_loader(&["gltf", "glb"]);
|
2023-07-23 01:27:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn finish(&self, app: &mut App) {
|
2023-04-24 14:20:13 +00:00
|
|
|
let supported_compressed_formats = match app.world.get_resource::<RenderDevice>() {
|
|
|
|
Some(render_device) => CompressedImageFormats::from_features(render_device.features()),
|
|
|
|
|
2023-07-23 01:27:37 +00:00
|
|
|
None => CompressedImageFormats::NONE,
|
2023-04-24 14:20:13 +00:00
|
|
|
};
|
|
|
|
app.add_asset_loader::<GltfLoader>(GltfLoader {
|
|
|
|
supported_compressed_formats,
|
|
|
|
custom_vertex_attributes: self.custom_vertex_attributes.clone(),
|
2023-07-23 01:27:37 +00:00
|
|
|
});
|
2020-04-19 17:08:47 +00:00
|
|
|
}
|
2020-05-16 07:27:30 +00:00
|
|
|
}
|
2020-12-31 20:57:15 +00:00
|
|
|
|
2023-07-31 18:55:42 +00:00
|
|
|
/// Representation of a loaded glTF file
|
|
|
|
/// (file loaded via the `AssetServer` with the extension `.glb` or `.gltf`).
|
2023-06-05 20:31:20 +00:00
|
|
|
#[derive(Debug, TypeUuid, TypePath)]
|
2020-12-31 20:57:15 +00:00
|
|
|
#[uuid = "5c7d5f8a-f7b0-4e45-a09e-406c0372fea2"]
|
|
|
|
pub struct Gltf {
|
|
|
|
pub scenes: Vec<Handle<Scene>>,
|
|
|
|
pub named_scenes: HashMap<String, Handle<Scene>>,
|
|
|
|
pub meshes: Vec<Handle<GltfMesh>>,
|
|
|
|
pub named_meshes: HashMap<String, Handle<GltfMesh>>,
|
|
|
|
pub materials: Vec<Handle<StandardMaterial>>,
|
|
|
|
pub named_materials: HashMap<String, Handle<StandardMaterial>>,
|
|
|
|
pub nodes: Vec<Handle<GltfNode>>,
|
|
|
|
pub named_nodes: HashMap<String, Handle<GltfNode>>,
|
|
|
|
pub default_scene: Option<Handle<Scene>>,
|
2022-04-02 22:36:02 +00:00
|
|
|
#[cfg(feature = "bevy_animation")]
|
|
|
|
pub animations: Vec<Handle<AnimationClip>>,
|
|
|
|
#[cfg(feature = "bevy_animation")]
|
|
|
|
pub named_animations: HashMap<String, Handle<AnimationClip>>,
|
2020-12-31 20:57:15 +00:00
|
|
|
}
|
|
|
|
|
2023-02-13 17:56:36 +00:00
|
|
|
/// A glTF node with all of its child nodes, its [`GltfMesh`],
|
|
|
|
/// [`Transform`](bevy_transform::prelude::Transform) and an optional [`GltfExtras`].
|
2023-06-05 20:31:20 +00:00
|
|
|
#[derive(Debug, Clone, TypeUuid, TypePath)]
|
2020-12-31 20:57:15 +00:00
|
|
|
#[uuid = "dad74750-1fd6-460f-ac51-0a7937563865"]
|
|
|
|
pub struct GltfNode {
|
|
|
|
pub children: Vec<GltfNode>,
|
|
|
|
pub mesh: Option<Handle<GltfMesh>>,
|
|
|
|
pub transform: bevy_transform::prelude::Transform,
|
2023-02-13 17:56:36 +00:00
|
|
|
pub extras: Option<GltfExtras>,
|
2020-12-31 20:57:15 +00:00
|
|
|
}
|
|
|
|
|
2023-02-13 17:56:36 +00:00
|
|
|
/// A glTF mesh, which may consist of multiple [`GltfPrimitives`](GltfPrimitive)
|
|
|
|
/// and an optional [`GltfExtras`].
|
2023-06-05 20:31:20 +00:00
|
|
|
#[derive(Debug, Clone, TypeUuid, TypePath)]
|
2020-12-31 20:57:15 +00:00
|
|
|
#[uuid = "8ceaec9a-926a-4f29-8ee3-578a69f42315"]
|
|
|
|
pub struct GltfMesh {
|
|
|
|
pub primitives: Vec<GltfPrimitive>,
|
2023-02-13 17:56:36 +00:00
|
|
|
pub extras: Option<GltfExtras>,
|
2020-12-31 20:57:15 +00:00
|
|
|
}
|
|
|
|
|
2023-02-13 17:56:36 +00:00
|
|
|
/// Part of a [`GltfMesh`] that consists of a [`Mesh`], an optional [`StandardMaterial`] and [`GltfExtras`].
|
2023-06-05 20:31:20 +00:00
|
|
|
#[derive(Debug, Clone, TypeUuid, TypePath)]
|
2020-12-31 20:57:15 +00:00
|
|
|
#[uuid = "cbfca302-82fd-41cb-af77-cab6b3d50af1"]
|
|
|
|
pub struct GltfPrimitive {
|
|
|
|
pub mesh: Handle<Mesh>,
|
|
|
|
pub material: Option<Handle<StandardMaterial>>,
|
2023-02-13 17:56:36 +00:00
|
|
|
pub extras: Option<GltfExtras>,
|
|
|
|
pub material_extras: Option<GltfExtras>,
|
2020-12-31 20:57:15 +00:00
|
|
|
}
|
2022-04-07 21:30:52 +00:00
|
|
|
|
bevy_reflect: `FromReflect` Ergonomics Implementation (#6056)
# Objective
**This implementation is based on
https://github.com/bevyengine/rfcs/pull/59.**
---
Resolves #4597
Full details and motivation can be found in the RFC, but here's a brief
summary.
`FromReflect` is a very powerful and important trait within the
reflection API. It allows Dynamic types (e.g., `DynamicList`, etc.) to
be formed into Real ones (e.g., `Vec<i32>`, etc.).
This mainly comes into play concerning deserialization, where the
reflection deserializers both return a `Box<dyn Reflect>` that almost
always contain one of these Dynamic representations of a Real type. To
convert this to our Real type, we need to use `FromReflect`.
It also sneaks up in other ways. For example, it's a required bound for
`T` in `Vec<T>` so that `Vec<T>` as a whole can be made `FromReflect`.
It's also required by all fields of an enum as it's used as part of the
`Reflect::apply` implementation.
So in other words, much like `GetTypeRegistration` and `Typed`, it is
very much a core reflection trait.
The problem is that it is not currently treated like a core trait and is
not automatically derived alongside `Reflect`. This makes using it a bit
cumbersome and easy to forget.
## Solution
Automatically derive `FromReflect` when deriving `Reflect`.
Users can then choose to opt-out if needed using the
`#[reflect(from_reflect = false)]` attribute.
```rust
#[derive(Reflect)]
struct Foo;
#[derive(Reflect)]
#[reflect(from_reflect = false)]
struct Bar;
fn test<T: FromReflect>(value: T) {}
test(Foo); // <-- OK
test(Bar); // <-- Panic! Bar does not implement trait `FromReflect`
```
#### `ReflectFromReflect`
This PR also automatically adds the `ReflectFromReflect` (introduced in
#6245) registration to the derived `GetTypeRegistration` impl— if the
type hasn't opted out of `FromReflect` of course.
<details>
<summary><h4>Improved Deserialization</h4></summary>
> **Warning**
> This section includes changes that have since been descoped from this
PR. They will likely be implemented again in a followup PR. I am mainly
leaving these details in for archival purposes, as well as for reference
when implementing this logic again.
And since we can do all the above, we might as well improve
deserialization. We can now choose to deserialize into a Dynamic type or
automatically convert it using `FromReflect` under the hood.
`[Un]TypedReflectDeserializer::new` will now perform the conversion and
return the `Box`'d Real type.
`[Un]TypedReflectDeserializer::new_dynamic` will work like what we have
now and simply return the `Box`'d Dynamic type.
```rust
// Returns the Real type
let reflect_deserializer = UntypedReflectDeserializer::new(®istry);
let mut deserializer = ron::de::Deserializer::from_str(input)?;
let output: SomeStruct = reflect_deserializer.deserialize(&mut deserializer)?.take()?;
// Returns the Dynamic type
let reflect_deserializer = UntypedReflectDeserializer::new_dynamic(®istry);
let mut deserializer = ron::de::Deserializer::from_str(input)?;
let output: DynamicStruct = reflect_deserializer.deserialize(&mut deserializer)?.take()?;
```
</details>
---
## Changelog
* `FromReflect` is now automatically derived within the `Reflect` derive
macro
* This includes auto-registering `ReflectFromReflect` in the derived
`GetTypeRegistration` impl
* ~~Renamed `TypedReflectDeserializer::new` and
`UntypedReflectDeserializer::new` to
`TypedReflectDeserializer::new_dynamic` and
`UntypedReflectDeserializer::new_dynamic`, respectively~~ **Descoped**
* ~~Changed `TypedReflectDeserializer::new` and
`UntypedReflectDeserializer::new` to automatically convert the
deserialized output using `FromReflect`~~ **Descoped**
## Migration Guide
* `FromReflect` is now automatically derived within the `Reflect` derive
macro. Items with both derives will need to remove the `FromReflect`
one.
```rust
// OLD
#[derive(Reflect, FromReflect)]
struct Foo;
// NEW
#[derive(Reflect)]
struct Foo;
```
If using a manual implementation of `FromReflect` and the `Reflect`
derive, users will need to opt-out of the automatic implementation.
```rust
// OLD
#[derive(Reflect)]
struct Foo;
impl FromReflect for Foo {/* ... */}
// NEW
#[derive(Reflect)]
#[reflect(from_reflect = false)]
struct Foo;
impl FromReflect for Foo {/* ... */}
```
<details>
<summary><h4>Removed Migrations</h4></summary>
> **Warning**
> This section includes changes that have since been descoped from this
PR. They will likely be implemented again in a followup PR. I am mainly
leaving these details in for archival purposes, as well as for reference
when implementing this logic again.
* The reflect deserializers now perform a `FromReflect` conversion
internally. The expected output of `TypedReflectDeserializer::new` and
`UntypedReflectDeserializer::new` is no longer a Dynamic (e.g.,
`DynamicList`), but its Real counterpart (e.g., `Vec<i32>`).
```rust
let reflect_deserializer =
UntypedReflectDeserializer::new_dynamic(®istry);
let mut deserializer = ron::de::Deserializer::from_str(input)?;
// OLD
let output: DynamicStruct = reflect_deserializer.deserialize(&mut
deserializer)?.take()?;
// NEW
let output: SomeStruct = reflect_deserializer.deserialize(&mut
deserializer)?.take()?;
```
Alternatively, if this behavior isn't desired, use the
`TypedReflectDeserializer::new_dynamic` and
`UntypedReflectDeserializer::new_dynamic` methods instead:
```rust
// OLD
let reflect_deserializer = UntypedReflectDeserializer::new(®istry);
// NEW
let reflect_deserializer =
UntypedReflectDeserializer::new_dynamic(®istry);
```
</details>
---------
Co-authored-by: Carter Anderson <mcanders1@gmail.com>
2023-06-29 01:31:34 +00:00
|
|
|
#[derive(Clone, Debug, Reflect, Default, Component)]
|
|
|
|
#[reflect(Component)]
|
2022-04-07 21:30:52 +00:00
|
|
|
pub struct GltfExtras {
|
|
|
|
pub value: String,
|
|
|
|
}
|