Skip to content
Open
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
18 changes: 10 additions & 8 deletions crates/bevy_pbr/src/prepass/prepass.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,20 @@ fn morph_vertex(vertex_in: Vertex, instance_index: u32) -> Vertex {
var vertex = vertex_in;
let first_vertex = mesh[instance_index].first_vertex_index;
let vertex_index = vertex.index - first_vertex;
let morph_descriptor_index = mesh[instance_index].morph_descriptor_index;

let weight_count = morph::layer_count(instance_index);
let weight_count = morph::layer_count(morph_descriptor_index);
for (var i: u32 = 0u; i < weight_count; i ++) {
let weight = morph::weight_at(i, instance_index);
let weight = morph::weight_at(i, morph_descriptor_index);
if weight == 0.0 {
continue;
}
vertex.position += weight * morph_position(vertex_index, i, instance_index);
vertex.position += weight * morph_position(vertex_index, i, morph_descriptor_index);
#ifdef VERTEX_NORMALS
vertex.normal += weight * morph_normal(vertex_index, i, instance_index);
vertex.normal += weight * morph_normal(vertex_index, i, morph_descriptor_index);
#endif
#ifdef VERTEX_TANGENTS
vertex.tangent += vec4(weight * morph_tangent(vertex_index, i, instance_index), 0.0);
vertex.tangent += vec4(weight * morph_tangent(vertex_index, i, morph_descriptor_index), 0.0);
#endif
}
return vertex;
Expand All @@ -50,13 +51,14 @@ fn morph_prev_vertex(vertex_in: Vertex, instance_index: u32) -> Vertex {
var vertex = vertex_in;
let first_vertex = mesh[instance_index].first_vertex_index;
let vertex_index = vertex.index - first_vertex;
let weight_count = morph::layer_count(instance_index);
let morph_descriptor_index = mesh[instance_index].morph_descriptor_index;
let weight_count = morph::layer_count(morph_descriptor_index);
for (var i: u32 = 0u; i < weight_count; i ++) {
let weight = morph::prev_weight_at(i, instance_index);
let weight = morph::prev_weight_at(i, morph_descriptor_index);
if weight == 0.0 {
continue;
}
vertex.position += weight * morph_position(vertex_index, i, instance_index);
vertex.position += weight * morph_position(vertex_index, i, morph_descriptor_index);
// Don't bother morphing normals and tangents; we don't need them for
// motion vector calculation.
}
Expand Down
11 changes: 6 additions & 5 deletions crates/bevy_pbr/src/render/mesh.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,20 @@ fn morph_vertex(vertex_in: Vertex, instance_index: u32) -> Vertex {
var vertex = vertex_in;
let first_vertex = mesh[instance_index].first_vertex_index;
let vertex_index = vertex.index - first_vertex;
let morph_descriptor_index = mesh[instance_index].morph_descriptor_index;

let weight_count = bevy_pbr::morph::layer_count(instance_index);
let weight_count = bevy_pbr::morph::layer_count(morph_descriptor_index);
for (var i: u32 = 0u; i < weight_count; i ++) {
let weight = bevy_pbr::morph::weight_at(i, instance_index);
let weight = bevy_pbr::morph::weight_at(i, morph_descriptor_index);
if weight == 0.0 {
continue;
}
vertex.position += weight * morph_position(vertex_index, i, instance_index);
vertex.position += weight * morph_position(vertex_index, i, morph_descriptor_index);
#ifdef VERTEX_NORMALS
vertex.normal += weight * morph_normal(vertex_index, i, instance_index);
vertex.normal += weight * morph_normal(vertex_index, i, morph_descriptor_index);
#endif
#ifdef VERTEX_TANGENTS
vertex.tangent += vec4(weight * morph_tangent(vertex_index, i, instance_index), 0.0);
vertex.tangent += vec4(weight * morph_tangent(vertex_index, i, morph_descriptor_index), 0.0);
#endif
}
return vertex;
Expand Down
35 changes: 15 additions & 20 deletions crates/bevy_pbr/src/render/morph.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#ifdef MORPH_TARGETS

#import bevy_pbr::mesh_types::{MorphAttributes, MorphDescriptor, MorphWeights}
#import bevy_pbr::mesh_bindings::mesh

#ifdef SKINS_USE_UNIFORM_BUFFERS

Expand All @@ -29,13 +28,12 @@ const normal_offset: u32 = 3u;
const tangent_offset: u32 = 6u;
const total_component_count: u32 = 9u;

fn layer_count(instance_index: u32) -> u32 {
fn layer_count(descriptor_index: u32) -> u32 {
#ifdef SKINS_USE_UNIFORM_BUFFERS
let dimensions = textureDimensions(morph_targets);
return u32(dimensions.z);
#else // SKINS_USE_UNIFORM_BUFFERS
let morph_descriptor_index = mesh[instance_index].morph_descriptor_index;
return morph_descriptors[morph_descriptor_index].weight_count;
return morph_descriptors[descriptor_index].weight_count;
#endif // SKINS_USE_UNIFORM_BUFFERS
}

Expand All @@ -47,24 +45,22 @@ fn component_texture_coord(vertex_index: u32, component_offset: u32) -> vec2<u32
}
#endif // SKINS_USE_UNIFORM_BUFFERS

fn weight_at(weight_index: u32, instance_index: u32) -> f32 {
fn weight_at(weight_index: u32, descriptor_index: u32) -> f32 {
#ifdef SKINS_USE_UNIFORM_BUFFERS
let i = weight_index;
return morph_weights.weights[i / 4u][i % 4u];
#else // SKINS_USE_UNIFORM_BUFFERS
let morph_descriptor_index = mesh[instance_index].morph_descriptor_index;
let weights_offset = morph_descriptors[morph_descriptor_index].current_weights_offset;
let weights_offset = morph_descriptors[descriptor_index].current_weights_offset;
return morph_weights[weights_offset + weight_index];
#endif // SKINS_USE_UNIFORM_BUFFERS
}

fn prev_weight_at(weight_index: u32, instance_index: u32) -> f32 {
fn prev_weight_at(weight_index: u32, descriptor_index: u32) -> f32 {
#ifdef SKINS_USE_UNIFORM_BUFFERS
let i = weight_index;
return prev_morph_weights.weights[i / 4u][i % 4u];
#else // SKINS_USE_UNIFORM_BUFFERS
let morph_descriptor_index = mesh[instance_index].morph_descriptor_index;
let weights_offset = morph_descriptors[morph_descriptor_index].prev_weights_offset;
let weights_offset = morph_descriptors[descriptor_index].prev_weights_offset;
return prev_morph_weights[weights_offset + weight_index];
#endif // SKINS_USE_UNIFORM_BUFFERS
}
Expand Down Expand Up @@ -101,23 +97,22 @@ fn morph_tangent(vertex_index: u32, weight_index: u32, instance_index: u32) -> v

#else // SKINS_USE_UNIFORM_BUFFERS

fn get_morph_target(vertex_index: u32, weight_index: u32, instance_index: u32) -> MorphAttributes {
let morph_descriptor_index = mesh[instance_index].morph_descriptor_index;
let targets_offset = morph_descriptors[morph_descriptor_index].targets_offset;
let vertex_count = morph_descriptors[morph_descriptor_index].vertex_count;
fn get_morph_target(vertex_index: u32, weight_index: u32, descriptor_index: u32) -> MorphAttributes {
let targets_offset = morph_descriptors[descriptor_index].targets_offset;
let vertex_count = morph_descriptors[descriptor_index].vertex_count;
return morph_targets[targets_offset + weight_index * vertex_count + vertex_index];
}

fn morph_position(vertex_index: u32, weight_index: u32, instance_index: u32) -> vec3<f32> {
return get_morph_target(vertex_index, weight_index, instance_index).position;
fn morph_position(vertex_index: u32, weight_index: u32, descriptor_index: u32) -> vec3<f32> {
return get_morph_target(vertex_index, weight_index, descriptor_index).position;
}

fn morph_normal(vertex_index: u32, weight_index: u32, instance_index: u32) -> vec3<f32> {
return get_morph_target(vertex_index, weight_index, instance_index).normal;
fn morph_normal(vertex_index: u32, weight_index: u32, descriptor_index: u32) -> vec3<f32> {
return get_morph_target(vertex_index, weight_index, descriptor_index).normal;
}

fn morph_tangent(vertex_index: u32, weight_index: u32, instance_index: u32) -> vec3<f32> {
return get_morph_target(vertex_index, weight_index, instance_index).tangent;
fn morph_tangent(vertex_index: u32, weight_index: u32, descriptor_index: u32) -> vec3<f32> {
return get_morph_target(vertex_index, weight_index, descriptor_index).tangent;
}

#endif // SKINS_USE_UNIFORM_BUFFERS
Expand Down
Loading