0
\$\begingroup\$

I'm using Vulkan to try and render a GLB cube mesh (as a simple example) with a UV texture in my game engine. I've already tried the below to try to map the coordinates to Vulkan space with the SharpGLTF nuget package:

var glb = ModelRoot.Load(glbPath);

var mesh = glb.LogicalMeshes.Single();
var primitive = mesh.Primitives.Single();

var positions = primitive.GetVertexAccessor("POSITION").AsVector3Array().ToArray();
var normals = primitive.GetVertexAccessor("NORMAL").AsVector3Array();
var uvs = primitive.GetVertexAccessor("TEXCOORD_0").AsVector2Array();

var indices = MemoryMarshal.Cast<byte, ushort>(
        indexAccessor.SourceBufferView.Content.Slice(indexAccessor.ByteOffset, indexAccessor.ByteLength))
    .ToArray();

using var mutator = vertexBuffer.CreateMutator(graphicsDevice, commandList);
for (var i = 0; i < positions.Length; i++)
{
    mutator[i] = new Vertex
    {
        Position = positions[i] with { Y = -positions[i].Y },
        Normal = normals[i],
        UV = uvs[i] with { Y = 1.0f - uvs[i].Y },
    };
}

But it really doesn't seem to be working as expected: Incorrect rendering

I'm using the default export settings for Blender, and I've made sure the UV unwrap is correct:

UV mapping in Blender

And the UV texture is just a simple outline: UV outline

I'm sure there is something simple I'm missing here, but I just don't have the requisite knowledge to figure out what.

The shaders are very simple:

// .vert
#version 450 core
#extension GL_ARB_separate_shader_objects: enable
#extension GL_ARB_shading_language_420pack: enable

layout (location = 0) in vec3 position;
layout (location = 1) in vec3 normal;
layout (location = 2) in vec2 texCoord;

layout (binding = 0) uniform UBOProjectionView
{
    mat4 proj;
    mat4 view;
};

layout (location = 0) out vec3 fragNormal;
layout (location = 1) out vec2 fragTexCoord;

void main()
{
    gl_Position = proj * view * vec4(position, 1.0);
    fragNormal = normal;
    fragTexCoord = texCoord;
}

// .frag
#version 450 core
#extension GL_ARB_separate_shader_objects: enable
#extension GL_ARB_shading_language_420pack: enable

layout (location = 0) in vec3 fragNormal;
layout (location = 1) in vec2 fragTexCoord;

layout (binding = 1) uniform sampler2D texUVs;

layout (location = 0) out vec4 fragColor;

void main()
{
    fragColor = texture(texUVs, fragTexCoord);
}

\$\endgroup\$
3
  • \$\begingroup\$ Can you share the mesh data you're using — the specific position and UV values for each vertex? \$\endgroup\$ Commented Jun 30, 2024 at 11:30
  • \$\begingroup\$ Thank you! This is my glb file, layout and uv files: filebin.net/88wab5uuns3kba4u It was created just by opening Blender, and saving the cube that you get by default as a glb file with default export options \$\endgroup\$ Commented Jun 30, 2024 at 13:52
  • \$\begingroup\$ Try using a UV test texture like this one - this can help us diagnose what's going wrong in the UV mapping, since it makes it easier to see which part of the texture is being sampled at each spot. \$\endgroup\$ Commented Jul 3, 2024 at 13:00

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.