UNPKG

@three.ez/batched-mesh-extensions

Version:
31 lines 1.41 kB
export function addGeometryLOD(geometryId, geoOrIndex, distance, hysteresis = 0) { const geometryInfo = this._geometryInfo[geometryId]; const srcIndexArray = geoOrIndex.isBufferGeometry ? geoOrIndex.index.array : geoOrIndex; distance = distance ** 2; geometryInfo.LOD ?? (geometryInfo.LOD = [{ start: geometryInfo.start, count: geometryInfo.count, distance: 0, hysteresis: 0 }]); const LOD = geometryInfo.LOD; const lastLOD = LOD[LOD.length - 1]; const start = lastLOD.start + lastLOD.count; const count = srcIndexArray.length; if ((start - geometryInfo.start) + count > geometryInfo.reservedIndexCount) { throw new Error('BatchedMesh LOD: Reserved space request exceeds the maximum buffer size.'); } LOD.push({ start, count, distance, hysteresis }); const dstIndex = this.geometry.getIndex(); const dstIndexArray = dstIndex.array; const vertexStart = geometryInfo.vertexStart; for (let i = 0; i < count; i++) { dstIndexArray[start + i] = srcIndexArray[i] + vertexStart; } dstIndex.needsUpdate = true; } export function getLODIndex(LODs, distance) { for (let i = LODs.length - 1; i > 0; i--) { const level = LODs[i]; const levelDistance = level.distance - (level.distance * level.hysteresis); if (distance >= levelDistance) return i; } return 0; } //# sourceMappingURL=LOD.js.map