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

Differentiation of Image, Texture and Sprite #2930

Closed
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
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,8 @@ name = "texture_atlas"
path = "examples/2d/texture_atlas.rs"

[[example]]
name = "pipelined_texture_atlas"
path = "examples/2d/pipelined_texture_atlas.rs"
name = "pipelined_image_atlas"
path = "examples/2d/pipelined_image_atlas.rs"

# 3D Rendering
[[example]]
Expand Down
98 changes: 98 additions & 0 deletions examples/2d/pipelined_image_atlas.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
use bevy::{
asset::LoadState,
math::Vec3,
prelude::{
App, AssetServer, Assets, Commands, HandleUntyped, Res, ResMut, State, SystemSet, Transform,
},
render2::{camera::OrthographicCameraBundle, image::Image},
sprite2::{
AtlasSprite, ImageAtlas, ImageAtlasBuilder, PipelinedAtlasSpriteBundle,
PipelinedSpriteBundle,
},
PipelinedDefaultPlugins,
};

/// In this example we generate a new [`ImageAtlas`] (sprite sheet) from a folder containing
/// individual images.
fn main() {
App::new()
.init_resource::<RpgImageHandles>()
.add_plugins(PipelinedDefaultPlugins)
.add_state(AppState::Setup)
.add_system_set(SystemSet::on_enter(AppState::Setup).with_system(load_images))
.add_system_set(SystemSet::on_update(AppState::Setup).with_system(check_images))
.add_system_set(SystemSet::on_enter(AppState::Finished).with_system(setup))
.run();
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
enum AppState {
Setup,
Finished,
}

#[derive(Default)]
struct RpgImageHandles {
handles: Vec<HandleUntyped>,
}

fn load_images(mut rpg_sprite_handles: ResMut<RpgImageHandles>, asset_server: Res<AssetServer>) {
rpg_sprite_handles.handles = asset_server.load_folder("textures/rpg").unwrap();
}

fn check_images(
mut state: ResMut<State<AppState>>,
rpg_image_handles: ResMut<RpgImageHandles>,
asset_server: Res<AssetServer>,
) {
if let LoadState::Loaded =
asset_server.get_group_load_state(rpg_image_handles.handles.iter().map(|handle| handle.id))
{
state.set(AppState::Finished).unwrap();
}
}

fn setup(
mut commands: Commands,
rpg_image_handles: Res<RpgImageHandles>,
asset_server: Res<AssetServer>,
mut image_atlases: ResMut<Assets<ImageAtlas>>,
mut images: ResMut<Assets<Image>>,
) {
let mut atlas_builder = ImageAtlasBuilder::default();
for handle in rpg_image_handles.handles.iter() {
let image = images.get(handle).unwrap();
atlas_builder.add_image(handle.clone_weak().typed::<Image>(), image);
}

let image_atlas = atlas_builder.finish(&mut images).unwrap();
let image_atlas_source = image_atlas.source_image.clone();
let vendor_handle = asset_server.get_handle("textures/rpg/chars/vendor/generic-rpg-vendor.png");
let vendor_index = image_atlas.get_region_index(&vendor_handle).unwrap();
let atlas_handle = image_atlases.add(image_atlas);

// set up a scene to display our texture atlas
commands.spawn_bundle(OrthographicCameraBundle::new_2d());
// draw a sprite from the atlas
commands.spawn_bundle(PipelinedAtlasSpriteBundle {
transform: Transform {
translation: Vec3::new(150.0, 0.0, 0.0),
scale: Vec3::splat(4.0),
..Default::default()
},
sprite: AtlasSprite {
region_index: vendor_index as u32,
color: Default::default(),
flip_x: true,
flip_y: true,
},
image_atlas: atlas_handle,
..Default::default()
});
// draw the atlas itself
commands.spawn_bundle(PipelinedSpriteBundle {
image: image_atlas_source,
transform: Transform::from_xyz(-300.0, 0.0, 0.0),
..Default::default()
});
}
94 changes: 0 additions & 94 deletions examples/2d/pipelined_texture_atlas.rs

This file was deleted.

2 changes: 1 addition & 1 deletion examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ Example | File | Description
`contributors` | [`2d/contributors.rs`](./2d/contributors.rs) | Displays each contributor as a bouncy bevy-ball!
`many_sprites` | [`2d/many_sprites.rs`](./2d/many_sprites.rs) | Displays many sprites in a grid arragement! Used for performance testing.
`mesh` | [`2d/mesh.rs`](./2d/mesh.rs) | Renders a custom mesh
`pipelined_texture_atlas` | [`2d/pipelined_texture_atlas.rs`](./2d/pipelined_texture_atlas.rs) | Generates a texture atlas (sprite sheet) from individual sprites
`pipelined_image_atlas` | [`2d/pipelined_image_atlas.rs`](./2d/pipelined_image_atlas.rs) | Generates a image atlas (sprite sheet) from individual images.
`sprite` | [`2d/sprite.rs`](./2d/sprite.rs) | Renders a sprite
`sprite_sheet` | [`2d/sprite_sheet.rs`](./2d/sprite_sheet.rs) | Renders an animated sprite
`text2d` | [`2d/text2d.rs`](./2d/text2d.rs) | Generates text in 2d
Expand Down
2 changes: 1 addition & 1 deletion examples/shader/custom_shader_pipelined.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use bevy::{
render2::{
camera::PerspectiveCameraBundle,
color::Color,
image::BevyDefault,
mesh::{shape, Mesh},
render_asset::{PrepareAssetError, RenderAsset, RenderAssetPlugin, RenderAssets},
render_component::ExtractComponentPlugin,
Expand All @@ -21,7 +22,6 @@ use bevy::{
render_resource::*,
renderer::RenderDevice,
shader::Shader,
texture::BevyDefault,
view::ExtractedView,
RenderApp, RenderStage,
},
Expand Down
22 changes: 11 additions & 11 deletions examples/tools/bevymark_pipelined.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use bevy::{
input::Input,
math::Vec3,
prelude::{App, AssetServer, Handle, MouseButton, Transform},
render2::{camera::OrthographicCameraBundle, color::Color, texture::Image},
render2::{camera::OrthographicCameraBundle, color::Color, image::Image},
sprite2::PipelinedSpriteBundle,
window::WindowDescriptor,
PipelinedDefaultPlugins,
Expand Down Expand Up @@ -62,15 +62,15 @@ fn main() {
.run();
}

struct BirdTexture(Handle<Image>);
struct BirdImage(Handle<Image>);

fn setup(
mut commands: Commands,
window: Res<WindowDescriptor>,
mut counter: ResMut<BevyCounter>,
asset_server: Res<AssetServer>,
) {
let texture = asset_server.load("branding/icon.png");
let image = asset_server.load("branding/icon.png");
if let Some(initial_count) = std::env::args()
.nth(1)
.and_then(|arg| arg.parse::<u128>().ok())
Expand All @@ -80,7 +80,7 @@ fn setup(
&window,
&mut counter,
initial_count,
texture.clone_weak(),
image.clone_weak(),
);
}
commands.spawn_bundle(OrthographicCameraBundle::new_2d());
Expand Down Expand Up @@ -135,7 +135,7 @@ fn setup(
// ..Default::default()
// });

commands.insert_resource(BirdTexture(texture));
commands.insert_resource(BirdImage(image));
}

#[allow(clippy::too_many_arguments)]
Expand All @@ -145,7 +145,7 @@ fn mouse_handler(
time: Res<Time>,
mouse_button_input: Res<Input<MouseButton>>,
window: Res<WindowDescriptor>,
bird_texture: Res<BirdTexture>,
bird_image: Res<BirdImage>,
// mut bird_material: ResMut<BirdMaterial>,
mut counter: ResMut<BevyCounter>,
// mut materials: ResMut<Assets<ColorMaterial>>,
Expand All @@ -154,11 +154,11 @@ fn mouse_handler(
// let mut rnd = rand::thread_rng();
// let color = gen_color(&mut rnd);

// let texture_handle = asset_server.load("branding/icon.png");
// let image = asset_server.load("branding/icon.png");

// bird_material.0 = materials.add(ColorMaterial {
// color: BASE_COLOR * color,
// texture: Some(texture_handle),
// image: Some(image),
// });
// }

Expand All @@ -169,7 +169,7 @@ fn mouse_handler(
&window,
&mut counter,
spawn_count,
bird_texture.0.clone(),
bird_image.0.clone(),
);
}
}
Expand All @@ -179,7 +179,7 @@ fn spawn_birds(
window: &WindowDescriptor,
counter: &mut BevyCounter,
spawn_count: u128,
texture: Handle<Image>,
image: Handle<Image>,
) {
let bird_x = (window.width / -2.) + HALF_BIRD_SIZE;
let bird_y = (window.height / 2.) - HALF_BIRD_SIZE;
Expand All @@ -188,7 +188,7 @@ fn spawn_birds(
commands
.spawn_bundle(PipelinedSpriteBundle {
// material: bird_material.0.clone(),
texture: texture.clone(),
image: image.clone(),
transform: Transform {
translation: Vec3::new(bird_x, bird_y, bird_z),
scale: Vec3::splat(BIRD_SCALE),
Expand Down
2 changes: 1 addition & 1 deletion pipelined/bevy_core_pipeline/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ use bevy_ecs::prelude::*;
use bevy_render2::{
camera::{ActiveCameras, CameraPlugin},
color::Color,
image::Image,
render_graph::{EmptyNode, RenderGraph, SlotInfo, SlotType},
render_phase::{sort_phase_system, DrawFunctionId, DrawFunctions, PhaseItem, RenderPhase},
render_resource::*,
renderer::RenderDevice,
texture::{Image, TextureCache},
view::ExtractedView,
RenderApp, RenderStage, RenderWorld,
};
Expand Down
4 changes: 2 additions & 2 deletions pipelined/bevy_gltf2/src/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ use bevy_render2::{
Camera, CameraPlugin, CameraProjection, OrthographicProjection, PerspectiveProjection,
},
color::Color,
image::{Image, ImageError, ImageType},
mesh::{Indices, Mesh, VertexAttributeValues},
texture::{Image, ImageType, TextureError},
};
use bevy_scene::Scene;
use bevy_transform::{
Expand Down Expand Up @@ -50,7 +50,7 @@ pub enum GltfError {
#[error("invalid image mime type: {0}")]
InvalidImageMimeType(String),
#[error("You may need to add the feature for the file format: {0}")]
ImageError(#[from] TextureError),
ImageError(#[from] ImageError),
#[error("failed to load an asset path: {0}")]
AssetIoError(#[from] AssetIoError),
}
Expand Down
2 changes: 1 addition & 1 deletion pipelined/bevy_pbr2/src/material.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ use bevy_math::Vec4;
use bevy_reflect::TypeUuid;
use bevy_render2::{
color::Color,
image::Image,
render_asset::{PrepareAssetError, RenderAsset, RenderAssetPlugin, RenderAssets},
render_resource::{BindGroup, Buffer, BufferInitDescriptor, BufferUsage, Sampler, TextureView},
renderer::RenderDevice,
texture::Image,
};
use crevice::std140::{AsStd140, Std140};
use wgpu::{BindGroupDescriptor, BindGroupEntry, BindingResource};
Expand Down
1 change: 0 additions & 1 deletion pipelined/bevy_pbr2/src/render/light.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ use bevy_render2::{
render_resource::*,
renderer::{RenderContext, RenderDevice, RenderQueue},
shader::Shader,
texture::*,
view::{ExtractedView, ViewUniformOffset, ViewUniforms},
};
use bevy_transform::components::GlobalTransform;
Expand Down
2 changes: 1 addition & 1 deletion pipelined/bevy_pbr2/src/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ use bevy_ecs::{
};
use bevy_math::Mat4;
use bevy_render2::{
image::{BevyDefault, GpuImage, Image, TextureFormatPixelInfo},
mesh::Mesh,
render_asset::RenderAssets,
render_component::{ComponentUniforms, DynamicUniformIndex},
render_phase::{DrawFunctions, RenderCommand, RenderPhase, TrackedRenderPass},
render_resource::*,
renderer::{RenderDevice, RenderQueue},
shader::Shader,
texture::{BevyDefault, GpuImage, Image, TextureFormatPixelInfo},
view::{ExtractedView, ViewUniformOffset, ViewUniforms},
};
use bevy_transform::components::GlobalTransform;
Expand Down
Loading