Add support for KHR_texture_transform

This commit is contained in:
Guillaume Meunier
2025-12-02 19:57:11 +01:00
parent ab82fb5866
commit f99fb8fa18
6 changed files with 92 additions and 49 deletions
+35 -6
View File
@@ -33,7 +33,16 @@ layout(set = 0, binding = 4) uniform sampler2D metallic_roughness_texture;
layout(set = 0, binding = 5) uniform sampler2D occlusion_texture;
layout(set = 0, binding = 6) uniform sampler2D emissive_texture;
layout(set = 0, binding = 7) uniform sampler2D normal_map_texture;
layout(set = 0, binding = 8) uniform material_ubo
struct texture_info
{
int texcoord;
float rotation;
vec2 offset;
vec2 scale;
};
layout(std140, set = 0, binding = 8) uniform material_ubo
{
vec4 base_color_factor;
vec4 base_emissive_factor;
@@ -43,11 +52,11 @@ layout(set = 0, binding = 8) uniform material_ubo
float normal_scale;
float alpha_cutoff;
int base_color_texcoord;
int metallic_roughness_texcoord;
int occlusion_texcoord;
int emissive_texcoord;
int normal_texcoord;
texture_info base_color;
texture_info metallic_roughness;
texture_info occlusion;
texture_info emissive;
texture_info normal;
} material;
const float dither_pattern[4][4] = {
@@ -91,6 +100,26 @@ vec4 dither(ivec2 frag_coord, vec4 colour)
return colour + one_over_d_linear_to_srgb(colour) * vec4(dither_thd, dither_thd, dither_thd, 0) / 255.0f;
}
vec2 compute_texcoord(texture_info info, vec2 in_texcoord)
{
mat3 translation = mat3(
1, 0, 0,
0, 1, 0,
info.offset.x, info.offset.y, 1);
mat3 rotation = mat3(
cos(info.rotation), sin(info.rotation), 0,
-sin(info.rotation), cos(info.rotation), 0,
0, 0, 1);
mat3 scale = mat3(
info.scale.x, 0, 0,
0, info.scale.y, 0,
0, 0, 1);
mat3 matrix = translation * rotation * scale;
return (matrix * vec3(in_texcoord, 1)).xy;
}
#ifdef FRAG_SHADER
#define VS_TO_FS in
#else
+7 -6
View File
@@ -34,14 +34,15 @@ layout(location = 5) in vec4 in_color;
out gl_PerVertex
{
vec4 gl_Position;
float gl_ClipDistance[nb_clipping];
float gl_ClipDistance[nb_clipping];
};
void main()
{
// TODO: use base_color_texcoord et al instead of always using texcoord 0
for (int i = 0; i < nb_texcoords; i++)
texcoord[i] = in_texcoord[i];
// TODO use base_color_texcoord et al instead of always using texcoord 0
// TODO use the correct transform
texcoord[0] = compute_texcoord(material.base_color, in_texcoord[material.base_color.texcoord]);
// TODO on CPU?
mat3 mv = inverse(transpose(mat3(mesh.modelview[gl_ViewIndex])));
@@ -57,8 +58,8 @@ void main()
frag_pos = mesh.modelview[gl_ViewIndex] * vec4(in_position, 1.0);
light_pos = scene.view[gl_ViewIndex] * scene.light_position;
for(int i = 0; i < nb_clipping; i++)
for (int i = 0; i < nb_clipping; i++)
{
gl_ClipDistance[i] = dot(mesh.clipping_plane[i], mesh.model * vec4(in_position, 1.0));
gl_ClipDistance[i] = dot(mesh.clipping_plane[i], mesh.model * vec4(in_position, 1.0));
}
}
+4 -4
View File
@@ -36,14 +36,14 @@ layout(location = 7) in vec4 in_weights;
out gl_PerVertex
{
vec4 gl_Position;
float gl_ClipDistance[nb_clipping];
float gl_ClipDistance[nb_clipping];
};
void main()
{
// TODO: use base_color_texcoord et al instead of always using texcoord 0
for (int i = 0; i < nb_texcoords; i++)
texcoord[i] = in_texcoord[i];
// TODO use base_color_texcoord et al instead of always using texcoord 0
// TODO use the correct transform
texcoord[0] = compute_texcoord(material.base_color, in_texcoord[material.base_color.texcoord]);
mat4 skinMatrix =
in_weights.x * joints.joint_matrices[int(in_joints.x)] +