UNPKG

@csi-foxbyte/cityjson-to-3d-tiles

Version:

A Node.js library that converts CityJSON files into Cesium 3D Tilesβ€”complete with automatic texture atlas packing, Basis compression, three LOD levels, and customizable threading.

129 lines (95 loc) β€’ 5.4 kB
# cityjson-to-3d-tiles πŸš€πŸ“¦ A Node.js library for converting [CityJSON](https://www.cityjson.org/) files into [Cesium 3D Tiles](https://cesium.com/3d-tiles/) with automatic texture packing and Basis compression. Supports generating three levels of detail (LODs) for different distance ranges. ## Table of Contents - [Features](#features) - [Installation](#installation) - [Usage](#usage) - [API](#api) - [Options Overview](#options-overview) - [CLI Wrapper Example](#cli-wrapper-example) - [Contributing](#contributing) - [License](#license) ## πŸŽ‰ Features - **πŸ™οΈ CityJSON to Tile Database**: Parses CityJSON files and builds a tile database optimized for 3D Tiles generation. πŸ› οΈ - **πŸ—ΊοΈ 3D Tiles Generation**: Converts the tile database into Cesium 3D Tiles, including geometry, textures, and metadata. 🎨 - **πŸ–ΌοΈ Automatic Texture Packing**: Packs textures into atlases and compresses them with [Basis](https://github.com/BinomialLLC/basis_universal) for efficient streaming. ⚑ - **πŸ“ Multiple LODs**: Generates three LODs (LOD0, LOD1, LOD2) to balance detail and performance based on camera distance. πŸ” - **🧡 Customizable Threading**: Control the number of worker threads for CPU-bound tasks. πŸ›‘οΈ ## πŸ“₯ Installation ```bash npm install cityjson-to-3d-tiles ``` ## πŸ’» Usage ```js import { generate3DTilesFromTileDatabase } from "cityjson-to-3d-tiles/3dtiles/index.js"; import { generateTileDatabaseFromCityJSON } from "cityjson-to-3d-tiles/cityjson/index.js"; const inputFolder = "D:\\generator_test\\src"; // Folder containing CityJSON files πŸ“‚ const appearance = "rgbTexture"; // Texture appearance (e.g., "rgbTexture", "vertexColor") 🎨 const outputFolder = "D:\\generator_test"; // Base output folder for the tile database and tiles πŸ“ (async () => { // Step 1: Convert CityJSON to an on-disk tile database πŸ—οΈ const { dbFilePath } = await generateTileDatabaseFromCityJSON( inputFolder, // Source folder outputFolder, // Destination folder appearance, // Appearance mode console.log, // Progress callback πŸ“Š { threadCount: 1 } // Options: number of worker threads 🧡 ); // Step 2: Generate Cesium 3D Tiles from the tile database πŸ› οΈ await generate3DTilesFromTileDatabase( dbFilePath, // Path to the generated tile database "D:\\generator_test\\tiles", // Output folder for 3D Tiles πŸ—‚οΈ console.log, // Progress callback πŸ“ˆ { threadCount: 1 } // Options: number of worker threads πŸ”§ ); })(); export { generate3DTilesFromTileDatabase, generateTileDatabaseFromCityJSON }; ``` ## βš™οΈ API ### `generateTileDatabaseFromCityJSON(inputFolder, outputFolder, appearance, progressCallback, options)` - **inputFolder** `(string)` – Path to a directory containing CityJSON files. πŸ“‚ - **outputFolder** `(string)` – Directory where the tile database will be created. πŸ“ - **appearance** `(string)` – Appearance mode: `"rgbTexture"` for textured meshes or `"vertexColor"` for vertex-colored output. 🌈 - **progressCallback** `(function)` – Function called with log messages or progress updates. πŸ“’ - **options** `(object)`: - `threadCount` `(number)` – Number of worker threads to use (default: number of CPU cores). 🧡 **Returns:** A promise that resolves with an object containing: - `dbFilePath` `(string)` – File path to the generated tile database (.db file). πŸ“œ ### `generate3DTilesFromTileDatabase(dbFilePath, tilesOutputFolder, progressCallback, options)` - **dbFilePath** `(string)` – Path to the tile database generated in the previous step. πŸ“‚ - **tilesOutputFolder** `(string)` – Directory where the Cesium 3D Tiles will be written. πŸ—‚οΈ - **progressCallback** `(function)` – Function called with log messages or progress updates. πŸ”” - **options** `(object)`: - `threadCount` `(number)` – Number of worker threads for tile generation (default: number of CPU cores). 🧡 **Returns:** A promise that resolves when 3D Tiles generation is complete. βœ… ## πŸ› οΈ Options Overview | Option | Default | Description | | ------------- | ------------------ | ---------------------------------------------------- | | `appearance` | `"rgbTexture"` | Texture mode (`"rgbTexture"` or `"vertexColor"`). 🎨 | | `threadCount` | `os.cpus().length` | Number of parallel worker threads. 🧡 | ## πŸ“œ CLI Wrapper Example Wrap the functions in a simple CLI script: ```js #!/usr/bin/env node import path from "path"; import { generateTileDatabaseFromCityJSON } from "cityjson-to-3d-tiles/cityjson/index.js"; import { generate3DTilesFromTileDatabase } from "cityjson-to-3d-tiles/3dtiles/index.js"; const [, , src, out, appearance] = process.argv; (async () => { const { dbFilePath } = await generateTileDatabaseFromCityJSON( path.resolve(src), path.resolve(out), appearance || "rgbTexture", console.log, { threadCount: 4 } ); await generate3DTilesFromTileDatabase( dbFilePath, path.join(out, "tiles"), console.log, { threadCount: 4 } ); })(); ``` ## 🀝 Contributing Contributions are welcome! Please open issues or pull requests on the GitHub repository. πŸ™Œ