var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var mesher_exports = {};
__export(mesher_exports, {
  buildVoxelMesh: () => buildVoxelMesh,
  hasVoxelData: () => hasVoxelData
});
module.exports = __toCommonJS(mesher_exports);
var import_boxel_core = require("@playdrop/boxel-core");
function buildVoxelMesh(options) {
  const { voxels, dimensions, size } = options;
  const quads = (0, import_boxel_core.generateGreedyQuads)(voxels, dimensions);
  const halfSize = [size[0] / 2, size[1] / 2, size[2] / 2];
  const cellSizes = [
    dimensions[0] > 0 ? size[0] / dimensions[0] : 1,
    dimensions[1] > 0 ? size[1] / dimensions[1] : 1,
    dimensions[2] > 0 ? size[2] / dimensions[2] : 1
  ];
  if (quads.length === 0) {
    return {
      positions: new Float32Array(0),
      normals: new Float32Array(0),
      uvs: new Float32Array(0),
      indices: new Uint32Array(0),
      groups: [],
      stats: {
        voxels: voxels.filter((value) => value !== 0).length,
        faces: 0
      }
    };
  }
  const positions = [];
  const normals = [];
  const uvs = [];
  const indices = [];
  const groups = [];
  const pushQuad = (quad) => {
    const baseIndex = positions.length / 3;
    const axis = quad.axis;
    const uAxis = (axis + 1) % 3;
    const vAxis = (axis + 2) % 3;
    const width = quad.size[0];
    const height = quad.size[1];
    const base = [quad.origin[0], quad.origin[1], quad.origin[2]];
    base[axis] = quad.plane;
    const corners = quad.normal === 1 ? [
      [0, 0],
      [width, 0],
      [width, height],
      [0, height]
    ] : [
      [0, 0],
      [0, height],
      [width, height],
      [width, 0]
    ];
    const normalVector = [0, 0, 0];
    normalVector[axis] = quad.normal;
    corners.forEach(([uOffset, vOffset]) => {
      const vertex = [base[0], base[1], base[2]];
      vertex[uAxis] = base[uAxis] + uOffset;
      vertex[vAxis] = base[vAxis] + vOffset;
      positions.push(vertex[0] * cellSizes[0] - halfSize[0], vertex[1] * cellSizes[1] - halfSize[1], vertex[2] * cellSizes[2] - halfSize[2]);
      normals.push(normalVector[0], normalVector[1], normalVector[2]);
      const u = width > 0 ? uOffset / width : 0;
      const v = height > 0 ? vOffset / height : 0;
      uvs.push(u, v);
    });
    const startIndex = indices.length;
    indices.push(baseIndex, baseIndex + 1, baseIndex + 2, baseIndex, baseIndex + 2, baseIndex + 3);
    const lastGroup = groups[groups.length - 1];
    if (lastGroup && lastGroup.material === quad.material && lastGroup.start + lastGroup.count === startIndex) {
      lastGroup.count += 6;
    } else {
      groups.push({ material: quad.material, start: startIndex, count: 6 });
    }
  };
  quads.forEach(pushQuad);
  return {
    positions: Float32Array.from(positions),
    normals: Float32Array.from(normals),
    uvs: Float32Array.from(uvs),
    indices: Uint32Array.from(indices),
    groups,
    stats: {
      voxels: voxels.filter((value) => value !== 0).length,
      faces: quads.length
    }
  };
}
function hasVoxelData(primitive) {
  return Array.isArray(primitive.voxels) && primitive.voxels.length > 0;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  buildVoxelMesh,
  hasVoxelData
});
