Advertisement
443eb9

Untitled

Oct 30th, 2023
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 4.84 KB | None | 0 0
  1. use bevy::{
  2.     core_pipeline::core_2d::Transparent2d,
  3.     ecs::system::lifetimeless::{Read, SRes},
  4.     render::{
  5.         mesh::GpuBufferInfo,
  6.         render_phase::{RenderCommand, RenderCommandResult},
  7.         render_resource::PipelineCache,
  8.         view::ViewUniformOffset,
  9.     },
  10. };
  11.  
  12. use crate::tilemap::Tilemap;
  13.  
  14. use super::{queue::TileViewBindGroup, BindGroups, RenderChunkStorage};
  15.  
  16. pub type DrawTilemap = (
  17.     SetTileViewBindGroup<0>,
  18.     SetTileTextureBindGroup<1>,
  19.     SetPipeline,
  20.     DrawTileMesh,
  21. );
  22.  
  23. pub struct SetTileViewBindGroup<const I: usize>;
  24. impl<const I: usize> RenderCommand<Transparent2d> for SetTileViewBindGroup<I> {
  25.     type Param = ();
  26.  
  27.     type ViewWorldQuery = (Read<ViewUniformOffset>, Read<TileViewBindGroup>);
  28.  
  29.     type ItemWorldQuery = ();
  30.  
  31.     fn render<'w>(
  32.        _item: &Transparent2d,
  33.        (view_uniform_offset, view_bind_group): bevy::ecs::query::ROQueryItem<
  34.            'w,
  35.             Self::ViewWorldQuery,
  36.         >,
  37.         _entity: bevy::ecs::query::ROQueryItem<'w, Self::ItemWorldQuery>,
  38.        _param: bevy::ecs::system::SystemParamItem<'w, '_, Self::Param>,
  39.        pass: &mut bevy::render::render_phase::TrackedRenderPass<'w>,
  40.     ) -> RenderCommandResult {
  41.         println!("SetTileViewBindGroup");
  42.         pass.set_bind_group(I, &view_bind_group.value, &[view_uniform_offset.offset]);
  43.  
  44.         RenderCommandResult::Success
  45.     }
  46. }
  47.  
  48. pub struct SetTileTextureBindGroup<const I: usize>;
  49. impl<const I: usize> RenderCommand<Transparent2d> for SetTileTextureBindGroup<I> {
  50.     type Param = SRes<BindGroups>;
  51.  
  52.     type ViewWorldQuery = ();
  53.  
  54.     type ItemWorldQuery = Read<Tilemap>;
  55.  
  56.     #[inline]
  57.     fn render<'w>(
  58.        _item: &Transparent2d,
  59.        _view: bevy::ecs::query::ROQueryItem<'w, Self::ViewWorldQuery>,
  60.         tilemaps: bevy::ecs::query::ROQueryItem<'w, Self::ItemWorldQuery>,
  61.        bind_groups: bevy::ecs::system::SystemParamItem<'w, '_, Self::Param>,
  62.        pass: &mut bevy::render::render_phase::TrackedRenderPass<'w>,
  63.     ) -> RenderCommandResult {
  64.         println!("SetTileTextureBindGroup");
  65.         pass.set_bind_group(
  66.             I,
  67.             bind_groups
  68.                 .into_inner()
  69.                 .tilemap_texture_arrays
  70.                 .get(&tilemaps.texture)
  71.                 .unwrap(),
  72.             &[],
  73.         );
  74.  
  75.         RenderCommandResult::Success
  76.     }
  77. }
  78.  
  79. pub struct SetPipeline;
  80. impl RenderCommand<Transparent2d> for SetPipeline {
  81.     type Param = SRes<PipelineCache>;
  82.  
  83.     type ViewWorldQuery = ();
  84.  
  85.     type ItemWorldQuery = ();
  86.  
  87.     #[inline]
  88.     fn render<'w>(
  89.        item: &Transparent2d,
  90.        _view: bevy::ecs::query::ROQueryItem<'w, Self::ViewWorldQuery>,
  91.         _entity: bevy::ecs::query::ROQueryItem<'w, Self::ItemWorldQuery>,
  92.        pipeline_cache: bevy::ecs::system::SystemParamItem<'w, '_, Self::Param>,
  93.        pass: &mut bevy::render::render_phase::TrackedRenderPass<'w>,
  94.     ) -> RenderCommandResult {
  95.         println!("SetPipeline");
  96.         if let Some(pipeline) = pipeline_cache
  97.             .into_inner()
  98.             .get_render_pipeline(item.pipeline)
  99.         {
  100.             pass.set_render_pipeline(pipeline);
  101.             RenderCommandResult::Success
  102.         } else {
  103.             RenderCommandResult::Failure
  104.         }
  105.     }
  106. }
  107.  
  108. pub struct DrawTileMesh;
  109. impl RenderCommand<Transparent2d> for DrawTileMesh {
  110.     type Param = SRes<RenderChunkStorage>;
  111.  
  112.     type ViewWorldQuery = ();
  113.  
  114.     type ItemWorldQuery = Read<Tilemap>;
  115.  
  116.     #[inline]
  117.     fn render<'w>(
  118.        _item: &Transparent2d,
  119.        _view: bevy::ecs::query::ROQueryItem<'w, Self::ViewWorldQuery>,
  120.         tilemap: bevy::ecs::query::ROQueryItem<'w, Self::ItemWorldQuery>,
  121.        render_chunks: bevy::ecs::system::SystemParamItem<'w, '_, Self::Param>,
  122.        pass: &mut bevy::render::render_phase::TrackedRenderPass<'w>,
  123.     ) -> RenderCommandResult {
  124.         println!("DrawTileMesh");
  125.         if let Some(chunks) = render_chunks.into_inner().value.get(&tilemap.id) {
  126.             for chunk in chunks.iter() {
  127.                 if let Some(gpu_mesh) = &chunk.gpu_mesh {
  128.                     pass.set_vertex_buffer(0, gpu_mesh.vertex_buffer.slice(..));
  129.                     match &gpu_mesh.buffer_info {
  130.                         GpuBufferInfo::Indexed {
  131.                             buffer,
  132.                             count,
  133.                             index_format,
  134.                         } => {
  135.                             pass.set_index_buffer(buffer.slice(..), 0, *index_format);
  136.                             pass.draw_indexed(0..*count, 0, 0..1);
  137.                         }
  138.                         GpuBufferInfo::NonIndexed => {
  139.                             pass.draw(0..gpu_mesh.vertex_count, 0..1);
  140.                         }
  141.                     }
  142.                 }
  143.             }
  144.         }
  145.  
  146.         RenderCommandResult::Success
  147.     }
  148. }
  149.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement