Skip to main content

geolith_tiles_builder/splat/
mod.rs

1//! Gaussian splat pipeline: 3DGS PLY to OGC 3D Tiles.
2//!
3//! Reads a trained 3D Gaussian Splatting PLY, subdivides it into an octree of
4//! streamable tiles, and emits an OGC 3D Tiles tileset whose GLB payloads carry
5//! `KHR_gaussian_splatting` with `KHR_gaussian_splatting_compression_spz_2`.
6
7pub mod glb;
8pub mod ply;
9pub mod spz;
10pub mod tileset;
11
12pub use ply::{SplatCloud, read_splat_ply};
13pub use spz::encode_spz;
14pub use tileset::{SplatTilesetOptions, SplatTilesetStats, write_splat_tileset};
15
16/// What one splat tileset build reads and writes.
17#[derive(Debug, Clone)]
18pub struct SplatJob {
19    /// The trained Gaussian splat PLY.
20    pub ply: std::path::PathBuf,
21    /// The tileset directory to write.
22    pub output: std::path::PathBuf,
23    /// Drop splats beyond this percentile of the distance from the centre;
24    /// 0 keeps all.
25    pub crop_percentile: f64,
26    /// Scale applied to the splats.
27    pub scale: f32,
28    /// Coverage boost scale; 0 turns it off.
29    pub coverage_boost_scale: f32,
30    /// Also write a standalone `scene.spz`.
31    pub emit_scene_spz: bool,
32    /// The tileset options.
33    pub options: SplatTilesetOptions,
34}
35
36/// Build an OGC 3D Tiles tileset from a trained Gaussian splat PLY.
37///
38/// # Errors
39///
40/// Returns an error when the PLY cannot be read or the tileset cannot be written.
41pub fn run(job: &SplatJob) -> Result<(), crate::TilesError> {
42    tracing::info!(ply = %job.ply.display(), "reading 3DGS PLY");
43    let mut cloud = read_splat_ply(&job.ply).map_err(|source| crate::TilesError::ReadPly {
44        path: job.ply.clone(),
45        source,
46    })?;
47    tracing::info!(
48        splats = cloud.len(),
49        sh_degree = cloud.sh_degree,
50        "splat cloud loaded"
51    );
52
53    if job.crop_percentile > 0.0 {
54        let before = cloud.len();
55        cloud = cloud.crop_percentile(job.crop_percentile)?;
56        tracing::info!(
57            kept = cloud.len(),
58            dropped = before - cloud.len(),
59            percentile = job.crop_percentile,
60            "cropped far-field splats"
61        );
62    }
63    cloud.rescale(job.scale);
64    if job.coverage_boost_scale > 0.0 {
65        cloud.coverage_boost(job.coverage_boost_scale);
66        tracing::info!(boost = job.coverage_boost_scale, "applied coverage boost");
67    }
68
69    let options = &job.options;
70    let stats = write_splat_tileset(&cloud, &job.output, options)?;
71    tracing::info!(
72        output = %job.output.display(),
73        tiles = stats.tiles,
74        splats = stats.splats,
75        max_depth = stats.max_depth,
76        "3D Tiles tileset written"
77    );
78
79    if job.emit_scene_spz {
80        let scene = job.output.join("scene.spz");
81        let mut centered = cloud.clone();
82        if let Some((min, max)) = cloud.bounds() {
83            let center = [
84                (min[0] + max[0]) / 2.0,
85                (min[1] + max[1]) / 2.0,
86                (min[2] + max[2]) / 2.0,
87            ];
88            for position in &mut centered.positions {
89                position[0] -= center[0];
90                position[1] -= center[1];
91                position[2] -= center[2];
92            }
93        }
94        std::fs::write(&scene, encode_spz(&centered, options.antialiased)?)?;
95        tracing::info!(path = %scene.display(), "standalone scene.spz written");
96    }
97
98    Ok(())
99}