2021-02-22 04:50:05 +00:00
<!-- MD024 - The Headers from the Platform - Specific Examples should be identical -->
<!-- markdownlint - disable - file MD024 -->
2020-08-17 23:02:59 +00:00
# Examples
These examples demonstrate the main features of Bevy and how to use them.
2020-08-25 00:06:08 +00:00
To run an example, use the command `cargo run --example <Example>` , and add the option `--features x11` or `--features wayland` to force the example to run on a specific window compositor, e.g.
2020-11-03 19:32:48 +00:00
```sh
2020-09-11 19:19:53 +00:00
cargo run --features wayland --example hello_world
2020-08-25 00:06:08 +00:00
```
2020-08-17 23:02:59 +00:00
2021-02-22 04:50:05 +00:00
**⚠️ Note: for users of releases on crates.io!**
2020-11-12 01:15:19 +00:00
2021-03-12 02:46:51 +00:00
There are often large differences and incompatible API changes between the latest [crates.io ](https://crates.io/crates/bevy ) release and the development version of Bevy in the git main branch!
2020-11-12 01:15:19 +00:00
2021-03-12 02:46:51 +00:00
If you are using a released version of bevy, you need to make sure you are viewing the correct version of the examples!
2021-04-13 17:18:47 +00:00
- Latest release: [https://github.com/bevyengine/bevy/tree/latest/examples ](https://github.com/bevyengine/bevy/tree/latest/examples )
- Specific version, such as `0.4` : [https://github.com/bevyengine/bevy/tree/v0.4.0/examples ](https://github.com/bevyengine/bevy/tree/v0.4.0/examples )
2021-03-12 02:46:51 +00:00
When you clone the repo locally to run the examples, use `git checkout` to get the correct version:
2021-02-22 04:50:05 +00:00
```bash
2021-03-12 02:46:51 +00:00
# `latest` always points to the newest release
git checkout latest
# or use a specific version
2021-02-01 04:19:10 +00:00
git checkout v0.4.0
2020-11-12 01:15:19 +00:00
```
---
2021-02-22 04:50:05 +00:00
## Table of Contents
2020-11-12 01:15:19 +00:00
Adding `WorldQuery` for `WithBundle` (#2024)
In response to #2023, here is a draft for a PR.
Fixes #2023
I've added an example to show how to use `WithBundle`, and also to test it out.
Right now there is a bug: If a bundle and a query are "the same", then it doesn't filter out
what it needs to filter out.
Example:
```
Print component initated from bundle.
[examples/ecs/query_bundle.rs:57] x = Dummy( <========= This should not get printed
111,
)
[examples/ecs/query_bundle.rs:57] x = Dummy(
222,
)
Show all components
[examples/ecs/query_bundle.rs:50] x = Dummy(
111,
)
[examples/ecs/query_bundle.rs:50] x = Dummy(
222,
)
```
However, it behaves the right way, if I add one more component to the bundle,
so the query and the bundle doesn't look the same:
```
Print component initated from bundle.
[examples/ecs/query_bundle.rs:57] x = Dummy(
222,
)
Show all components
[examples/ecs/query_bundle.rs:50] x = Dummy(
111,
)
[examples/ecs/query_bundle.rs:50] x = Dummy(
222,
)
```
I hope this helps. I'm definitely up for tinkering with this, and adding anything that I'm asked to add
or change.
Co-authored-by: Carter Anderson <mcanders1@gmail.com>
2021-04-28 21:03:10 +00:00
- [Examples ](#examples )
- [Table of Contents ](#table-of-contents )
2020-11-15 19:24:18 +00:00
- [The Bare Minimum ](#the-bare-minimum )
- [Hello, World! ](#hello-world )
- [Cross-Platform Examples ](#cross-platform-examples )
- [2D Rendering ](#2d-rendering )
- [3D Rendering ](#3d-rendering )
- [Application ](#application )
- [Assets ](#assets )
2021-05-23 20:13:55 +00:00
- [Async Tasks ](#async-tasks )
2020-11-15 19:24:18 +00:00
- [Audio ](#audio )
- [Diagnostics ](#diagnostics )
- [ECS (Entity Component System) ](#ecs-entity-component-system )
- [Games ](#games )
- [Input ](#input )
2020-12-02 22:35:27 +00:00
- [Reflection ](#reflection )
2020-11-15 19:24:18 +00:00
- [Scene ](#scene )
- [Shaders ](#shaders )
2021-04-15 00:57:37 +00:00
- [Tests ](#tests )
2020-11-15 19:24:18 +00:00
- [Tools ](#tools )
- [UI (User Interface) ](#ui-user-interface )
- [Window ](#window )
- [Platform-Specific Examples ](#platform-specific-examples )
- [Android ](#android )
Adding `WorldQuery` for `WithBundle` (#2024)
In response to #2023, here is a draft for a PR.
Fixes #2023
I've added an example to show how to use `WithBundle`, and also to test it out.
Right now there is a bug: If a bundle and a query are "the same", then it doesn't filter out
what it needs to filter out.
Example:
```
Print component initated from bundle.
[examples/ecs/query_bundle.rs:57] x = Dummy( <========= This should not get printed
111,
)
[examples/ecs/query_bundle.rs:57] x = Dummy(
222,
)
Show all components
[examples/ecs/query_bundle.rs:50] x = Dummy(
111,
)
[examples/ecs/query_bundle.rs:50] x = Dummy(
222,
)
```
However, it behaves the right way, if I add one more component to the bundle,
so the query and the bundle doesn't look the same:
```
Print component initated from bundle.
[examples/ecs/query_bundle.rs:57] x = Dummy(
222,
)
Show all components
[examples/ecs/query_bundle.rs:50] x = Dummy(
111,
)
[examples/ecs/query_bundle.rs:50] x = Dummy(
222,
)
```
I hope this helps. I'm definitely up for tinkering with this, and adding anything that I'm asked to add
or change.
Co-authored-by: Carter Anderson <mcanders1@gmail.com>
2021-04-28 21:03:10 +00:00
- [Setup ](#setup )
- [Build & Run ](#build--run )
- [Old phones ](#old-phones )
2020-11-15 19:24:18 +00:00
- [iOS ](#ios )
Adding `WorldQuery` for `WithBundle` (#2024)
In response to #2023, here is a draft for a PR.
Fixes #2023
I've added an example to show how to use `WithBundle`, and also to test it out.
Right now there is a bug: If a bundle and a query are "the same", then it doesn't filter out
what it needs to filter out.
Example:
```
Print component initated from bundle.
[examples/ecs/query_bundle.rs:57] x = Dummy( <========= This should not get printed
111,
)
[examples/ecs/query_bundle.rs:57] x = Dummy(
222,
)
Show all components
[examples/ecs/query_bundle.rs:50] x = Dummy(
111,
)
[examples/ecs/query_bundle.rs:50] x = Dummy(
222,
)
```
However, it behaves the right way, if I add one more component to the bundle,
so the query and the bundle doesn't look the same:
```
Print component initated from bundle.
[examples/ecs/query_bundle.rs:57] x = Dummy(
222,
)
Show all components
[examples/ecs/query_bundle.rs:50] x = Dummy(
111,
)
[examples/ecs/query_bundle.rs:50] x = Dummy(
222,
)
```
I hope this helps. I'm definitely up for tinkering with this, and adding anything that I'm asked to add
or change.
Co-authored-by: Carter Anderson <mcanders1@gmail.com>
2021-04-28 21:03:10 +00:00
- [Setup ](#setup-1 )
- [Build & Run ](#build--run-1 )
2020-11-15 19:24:18 +00:00
- [WASM ](#wasm )
Adding `WorldQuery` for `WithBundle` (#2024)
In response to #2023, here is a draft for a PR.
Fixes #2023
I've added an example to show how to use `WithBundle`, and also to test it out.
Right now there is a bug: If a bundle and a query are "the same", then it doesn't filter out
what it needs to filter out.
Example:
```
Print component initated from bundle.
[examples/ecs/query_bundle.rs:57] x = Dummy( <========= This should not get printed
111,
)
[examples/ecs/query_bundle.rs:57] x = Dummy(
222,
)
Show all components
[examples/ecs/query_bundle.rs:50] x = Dummy(
111,
)
[examples/ecs/query_bundle.rs:50] x = Dummy(
222,
)
```
However, it behaves the right way, if I add one more component to the bundle,
so the query and the bundle doesn't look the same:
```
Print component initated from bundle.
[examples/ecs/query_bundle.rs:57] x = Dummy(
222,
)
Show all components
[examples/ecs/query_bundle.rs:50] x = Dummy(
111,
)
[examples/ecs/query_bundle.rs:50] x = Dummy(
222,
)
```
I hope this helps. I'm definitely up for tinkering with this, and adding anything that I'm asked to add
or change.
Co-authored-by: Carter Anderson <mcanders1@gmail.com>
2021-04-28 21:03:10 +00:00
- [Setup ](#setup-2 )
- [Build & Run ](#build--run-2 )
2020-11-15 19:24:18 +00:00
# The Bare Minimum
2020-11-12 01:15:19 +00:00
2021-02-22 04:50:05 +00:00
<!-- MD026 - Hello, World! looks better with the ! -->
<!-- markdownlint - disable - next - line MD026 -->
2020-08-17 23:02:59 +00:00
## Hello, World!
2021-05-30 18:14:58 +00:00
Example | File | Description
2020-08-17 23:02:59 +00:00
--- | --- | ---
`hello_world` | [`hello_world.rs` ](./hello_world.rs ) | Runs a minimal example that outputs "hello world"
2020-11-15 19:24:18 +00:00
# Cross-Platform Examples
2020-08-17 23:02:59 +00:00
## 2D Rendering
2021-05-30 18:14:58 +00:00
Example | File | Description
2020-08-17 23:02:59 +00:00
--- | --- | ---
2020-11-12 01:15:19 +00:00
`contributors` | [`2d/contributors.rs` ](./2d/contributors.rs ) | Displays each contributor as a bouncy bevy-ball!
2021-03-25 01:46:22 +00:00
`many_sprites` | [`2d/many_sprites.rs` ](./2d/many_sprites.rs ) | Displays many sprites in a grid arragement! Used for performance testing.
Add 2d meshes and materials (#3460)
# Objective
The current 2d rendering is specialized to render sprites, we need a generic way to render 2d items, using meshes and materials like we have for 3d.
## Solution
I cloned a good part of `bevy_pbr` into `bevy_sprite/src/mesh2d`, removed lighting and pbr itself, adapted it to 2d rendering, added a `ColorMaterial`, and modified the sprite rendering to break batches around 2d meshes.
~~The PR is a bit crude; I tried to change as little as I could in both the parts copied from 3d and the current sprite rendering to make reviewing easier. In the future, I expect we could make the sprite rendering a normal 2d material, cleanly integrated with the rest.~~ _edit: see <https://github.com/bevyengine/bevy/pull/3460#issuecomment-1003605194>_
## Remaining work
- ~~don't require mesh normals~~ _out of scope_
- ~~add an example~~ _done_
- support 2d meshes & materials in the UI?
- bikeshed names (I didn't think hard about naming, please check if it's fine)
## Remaining questions
- ~~should we add a depth buffer to 2d now that there are 2d meshes?~~ _let's revisit that when we have an opaque render phase_
- ~~should we add MSAA support to the sprites, or remove it from the 2d meshes?~~ _I added MSAA to sprites since it's really needed for 2d meshes_
- ~~how to customize vertex attributes?~~ _#3120_
Co-authored-by: Carter Anderson <mcanders1@gmail.com>
2022-01-08 01:29:08 +00:00
`mesh2d` | [`2d/mesh2d.rs` ](./2d/mesh2d.rs ) | Renders a 2d mesh
`mesh2d_manual` | [`2d/mesh2d_manual.rs` ](./2d/mesh2d_manual.rs ) | Renders a custom mesh "manually" with "mid-level" renderer apis.
Add an example to draw a rectangle (#2957)
# Objective
Every time I come back to Bevy I face the same issue: how do I draw a rectangle again? How did that work? So I go to https://github.com/bevyengine/bevy/tree/main/examples in the hope of finding literally the simplest possible example that draws something on the screen without any dependency such as an image. I don't want to have to add some image first, I just quickly want to get something on the screen with `main.rs` alone so that I can continue building on from that point on. Such an example is particularly helpful for a quick start for smaller projects that don't even need any assets such as images (this is my case currently).
Currently every single example of https://github.com/bevyengine/bevy/tree/main/examples#2d-rendering (which is the first section after hello world that beginners will look for for very minimalistic and quick examples) depends on at least an asset or is too complex. This PR solves this.
It also serves as a great comparison for a beginner to realize what Bevy is really like and how different it is from what they may expect Bevy to be. For example for someone coming from [LÖVE](https://love2d.org/), they will have something like this in their head when they think of drawing a rectangle:
```lua
function love.draw()
love.graphics.setColor(0.25, 0.25, 0.75);
love.graphics.rectangle("fill", 0, 0, 50, 50);
end
```
This, of course, differs quite a lot from what you do in Bevy. I imagine there will be people that just want to see something as simple as this in comparison to have a better understanding for the amount of differences.
## Solution
Add a dead simple example drawing a blue 50x50 rectangle in the center with no more and no less than needed.
2021-12-18 00:52:37 +00:00
`rect` | [`2d/rect.rs` ](./2d/rect.rs ) | Renders a rectangle
2020-11-15 19:24:18 +00:00
`sprite` | [`2d/sprite.rs` ](./2d/sprite.rs ) | Renders a sprite
2021-01-28 21:58:20 +00:00
`sprite_sheet` | [`2d/sprite_sheet.rs` ](./2d/sprite_sheet.rs ) | Renders an animated sprite
2021-02-22 04:50:05 +00:00
`text2d` | [`2d/text2d.rs` ](./2d/text2d.rs ) | Generates text in 2d
Add Sprite Flipping (#1407)
OK, here's my attempt at sprite flipping. There are a couple of points that I need review/help on, but I think the UX is about ideal:
```rust
.spawn(SpriteBundle {
material: materials.add(texture_handle.into()),
sprite: Sprite {
// Flip the sprite along the x axis
flip: SpriteFlip { x: true, y: false },
..Default::default()
},
..Default::default()
});
```
Now for the issues. The big issue is that for some reason, when flipping the UVs on the sprite, there is a light "bleeding" or whatever you call it where the UV tries to sample past the texture boundry and ends up clipping. This is only noticed when resizing the window, though. You can see a screenshot below.
![image](https://user-images.githubusercontent.com/25393315/107098172-397aaa00-67d4-11eb-8e02-c90c820cd70e.png)
I am quite baffled why the texture sampling is overrunning like it is and could use some guidance if anybody knows what might be wrong.
The other issue, which I just worked around, is that I had to remove the `#[render_resources(from_self)]` annotation from the Spritesheet because the `SpriteFlip` render resource wasn't being picked up properly in the shader when using it. I'm not sure what the cause of that was, but by removing the annotation and re-organizing the shader inputs accordingly the problem was fixed.
I'm not sure if this is the most efficient way to do this or if there is a better way, but I wanted to try it out if only for the learning experience. Let me know what you think!
2021-03-03 19:26:45 +00:00
`sprite_flipping` | [`2d/sprite_flipping.rs` ](./2d/sprite_flipping.rs ) | Renders a sprite flipped along an axis
2020-08-21 00:29:10 +00:00
`texture_atlas` | [`2d/texture_atlas.rs` ](./2d/texture_atlas.rs ) | Generates a texture atlas (sprite sheet) from individual sprites
2020-08-17 23:02:59 +00:00
## 3D Rendering
Example | File | Description
--- | --- | ---
2020-11-15 19:24:18 +00:00
`3d_scene` | [`3d/3d_scene.rs` ](./3d/3d_scene.rs ) | Simple 3D scene with basic shapes and lighting
2021-12-14 03:58:23 +00:00
`lighting` | [`3d/lighting.rs` ](./3d/lighting.rs ) | Illustrates various lighting options in a simple scene
2020-10-18 20:48:15 +00:00
`load_gltf` | [`3d/load_gltf.rs` ](./3d/load_gltf.rs ) | Loads and renders a gltf file as a scene
2021-12-14 03:58:23 +00:00
`many_cubes` | [`3d/many_cubes.rs` ](./3d/many_cubes.rs ) | Simple benchmark to test per-entity draw overhead
2020-08-17 23:02:59 +00:00
`msaa` | [`3d/msaa.rs` ](./3d/msaa.rs ) | Configures MSAA (Multi-Sample Anti-Aliasing) for smoother edges
2021-02-01 00:22:06 +00:00
`orthographic` | [`3d/orthographic.rs` ](./3d/orthographic.rs ) | Shows how to create a 3D orthographic view (for isometric-look games or CAD applications)
2020-08-17 23:02:59 +00:00
`parenting` | [`3d/parenting.rs` ](./3d/parenting.rs ) | Demonstrates parent->child relationships and relative transformations
2021-05-01 18:51:54 +00:00
`pbr` | [`3d/pbr.rs` ](./3d/pbr.rs ) | Demonstrates use of Physically Based Rendering (PBR) properties
2021-12-14 03:58:23 +00:00
`shadow_caster_receiver` | [`3d/shadow_caster_receiver.rs` ](./3d/shadow_caster_receiver.rs ) | Demonstrates how to prevent meshes from casting/receiving shadows in a 3d scene
`shadow_biases` | [`3d/shadow_biases.rs` ](./3d/shadow_biases.rs ) | Demonstrates how shadow biases affect shadows in a 3d scene
2021-12-30 21:07:26 +00:00
`spherical_area_lights` | [`3d/spherical_area_lights.rs` ](./3d/spherical_area_lights.rs ) | Demonstrates how point light radius values affect light behavior.
2020-08-17 23:02:59 +00:00
`texture` | [`3d/texture.rs` ](./3d/texture.rs ) | Shows configuration of texture materials
2021-01-01 20:58:49 +00:00
`update_gltf_scene` | [`3d/update_gltf_scene.rs` ](./3d/update_gltf_scene.rs ) | Update a scene from a gltf file, either by spawning the scene as a child of another entity, or by accessing the entities of the scene
2021-03-09 23:25:49 +00:00
`wireframe` | [`3d/wireframe.rs` ](./3d/wireframe.rs ) | Showcases wireframe rendering
2020-08-17 23:02:59 +00:00
## Application
Example | File | Description
--- | --- | ---
2020-11-12 01:15:19 +00:00
`custom_loop` | [`app/custom_loop.rs` ](./app/custom_loop.rs ) | Demonstrates how to create a custom runner (to update an app manually).
2021-01-01 21:31:22 +00:00
`drag_and_drop` | [`app/drag_and_drop.rs` ](./app/drag_and_drop.rs ) | An example that shows how to handle drag and drop in an app.
2020-11-15 19:24:18 +00:00
`empty` | [`app/empty.rs` ](./app/empty.rs ) | An empty application (does nothing)
2021-01-28 21:58:20 +00:00
`empty_defaults` | [`app/empty_defaults.rs` ](./app/empty_defaults.rs ) | An empty application with default plugins
2020-08-17 23:02:59 +00:00
`headless` | [`app/headless.rs` ](./app/headless.rs ) | An application that runs without default plugins
2020-11-15 19:24:18 +00:00
`logs` | [`app/logs.rs` ](./app/logs.rs ) | Illustrate how to use generate log output
2020-08-17 23:02:59 +00:00
`plugin` | [`app/plugin.rs` ](./app/plugin.rs ) | Demonstrates the creation and registration of a custom plugin
2021-01-28 21:58:20 +00:00
`plugin_group` | [`app/plugin_group.rs` ](./app/plugin_group.rs ) | Demonstrates the creation and registration of a custom plugin group
2020-11-15 19:24:18 +00:00
`return_after_run` | [`app/return_after_run.rs` ](./app/return_after_run.rs ) | Show how to return to main after the Bevy app has exited
2020-08-17 23:02:59 +00:00
`thread_pool_resources` | [`app/thread_pool_resources.rs` ](./app/thread_pool_resources.rs ) | Creates and customizes the internal thread pool
2021-12-15 00:15:47 +00:00
`without_winit` | [`app/without_winit.rs` ](./app/without_winit.rs ) | Create an application without winit (runs single time, no event loop)
2020-08-17 23:02:59 +00:00
## Assets
Example | File | Description
--- | --- | ---
`asset_loading` | [`asset/asset_loading.rs` ](./asset/asset_loading.rs ) | Demonstrates various methods to load assets
2020-11-12 01:15:19 +00:00
`custom_asset` | [`asset/custom_asset.rs` ](./asset/custom_asset.rs ) | Implements a custom asset loader
2021-01-28 21:58:20 +00:00
`custom_asset_io` | [`asset/custom_asset_io.rs` ](./asset/custom_asset_io.rs ) | Implements a custom asset io loader
2020-08-17 23:02:59 +00:00
`hot_asset_reloading` | [`asset/hot_asset_reloading.rs` ](./asset/hot_asset_reloading.rs ) | Demonstrates automatic reloading of assets when modified on disk
2021-05-23 20:13:55 +00:00
## Async Tasks
Example | File | Description
--- | --- | ---
`async_compute` | [`async_tasks/async_compute.rs` ](async_tasks/async_compute.rs ) | How to use `AsyncComputeTaskPool` to complete longer running tasks
2020-08-17 23:02:59 +00:00
## Audio
Example | File | Description
--- | --- | ---
`audio` | [`audio/audio.rs` ](./audio/audio.rs ) | Shows how to load and play an audio file
## Diagnostics
Example | File | Description
--- | --- | ---
2021-02-22 04:50:05 +00:00
`custom_diagnostic` | [`diagnostics/custom_diagnostic.rs` ](./diagnostics/custom_diagnostic.rs ) | Shows how to create a custom diagnostic
2021-05-14 18:26:09 +00:00
`log_diagnostics` | [`diagnostics/log_diagnostics.rs` ](./diagnostics/log_diagnostics.rs ) | Add a plugin that logs diagnostics, like frames per second (FPS), to the console
2020-08-17 23:02:59 +00:00
## ECS (Entity Component System)
Example | File | Description
--- | --- | ---
`ecs_guide` | [`ecs/ecs_guide.rs` ](./ecs/ecs_guide.rs ) | Full guide to Bevy's ECS
2021-07-12 20:09:43 +00:00
`component_change_detection` | [`ecs/component_change_detection.rs` ](./ecs/component_change_detection.rs ) | Change detection on components
2020-11-15 19:24:18 +00:00
`event` | [`ecs/event.rs` ](./ecs/event.rs ) | Illustrates event creation, activation, and reception
2021-01-28 21:58:20 +00:00
`fixed_timestep` | [`ecs/fixed_timestep.rs` ](./ecs/fixed_timestep.rs ) | Shows how to create systems that run every fixed timestep, rather than every tick
2020-11-12 01:15:19 +00:00
`hierarchy` | [`ecs/hierarchy.rs` ](./ecs/hierarchy.rs ) | Creates a hierarchy of parents and children entities
2021-05-29 01:30:28 +00:00
`iter_combinations` | [`ecs/iter_combinations.rs` ](./ecs/iter_combinations.rs ) | Shows how to iterate over combinations of query results.
2020-09-09 18:39:37 +00:00
`parallel_query` | [`ecs/parallel_query.rs` ](./ecs/parallel_query.rs ) | Illustrates parallel queries with `ParallelIterator`
2020-12-02 22:35:27 +00:00
`removal_detection` | [`ecs/removal_detection.rs` ](./ecs/removal_detection.rs ) | Query for entities that had a specific component removed in a previous stage during the current frame.
2020-08-17 23:02:59 +00:00
`startup_system` | [`ecs/startup_system.rs` ](./ecs/startup_system.rs ) | Demonstrates a startup system (one that runs once when the app starts up)
2021-01-28 21:58:20 +00:00
`state` | [`ecs/state.rs` ](./ecs/state.rs ) | Illustrates how to use States to control transitioning from a Menu state to an InGame state
2020-12-02 22:35:27 +00:00
`system_chaining` | [`ecs/system_chaining.rs` ](./ecs/system_chaining.rs ) | Chain two systems together, specifying a return type in a system (such as `Result` )
2021-03-03 03:11:11 +00:00
`system_param` | [`ecs/system_param.rs` ](./ecs/system_param.rs ) | Illustrates creating custom system parameters with `SystemParam`
2021-04-23 19:08:16 +00:00
`system_sets` | [`ecs/system_sets.rs` ](./ecs/system_sets.rs ) | Shows `SystemSet` use along with run criterion
2020-12-02 22:35:27 +00:00
`timers` | [`ecs/timers.rs` ](./ecs/timers.rs ) | Illustrates ticking `Timer` resources inside systems and handling their state
2020-08-17 23:02:59 +00:00
## Games
Example | File | Description
--- | --- | ---
2021-01-21 22:10:02 +00:00
`alien_cake_addict` | [`game/alien_cake_addict.rs` ](./game/alien_cake_addict.rs ) | Eat the cakes. Eat them all. An example 3D game
2020-08-17 23:02:59 +00:00
`breakout` | [`game/breakout.rs` ](./game/breakout.rs ) | An implementation of the classic game "Breakout"
## Input
Example | File | Description
--- | --- | ---
2020-11-12 01:15:19 +00:00
`char_input_events` | [`input/char_input_events.rs` ](./input/char_input_events.rs ) | Prints out all chars as they are inputted.
2020-11-15 19:24:18 +00:00
`gamepad_input` | [`input/gamepad_input.rs` ](./input/gamepad_input.rs ) | Shows handling of gamepad input, connections, and disconnections
2021-01-28 21:58:20 +00:00
`gamepad_input_events` | [`input/gamepad_input_events.rs` ](./input/gamepad_input_events.rs ) | Iterates and prints gamepad input and connection events
2020-11-15 19:24:18 +00:00
`keyboard_input` | [`input/keyboard_input.rs` ](./input/keyboard_input.rs ) | Demonstrates handling a key press/release
2021-01-28 21:58:20 +00:00
`keyboard_input_events` | [`input/keyboard_input_events.rs` ](./input/keyboard_input_events.rs ) | Prints out all keyboard events
2021-03-14 21:00:36 +00:00
`keyboard_modifiers` | [`input/keyboard_modifiers.rs` ](./input/keyboard_modifiers.rs ) | Demonstrates using key modifiers (ctrl, shift)
2020-11-15 19:24:18 +00:00
`mouse_input` | [`input/mouse_input.rs` ](./input/mouse_input.rs ) | Demonstrates handling a mouse button press/release
2021-01-28 21:58:20 +00:00
`mouse_input_events` | [`input/mouse_input_events.rs` ](./input/mouse_input_events.rs ) | Prints out all mouse events (buttons, movement, etc.)
2020-11-15 19:24:18 +00:00
`touch_input` | [`input/touch_input.rs` ](./input/touch_input.rs ) | Displays touch presses, releases, and cancels
2021-05-01 18:51:55 +00:00
`touch_input_events` | [`input/touch_input_events.rs` ](./input/touch_input_events.rs ) | Prints out all touch inputs
2020-08-17 23:02:59 +00:00
2020-12-02 22:35:27 +00:00
## Reflection
Example | File | Description
--- | --- | ---
2021-05-30 18:14:58 +00:00
`reflection` | [`reflection/reflection.rs` ](./reflection/reflection.rs ) | Demonstrates how reflection in Bevy provides a way to dynamically interact with Rust types
`generic_reflection` | [`reflection/generic_reflection.rs` ](./reflection/generic_reflection.rs ) | Registers concrete instances of generic types that may be used with reflection
`reflection_types` | [`reflection/reflection_types.rs` ](./reflection/reflection_types.rs ) | Illustrates the various reflection types available
`trait_reflection` | [`reflection/trait_reflection.rs` ](./reflection/trait_reflection.rs ) | Allows reflection with trait objects
2020-12-02 22:35:27 +00:00
2020-08-17 23:02:59 +00:00
## Scene
Example | File | Description
--- | --- | ---
2020-11-15 19:24:18 +00:00
`scene` | [`scene/scene.rs` ](./scene/scene.rs ) | Demonstrates loading from and saving scenes to files
2020-08-17 23:02:59 +00:00
## Shaders
Example | File | Description
--- | --- | ---
2021-12-14 03:58:23 +00:00
`shader_material` | [`shader/shader_material.rs` ](./shader/shader_material.rs ) | Illustrates creating a custom material and a shader that uses it
2022-01-05 19:43:11 +00:00
`shader_material_glsl` | [`shader/shader_material_glsl.rs` ](./shader/shader_material_glsl.rs ) | A custom shader using the GLSL shading language.
`shader_instancing` | [`shader/shader_instancing.rs` ](./shader/shader_instancing.rs ) | A custom shader showing off rendering a mesh multiple times in one draw call.
`animate_shader` | [`shader/animate_shader.rs` ](./shader/animate_shader.rs ) | Shows how to pass changing data like the time since startup into a shader.
`compute_shader_game_of_life` | [`shader/compute_shader_game_of_life.rs` ](./shader/compute_shader_game_of_life.rs ) | A compute shader simulating Conway's Game of Life
2020-08-17 23:02:59 +00:00
`shader_defs` | [`shader/shader_defs.rs` ](./shader/shader_defs.rs ) | Demonstrates creating a custom material that uses "shaders defs" (a tool to selectively toggle parts of a shader)
2021-04-15 00:57:37 +00:00
## Tests
Example | File | Description
--- | --- | ---
`how_to_test_systems` | [`../tests/how_to_test_systems.rs` ](../tests/how_to_test_systems.rs ) | How to test systems with commands, queries or resources
2020-11-15 19:24:18 +00:00
## Tools
Example | File | Description
--- | --- | ---
2021-12-14 03:58:23 +00:00
`bevymark` | [`tools/bevymark.rs` ](./tools/bevymark.rs ) | A heavy sprite rendering workload to benchmark your system with Bevy
2020-11-15 19:24:18 +00:00
2020-08-17 23:02:59 +00:00
## UI (User Interface)
Example | File | Description
--- | --- | ---
`button` | [`ui/button.rs` ](./ui/button.rs ) | Illustrates creating and updating a button
`font_atlas_debug` | [`ui/font_atlas_debug.rs` ](./ui/font_atlas_debug.rs ) | Illustrates how FontAtlases are populated (used to optimize text rendering internally)
2020-11-15 19:24:18 +00:00
`text` | [`ui/text.rs` ](./ui/text.rs ) | Illustrates creating and updating text
2021-02-22 04:50:05 +00:00
`text_debug` | [`ui/text_debug.rs` ](./ui/text_debug.rs ) | An example for debugging text layout
2020-08-17 23:02:59 +00:00
`ui` | [`ui/ui.rs` ](./ui/ui.rs ) | Illustrates various features of Bevy UI
## Window
Example | File | Description
--- | --- | ---
`clear_color` | [`window/clear_color.rs` ](./window/clear_color.rs ) | Creates a solid color window
2021-12-18 19:38:05 +00:00
`multiple_windows` | [`window/multiple_windows.rs` ](./window/multiple_windows.rs ) | Demonstrates creating multiple windows, and rendering to them
2021-01-28 21:58:20 +00:00
`scale_factor_override` | [`window/scale_factor_override.rs` ](./window/scale_factor_override.rs ) | Illustrates how to customize the default window settings
2021-12-08 20:53:35 +00:00
`transparent_window` | [`window/transparent_window.rs` ](./window/transparent_window.rs ) | Illustrates making the window transparent and hiding the window decoration
2020-08-17 23:02:59 +00:00
`window_settings` | [`window/window_settings.rs` ](./window/window_settings.rs ) | Demonstrates customizing default window settings
2020-09-16 01:05:31 +00:00
2020-11-15 19:24:18 +00:00
# Platform-Specific Examples
## Android
2020-09-16 01:05:31 +00:00
2021-02-22 04:50:05 +00:00
### Setup
2020-09-16 01:05:31 +00:00
2020-11-03 19:32:48 +00:00
```sh
2020-11-15 19:24:18 +00:00
rustup target add aarch64-linux-android armv7-linux-androideabi
cargo install cargo-apk
2020-11-03 19:32:48 +00:00
```
2020-09-16 01:05:31 +00:00
2020-11-15 19:24:18 +00:00
The Android SDK must be installed, and the environment variable `ANDROID_SDK_ROOT` set to the root Android `sdk` folder.
When using `NDK (Side by side)` , the environment variable `ANDROID_NDK_ROOT` must also be set to one of the NDKs in `sdk\ndk\[NDK number]` .
2021-02-22 04:50:05 +00:00
### Build & Run
2020-09-16 01:05:31 +00:00
2020-11-15 19:24:18 +00:00
To run on a device setup for Android development, run:
2020-09-19 03:11:26 +00:00
2020-11-03 19:32:48 +00:00
```sh
2020-11-15 19:24:18 +00:00
cargo apk run --example android
2020-11-03 19:32:48 +00:00
```
2020-09-16 01:05:31 +00:00
2020-11-15 19:24:18 +00:00
:warning: At this time Bevy does not work in Android Emulator.
2020-09-16 01:05:31 +00:00
2020-11-15 19:24:18 +00:00
When using Bevy as a library, the following fields must be added to `Cargo.toml` :
```toml
[package.metadata.android]
build_targets = ["aarch64-linux-android", "armv7-linux-androideabi"]
target_sdk_version = 29
min_sdk_version = 16
2020-11-03 19:32:48 +00:00
```
2020-10-31 21:36:24 +00:00
2020-11-15 19:24:18 +00:00
Please reference `cargo-apk` [README ](https://crates.io/crates/cargo-apk ) for other Android Manifest fields.
2021-02-22 04:50:05 +00:00
### Old phones
2020-11-12 01:15:19 +00:00
2021-05-02 20:22:32 +00:00
Bevy by default targets Android API level 29 in its examples which is the <!-- markdown - link - check - disable -->
[Play Store's minimum API to upload or update apps ](https://developer.android.com/distribute/best-practices/develop/target-sdk ). <!-- markdown-link-check-enable -->
Users of older phones may want to use an older API when testing.
2020-11-15 19:24:18 +00:00
To use a different API, the following fields must be updated in Cargo.toml:
```toml
[package.metadata.android]
target_sdk_version = >>API< <
min_sdk_version = >>API or less< <
```
2020-11-12 01:15:19 +00:00
2021-01-28 21:58:20 +00:00
Example | File | Description
--- | --- | ---
`android` | [`android/android.rs` ](./android/android.rs ) | The `3d/3d_scene.rs` example for Android
2020-10-31 21:36:24 +00:00
## iOS
2021-02-22 04:50:05 +00:00
### Setup
2020-10-31 21:36:24 +00:00
2021-11-29 23:25:22 +00:00
You need to install the correct rust targets:
- `aarch64-apple-ios` : iOS devices
- `x86_64-apple-ios` : iOS simulator on x86 processors
- `aarch64-apple-ios-sim` : iOS simulator on Apple processors
2020-11-03 19:32:48 +00:00
```sh
2021-11-29 23:25:22 +00:00
rustup target add aarch64-apple-ios x86_64-apple-ios aarch64-apple-ios-sim
2020-11-03 19:32:48 +00:00
```
2020-10-31 21:36:24 +00:00
2021-02-22 04:50:05 +00:00
### Build & Run
2020-10-31 21:36:24 +00:00
Using bash:
2020-11-03 06:54:08 +00:00
2020-11-03 19:32:48 +00:00
```sh
cd examples/ios
make run
```
2020-10-31 21:36:24 +00:00
In an ideal world, this will boot up, install and run the app for the first
iOS simulator in your `xcrun simctl devices list` . If this fails, you can
specify the simulator device UUID via:
2020-11-03 06:54:08 +00:00
2020-11-03 19:32:48 +00:00
```sh
DEVICE_ID=${YOUR_DEVICE_ID} make run
```
2020-10-31 21:36:24 +00:00
If you'd like to see xcode do stuff, you can run
2020-11-03 06:54:08 +00:00
2020-11-03 19:32:48 +00:00
```sh
open bevy_ios_example.xcodeproj/
```
2020-10-31 21:36:24 +00:00
which will open xcode. You then must push the zoom zoom play button and wait
for the magic.
2021-01-28 21:58:20 +00:00
Example | File | Description
--- | --- | ---
`ios` | [`ios/src/lib.rs` ](./ios/src/lib.rs ) | The `3d/3d_scene.rs` example for iOS
2020-11-15 19:24:18 +00:00
## WASM
2020-11-03 06:54:08 +00:00
2021-02-22 04:50:05 +00:00
### Setup
2020-11-03 06:54:08 +00:00
2020-11-03 19:32:48 +00:00
```sh
2020-11-15 19:24:18 +00:00
rustup target add wasm32-unknown-unknown
cargo install wasm-bindgen-cli
2020-11-03 19:32:48 +00:00
```
2020-11-03 06:54:08 +00:00
2021-02-22 04:50:05 +00:00
### Build & Run
2020-11-03 06:54:08 +00:00
2020-11-15 19:24:18 +00:00
Following is an example for `headless_wasm` . For other examples in wasm/ directory,
change the `headless_wasm` in the following commands **and edit** `examples/wasm/index.html`
to point to the correct `.js` file.
2020-11-03 06:54:08 +00:00
2020-11-03 19:32:48 +00:00
```sh
2020-11-15 19:24:18 +00:00
cargo build --example headless_wasm --target wasm32-unknown-unknown --no-default-features
wasm-bindgen --out-dir examples/wasm/target --target web target/wasm32-unknown-unknown/debug/examples/headless_wasm.wasm
2020-11-03 19:32:48 +00:00
```
2020-11-03 06:54:08 +00:00
2020-11-15 19:24:18 +00:00
Then serve `examples/wasm` dir to browser. i.e.
2020-11-03 06:54:08 +00:00
2020-11-15 19:24:18 +00:00
```sh
basic-http-server examples/wasm
2020-11-03 19:32:48 +00:00
```
2020-11-03 06:54:08 +00:00
2020-11-15 19:24:18 +00:00
Example | File | Description
--- | --- | ---
`hello_wasm` | [`wasm/hello_wasm.rs` ](./wasm/hello_wasm.rs ) | Runs a minimal example that logs "hello world" to the browser's console
`assets_wasm` | [`wasm/assets_wasm.rs` ](./wasm/assets_wasm.rs ) | Demonstrates how to load assets from wasm
2021-02-22 04:50:05 +00:00
`headless_wasm` | [`wasm/headless_wasm.rs` ](./wasm/headless_wasm.rs ) | Sets up a schedule runner and continually logs a counter to the browser's console
2020-11-15 19:24:18 +00:00
`winit_wasm` | [`wasm/winit_wasm.rs` ](./wasm/winit_wasm.rs ) | Logs user input to the browser's console. Requires the `bevy_winit` features