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@if(MORPH_TARGETS) import package::render::mesh_types::{MorphAttributes, MorphDescriptor, MorphWeights};
3@if(MORPH_TARGETS && SKINS_USE_UNIFORM_BUFFERS) {
4@group(2) @binding(2) var<uniform> morph_weights: MorphWeights;
5@group(2) @binding(3) var morph_targets: texture_3d<f32>;
6@group(2) @binding(7) var<uniform> prev_morph_weights: MorphWeights;
7}
9@if(MORPH_TARGETS && !SKINS_USE_UNIFORM_BUFFERS) {
10@group(2) @binding(2) var<storage> morph_weights: array<f32>;
11@group(2) @binding(3) var<storage> morph_targets: array<MorphAttributes>;
12@group(2) @binding(7) var<storage> prev_morph_weights: array<f32>;
13@group(2) @binding(8) var<storage> morph_descriptors: array<MorphDescriptor>;
16// NOTE: Those are the "hardcoded" values found in `MorphAttributes` struct
17// in crates/bevy_render/src/mesh/morph/visitors.rs
18// In an ideal world, the offsets are established dynamically and passed as #defines
19// to the shader, but it's out of scope for the initial implementation of morph targets.
20@if(MORPH_TARGETS)
21const position_offset: u32 = 0u;
22@if(MORPH_TARGETS)
23const normal_offset: u32 = 3u;
24@if(MORPH_TARGETS)
25const tangent_offset: u32 = 6u;
26@if(MORPH_TARGETS)
27const total_component_count: u32 = 9u;
29@if(MORPH_TARGETS)
30fn layer_count(descriptor_index: u32) -> u32 {
31@if(SKINS_USE_UNIFORM_BUFFERS)
32 let dimensions = textureDimensions(morph_targets);
33@if(SKINS_USE_UNIFORM_BUFFERS)
34 return u32(dimensions.z);
35@if(!SKINS_USE_UNIFORM_BUFFERS)
36 return morph_descriptors[descriptor_index].weight_count;
39@if(MORPH_TARGETS && SKINS_USE_UNIFORM_BUFFERS)
40fn component_texture_coord(vertex_index: u32, component_offset: u32) -> vec2<u32> {
41 let width = u32(textureDimensions(morph_targets).x);
42 let component_index = total_component_count * vertex_index + component_offset;
43 return vec2<u32>(component_index % width, component_index / width);
46@if(MORPH_TARGETS)
47fn weight_at(weight_index: u32, descriptor_index: u32) -> f32 {
48@if(SKINS_USE_UNIFORM_BUFFERS)
49 let i = weight_index;
50@if(SKINS_USE_UNIFORM_BUFFERS)
51 return morph_weights.weights[i / 4u][i % 4u];
52@if(!SKINS_USE_UNIFORM_BUFFERS)
53 let weights_offset = morph_descriptors[descriptor_index].current_weights_offset;
54@if(!SKINS_USE_UNIFORM_BUFFERS)
55 return morph_weights[weights_offset + weight_index];
58@if(MORPH_TARGETS)
59fn prev_weight_at(weight_index: u32, descriptor_index: u32) -> f32 {
60@if(SKINS_USE_UNIFORM_BUFFERS)
61 let i = weight_index;
62@if(SKINS_USE_UNIFORM_BUFFERS)
63 return prev_morph_weights.weights[i / 4u][i % 4u];
64@if(!SKINS_USE_UNIFORM_BUFFERS)
65 let weights_offset = morph_descriptors[descriptor_index].prev_weights_offset;
66@if(!SKINS_USE_UNIFORM_BUFFERS)
67 return prev_morph_weights[weights_offset + weight_index];
70@if(MORPH_TARGETS && SKINS_USE_UNIFORM_BUFFERS)
71fn morph_pixel(vertex: u32, component: u32, weight: u32) -> f32 {
72 let coord = component_texture_coord(vertex, component);
73 // Due to https://gpuweb.github.io/gpuweb/wgsl/#texel-formats
74 // While the texture stores a f32, the textureLoad returns a vec4<>, where
75 // only the first component is set.
76 return textureLoad(morph_targets, vec3(coord, weight), 0).r;
79@if(MORPH_TARGETS && SKINS_USE_UNIFORM_BUFFERS)
80fn morph(vertex_index: u32, component_offset: u32, weight_index: u32) -> vec3<f32> {
81 return vec3<f32>(
82 morph_pixel(vertex_index, component_offset, weight_index),
83 morph_pixel(vertex_index, component_offset + 1u, weight_index),
84 morph_pixel(vertex_index, component_offset + 2u, weight_index),
85 );
88@if(MORPH_TARGETS && SKINS_USE_UNIFORM_BUFFERS)
89fn morph_position(vertex_index: u32, weight_index: u32, instance_index: u32) -> vec3<f32> {
90 return morph(vertex_index, position_offset, weight_index);
93@if(MORPH_TARGETS && SKINS_USE_UNIFORM_BUFFERS)
94fn morph_normal(vertex_index: u32, weight_index: u32, instance_index: u32) -> vec3<f32> {
95 return morph(vertex_index, normal_offset, weight_index);
98@if(MORPH_TARGETS && SKINS_USE_UNIFORM_BUFFERS)
99fn morph_tangent(vertex_index: u32, weight_index: u32, instance_index: u32) -> vec3<f32> {
100 return morph(vertex_index, tangent_offset, weight_index);
103@if(MORPH_TARGETS && !SKINS_USE_UNIFORM_BUFFERS)
104fn get_morph_target(vertex_index: u32, weight_index: u32, descriptor_index: u32) -> MorphAttributes {
105 let targets_offset = morph_descriptors[descriptor_index].targets_offset;
106 let vertex_count = morph_descriptors[descriptor_index].vertex_count;
107 return morph_targets[targets_offset + weight_index * vertex_count + vertex_index];
110@if(MORPH_TARGETS && !SKINS_USE_UNIFORM_BUFFERS)
111fn morph_position(vertex_index: u32, weight_index: u32, descriptor_index: u32) -> vec3<f32> {
112 return get_morph_target(vertex_index, weight_index, descriptor_index).position;
115@if(MORPH_TARGETS && !SKINS_USE_UNIFORM_BUFFERS)
116fn morph_normal(vertex_index: u32, weight_index: u32, descriptor_index: u32) -> vec3<f32> {
117 return get_morph_target(vertex_index, weight_index, descriptor_index).normal;
120@if(MORPH_TARGETS && !SKINS_USE_UNIFORM_BUFFERS)
121fn morph_tangent(vertex_index: u32, weight_index: u32, descriptor_index: u32) -> vec3<f32> {
122 return get_morph_target(vertex_index, weight_index, descriptor_index).tangent;
Contents