diff --git a/cocos/3d/misc/create-mesh.ts b/cocos/3d/misc/create-mesh.ts index c6b14742a8..fcbad3f39b 100644 --- a/cocos/3d/misc/create-mesh.ts +++ b/cocos/3d/misc/create-mesh.ts @@ -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. diff --git a/native/cocos/3d/misc/CreateMesh.cpp b/native/cocos/3d/misc/CreateMesh.cpp index 21626ba833..ccff5acb7a 100644 --- a/native/cocos/3d/misc/CreateMesh.cpp +++ b/native/cocos/3d/misc/CreateMesh.cpp @@ -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 &indices = geometry.indices.value(); + uint32_t maxIndex = 0; + for (uint32_t idx : indices) { + maxIndex = std::max(maxIndex, idx); + } + if (maxIndex > std::numeric_limits::max()) { + idxStride = 4; + indexFormat = gfx::Format::R32UI; + } idxCount = static_cast(indices.size()); indexBuffer = ccnew ArrayBuffer(idxStride * idxCount); DataView indexBufferView(indexBuffer); - writeBuffer(indexBufferView, indices, gfx::Format::R16UI); + writeBuffer(indexBufferView, indices, indexFormat); } // Create primitive.