fix(mesh): hoose index format by max index value in createMesh

This commit is contained in:
MagicXHouse
2026-09-14 11:35:55 +08:00
parent e6924a7f29
commit ffc817b76f
2 changed files with 18 additions and 4 deletions
+7 -2
View File
@@ -172,13 +172,18 @@ export function createMesh (geometry: IGeometry, out?: Mesh, options?: ICreateMe
// Fill index buffer.
let indexBuffer: ArrayBuffer | null = null;
let idxCount = 0;
const idxStride = 2;
let idxStride = 2;
let indexFormat = Format.R16UI;
if (geometry.indices) {
const { indices } = geometry;
idxCount = indices.length;
if (indices.some((i) => i > 0xFFFF)) {
idxStride = 4;
indexFormat = Format.R32UI;
}
indexBuffer = new ArrayBuffer(idxStride * idxCount);
const indexBufferView = new DataView(indexBuffer);
writeBuffer(indexBufferView, indices, Format.R16UI);
writeBuffer(indexBufferView, indices, indexFormat);
}
// Create primitive.
+11 -2
View File
@@ -222,13 +222,22 @@ Mesh::ICreateInfo MeshUtils::createMeshInfo(const IGeometry &geometry, const ccs
// Fill index buffer.
ArrayBuffer::Ptr indexBuffer;
uint32_t idxCount = 0;
const uint32_t idxStride = 2;
uint32_t idxStride = 2;
gfx::Format indexFormat = gfx::Format::R16UI;
if (geometry.indices.has_value()) {
const ccstd::vector<uint32_t> &indices = geometry.indices.value();
uint32_t maxIndex = 0;
for (uint32_t idx : indices) {
maxIndex = std::max(maxIndex, idx);
}
if (maxIndex > std::numeric_limits<uint16_t>::max()) {
idxStride = 4;
indexFormat = gfx::Format::R32UI;
}
idxCount = static_cast<uint32_t>(indices.size());
indexBuffer = ccnew ArrayBuffer(idxStride * idxCount);
DataView indexBufferView(indexBuffer);
writeBuffer(indexBufferView, indices, gfx::Format::R16UI);
writeBuffer(indexBufferView, indices, indexFormat);
}
// Create primitive.