Skip to content

Commit 44a9a4c

Browse files
authored
Import the second UV map if present in glTF files. (#9992)
Conventionally, the second UV map (`TEXCOORD1`, `UV1`) is used for lightmap UVs. This commit allows Bevy to import them, so that a custom shader that applies lightmaps can use those UVs if desired. Note that this doesn't actually apply lightmaps to Bevy meshes; that will be a followup. It does, however, open the door to future Bevy plugins that implement baked global illumination. ## Changelog ### Added The Bevy glTF loader now imports a second UV channel (`TEXCOORD1`, `UV1`) from meshes if present. This can be used by custom shaders to implement lightmapping.
1 parent 0196494 commit 44a9a4c

File tree

4 files changed

+26
-11
lines changed

4 files changed

+26
-11
lines changed

crates/bevy_gltf/src/vertex_attributes.rs

+1
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,7 @@ pub(crate) fn convert_attribute(
263263
gltf::Semantic::Tangents => Some((Mesh::ATTRIBUTE_TANGENT, ConversionMode::Any)),
264264
gltf::Semantic::Colors(0) => Some((Mesh::ATTRIBUTE_COLOR, ConversionMode::Rgba)),
265265
gltf::Semantic::TexCoords(0) => Some((Mesh::ATTRIBUTE_UV_0, ConversionMode::TexCoord)),
266+
gltf::Semantic::TexCoords(1) => Some((Mesh::ATTRIBUTE_UV_1, ConversionMode::TexCoord)),
266267
gltf::Semantic::Joints(0) => {
267268
Some((Mesh::ATTRIBUTE_JOINT_INDEX, ConversionMode::JointIndex))
268269
}

crates/bevy_pbr/src/render/mesh.rs

+8-3
Original file line numberDiff line numberDiff line change
@@ -779,14 +779,19 @@ impl SpecializedMeshPipeline for MeshPipeline {
779779
vertex_attributes.push(Mesh::ATTRIBUTE_UV_0.at_shader_location(2));
780780
}
781781

782+
if layout.contains(Mesh::ATTRIBUTE_UV_1) {
783+
shader_defs.push("VERTEX_UVS_1".into());
784+
vertex_attributes.push(Mesh::ATTRIBUTE_UV_1.at_shader_location(3));
785+
}
786+
782787
if layout.contains(Mesh::ATTRIBUTE_TANGENT) {
783788
shader_defs.push("VERTEX_TANGENTS".into());
784-
vertex_attributes.push(Mesh::ATTRIBUTE_TANGENT.at_shader_location(3));
789+
vertex_attributes.push(Mesh::ATTRIBUTE_TANGENT.at_shader_location(4));
785790
}
786791

787792
if layout.contains(Mesh::ATTRIBUTE_COLOR) {
788793
shader_defs.push("VERTEX_COLORS".into());
789-
vertex_attributes.push(Mesh::ATTRIBUTE_COLOR.at_shader_location(4));
794+
vertex_attributes.push(Mesh::ATTRIBUTE_COLOR.at_shader_location(5));
790795
}
791796

792797
let mut bind_group_layout = match key.msaa_samples() {
@@ -800,7 +805,7 @@ impl SpecializedMeshPipeline for MeshPipeline {
800805
bind_group_layout.push(setup_morph_and_skinning_defs(
801806
&self.mesh_layouts,
802807
layout,
803-
5,
808+
6,
804809
&key,
805810
&mut shader_defs,
806811
&mut vertex_attributes,

crates/bevy_pbr/src/render/mesh.wgsl

+5-4
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,16 @@ struct Vertex {
1616
#ifdef VERTEX_UVS
1717
@location(2) uv: vec2<f32>,
1818
#endif
19+
// (Alternate UVs are at location 3, but they're currently unused here.)
1920
#ifdef VERTEX_TANGENTS
20-
@location(3) tangent: vec4<f32>,
21+
@location(4) tangent: vec4<f32>,
2122
#endif
2223
#ifdef VERTEX_COLORS
23-
@location(4) color: vec4<f32>,
24+
@location(5) color: vec4<f32>,
2425
#endif
2526
#ifdef SKINNED
26-
@location(5) joint_indices: vec4<u32>,
27-
@location(6) joint_weights: vec4<f32>,
27+
@location(6) joint_indices: vec4<u32>,
28+
@location(7) joint_weights: vec4<f32>,
2829
#endif
2930
#ifdef MORPH_TARGETS
3031
@builtin(vertex_index) index: u32,

crates/bevy_render/src/mesh/mesh/mod.rs

+12-4
Original file line numberDiff line numberDiff line change
@@ -146,22 +146,30 @@ impl Mesh {
146146
pub const ATTRIBUTE_UV_0: MeshVertexAttribute =
147147
MeshVertexAttribute::new("Vertex_Uv", 2, VertexFormat::Float32x2);
148148

149+
/// Alternate texture coordinates for the vertex. Use in conjunction with
150+
/// [`Mesh::insert_attribute`].
151+
///
152+
/// Typically, these are used for lightmaps, textures that provide
153+
/// precomputed illumination.
154+
pub const ATTRIBUTE_UV_1: MeshVertexAttribute =
155+
MeshVertexAttribute::new("Vertex_Uv_1", 3, VertexFormat::Float32x2);
156+
149157
/// The direction of the vertex tangent. Used for normal mapping.
150158
/// Usually generated with [`generate_tangents`](Mesh::generate_tangents).
151159
pub const ATTRIBUTE_TANGENT: MeshVertexAttribute =
152-
MeshVertexAttribute::new("Vertex_Tangent", 3, VertexFormat::Float32x4);
160+
MeshVertexAttribute::new("Vertex_Tangent", 4, VertexFormat::Float32x4);
153161

154162
/// Per vertex coloring. Use in conjunction with [`Mesh::insert_attribute`].
155163
pub const ATTRIBUTE_COLOR: MeshVertexAttribute =
156-
MeshVertexAttribute::new("Vertex_Color", 4, VertexFormat::Float32x4);
164+
MeshVertexAttribute::new("Vertex_Color", 5, VertexFormat::Float32x4);
157165

158166
/// Per vertex joint transform matrix weight. Use in conjunction with [`Mesh::insert_attribute`].
159167
pub const ATTRIBUTE_JOINT_WEIGHT: MeshVertexAttribute =
160-
MeshVertexAttribute::new("Vertex_JointWeight", 5, VertexFormat::Float32x4);
168+
MeshVertexAttribute::new("Vertex_JointWeight", 6, VertexFormat::Float32x4);
161169

162170
/// Per vertex joint transform matrix index. Use in conjunction with [`Mesh::insert_attribute`].
163171
pub const ATTRIBUTE_JOINT_INDEX: MeshVertexAttribute =
164-
MeshVertexAttribute::new("Vertex_JointIndex", 6, VertexFormat::Uint16x4);
172+
MeshVertexAttribute::new("Vertex_JointIndex", 7, VertexFormat::Uint16x4);
165173

166174
/// Construct a new mesh. You need to provide a [`PrimitiveTopology`] so that the
167175
/// renderer knows how to treat the vertex data. Most of the time this will be

0 commit comments

Comments
 (0)