mirror of
https://github.com/cocos/cocos4.git
synced 2026-11-19 14:52:40 +08:00
fix(mesh): hoose index format by max index value in createMesh (#336)
* Changed the GeometryRenderer and DebugRenderer from using intrusive calls to registering callbacks (custom pipeline). * Changed the GeometryRenderer and DebugRenderer from using intrusive calls to registering callbacks. (old pipeline) * CI error: Function parameter names cannot be omitted. * Move the devil string to macro * fix(mesh): hoose index format by max index value in createMesh
This commit is contained in:
@@ -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.
|
||||
|
||||
@@ -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.
|
||||
|
||||
Reference in New Issue
Block a user