Skip to content

Commit

Permalink
Added support for gltf without indexbuffer. This is not a complete im…
Browse files Browse the repository at this point in the history
…plementation as our meshbuilder doesn't support it. For now, we generate an indexbuffer for these meshes.
  • Loading branch information
fLindahl committed Nov 14, 2024
1 parent c3cbe11 commit 66ad24b
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 14 deletions.
48 changes: 34 additions & 14 deletions toolkit/toolkitutil/model/import/gltf/node/meshprimitive.cc
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,10 @@ MeshPrimitiveFunc(SizeT totalJobs, SizeT groupSize, IndexT groupIndex, SizeT inv

// Find how many vertices and triangle we need
uint const vertCount = context->scene->accessors[primitive->attributes[Gltf::Primitive::Attribute::Position]].count;
uint const triCount = context->scene->accessors[primitive->indices].count / 3;
uint const triCount = primitive->indices == -1 ? vertCount / 3 : context->scene->accessors[primitive->indices].count / 3;

meshBuilder->NewMesh(vertCount, triCount);

for (uint j = 0; j < vertCount; j++)
{
meshBuilder->AddVertex(MeshBuilderVertex());
Expand Down Expand Up @@ -292,25 +293,44 @@ MeshPrimitiveFunc(SizeT totalJobs, SizeT groupSize, IndexT groupIndex, SizeT inv
node->base.boundingBox = meshBuilder->ComputeBoundingBox();

// Setup triangles
Gltf::Accessor const& indexBufferAccessor = context->scene->accessors[primitive->indices];
Gltf::BufferView const& indexBufferView = context->scene->bufferViews[indexBufferAccessor.bufferView];
Gltf::Buffer const& buffer = context->scene->buffers[indexBufferView.buffer];
Util::Blob const& indexBuffer = buffer.data;
const uint bufferOffset = indexBufferAccessor.byteOffset + indexBufferView.byteOffset;
if (primitive->indices == -1)
{
// TODO: Mesh builder needs to support meshes without indexbuffer.
for (size_t i = 0; i < triCount * 3; i += 3)
{
MeshBuilderTriangle triangle;
triangle.SetVertexIndices(i, i + 1, i + 2);
meshBuilder->AddTriangle(triangle);
}
}
else // Read indexbuffer from GLTF
{
Gltf::Accessor const& indexBufferAccessor = context->scene->accessors[primitive->indices];
Gltf::BufferView const& indexBufferView = context->scene->bufferViews[indexBufferAccessor.bufferView];
Gltf::Buffer const& buffer = context->scene->buffers[indexBufferView.buffer];
Util::Blob const& indexBuffer = buffer.data;
const uint bufferOffset = indexBufferAccessor.byteOffset + indexBufferView.byteOffset;

// TODO: sparse accessors
n_assert2(indexBufferAccessor.sparse.count == 0, "Sparse accessors not yet supported!");
// TODO: sparse accessors
n_assert2(indexBufferAccessor.sparse.count == 0, "Sparse accessors not yet supported!");

switch (indexBufferAccessor.componentType)
{
case Gltf::Accessor::ComponentType::UnsignedByte: SetupIndexBuffer<uchar>(*meshBuilder, indexBuffer, bufferOffset, indexBufferAccessor, 0); break;
case Gltf::Accessor::ComponentType::UnsignedShort: SetupIndexBuffer<ushort>(*meshBuilder, indexBuffer, bufferOffset, indexBufferAccessor, 0); break;
case Gltf::Accessor::ComponentType::UnsignedInt: SetupIndexBuffer<uint>(*meshBuilder, indexBuffer, bufferOffset, indexBufferAccessor, 0); break;
switch (indexBufferAccessor.componentType)
{
case Gltf::Accessor::ComponentType::UnsignedByte:
SetupIndexBuffer<uchar>(*meshBuilder, indexBuffer, bufferOffset, indexBufferAccessor, 0);
break;
case Gltf::Accessor::ComponentType::UnsignedShort:
SetupIndexBuffer<ushort>(*meshBuilder, indexBuffer, bufferOffset, indexBufferAccessor, 0);
break;
case Gltf::Accessor::ComponentType::UnsignedInt:
SetupIndexBuffer<uint>(*meshBuilder, indexBuffer, bufferOffset, indexBufferAccessor, 0);
break;
default:
n_error("ERROR: Invalid vertex index type!");
break;
}
}

if (!(attributeFlags.IsSet<(IndexT)Gltf::Primitive::Attribute::Normal>() &&
attributeFlags.IsSet<(IndexT)Gltf::Primitive::Attribute::Tangent>()))
{
Expand Down
4 changes: 4 additions & 0 deletions toolkit/toolkitutil/model/scenewriter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,10 @@ SetupDefaultState(
{
state.material = skinned ? "syssur:placeholder_skinned.sur" : "syssur:placeholder.sur";
}
else if (!state.material.EndsWithString(".sur")) // Temporary to fix all bad material assignments
{
state.material = Util::String::Sprintf("%s.sur", state.material.AsCharPtr());
}

// set state for attributes
attributes->SetState(nodePath, state);
Expand Down

0 comments on commit 66ad24b

Please sign in to comment.