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

Morph targets #1

Merged
merged 2 commits into from
Jul 26, 2023
Merged
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
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ bevy = { version = "0.11", default-features = false, features = [
"png",
"x11",
] }
bevy_mod_gltf_patched = { git = "https://github.com/luggage66/bevy_mod_gltf_patched/", branch = "bevy-0.11" }
bevy_mod_gltf_patched = { git = "https://github.com/zainthemaynnn/bevy_mod_gltf_patched/", branch = "bevy-0.11" }

[features]
default = ["bevy_ui"]
Expand Down
6 changes: 4 additions & 2 deletions src/draw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ pub(crate) fn queue_outline_stencil_mesh(
let key = base_key
.with_primitive_topology(mesh.primitive_topology)
.with_depth_mode(stencil_flags.depth_mode)
.with_offset_zero(stencil_uniform.offset == 0.0);
.with_offset_zero(stencil_uniform.offset == 0.0)
.with_morph_targets(mesh.morph_targets.is_some());
let pipeline = pipelines
.specialize(&pipeline_cache, &stencil_pipeline, key, &mesh.layout)
.unwrap();
Expand Down Expand Up @@ -161,7 +162,8 @@ pub(crate) fn queue_outline_volume_mesh(
})
.with_depth_mode(volume_flags.depth_mode)
.with_offset_zero(volume_uniform.offset == 0.0)
.with_hdr_format(view.hdr);
.with_hdr_format(view.hdr)
.with_morph_targets(mesh.morph_targets.is_some());
let pipeline = pipelines
.specialize(&pipeline_cache, &outline_pipeline, key, &mesh.layout)
.unwrap();
Expand Down
11 changes: 4 additions & 7 deletions src/outline.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ struct VertexInput {
@location(1) normal: vec3<f32>,
#endif
#ifdef SKINNED
@location(2) joint_indexes: vec4<u32>,
@location(3) joint_weights: vec4<f32>,
@location(5) joint_indices: vec4<u32>,
@location(6) joint_weights: vec4<f32>,
#endif
};

Expand Down Expand Up @@ -37,11 +37,8 @@ var<uniform> view: View;
@group(1) @binding(0)
var<uniform> mesh: Mesh;

#ifdef SKINNED
@group(1) @binding(1)
var<uniform> joint_matrices: SkinnedMesh;
#import bevy_pbr::skinning
#endif
#import bevy_pbr::morph

@group(2) @binding(0)
var<uniform> view_uniform: OutlineViewUniform;
Expand All @@ -66,7 +63,7 @@ fn model_origin_z(plane: vec3<f32>, view_proj: mat4x4<f32>) -> f32 {
@vertex
fn vertex(vertex: VertexInput) -> VertexOutput {
#ifdef SKINNED
let model = bevy_pbr::skinning::skin_model(vertex.joint_indexes, vertex.joint_weights);
let model = bevy_pbr::skinning::skin_model(vertex.joint_indices, vertex.joint_weights);
#else
let model = mesh.model;
#endif
Expand Down
43 changes: 29 additions & 14 deletions src/pipeline.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::borrow::Cow;

use bevy::pbr::{setup_morph_and_skinning_defs, MeshPipelineKey};
use bevy::prelude::*;
use bevy::reflect::TypeUuid;
use bevy::render::render_resource::{
Expand Down Expand Up @@ -56,6 +57,7 @@ impl PipelineKey {
pub offset_zero, set_offset_zero: 13;
pub hdr_format, set_hdr_format: 14;
pub opengl_workaround, set_opengl_workaround: 15;
pub morph_targets, set_morph_targets: 16;
}

pub(crate) fn new() -> Self {
Expand Down Expand Up @@ -134,6 +136,21 @@ impl PipelineKey {
self.set_opengl_workaround(opengl_workaround);
self
}

pub(crate) fn with_morph_targets(mut self, morph_targets: bool) -> Self {
self.set_morph_targets(morph_targets);
self
}
}

impl From<PipelineKey> for MeshPipelineKey {
fn from(key: PipelineKey) -> Self {
if key.morph_targets() {
MeshPipelineKey::empty() | MeshPipelineKey::MORPH_TARGETS
} else {
MeshPipelineKey::empty()
}
}
}

#[derive(Resource)]
Expand Down Expand Up @@ -230,22 +247,20 @@ impl SpecializedMeshPipeline for OutlinePipeline {
} else {
self.mesh_pipeline.view_layout_multisampled.clone()
}];

let mut buffer_attrs = vec![Mesh::ATTRIBUTE_POSITION.at_shader_location(0)];
let mut vertex_defs = vec![];
let mut vertex_defs = vec!["MESH_BINDGROUP_1".into()];
let mut fragment_defs = vec![];
bind_layouts.push(
if mesh_layout.contains(Mesh::ATTRIBUTE_JOINT_INDEX)
&& mesh_layout.contains(Mesh::ATTRIBUTE_JOINT_WEIGHT)
{
vertex_defs.push(ShaderDefVal::from("SKINNED"));
vertex_defs.push("MESH_BINDGROUP_1".into());
buffer_attrs.push(Mesh::ATTRIBUTE_JOINT_INDEX.at_shader_location(2));
buffer_attrs.push(Mesh::ATTRIBUTE_JOINT_WEIGHT.at_shader_location(3));
self.mesh_pipeline.mesh_layouts.skinned.clone()
} else {
self.mesh_pipeline.mesh_layouts.model_only.clone()
},
);

bind_layouts.push(setup_morph_and_skinning_defs(
&self.mesh_pipeline.mesh_layouts,
mesh_layout,
5,
&key.into(),
&mut vertex_defs,
&mut buffer_attrs,
));

bind_layouts.push(self.outline_view_bind_group_layout.clone());
let cull_mode;
if key.depth_mode() == DepthMode::Flat {
Expand Down