Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for KHR_texture_transform #11904

Merged
merged 18 commits into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions crates/bevy_gltf/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ gltf = { version = "1.4.0", default-features = false, features = [
"KHR_materials_volume",
"KHR_materials_unlit",
"KHR_materials_emissive_strength",
"KHR_texture_transform",
"extras",
"extensions",
"names",
Expand Down
25 changes: 23 additions & 2 deletions crates/bevy_gltf/src/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use bevy_ecs::entity::EntityHashMap;
use bevy_ecs::{entity::Entity, world::World};
use bevy_hierarchy::{BuildWorldChildren, WorldChildBuilder};
use bevy_log::{error, warn};
use bevy_math::{Mat4, Vec3};
use bevy_math::{Mat3, Mat4, Vec3};
use bevy_pbr::{
AlphaMode, DirectionalLight, DirectionalLightBundle, PbrBundle, PointLight, PointLightBundle,
SpotLight, SpotLightBundle, StandardMaterial, MAX_JOINTS,
Expand Down Expand Up @@ -38,7 +38,7 @@ use bevy_utils::{HashMap, HashSet};
use gltf::{
accessor::Iter,
mesh::{util::ReadIndices, Mode},
texture::{MagFilter, MinFilter, WrappingMode},
texture::{MagFilter, MinFilter, TextureTransform, WrappingMode},
Material, Node, Primitive, Semantic,
};
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -791,6 +791,15 @@ async fn load_image<'a, 'b>(
}
}

/// Converts [`TextureTransform`] to [`Mat3`].
fn texture_transform_mat3(texture_transform: TextureTransform) -> Mat3 {
Mat3::from_scale_angle_translation(
texture_transform.scale().into(),
-texture_transform.rotation(),
texture_transform.offset().into(),
)
}

/// Loads a glTF material as a bevy [`StandardMaterial`] and returns it.
fn load_material(
material: &Material,
Expand All @@ -808,6 +817,11 @@ fn load_material(
texture_handle(load_context, &info.texture())
});

let uv_transform = pbr
.base_color_texture()
.and_then(|info| info.texture_transform().map(texture_transform_mat3))
.unwrap_or_default();

let normal_map_texture: Option<Handle<Image>> =
material.normal_texture().map(|normal_texture| {
// TODO: handle normal_texture.scale
Expand All @@ -817,6 +831,9 @@ fn load_material(

let metallic_roughness_texture = pbr.metallic_roughness_texture().map(|info| {
// TODO: handle info.tex_coord() (the *set* index for the right texcoords)
if info.texture_transform().map(texture_transform_mat3) != Some(uv_transform) {
warn!("Only the texture transform on the base color texture is supported, ignoring the texture transform on the metallic/roughness texture");
janhohenheim marked this conversation as resolved.
Show resolved Hide resolved
}
texture_handle(load_context, &info.texture())
});

Expand All @@ -830,6 +847,9 @@ fn load_material(
let emissive_texture = material.emissive_texture().map(|info| {
// TODO: handle occlusion_texture.tex_coord() (the *set* index for the right texcoords)
// TODO: handle occlusion_texture.strength() (a scalar multiplier for occlusion strength)
if info.texture_transform().map(texture_transform_mat3) != Some(uv_transform) {
warn!("Only the texture transform on the base color texture is supported, ignoring the texture transform on the emissive texture");
}
texture_handle(load_context, &info.texture())
});

Expand Down Expand Up @@ -917,6 +937,7 @@ fn load_material(
),
unlit: material.unlit(),
alpha_mode: alpha_mode(material),
uv_transform,
..Default::default()
}
})
Expand Down
9 changes: 8 additions & 1 deletion crates/bevy_pbr/src/pbr_material.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use bevy_asset::{Asset, Handle};
use bevy_math::Vec4;
use bevy_math::{Mat3, Vec4};
use bevy_reflect::{std_traits::ReflectDefault, Reflect};
use bevy_render::{
color::Color, mesh::MeshVertexBufferLayout, render_asset::RenderAssets, render_resource::*,
Expand Down Expand Up @@ -472,6 +472,9 @@ pub struct StandardMaterial {
/// Default is [`DEFAULT_PBR_DEFERRED_LIGHTING_PASS_ID`] for default
/// PBR deferred lighting pass. Ignored in the case of forward materials.
pub deferred_lighting_pass_id: u8,

/// Texture UV transform.
janhohenheim marked this conversation as resolved.
Show resolved Hide resolved
pub uv_transform: Mat3,
janhohenheim marked this conversation as resolved.
Show resolved Hide resolved
}

impl Default for StandardMaterial {
Expand Down Expand Up @@ -520,6 +523,7 @@ impl Default for StandardMaterial {
parallax_mapping_method: ParallaxMappingMethod::Occlusion,
opaque_render_method: OpaqueRendererMethod::Auto,
deferred_lighting_pass_id: DEFAULT_PBR_DEFERRED_LIGHTING_PASS_ID,
uv_transform: Mat3::IDENTITY,
}
}
}
Expand Down Expand Up @@ -632,6 +636,8 @@ pub struct StandardMaterialUniform {
pub max_relief_mapping_search_steps: u32,
/// ID for specifying which deferred lighting pass should be used for rendering this material, if any.
pub deferred_lighting_pass_id: u32,
/// Texture UV transform.
pub uv_transform: Mat3,
}

impl AsBindGroupShaderType<StandardMaterialUniform> for StandardMaterial {
Expand Down Expand Up @@ -729,6 +735,7 @@ impl AsBindGroupShaderType<StandardMaterialUniform> for StandardMaterial {
lightmap_exposure: self.lightmap_exposure,
max_relief_mapping_search_steps: self.parallax_mapping_method.max_steps(),
deferred_lighting_pass_id: self.deferred_lighting_pass_id as u32,
uv_transform: self.uv_transform,
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_pbr/src/render/pbr_fragment.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ fn pbr_input_from_standard_material(
let NdotV = max(dot(pbr_input.N, pbr_input.V), 0.0001);

#ifdef VERTEX_UVS
var uv = in.uv;
var uv = (pbr_bindings::material.uv_transform * vec3(in.uv, 1.0)).xy;
janhohenheim marked this conversation as resolved.
Show resolved Hide resolved

#ifdef VERTEX_TANGENTS
if ((pbr_bindings::material.flags & pbr_types::STANDARD_MATERIAL_FLAGS_DEPTH_MAP_BIT) != 0u) {
Expand Down
3 changes: 2 additions & 1 deletion crates/bevy_pbr/src/render/pbr_prepass_functions.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ fn prepass_alpha_discard(in: VertexOutput) {
var output_color: vec4<f32> = pbr_bindings::material.base_color;

#ifdef VERTEX_UVS
let uv = (pbr_bindings::material.uv_transform * vec3(in.uv, 1.0)).xy;
if (pbr_bindings::material.flags & pbr_types::STANDARD_MATERIAL_FLAGS_BASE_COLOR_TEXTURE_BIT) != 0u {
output_color = output_color * textureSampleBias(pbr_bindings::base_color_texture, pbr_bindings::base_color_sampler, in.uv, view.mip_bias);
output_color = output_color * textureSampleBias(pbr_bindings::base_color_texture, pbr_bindings::base_color_sampler, uv, view.mip_bias);
}
#endif // VERTEX_UVS

Expand Down
2 changes: 2 additions & 0 deletions crates/bevy_pbr/src/render/pbr_types.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ struct StandardMaterial {
max_relief_mapping_search_steps: u32,
/// ID for specifying which deferred lighting pass should be used for rendering this material, if any.
deferred_lighting_pass_id: u32,
uv_transform: mat3x3<f32>,
};

// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Expand Down Expand Up @@ -74,6 +75,7 @@ fn standard_material_new() -> StandardMaterial {
material.max_parallax_layer_count = 16.0;
material.max_relief_mapping_search_steps = 5u;
material.deferred_lighting_pass_id = 1u;
material.uv_transform = mat3x3<f32>(1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0);

return material;
}
Expand Down
Loading