write_spz/
write_spz.rs

1//! This example generates a SPZ file containing 3 hardcoded Gaussians.
2//!
3//! Run with:
4//!
5//! ```sh
6//! cargo run --example write_spz -- "path/to/output.spz"
7//! ```
8
9use glam::*;
10use wgpu_3dgs_core::{self as gs, WriteIterGaussian};
11
12fn main() {
13    let model_path = std::env::args()
14        .nth(1)
15        .unwrap_or_else(|| "target/output.spz".to_string());
16
17    let gaussians = [
18        gs::Gaussian {
19            rot: Quat::from_axis_angle((Vec3::X + Vec3::Y / 2.0 + Vec3::Z).normalize(), 0.5),
20            pos: Vec3::ZERO,
21            scale: Vec3::new(0.5, 1.0, 0.75),
22            color: U8Vec4::new(255, 0, 0, 255),
23            sh: [Vec3::ZERO; 15],
24        },
25        gs::Gaussian {
26            rot: Quat::from_axis_angle((Vec3::X + Vec3::Z / 3.0).normalize(), 0.3),
27            pos: Vec3::new(0.0, 8.0, 4.0),
28            scale: Vec3::new(1.0, 1.9, 0.75),
29            color: U8Vec4::new(0, 255, 0, 255),
30            sh: [Vec3::ZERO; 15],
31        },
32        gs::Gaussian {
33            rot: Quat::from_axis_angle((Vec3::X - Vec3::Z).normalize(), 0.2),
34            pos: Vec3::new(4.0, 0.0, 6.0),
35            scale: Vec3::new(1.0, 1.1, 0.8),
36            color: U8Vec4::new(0, 0, 255, 255),
37            sh: [Vec3::ZERO; 15],
38        },
39    ];
40
41    // Alternatively, use `SpzGaussians::from_gaussians` for default options.
42    let gaussians = gs::SpzGaussians::from_gaussians_with_options(
43        gaussians.as_slice(),
44        &gs::SpzGaussiansFromGaussianSliceOptions {
45            version: 2,
46            ..Default::default()
47        },
48    )
49    .expect("valid options");
50
51    println!("Header: {:?}", gaussians.header);
52
53    println!(
54        "Writing {} gaussians to {}",
55        gaussians.header.num_points(),
56        model_path
57    );
58
59    gaussians
60        .write_to_file(&model_path)
61        .expect("write SPZ file");
62}