@@ -1010,18 +1010,6 @@ impl Default for StageLegend {
1010
1010
}
1011
1011
}
1012
1012
1013
- #[ cfg_attr( not( target_arch = "spirv" ) , derive( Debug ) ) ]
1014
- #[ repr( C ) ]
1015
- #[ derive( Default , Clone , Copy , PartialEq , Slabbed ) ]
1016
- pub struct NativeVertexData {
1017
- // Points to an array of `Vertex` in the stage's slab.
1018
- pub vertices : Array < Vertex > ,
1019
- // Points to an array of `Indices` in the stage's slab.
1020
- pub indices : Array < u32 > ,
1021
- // Points to a `PbrMaterial` in the stage's slab.
1022
- pub material : Id < PbrMaterial > ,
1023
- }
1024
-
1025
1013
#[ cfg_attr( not( target_arch = "spirv" ) , derive( Debug ) ) ]
1026
1014
#[ repr( C ) ]
1027
1015
#[ derive( Default , Clone , Copy , PartialEq , Slabbed ) ]
@@ -1034,53 +1022,6 @@ pub struct GltfVertexData {
1034
1022
pub primitive_index : u32 ,
1035
1023
}
1036
1024
1037
- #[ repr( C ) ]
1038
- #[ cfg_attr( not( target_arch = "spirv" ) , derive( Debug ) ) ]
1039
- #[ derive( Clone , Copy , PartialEq , Slabbed ) ]
1040
- pub struct VertexData {
1041
- // The hash of the vertex data. This is used to determine how to read
1042
- // the vertex data from the slab.
1043
- pub hash : u32 ,
1044
- // The first index of the vertex data in the slab.
1045
- pub index : u32 ,
1046
- }
1047
-
1048
- impl Default for VertexData {
1049
- fn default ( ) -> Self {
1050
- VertexData {
1051
- hash : 0 ,
1052
- index : u32:: MAX ,
1053
- }
1054
- }
1055
- }
1056
-
1057
- impl VertexData {
1058
- pub const NATIVE : u32 = 0 ;
1059
- pub const GLTF : u32 = 1 ;
1060
-
1061
- pub fn new_native ( id : Id < NativeVertexData > ) -> Self {
1062
- Self {
1063
- hash : Self :: NATIVE ,
1064
- index : id. into ( ) ,
1065
- }
1066
- }
1067
-
1068
- pub fn new_gltf ( id : Id < GltfVertexData > ) -> Self {
1069
- Self {
1070
- hash : Self :: GLTF ,
1071
- index : id. into ( ) ,
1072
- }
1073
- }
1074
-
1075
- pub fn is_native ( & self ) -> bool {
1076
- self . hash == Self :: NATIVE
1077
- }
1078
-
1079
- pub fn is_gltf ( & self ) -> bool {
1080
- self . hash == Self :: GLTF
1081
- }
1082
- }
1083
-
1084
1025
#[ cfg_attr( not( target_arch = "spirv" ) , derive( Debug ) ) ]
1085
1026
#[ repr( C ) ]
1086
1027
#[ derive( Clone , Copy , PartialEq , Slabbed ) ]
@@ -1100,18 +1041,27 @@ impl Default for Transform {
1100
1041
}
1101
1042
}
1102
1043
1103
- /// A fully-computed unit of rendering, roughly meaning a mesh with model matrix
1104
- /// transformations.
1044
+ /// A rendering "command" that draws a single mesh from a top-level node.
1105
1045
#[ cfg_attr( not( target_arch = "spirv" ) , derive( Debug ) ) ]
1106
1046
#[ repr( C ) ]
1107
1047
#[ derive( Default , Clone , Copy , PartialEq , Slabbed ) ]
1108
1048
pub struct RenderUnit {
1109
- pub vertex_data : VertexData ,
1049
+ // Which node are we rendering, and what is the path through its
1050
+ // ancestors to get to it.
1051
+ pub node_path : Array < Id < GltfNode > > ,
1052
+ // Index of the mesh within the child node that we're rendering.
1053
+ pub mesh_index : u32 ,
1054
+ // Index of the primitive within the mesh that we're rendering.
1055
+ pub primitive_index : u32 ,
1110
1056
// Points to a `Camera` in the stage's slab.
1111
1057
pub camera : Id < Camera > ,
1112
- // Points to a `Transform` in the stage's slab.
1058
+ // Points to a top-level `Transform` in the stage's slab.
1059
+ //
1060
+ // This is used to transform your GLTF models.
1113
1061
pub transform : Id < Transform > ,
1114
1062
// Number of vertices to draw for this unit.
1063
+ //
1064
+ // This is a cache for convenience on CPU.
1115
1065
pub vertex_count : u32 ,
1116
1066
}
1117
1067
@@ -1121,45 +1071,34 @@ impl RenderUnit {
1121
1071
vertex_index : u32 ,
1122
1072
slab : & [ u32 ] ,
1123
1073
) -> ( Vertex , Transform , Id < PbrMaterial > ) {
1124
- let transform = slab. read ( self . transform ) ;
1125
- match self . vertex_data . hash {
1126
- VertexData :: NATIVE => {
1127
- let id = Id :: < NativeVertexData > :: from ( self . vertex_data . index ) ;
1128
- let NativeVertexData {
1129
- vertices,
1130
- indices,
1131
- material,
1132
- } = slab. read ( id) ;
1133
- let index = if indices. is_empty ( ) {
1134
- vertex_index
1135
- } else {
1136
- slab. read ( indices. at ( vertex_index as usize ) )
1137
- } ;
1138
- let vertex = slab. read ( vertices. at ( index as usize ) ) ;
1139
- ( vertex, transform, material)
1140
- }
1141
- VertexData :: GLTF => {
1142
- let id = Id :: < GltfVertexData > :: from ( self . vertex_data . index ) ;
1143
- // TODO: Take the GLTF parent node's transform into account
1144
- let GltfVertexData {
1145
- parent_node_path : _,
1146
- mesh,
1147
- primitive_index,
1148
- } = slab. read ( id) ;
1149
- // TODO: check nodes for skinning
1150
- let mesh = slab. read ( mesh) ;
1151
- let primitive = slab. read ( mesh. primitives . at ( primitive_index as usize ) ) ;
1152
- let material = primitive. material ;
1153
- let vertex = primitive. get_vertex ( vertex_index as usize , slab) ;
1154
- ( vertex, transform, material)
1155
- }
1156
- _ => Default :: default ( ) ,
1074
+ let t = slab. read ( self . transform ) ;
1075
+ let mut model = Mat4 :: from_scale_rotation_translation ( t. scale , t. rotation , t. translation ) ;
1076
+ let mut node = GltfNode :: default ( ) ;
1077
+ for id_id in self . node_path . iter ( ) {
1078
+ let node_id = slab. read ( id_id) ;
1079
+ node = slab. read ( node_id) ;
1080
+ let node_transform =
1081
+ Mat4 :: from_scale_rotation_translation ( node. scale , node. rotation , node. translation ) ;
1082
+ model = model * node_transform;
1157
1083
}
1084
+ // TODO: check nodes for skinning
1085
+ let mesh = slab. read ( node. mesh ) ;
1086
+ let primitive_id = mesh. primitives . at ( self . primitive_index as usize ) ;
1087
+ let primitive = slab. read ( primitive_id) ;
1088
+ let material = primitive. material ;
1089
+ let vertex = primitive. get_vertex ( vertex_index as usize , slab) ;
1090
+ let ( s, r, t) = model. to_scale_rotation_translation_or_id ( ) ;
1091
+ let transform = Transform {
1092
+ translation : t,
1093
+ rotation : r,
1094
+ scale : s,
1095
+ } ;
1096
+ ( vertex, transform, material)
1158
1097
}
1159
1098
}
1160
1099
1161
1100
#[ spirv( vertex) ]
1162
- pub fn new_stage_vertex (
1101
+ pub fn gltf_vertex (
1163
1102
// Which render unit are we rendering
1164
1103
#[ spirv( instance_index) ] instance_index : u32 ,
1165
1104
// Which vertex within the render unit are we rendering
@@ -1234,7 +1173,7 @@ pub fn get_material(material_index: u32, has_lighting: bool, slab: &[u32]) -> pb
1234
1173
#[ allow( clippy:: too_many_arguments) ]
1235
1174
#[ spirv( fragment) ]
1236
1175
/// Scene fragment shader.
1237
- pub fn stage_fragment (
1176
+ pub fn gltf_fragment (
1238
1177
#[ spirv( descriptor_set = 1 , binding = 0 ) ] atlas : & Image2d ,
1239
1178
#[ spirv( descriptor_set = 1 , binding = 1 ) ] atlas_sampler : & Sampler ,
1240
1179
@@ -1262,7 +1201,7 @@ pub fn stage_fragment(
1262
1201
output : & mut Vec4 ,
1263
1202
brigtness : & mut Vec4 ,
1264
1203
) {
1265
- stage_fragment_impl (
1204
+ gltf_fragment_impl (
1266
1205
atlas,
1267
1206
atlas_sampler,
1268
1207
irradiance,
@@ -1288,7 +1227,7 @@ pub fn stage_fragment(
1288
1227
1289
1228
#[ allow( clippy:: too_many_arguments) ]
1290
1229
/// Scene fragment shader.
1291
- pub fn stage_fragment_impl < T , C , S > (
1230
+ pub fn gltf_fragment_impl < T , C , S > (
1292
1231
atlas : & T ,
1293
1232
atlas_sampler : & S ,
1294
1233
irradiance : & C ,
0 commit comments