Use sprites in a 3d bevy scene.

Overview

bevy_sprite3d

Crates.io MIT

Use 2d sprites in a 3d bevy scene.

This is a pretty common setup in other engines (unity, godot, etc). Useful for:

  • 2d games using bevy's lighting (orthographic camera, 3d sprites)
  • 2d games with easier parallax and scale (perspective camera, 3d sprites)
  • 2d games in a 3d world (perspective camera, both 3d sprites and meshes)
  • 3d games with billboard sprites (a la Delver)

Both meshes and materials are internally cached, so this crate can be used for things like tilemaps without issue.

Examples

Example using bevy_sprite3d:

chaos

A more complicated scene: examples/dungeon.rs. Try this one with cargo run --example dungeon.

Some more examples. These don't use bevy, but demonstrate the effect style:

the last night

the last night

hollow knight

Usage

Check out the examples for details. Tl;dr initialize the plugin with

app.add_plugin(Sprite3dPlugin)

and spawn sprites with

fn setup(
    mut commands: Commands, 
    images: Res<ImageAssets>,
    mut sprite_params: Sprite3dParams
) {

    // ----------------------- Single Static Sprite ----------------------------

    commands.spawn_bundle(Sprite3d {
            image: images.sprite.clone(),

            pixels_per_metre: 400.,

            partial_alpha: true,

            unlit: true,

            ..default()

            // transform: Transform::from_xyz(0., 0., 0.),
            // pivot: Some(Vec2::new(0.5, 0.5)),
            // double_sided: true,

    }.bundle(&mut sprite_params));

    // ------------------- Texture Atlas (Sprite Sheet) ------------------------

    commands.spawn_bundle(AtlasSprite3d {
            atlas: images.sprite_sheet.clone(),

            pixels_per_metre: 32.,
            partial_alpha: true,
            unlit: true,

            index: 3,

            ..default()

            // transform: Transform::from_xyz(0., 0., 0.),
            // pivot: Some(Vec2::new(0.5, 0.5)),
            // double_sided: true,

    }.bundle(&mut sprite_params));
}

One small complication: your image assets should be loaded prior to spawning, as bevy_sprite3d uses some properties of the image (such as size and aspect ratio) in constructing the 3d mesh.

To that end, I'd recommend using bevy_asset_loader for simplicity. Examples are provided both with and without it.

Versioning

bevy_sprite3d version bevy version
2.4 0.10
2.3 0.9
2.2 0.9
2.1 0.9
2.0 0.8
1.1 0.8
1.0 0.7
You might also like...
A simple extension for `bevy-editor-pls` to support tilemap editing right inside the bevy app.

What is this This is a simple tilemap editor plugin, that hooks right into bevy_editor_pls to work with bevy_ecs_tilemap. It works completely within i

Bevy Simple Portals is a Bevy game engine plugin aimed to create portals.
Bevy Simple Portals is a Bevy game engine plugin aimed to create portals.

Portals for Bevy Bevy Simple Portals is a Bevy game engine plugin aimed to create portals. Those portals are (for now) purely visual and can be used t

Minecraft using Bevy and Bevy-Meshem

minecraft_bevy Minecraft_bevy was built to showcase bevy_meshem. After a week of developing it has: Chunk loading / unloading each chunk's mesh is bei

A Bevy plugin to use Kira for game audio

Bevy Kira audio This bevy plugin is intended to try integrating Kira into Bevy. The end goal would be to replace or update bevy_audio, if Kira turns o

A place to start when building webgl apps in Bevy. Use this to avoid writing the boilerplate.

Template Bevy project with WebGL enabled Prerequisites cargo install cargo-make Build and serve WASM version Set your local ip address in Makefile.to

Bevy plugin for an AssetServer that can load embedded resources, or use other AssetServers based on the path.

Bevy-Embasset Embed your asset folder inside your binary. bevy-embasset adds support for loading assets embedded into the binary. Furthermore, it can

A plugin to use the kajiya renderer with bevy

🕊️ 💡 bevy-kajiya A plugin that enables use of the kajiya renderer in bevy WARNING: This plugin is barebones and supports a limited set of features.

A simple example showcasing how to use Bevy to display a square with acceleration (controllable with your keyboard) that wraps around the screen!
A simple example showcasing how to use Bevy to display a square with acceleration (controllable with your keyboard) that wraps around the screen!

Bevy Wrapping Square example A simple example showcasing how to use Bevy to display a square with acceleration (controllable with your keyboard) that

An opinionated 2D sparse grid made for use with Bevy. For storing and querying entities

bevy_sparse_grid_2d An opinionated 2D sparse grid made for use with Bevy. For storing and querying entities. Personally, I'm using it for simple stupi

Comments
  • panicked at 'called `Option::unwrap()` on a `None` value'

    panicked at 'called `Option::unwrap()` on a `None` value'

    What's the Error? when attempting to spawn a sprite3d and upon calling bundle this line of code panics: let image_size = params.images.get(&self.image).unwrap().texture_descriptor.size; however bevy is able to load the image just fine. here's my full code.

    
    use bevy::prelude::*;
    use bevy_inspector_egui::quick::WorldInspectorPlugin;
    use bevy_sprite3d::Sprite3d;
    use bevy_sprite3d::*;
    use std::sync::Mutex;
    use kira::{
    	manager::{
    		AudioManager, AudioManagerSettings,
    		backend::cpal::CpalBackend,
    	},
    	sound::streaming::{StreamingSoundData, StreamingSoundSettings},
    };
    #[derive(Resource)]
    struct Epic
    {
        manager: Mutex<AudioManager>
    }
    
    fn main() {
        let mut manager = Epic { manager: AudioManager::<CpalBackend>::new(AudioManagerSettings::default()).unwrap().into()};
        App::new()
            .insert_resource(manager)
            .add_plugins(DefaultPlugins)
            .add_plugin(Sprite3dPlugin)
            .add_plugin(WorldInspectorPlugin::new())
            .add_startup_system(setup)
            .run();
    }
    
    fn setup(mut manager: ResMut<Epic>, mut commands: Commands,  asset_server: Res<AssetServer>,  mut sprite_params : Sprite3dParams) 
    {
        println!("do something");
        
        let sound_data = StreamingSoundData::from_file("assets/audio/isle.mp3", StreamingSoundSettings::default()).unwrap();
    
        manager.manager.lock().unwrap().play(sound_data).unwrap();
        commands.spawn(Camera3dBundle {
            transform: Transform::from_xyz(0.0, 0.0, 0.0),
            ..default()
        });
        let image = asset_server.load("bf.png");
        
        commands.spawn(Sprite3d
        {
            image: image,
            unlit: true,
            ..default()
        }.bundle(&mut sprite_params));
    }
    
    opened by rayanmargham 2
  • Weird behaviour under a 3d camera

    Weird behaviour under a 3d camera

    https://user-images.githubusercontent.com/22399952/225992560-c1927420-4f9d-4948-b9cd-c742c50b0211.mov

    Setup: bevy 0.10 + bevy_sprite3d Issue: A part of character sprite disappears at some positions

    The character and all tiles are individual 3d sprites. The tiles are rotated PI/2 along x axis to make a flat floor. (as shown in the video, the floor is indeed flat)

    The character moves along x/z and after passing half of the tile, the lower part of the character disappears. This happens for every single tile.

    Other than this issue, everything else works fine, including lighting and shadows.

    opened by Hakukano 2
  • How to step index forward?

    How to step index forward?

    it seems to be turned into a PbrBundle so i cant figure out how to access the index. Id like to achieve something like https://bevyengine.org/examples/2d/sprite-sheet/

    enhancement 
    opened by mrshmllow 2
  • Alpha-channel shadow support

    Alpha-channel shadow support

    There's an open bevy issue for this at https://github.com/bevyengine/bevy/issues/4372, seems like the most straight-forward path would be to go in and resolve this in the engine. Barring that, there are a few more patch-style fixes we could try:

    • Manually creating a far simpler interim shader for sprites, possibly at the cost of PBR
    • Analytically modifying mesh shape to fit the contours of a non-transparent sprite
      • This might have some side-benefits in collision, mouse picking, etc.

    Will look into this more later.

    opened by FraserLee 1
Owner
McGill compsci+physics '24
null
Apply a pixelation effect to any Bevy mesh or scene without post-processing.

Pixelate Mesh Apply a pixelation effect to any Bevy mesh or scene without post-processing. Usage Add the PixelateMeshPlugin, where you specify a compo

Jan Hohenheim 16 Mar 25, 2023
Materials for sprites in Bevy

bevy_sprite_material Materials for sprites in Bevy This bevy plugin changes the bevy_sprite implementation using a material. You might be interested i

Félix Lescaudey de Maneville 6 Aug 21, 2022
Explicitly set sprite layers for sprites in Bevy games.

extol_sprite_layer lets you specify the drawing order for sprites in your Bevy game using a separate component rather than via the z-coordinate of you

Ash 5 May 2, 2023
A feature-rich, production-ready, general purpose 2D/3D game engine written in Rust with a scene editor.

A feature-rich, production-ready, general purpose 2D/3D game engine written in Rust with a scene editor.

rg3d engine 5.4k Jan 4, 2023
A tool to generate inbetweens for animated sprites, written in godot-rust

Bitmapflow is a tool to help you generate inbetweens for animated sprites. In other words, it makes your animations smoother. It uses optical flow to

null 411 Dec 21, 2022
A Rust library for blitting 2D sprites

blit A Rust library for blitting 2D sprites Documentation Usage Add this to your Cargo.toml:

Thomas Versteeg 23 Dec 6, 2022
🎨 Procedurally generate 2D sprites

sprite (Executable) Run On Linux you need the gtk-rs dependencies to compile: cargo install sprite sprite This should produce the following window: s

Thomas Versteeg 68 Nov 25, 2022
Random utility stuff for bevy for my personal use. You're welcome to use it, too, if you like.

bevy_cleancut Random utility stuff for bevy for my personal use. You're welcome to use it, too, if you like. Stuff It's all in the prelude: use bevy_c

Nathan Stocks 3 Aug 15, 2024
Minecraft-esque voxel engine prototype made with the bevy game engine. Pending bevy 0.6 release to undergo a full rewrite.

vx_bevy A voxel engine prototype made using the Bevy game engine. Goals and features Very basic worldgen Animated chunk loading (ala cube world) Optim

Lucas Arriesse 125 Dec 31, 2022
bevy-hikari is an implementation of voxel cone tracing global illumination with anisotropic mip-mapping in Bevy

Bevy Voxel Cone Tracing bevy-hikari is an implementation of voxel cone tracing global illumination with anisotropic mip-mapping in Bevy. Bevy Version

研究社交 208 Dec 27, 2022