Search across packages, versions, files, declarations, and comments. Use multiple terms to narrow results (AND), and add @vertex, @fragment, or @compute to filter by shader stage.
1#define_import_path bevy_pbr::morph
3#ifdef MORPH_TARGETS
5#import bevy_pbr::mesh_types::MorphWeights;
7@group(1) @binding(2) var<uniform> morph_weights: MorphWeights;
8@group(1) @binding(3) var morph_targets: texture_3d<f32>;
9@group(1) @binding(7) var<uniform> prev_morph_weights: MorphWeights;
11// NOTE: Those are the "hardcoded" values found in `MorphAttributes` struct
12// in crates/bevy_render/src/mesh/morph/visitors.rs
13// In an ideal world, the offsets are established dynamically and passed as #defines
14// to the shader, but it's out of scope for the initial implementation of morph targets.
15const position_offset: u32 = 0u;
16const normal_offset: u32 = 3u;
17const tangent_offset: u32 = 6u;
18const total_component_count: u32 = 9u;
20fn layer_count() -> u32 {
21 let dimensions = textureDimensions(morph_targets);
22 return u32(dimensions.z);
24fn component_texture_coord(vertex_index: u32, component_offset: u32) -> vec2<u32> {
25 let width = u32(textureDimensions(morph_targets).x);
26 let component_index = total_component_count * vertex_index + component_offset;
27 return vec2<u32>(component_index % width, component_index / width);
29fn weight_at(weight_index: u32) -> f32 {
30 let i = weight_index;
31 return morph_weights.weights[i / 4u][i % 4u];
33fn prev_weight_at(weight_index: u32) -> f32 {
34 let i = weight_index;
35 return prev_morph_weights.weights[i / 4u][i % 4u];
37fn morph_pixel(vertex: u32, component: u32, weight: u32) -> f32 {
38 let coord = component_texture_coord(vertex, component);
39 // Due to https://gpuweb.github.io/gpuweb/wgsl/#texel-formats
40 // While the texture stores a f32, the textureLoad returns a vec4<>, where
41 // only the first component is set.
42 return textureLoad(morph_targets, vec3(coord, weight), 0).r;
44fn morph(vertex_index: u32, component_offset: u32, weight_index: u32) -> vec3<f32> {
45 return vec3<f32>(
46 morph_pixel(vertex_index, component_offset, weight_index),
47 morph_pixel(vertex_index, component_offset + 1u, weight_index),
48 morph_pixel(vertex_index, component_offset + 2u, weight_index),
49 );
52#endif // MORPH_TARGETS
Contents