Skip to content

Commit 6b3cc8c

Browse files
authored
Merge pull request bevyengine#1 from mockersf/skeletal-animation
handling shadows with skinned animations
2 parents 0e7e150 + 3fe1842 commit 6b3cc8c

File tree

2 files changed

+36
-6
lines changed

2 files changed

+36
-6
lines changed

crates/bevy_pbr/src/render/light.rs

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::{
22
point_light_order, AmbientLight, Clusters, CubemapVisibleEntities, DirectionalLight,
33
DirectionalLightShadowMap, DrawMesh, MeshPipeline, NotShadowCaster, PointLight,
44
PointLightShadowMap, SetMeshBindGroup, SetSkinnedMeshBindGroup, VisiblePointLights,
5-
SHADOW_SHADER_HANDLE,
5+
JOINT_BUFFER_SIZE, SHADOW_SHADER_HANDLE,
66
};
77
use bevy_asset::Handle;
88
use bevy_core::FloatOrd;
@@ -160,6 +160,7 @@ pub const SHADOW_FORMAT: TextureFormat = TextureFormat::Depth32Float;
160160
pub struct ShadowPipeline {
161161
pub view_layout: BindGroupLayout,
162162
pub mesh_layout: BindGroupLayout,
163+
pub skinned_mesh_layout: BindGroupLayout,
163164
pub point_light_sampler: Sampler,
164165
pub directional_light_sampler: Sampler,
165166
}
@@ -189,9 +190,25 @@ impl FromWorld for ShadowPipeline {
189190

190191
let mesh_pipeline = world.get_resource::<MeshPipeline>().unwrap();
191192

193+
let skinned_mesh_layout =
194+
render_device.create_bind_group_layout(&BindGroupLayoutDescriptor {
195+
entries: &[BindGroupLayoutEntry {
196+
binding: 0,
197+
visibility: ShaderStages::VERTEX,
198+
ty: BindingType::Buffer {
199+
ty: BufferBindingType::Uniform,
200+
has_dynamic_offset: false,
201+
min_binding_size: BufferSize::new(JOINT_BUFFER_SIZE as u64),
202+
},
203+
count: None,
204+
}],
205+
label: Some("mesh_layout"),
206+
});
207+
192208
ShadowPipeline {
193209
view_layout,
194210
mesh_layout: mesh_pipeline.mesh_layout.clone(),
211+
skinned_mesh_layout,
195212
point_light_sampler: render_device.create_sampler(&SamplerDescriptor {
196213
address_mode_u: AddressMode::ClampToEdge,
197214
address_mode_v: AddressMode::ClampToEdge,
@@ -257,18 +274,31 @@ impl SpecializedMeshPipeline for ShadowPipeline {
257274
key: Self::Key,
258275
layout: &MeshVertexBufferLayout,
259276
) -> Result<RenderPipelineDescriptor, SpecializedMeshPipelineError> {
260-
let vertex_buffer_layout =
261-
layout.get_layout(&[Mesh::ATTRIBUTE_POSITION.at_shader_location(0)])?;
277+
let mut vertex_attributes = vec![Mesh::ATTRIBUTE_POSITION.at_shader_location(0)];
278+
279+
let mut bind_group_layout = vec![self.view_layout.clone(), self.mesh_layout.clone()];
280+
let mut shader_defs = Vec::new();
281+
282+
if layout.contains(Mesh::ATTRIBUTE_JOINT_INDEX)
283+
&& layout.contains(Mesh::ATTRIBUTE_JOINT_WEIGHT)
284+
{
285+
shader_defs.push(String::from("SKINNED"));
286+
vertex_attributes.push(Mesh::ATTRIBUTE_JOINT_INDEX.at_shader_location(4));
287+
vertex_attributes.push(Mesh::ATTRIBUTE_JOINT_WEIGHT.at_shader_location(5));
288+
bind_group_layout.push(self.skinned_mesh_layout.clone());
289+
}
290+
291+
let vertex_buffer_layout = layout.get_layout(&vertex_attributes)?;
262292

263293
Ok(RenderPipelineDescriptor {
264294
vertex: VertexState {
265295
shader: SHADOW_SHADER_HANDLE.typed::<Shader>(),
266296
entry_point: "vertex".into(),
267-
shader_defs: vec![],
297+
shader_defs,
268298
buffers: vec![vertex_buffer_layout],
269299
},
270300
fragment: None,
271-
layout: Some(vec![self.view_layout.clone(), self.mesh_layout.clone()]),
301+
layout: Some(bind_group_layout),
272302
primitive: PrimitiveState {
273303
topology: key.primitive_topology(),
274304
strip_index_format: None,

crates/bevy_pbr/src/render/mesh.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ pub struct MeshRenderPlugin;
3333

3434
const MAX_JOINTS: usize = 256;
3535
const JOINT_SIZE: usize = std::mem::size_of::<Mat4>();
36-
const JOINT_BUFFER_SIZE: usize = MAX_JOINTS * JOINT_SIZE;
36+
pub(crate) const JOINT_BUFFER_SIZE: usize = MAX_JOINTS * JOINT_SIZE;
3737

3838
pub const MESH_VIEW_BIND_GROUP_HANDLE: HandleUntyped =
3939
HandleUntyped::weak_from_u64(Shader::TYPE_UUID, 9076678235888822571);

0 commit comments

Comments
 (0)