This commit is contained in:
Carter Anderson 2019-12-03 09:01:15 -08:00
parent 816d0c9bdd
commit 0e0eb97430
3 changed files with 33 additions and 32 deletions

View file

@ -2,9 +2,9 @@ use bevy::*;
use bevy::{render::*, asset::{Asset, AssetStorage, Handle}, math, Schedulable}; use bevy::{render::*, asset::{Asset, AssetStorage, Handle}, math, Schedulable};
use rand::{rngs::StdRng, Rng, SeedableRng, random}; use rand::{rngs::StdRng, Rng, SeedableRng, random};
fn build_wander_system(world: &mut World) -> Box<dyn Schedulable> { fn build_wander_system() -> Box<dyn Schedulable> {
let mut rng = StdRng::from_entropy(); let mut rng = StdRng::from_entropy();
SystemBuilder::new("Wander") SystemBuilder::new("Wander")
.read_resource::<Time>() .read_resource::<Time>()
.with_query(<( .with_query(<(
@ -31,7 +31,7 @@ fn build_wander_system(world: &mut World) -> Box<dyn Schedulable> {
}) })
} }
fn build_navigate_system(world: &mut World) -> Box<dyn Schedulable> { fn build_navigate_system() -> Box<dyn Schedulable> {
SystemBuilder::new("Navigate") SystemBuilder::new("Navigate")
.with_query(<( .with_query(<(
Read<Person>, Read<Person>,
@ -40,7 +40,7 @@ fn build_navigate_system(world: &mut World) -> Box<dyn Schedulable> {
Write<NavigationPoint>, Write<NavigationPoint>,
)>::query()) )>::query())
.build(move |_, world, _, person_query| { .build(move |_, world, _, person_query| {
for (_, mut translation, mut velocity, mut navigation_point) in person_query.iter(world) { for (_, translation, mut velocity, navigation_point) in person_query.iter(world) {
let distance = navigation_point.target - translation.vector; let distance = navigation_point.target - translation.vector;
if math::length(&distance) > 0.01 { if math::length(&distance) > 0.01 {
let direction = distance.normalize(); let direction = distance.normalize();
@ -52,7 +52,7 @@ fn build_navigate_system(world: &mut World) -> Box<dyn Schedulable> {
}) })
} }
fn build_move_system(world: &mut World) -> Box<dyn Schedulable> { fn build_move_system() -> Box<dyn Schedulable> {
SystemBuilder::new("Move") SystemBuilder::new("Move")
.read_resource::<Time>() .read_resource::<Time>()
.with_query(<( .with_query(<(
@ -66,7 +66,7 @@ fn build_move_system(world: &mut World) -> Box<dyn Schedulable> {
}) })
} }
fn build_print_status_system(world: &mut World) -> Box<dyn Schedulable> { fn build_print_status_system() -> Box<dyn Schedulable> {
let mut elapsed = 0.0; let mut elapsed = 0.0;
SystemBuilder::new("PrintStatus") SystemBuilder::new("PrintStatus")
.read_resource::<Time>() .read_resource::<Time>()
@ -85,19 +85,23 @@ fn build_print_status_system(world: &mut World) -> Box<dyn Schedulable> {
fn build_spawner_system(world: &mut World) -> Box<dyn Schedulable> { fn build_spawner_system(world: &mut World) -> Box<dyn Schedulable> {
let mesh_handle = { let mesh_handle = {
let mut mesh_storage = world.resources.get_mut::<AssetStorage<Mesh, MeshType>>().unwrap(); let mesh_storage = world.resources.get_mut::<AssetStorage<Mesh, MeshType>>().unwrap();
mesh_storage.get_named("cube").unwrap() mesh_storage.get_named("cube").unwrap()
}; };
let mut elapsed = 0.0;
let duration = 1000000.0;
let mut elapsed = duration;
let batch_size = 1;
SystemBuilder::new("Spawner") SystemBuilder::new("Spawner")
.read_resource::<Time>() .read_resource::<Time>()
.with_query(<( .with_query(<(
Read<Person>, Read<Person>,
)>::query()) )>::query())
.build(move |command_buffer, world, time , person_query| { .build(move |command_buffer, _, time , _| {
elapsed += time.delta_seconds; elapsed += time.delta_seconds;
if elapsed > 0.5 { if elapsed > duration {
for i in 0..20 { for _ in 0..batch_size {
spawn_person(command_buffer, mesh_handle.clone()); spawn_person(command_buffer, mesh_handle.clone());
} }
elapsed = 0.0; elapsed = 0.0;
@ -132,17 +136,17 @@ fn main() {
let plane = Mesh::load(MeshType::Plane{ size: 25 }); let plane = Mesh::load(MeshType::Plane{ size: 25 });
let mut mesh_storage = AssetStorage::<Mesh, MeshType>::new(); let mut mesh_storage = AssetStorage::<Mesh, MeshType>::new();
let mesh_handle = mesh_storage.add(cube, "cube"); let _ = mesh_storage.add(cube, "cube");
let plane_handle = mesh_storage.add(plane, "plane"); let plane_handle = mesh_storage.add(plane, "plane");
world.resources.insert(mesh_storage); world.resources.insert(mesh_storage);
let transform_system_bundle = transform_system_bundle::build(&mut world); let transform_system_bundle = transform_system_bundle::build(&mut world);
scheduler.add_systems(ApplicationStage::Update, transform_system_bundle); scheduler.add_systems(ApplicationStage::Update, transform_system_bundle);
scheduler.add_system(ApplicationStage::Update, build_wander_system(&mut world)); scheduler.add_system(ApplicationStage::Update, build_wander_system());
scheduler.add_system(ApplicationStage::Update, build_navigate_system(&mut world)); scheduler.add_system(ApplicationStage::Update, build_navigate_system());
scheduler.add_system(ApplicationStage::Update, build_move_system(&mut world)); scheduler.add_system(ApplicationStage::Update, build_move_system());
scheduler.add_system(ApplicationStage::Update, build_spawner_system(&mut world)); scheduler.add_system(ApplicationStage::Update, build_spawner_system(&mut world));
scheduler.add_system(ApplicationStage::Update, build_print_status_system(&mut world)); scheduler.add_system(ApplicationStage::Update, build_print_status_system());
world.insert((), vec![ world.insert((), vec![
// plane // plane
@ -158,7 +162,6 @@ fn main() {
// lights // lights
( (
Light { Light {
pos: math::vec3(7.0, -5.0, 10.0),
color: wgpu::Color { color: wgpu::Color {
r: 0.5, r: 0.5,
g: 1.0, g: 1.0,
@ -174,7 +177,6 @@ fn main() {
), ),
( (
Light { Light {
pos: math::vec3(-5.0, 7.0, 10.0),
color: wgpu::Color { color: wgpu::Color {
r: 1.0, r: 1.0,
g: 0.5, g: 0.5,

View file

@ -1,10 +1,9 @@
use crate::{math, render::camera}; use crate::{math, render::camera, Translation};
use std::ops::Range; use std::ops::Range;
use zerocopy::{AsBytes, FromBytes}; use zerocopy::{AsBytes, FromBytes};
pub struct Light { pub struct Light {
pub pos: math::Vec3,
pub color: wgpu::Color, pub color: wgpu::Color,
pub fov: f32, pub fov: f32,
pub depth: Range<f32>, pub depth: Range<f32>,
@ -19,16 +18,16 @@ pub struct LightRaw {
pub color: [f32; 4], pub color: [f32; 4],
} }
impl Light { impl LightRaw {
pub fn to_raw(&self, transform: &math::Mat4) -> LightRaw { pub fn from(light: &Light, transform: &math::Mat4, translation: &Translation) -> LightRaw {
let proj = camera::get_projection_matrix(self.fov, 1.0, self.depth.start, self.depth.end) * transform; let proj = camera::get_projection_matrix(light.fov, 1.0, light.depth.start, light.depth.end) * transform;
LightRaw { LightRaw {
proj: proj.into(), proj: proj.into(),
pos: [self.pos.x, self.pos.y, self.pos.z, 1.0], pos: [translation.vector.x, translation.vector.y, translation.vector.z, 1.0],
color: [ color: [
self.color.r as f32, light.color.r as f32,
self.color.g as f32, light.color.g as f32,
self.color.b as f32, light.color.b as f32,
1.0, 1.0,
], ],
} }

View file

@ -1,4 +1,4 @@
use crate::{render::*, asset::*, LocalToWorld}; use crate::{render::*, asset::*, LocalToWorld, Translation};
use wgpu::{BindGroupLayout, Buffer, CommandEncoder, Device, VertexBufferDescriptor, SwapChainOutput, SwapChainDescriptor}; use wgpu::{BindGroupLayout, Buffer, CommandEncoder, Device, VertexBufferDescriptor, SwapChainOutput, SwapChainDescriptor};
use legion::prelude::*; use legion::prelude::*;
use zerocopy::AsBytes; use zerocopy::AsBytes;
@ -23,7 +23,7 @@ pub struct ShadowUniforms {
impl Pass for ShadowPass { impl Pass for ShadowPass {
fn render(&mut self, device: &Device, _: &SwapChainOutput, encoder: &mut CommandEncoder, world: &mut World) { fn render(&mut self, device: &Device, _: &SwapChainOutput, encoder: &mut CommandEncoder, world: &mut World) {
let mut light_query = <(Read<Light>, Read<LocalToWorld>)>::query(); let mut light_query = <(Read<Light>, Read<LocalToWorld>, Read<Translation>)>::query();
let mut mesh_query = <(Read<Material>, Read<Handle<Mesh>>)>::query(); let mut mesh_query = <(Read<Material>, Read<Handle<Mesh>>)>::query();
let light_count = light_query.iter(world).count(); let light_count = light_query.iter(world).count();
@ -33,11 +33,11 @@ impl Pass for ShadowPass {
let total_size = size * light_count; let total_size = size * light_count;
let temp_buf_data = let temp_buf_data =
device.create_buffer_mapped(total_size, wgpu::BufferUsage::COPY_SRC); device.create_buffer_mapped(total_size, wgpu::BufferUsage::COPY_SRC);
for ((light, local_to_world), slot) in light_query for ((light, local_to_world, translation), slot) in light_query
.iter(world) .iter(world)
.zip(temp_buf_data.data.chunks_exact_mut(size)) .zip(temp_buf_data.data.chunks_exact_mut(size))
{ {
slot.copy_from_slice(light.to_raw(&local_to_world.0).as_bytes()); slot.copy_from_slice(LightRaw::from(&light, &local_to_world.0, &translation).as_bytes());
} }
encoder.copy_buffer_to_buffer( encoder.copy_buffer_to_buffer(
&temp_buf_data.finish(), &temp_buf_data.finish(),
@ -86,7 +86,7 @@ impl Pass for ShadowPass {
} }
} }
for (i, (light, _)) in light_query.iter_immutable(world).enumerate() { for (i, (light, _, _)) in light_query.iter_immutable(world).enumerate() {
// The light uniform buffer already has the projection, // The light uniform buffer already has the projection,
// let's just copy it over to the shadow uniform buffer. // let's just copy it over to the shadow uniform buffer.
encoder.copy_buffer_to_buffer( encoder.copy_buffer_to_buffer(