Add support for KHR_texture_transform
This commit is contained in:
@@ -55,6 +55,14 @@ struct texture
|
||||
|
||||
struct material
|
||||
{
|
||||
struct alignas(16) texture_info
|
||||
{
|
||||
alignas(4) uint32_t texcoord = 0;
|
||||
alignas(4) float rotation = 0;
|
||||
alignas(8) glm::vec2 offset = {0, 0};
|
||||
alignas(8) glm::vec2 scale = {1, 1};
|
||||
};
|
||||
|
||||
struct gpu_data
|
||||
{
|
||||
glm::vec4 base_color_factor = glm::vec4(1, 1, 1, 1);
|
||||
@@ -65,13 +73,11 @@ struct material
|
||||
float normal_scale = 1;
|
||||
float alpha_cutoff = 0.5;
|
||||
|
||||
uint32_t base_color_texcoord = 0;
|
||||
uint32_t metallic_roughness_texcoord = 0;
|
||||
uint32_t occlusion_texcoord = 0;
|
||||
uint32_t emissive_texcoord = 0;
|
||||
uint32_t normal_texcoord = 0;
|
||||
|
||||
// TODO: add fastgltf::TextureTransform?
|
||||
texture_info base_color;
|
||||
texture_info metallic_roughness;
|
||||
texture_info occlusion;
|
||||
texture_info emissive;
|
||||
texture_info normal;
|
||||
};
|
||||
|
||||
std::shared_ptr<texture> base_color_texture;
|
||||
|
||||
@@ -168,6 +168,11 @@ glm::vec3 convert(const fastgltf::math::nvec3 & v)
|
||||
return {v[0], v[1], v[2]};
|
||||
}
|
||||
|
||||
glm::vec2 convert(const fastgltf::math::nvec2 & v)
|
||||
{
|
||||
return {v[0], v[1]};
|
||||
}
|
||||
|
||||
components::animation_track_base::interpolation_t convert(fastgltf::AnimationInterpolation interpolation)
|
||||
{
|
||||
switch (interpolation)
|
||||
@@ -443,7 +448,9 @@ struct : std::error_category
|
||||
|
||||
fastgltf::Asset load_gltf_asset(fastgltf::GltfDataBuffer & buffer, const std::filesystem::path & directory)
|
||||
{
|
||||
fastgltf::Parser parser(fastgltf::Extensions::KHR_texture_basisu);
|
||||
fastgltf::Parser parser(
|
||||
fastgltf::Extensions::KHR_texture_basisu |
|
||||
fastgltf::Extensions::KHR_texture_transform);
|
||||
|
||||
auto gltf_options =
|
||||
fastgltf::Options::DontRequireValidAssetMember |
|
||||
@@ -752,37 +759,37 @@ public:
|
||||
material_data.roughness_factor = gltf_material.pbrData.roughnessFactor;
|
||||
material_data.alpha_cutoff = gltf_material.alphaCutoff;
|
||||
|
||||
if (gltf_material.pbrData.baseColorTexture)
|
||||
{
|
||||
material.base_color_texture = textures.at(gltf_material.pbrData.baseColorTexture->textureIndex);
|
||||
material_data.base_color_texcoord = gltf_material.pbrData.baseColorTexture->texCoordIndex;
|
||||
}
|
||||
auto f = [&](const auto & texture_info,
|
||||
std::shared_ptr<renderer::texture> & texture,
|
||||
renderer::material::texture_info & info) {
|
||||
if (not texture_info)
|
||||
return;
|
||||
|
||||
if (gltf_material.pbrData.metallicRoughnessTexture)
|
||||
{
|
||||
material.metallic_roughness_texture = textures.at(gltf_material.pbrData.metallicRoughnessTexture->textureIndex);
|
||||
material_data.metallic_roughness_texcoord = gltf_material.pbrData.metallicRoughnessTexture->texCoordIndex;
|
||||
}
|
||||
texture = textures.at(texture_info->textureIndex);
|
||||
info.texcoord = texture_info->texCoordIndex;
|
||||
|
||||
if (auto & xform = texture_info->transform)
|
||||
{
|
||||
if (xform->texCoordIndex)
|
||||
info.texcoord = *xform->texCoordIndex;
|
||||
|
||||
info.rotation = xform->rotation;
|
||||
info.offset = convert(xform->uvOffset);
|
||||
info.scale = convert(xform->uvScale);
|
||||
}
|
||||
};
|
||||
|
||||
f(gltf_material.pbrData.baseColorTexture, material.base_color_texture, material_data.base_color);
|
||||
f(gltf_material.pbrData.metallicRoughnessTexture, material.metallic_roughness_texture, material_data.metallic_roughness);
|
||||
f(gltf_material.occlusionTexture, material.occlusion_texture, material_data.occlusion);
|
||||
f(gltf_material.emissiveTexture, material.emissive_texture, material_data.emissive);
|
||||
f(gltf_material.normalTexture, material.normal_texture, material_data.normal);
|
||||
|
||||
if (gltf_material.occlusionTexture)
|
||||
{
|
||||
material.occlusion_texture = textures.at(gltf_material.occlusionTexture->textureIndex);
|
||||
material_data.occlusion_texcoord = gltf_material.occlusionTexture->texCoordIndex;
|
||||
material_data.occlusion_strength = gltf_material.occlusionTexture->strength;
|
||||
}
|
||||
|
||||
if (gltf_material.emissiveTexture)
|
||||
{
|
||||
material.emissive_texture = textures.at(gltf_material.emissiveTexture->textureIndex);
|
||||
material_data.emissive_texcoord = gltf_material.emissiveTexture->texCoordIndex;
|
||||
}
|
||||
|
||||
if (gltf_material.normalTexture)
|
||||
{
|
||||
material.normal_texture = textures.at(gltf_material.normalTexture->textureIndex);
|
||||
material_data.normal_texcoord = gltf_material.normalTexture->texCoordIndex;
|
||||
material_data.normal_scale = gltf_material.normalTexture->scale;
|
||||
}
|
||||
|
||||
material.offset = staging_buffer.add_uniform(material_data);
|
||||
}
|
||||
|
||||
@@ -261,7 +261,7 @@ static std::array layout_bindings_0{
|
||||
.binding = 8,
|
||||
.descriptorType = vk::DescriptorType::eUniformBuffer,
|
||||
.descriptorCount = 1,
|
||||
.stageFlags = vk::ShaderStageFlagBits::eFragment,
|
||||
.stageFlags = vk::ShaderStageFlagBits::eVertex | vk::ShaderStageFlagBits::eFragment,
|
||||
},
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user