展示 HN: VoxCSS – 一个基于 DOM 的体素引擎
Show HN: VoxCSS – A DOM based voxel engine

原始链接: https://github.com/LayoutitStudio/voxcss

## VoxCSS:一个基于CSS的3D体素引擎 VoxCSS是一个轻量级的JavaScript库,使用CSS变换直接在DOM中创建3D体素艺术。它通过堆叠网格层渲染HTML长方体,提供了一种在没有WebGL的情况下构建3D场景的简单方法。 主要特性包括对颜色、纹理、交互控制的支持,以及像剔除和体素合并这样的性能优化。你可以使用体素数组定义场景,指定坐标、形状(立方体、斜坡、楔形、尖峰)、颜色和纹理。 VoxCSS与框架无关,可以与Vue、React、Svelte或纯JavaScript无缝协作。专用的组件,如``和``简化了集成。它还包含一个内置的解析器,用于加载MagicaVoxel (.vox) 文件。 性能通过仅渲染可见面,并可选地将相邻体素合并成更大的元素来管理,这对于复杂的场景尤其有益。 了解更多信息并探索示例,请访问[voxcss.com](voxcss.com) 或通过npm安装:`npm install @layoutit/voxcss`。

Hacker News 新闻 | 过去 | 评论 | 提问 | 展示 | 招聘 | 提交 登录 Show HN: VoxCSS – 一个基于 DOM 的体素引擎 (github.com/layoutitstudio) 5 分,由 rofko 1 小时前发布 | 隐藏 | 过去 | 收藏 | 讨论 指南 | 常见问题 | 列表 | API | 安全 | 法律 | 申请 YC | 联系方式 搜索:
相关文章

原文

A CSS voxel engine. A 3D grid for the DOM. Renders HTML cuboids by stacking grid layers and applying transforms. Supports colors and textures, interactions and culling, plus shapes, areas and projections. Works with Vue, React, Svelte, or plain JavaScript.

Visit voxcss.com for docs and model examples.

voxscene
npm install @layoutit/voxcss

You can also load VoxCSS directly from unpkg. Here is a minimal example:

<div id="voxcss"></div>

<script type="module">
  import { renderScene } from "https://unpkg.com/@layoutit/voxcss@latest/dist/index.js";

  renderScene({
    element: document.getElementById("voxcss"),
    camera: { interactive: true },
    scene: {
      voxels: [
        { x: 3, y: 3, z: 0 },
        { x: 3, y: 3, z: 1 }
      ],
      showFloor: true
    }
  });
</script>

Vue, React, and Svelte wrappers all expose the same components with identical props: <VoxCamera> controls the viewpoint (zoom, pan, tilt, rotation, perspective), while <VoxScene> receives the voxel array and manages the 3D grid and its decorations.

import { VoxCamera, VoxScene } from "@layoutit/voxcss/react";

export default function App() {
  const voxels = [{ x: 1, y: 1, z: 0, color: "#f00" }];

  return (
    <VoxCamera interactive>
      <VoxScene voxels={voxels} />
    </VoxCamera>
  );
}

apple

  • zoom, pan, tilt – translate the camera in/out, vertically, and horizontally.
  • rotX, rotY – rotate around the X/Y axis.
  • perspective – control CSS perspective depth (or disable it).
  • interactive – enable pointer drag controls.
  • invert – flip pointer drag direction.
  • animate – auto-rotate the camera; accepts true, a speed number, or { axis, speed, pauseOnInteraction }.
  • voxels – array of voxel objects; optional (defaults to empty) to render a blank scene.
  • rows, cols, depth – override the inferred bounds and explicitly set the 3D grid size.
  • show-walls, show-floor – toggle structural planes.
  • projection – pick "cubic" or "dimetric" presets to change the layer spacing (50/25px).
  • mergeVoxels – collapse contiguous cubes into larger areas for better performance. Accepts true, false, or a number threshold. Defaults to auto-merging large scenes (>2000 voxels).

Each voxel describes a single cell in the grid:

  • x, y, z – required integer coordinates; x2/y2 optional for area footprints.
  • shapecube (default), ramp, wedge, or spike.
  • color / texture – apply solid fills or image URLs per voxel.
  • rot – rotation in degrees; ramps/wedges/spikes snap to 90° increments.

Example:

const voxels = [
  { x: 2, y: 2, z: 0, color: "#f97316" },
  { x: 3, y: 2, z: 0, shape: "ramp", rot: 90, color: "#94a3b8" },
  { x: 3, y: 3, z: 0, texture: "/example.png" }
];

VoxCSS renders everything in the DOM, so performance is determined by how many elements the browser has to manage. The engine reduces work through culling based on neighbors and camera rotation, only drawing the outer shell of the model with the faces that are actually visible.

The mergeVoxels prop can be essential: instead of rendering each cube as its own node, the engine scans each layer and groups adjacent voxels into larger rectangles in the x/y dimensions. Merging is auto-enabled for bigger scenes (over 2000 voxels) and can cut DOM size dramatically.

Loading MagicaVoxel (.vox) files

Use the built-in parser to turn a MagicaVoxel .vox binary into a voxel object and feed it to renderScene:

import { parseMagicaVoxel, renderScene } from "@layoutit/voxcss";

const rootEl = document.getElementById("voxcss")!;

fetch("/models/example.vox")
  .then((r) => r.arrayBuffer())
  .then((buffer) => parseMagicaVoxel(buffer))
  .then(({ voxels }) => {
    renderScene({
      element: rootEl,
      camera: { interactive: true },
      scene: {
        voxels,
        showFloor: true
      }
    });
  });

Layoutit Voxels → A CSS Voxel editor

layoutit-voxels

Layoutit Terra → A CSS Terrain Generator

layoutit-terra

MIT.

联系我们 contact @ memedata.com