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) -> anyhow::Result<()> {
42    tracing::info!(ply = %job.ply.display(), "reading 3DGS PLY");
43    let mut cloud = read_splat_ply(&job.ply)?;
44    tracing::info!(
45        splats = cloud.len(),
46        sh_degree = cloud.sh_degree,
47        "splat cloud loaded"
48    );
49
50    if job.crop_percentile > 0.0 {
51        let before = cloud.len();
52        cloud = cloud.crop_percentile(job.crop_percentile)?;
53        tracing::info!(
54            kept = cloud.len(),
55            dropped = before - cloud.len(),
56            percentile = job.crop_percentile,
57            "cropped far-field splats"
58        );
59    }
60    cloud.rescale(job.scale);
61    if job.coverage_boost_scale > 0.0 {
62        cloud.coverage_boost(job.coverage_boost_scale);
63        tracing::info!(boost = job.coverage_boost_scale, "applied coverage boost");
64    }
65
66    let options = &job.options;
67    let stats = write_splat_tileset(&cloud, &job.output, options)?;
68    tracing::info!(
69        output = %job.output.display(),
70        tiles = stats.tiles,
71        splats = stats.splats,
72        max_depth = stats.max_depth,
73        "3D Tiles tileset written"
74    );
75
76    if job.emit_scene_spz {
77        let scene = job.output.join("scene.spz");
78        let mut centered = cloud.clone();
79        if let Some((min, max)) = cloud.bounds() {
80            let center = [
81                (min[0] + max[0]) / 2.0,
82                (min[1] + max[1]) / 2.0,
83                (min[2] + max[2]) / 2.0,
84            ];
85            for position in &mut centered.positions {
86                position[0] -= center[0];
87                position[1] -= center[1];
88                position[2] -= center[2];
89            }
90        }
91        std::fs::write(&scene, encode_spz(&centered, options.antialiased)?)?;
92        tracing::info!(path = %scene.display(), "standalone scene.spz written");
93    }
94
95    Ok(())
96}