11#import bevy_render :: view :: View
22#import bevy_render :: maths
3- #import bevy_pbr :: mesh_types :: SkinnedMesh
3+ #import bevy_pbr :: mesh_types :: { SkinnedMesh , MorphAttributes , MorphDescriptor , MorphWeights }
44#import bevy_pbr :: skinning :: joint_matrices
55#import bevy_mod_outline :: common :: {OutlineViewUniform , VertexOutput }
66
@@ -39,7 +39,6 @@ struct Vertex {
3939var <uniform > view_uniform : OutlineViewUniform ;
4040
4141#import bevy_pbr :: skinning
42- #import bevy_pbr :: morph
4342
4443#ifdef INSTANCE_BATCH_SIZE
4544@group (1 ) @binding (0 ) var <uniform > mesh : array <Instance , #{INSTANCE_BATCH_SIZE }u >;
@@ -48,19 +47,85 @@ var<uniform> view_uniform: OutlineViewUniform;
4847#endif
4948
5049#ifdef MORPH_TARGETS
50+ #ifdef SKINS_USE_UNIFORM_BUFFERS
51+ @group (2 ) @binding (2 ) var <uniform > morph_weights : MorphWeights ;
52+ @group (2 ) @binding (3 ) var morph_targets : texture_3d <f32 >;
53+ #else
54+ @group (2 ) @binding (2 ) var <storage > morph_weights : array <f32 >;
55+ @group (2 ) @binding (3 ) var <storage > morph_targets : array <MorphAttributes >;
56+ @group (2 ) @binding (8 ) var <storage > morph_descriptors : array <MorphDescriptor >;
57+ #endif
58+
59+ fn morph_layer_count (descriptor_index : u32 ) -> u32 {
60+ #ifdef SKINS_USE_UNIFORM_BUFFERS
61+ let dimensions = textureDimensions (morph_targets );
62+ return u32 (dimensions . z );
63+ #else
64+ return morph_descriptors [descriptor_index ]. weight_count ;
65+ #endif
66+ }
67+
68+ fn morph_weight_at (weight_index : u32 , descriptor_index : u32 ) -> f32 {
69+ #ifdef SKINS_USE_UNIFORM_BUFFERS
70+ let i = weight_index ;
71+ return morph_weights . weights [i / 4u ][i % 4u ];
72+ #else
73+ let weights_offset = morph_descriptors [descriptor_index ]. current_weights_offset ;
74+ return morph_weights [weights_offset + weight_index ];
75+ #endif
76+ }
77+
78+ #ifdef SKINS_USE_UNIFORM_BUFFERS
79+ const morph_position_offset : u32 = 0u ;
80+ const morph_total_component_count : u32 = 9u ;
81+
82+ fn morph_component_texture_coord (vertex_index : u32 , component_offset : u32 ) -> vec2 <u32 > {
83+ let width = u32 (textureDimensions (morph_targets ). x );
84+ let component_index = morph_total_component_count * vertex_index + component_offset ;
85+ return vec2 <u32 >(component_index % width , component_index / width );
86+ }
87+
88+ fn morph_pixel (vertex : u32 , component : u32 , weight : u32 ) -> f32 {
89+ let coord = morph_component_texture_coord (vertex , component );
90+ return textureLoad (morph_targets , vec3 (coord , weight ), 0 ). r ;
91+ }
92+
93+ fn morph (vertex_index : u32 , component_offset : u32 , weight_index : u32 ) -> vec3 <f32 > {
94+ return vec3 <f32 >(
95+ morph_pixel (vertex_index , component_offset , weight_index ),
96+ morph_pixel (vertex_index , component_offset + 1u , weight_index ),
97+ morph_pixel (vertex_index , component_offset + 2u , weight_index ),
98+ );
99+ }
100+
101+ fn morph_position (vertex_index : u32 , weight_index : u32 , instance_index : u32 ) -> vec3 <f32 > {
102+ return morph (vertex_index , morph_position_offset , weight_index );
103+ }
104+ #else
105+ fn get_morph_target (vertex_index : u32 , weight_index : u32 , descriptor_index : u32 ) -> MorphAttributes {
106+ let targets_offset = morph_descriptors [descriptor_index ]. targets_offset ;
107+ let vertex_count = morph_descriptors [descriptor_index ]. vertex_count ;
108+ return morph_targets [targets_offset + weight_index * vertex_count + vertex_index ];
109+ }
110+
111+ fn 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 ;
113+ }
114+ #endif
115+
51116fn morph_vertex (vertex_in : Vertex , instance_index : u32 ) -> Vertex {
52117 var vertex = vertex_in ;
53118 let first_vertex = mesh [instance_index ]. first_vertex_index ;
54119 var morph_index = mesh [instance_index ]. current_morph_index ;
55120 let vertex_index = vertex . index - first_vertex ;
56121
57- let weight_count = bevy_pbr :: morph :: layer_count (morph_index );
122+ let weight_count = morph_layer_count (morph_index );
58123 for (var i : u32 = 0u ; i < weight_count ; i ++ ) {
59- let weight = bevy_pbr :: morph :: weight_at (i , morph_index );
124+ let weight = morph_weight_at (i , morph_index );
60125 if weight == 0 .0 {
61126 continue ;
62127 }
63- vertex . position += weight * bevy_pbr :: morph :: morph_position (vertex_index , i , morph_index );
128+ vertex . position += weight * morph_position (vertex_index , i , morph_index );
64129 }
65130 return vertex ;
66131}
0 commit comments