bevy_materialize
Crate for loading and applying type-erased materials in Bevy.
Built-in supported formats are json, and toml, but you can easily add more.
Usage Example (TOML)
First, add the MaterializePlugin to your App.
use *;
use *;
The API for adding to an entity is quite similar to MeshMaterial3d<...>, just with GenericMaterial3d storing a Handle<GenericMaterial> instead, which you can load from a file.
use *;
use *;
assets/materials/example.material
# The type name of the material. Can either be the full path (e.g. bevy_pbr::pbr_material::StandardMaterial),
# or, if only one registered material has the name, just the name itself.
# If this field is not specified, defaults to StandardMaterial
= "StandardMaterial"
[]
# Asset paths are relative to the material's path.
= "example.png"
= [0.1, 0.2, 0.5, 1.0]
= { = 0.5 }
# Optional custom properties, these can be whatever you want.
[]
# This one is built-in, and sets the entity's Visibility when the material is applied.
= "Hidden"
= true
= "wood"
For simplicity, you can also load a GenericMaterial directly from an image file, which puts a StandardMaterial internally. You can change the default StandardMaterial that it uses via
use *;
use ;
new.with_simple_loader_settings;
// This would disable the image loading functionality entirely.
new.with_simple_loader_settings;
For retrieving properties from a material, the easiest way is with a GenericMaterialView, which you can get via the GenericMaterials system param.
It's not as easy as getting it from the GenericMaterial because properties need additional references to parse, such as the asset server and type registry.
use *;
use *;
use GenericMaterialError;
For creating your own properties, you should make an extension trait for GenericMaterial.
use *;
When creating your own custom materials, all you have to do is register them in your app like so.
new
// ...
.
This will also register the type if it hasn't been registered already.
You can also register a shorthand if your material's name is very long (like if it's an ExtendedMaterial<...>).
new
// ...
.
This will allow you to put the shorthand in your file's type field instead of the type name.
Supported Bevy Versions
| Bevy | bevy_materialize |
|---|---|
| 0.15 | 0.1 |