Mildly scuffed 3d view

TODO: toggle between views
TODO: update texture on 3d sphere
TODO: Hide info box in 3d view
This commit is contained in:
Tobias Berger 2022-09-06 16:49:41 +02:00
parent 26d3e9b4a6
commit f6ec1ba1a2
Signed by: toby
GPG key ID: 2D05EFAB764D6A88
2 changed files with 64 additions and 16 deletions

View file

@ -43,8 +43,11 @@ use bevy::{
}; };
#[cfg(feature = "render")] #[cfg(feature = "render")]
use bevy::{ use bevy::{
asset::{AssetServer, Assets}, asset::{AssetServer, Assets, Handle},
core_pipeline::core_2d::{Camera2d, Camera2dBundle}, core_pipeline::{
core_2d::{Camera2d, Camera2dBundle},
core_3d::{Camera3d, Camera3dBundle},
},
ecs::{ ecs::{
change_detection::ResMut, change_detection::ResMut,
component::Component, component::Component,
@ -52,10 +55,12 @@ use bevy::{
system::{Commands, Query, Res}, system::{Commands, Query, Res},
}, },
hierarchy::BuildChildren, hierarchy::BuildChildren,
prelude::Vec2, pbr::{PbrBundle, PointLight, PointLightBundle, StandardMaterial},
prelude::{Vec2, Vec3},
render::{ render::{
camera::{Camera, RenderTarget}, camera::{Camera, OrthographicProjection, RenderTarget},
color::Color, color::Color,
mesh::{shape::Icosphere, Mesh},
render_resource::{ render_resource::{
Extent3d, TextureDescriptor, TextureDimension, TextureFormat, TextureUsages, Extent3d, TextureDescriptor, TextureDimension, TextureFormat, TextureUsages,
}, },
@ -63,12 +68,12 @@ use bevy::{
}, },
sprite::{Sprite, SpriteBundle}, sprite::{Sprite, SpriteBundle},
text::Text, text::Text,
transform::components::GlobalTransform, transform::components::{GlobalTransform, Transform},
ui::{ ui::{
entity::{ButtonBundle, NodeBundle, TextBundle}, entity::{ButtonBundle, NodeBundle, TextBundle},
widget::Button, widget::Button,
AlignItems, FocusPolicy, Interaction, JustifyContent, PositionType, Size, Style, UiColor, AlignItems, AlignSelf, FocusPolicy, Interaction, JustifyContent, PositionType, Size, Style,
UiRect, Val, UiColor, UiRect, Val,
}, },
utils::default, utils::default,
window::{CursorIcon, WindowDescriptor, Windows}, window::{CursorIcon, WindowDescriptor, Windows},
@ -84,6 +89,8 @@ fn refresh_world_texture(images: &mut Assets<Image>, world_manager: &WorldManage
debug!("refreshing world texture"); debug!("refreshing world texture");
let image_handle = images.get_handle(world_manager.image_handle_id); let image_handle = images.get_handle(world_manager.image_handle_id);
images.get_mut(&image_handle).unwrap().data = world_manager.world_color_bytes(); images.get_mut(&image_handle).unwrap().data = world_manager.world_color_bytes();
// TODO: Update Icosphere material... try to find out why it doesn't automatically=
} }
#[cfg(feature = "render")] #[cfg(feature = "render")]
@ -247,6 +254,12 @@ fn update_cursor_map_position(
} }
} }
const ROTATION_SPEED: f32 = 0.002;
#[cfg(feature = "render")]
fn rotate_planet(mut planet_transform: Query<'_, '_, &mut Transform, With<Handle<Mesh>>>) {
planet_transform.single_mut().rotate_y(ROTATION_SPEED);
}
#[cfg(feature = "render")] #[cfg(feature = "render")]
fn update_info_panel( fn update_info_panel(
cursor_position: Res<'_, CursorMapPosition>, cursor_position: Res<'_, CursorMapPosition>,
@ -273,11 +286,11 @@ fn update_info_panel(
fn generate_graphics( fn generate_graphics(
mut commands: Commands<'_, '_>, mut commands: Commands<'_, '_>,
mut images: ResMut<'_, Assets<Image>>, mut images: ResMut<'_, Assets<Image>>,
mut materials: ResMut<'_, Assets<StandardMaterial>>,
mut meshes: ResMut<'_, Assets<Mesh>>,
mut world_manager: ResMut<'_, WorldManager>, mut world_manager: ResMut<'_, WorldManager>,
asset_server: Res<'_, AssetServer>, asset_server: Res<'_, AssetServer>,
) { ) {
use bevy::ui::AlignSelf;
let world = world_manager.world(); let world = world_manager.world();
let custom_sprite_size = Vec2 { let custom_sprite_size = Vec2 {
x: (WORLD_SCALE * world.width) as f32, x: (WORLD_SCALE * world.width) as f32,
@ -303,8 +316,42 @@ fn generate_graphics(
}); });
world_manager.image_handle_id = image_handle.id; world_manager.image_handle_id = image_handle.id;
_ = commands.spawn_bundle(Camera3dBundle {
transform: Transform::from_xyz(0.0, 0.0, 8.0).looking_at(default(), Vec3::Y),
projection: OrthographicProjection {
scale: 0.01,
..default()
}
.into(),
..default()
});
_ = commands.spawn_bundle(PbrBundle {
mesh: meshes.add(Mesh::from(Icosphere {
radius: 2.0,
subdivisions: 9,
})),
material: materials.add(images.get_handle(world_manager.image_handle_id).into()),
transform: Transform::from_translation(default()),
..default()
});
_ = commands.spawn_bundle(PointLightBundle {
transform: Transform::from_xyz(-20.0, 20.0, 50.0),
point_light: PointLight {
intensity: 600000.,
range: 100.,
..default()
},
..default()
});
_ = commands _ = commands
.spawn_bundle(Camera2dBundle::default()) .spawn_bundle(Camera2dBundle {
camera: Camera {
is_active: false,
..default()
},
..default()
})
.insert(PanCam::default()); .insert(PanCam::default());
_ = commands.spawn_bundle(SpriteBundle { _ = commands.spawn_bundle(SpriteBundle {
texture: images.get_handle(world_manager.image_handle_id), texture: images.get_handle(world_manager.image_handle_id),
@ -327,7 +374,6 @@ fn generate_graphics(
_ = root_node _ = root_node
.spawn_bundle(NodeBundle { .spawn_bundle(NodeBundle {
style: Style { style: Style {
// align_items: AlignItems::FlexEnd,
align_self: AlignSelf::FlexEnd, align_self: AlignSelf::FlexEnd,
padding: UiRect::all(Val::Px(2.0)), padding: UiRect::all(Val::Px(2.0)),
..default() ..default()
@ -452,8 +498,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
{ {
let world = manager.new_world()?; let world = manager.new_world()?;
_ = app _ = app
// Only run the app when there is user input. This will significantly reduce CPU/GPU use. .insert_resource(WinitSettings::game())
.insert_resource(WinitSettings::desktop_app())
// Use nearest-neighbor rendering for cripsier pixels // Use nearest-neighbor rendering for cripsier pixels
.insert_resource(ImageSettings::default_nearest()) .insert_resource(ImageSettings::default_nearest())
.insert_resource(WindowDescriptor { .insert_resource(WindowDescriptor {
@ -469,7 +514,8 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
.add_system(handle_temperature_button) .add_system(handle_temperature_button)
.add_system(handle_contours_button) .add_system(handle_contours_button)
.add_system(update_cursor_map_position) .add_system(update_cursor_map_position)
.add_system(update_info_panel); .add_system(update_info_panel)
.add_system(rotate_planet);
} }
#[cfg(not(feature = "render"))] #[cfg(not(feature = "render"))]
{ {

View file

@ -19,8 +19,9 @@ impl PluginGroup for WorldPlugins {
{ {
use bevy::{ use bevy::{
asset::AssetPlugin, core_pipeline::CorePipelinePlugin, hierarchy::HierarchyPlugin, asset::AssetPlugin, core_pipeline::CorePipelinePlugin, hierarchy::HierarchyPlugin,
input::InputPlugin, render::RenderPlugin, sprite::SpritePlugin, text::TextPlugin, input::InputPlugin, pbr::PbrPlugin, render::RenderPlugin, sprite::SpritePlugin,
transform::TransformPlugin, ui::UiPlugin, window::WindowPlugin, winit::WinitPlugin, text::TextPlugin, transform::TransformPlugin, ui::UiPlugin, window::WindowPlugin,
winit::WinitPlugin,
}; };
use bevy_pancam::PanCamPlugin; use bevy_pancam::PanCamPlugin;
@ -37,6 +38,7 @@ impl PluginGroup for WorldPlugins {
.add(SpritePlugin::default()) .add(SpritePlugin::default())
.add(TextPlugin::default()) .add(TextPlugin::default())
.add(UiPlugin::default()) .add(UiPlugin::default())
.add(PbrPlugin::default())
.add(PanCamPlugin::default()); .add(PanCamPlugin::default());
} }
#[cfg(not(feature = "render"))] #[cfg(not(feature = "render"))]