mod strip;
#[cfg(not(target_arch = "wasm32"))]
use std::path::Path;
#[cfg(not(target_arch = "wasm32"))]
use std::process::Command;
pub use strip::{strip, Step, Strip, StripFrame};
#[derive(Debug, Clone)]
pub struct DeterministicRandom {
seed: f64,
}
impl Default for DeterministicRandom {
fn default() -> Self {
Self::new()
}
}
impl DeterministicRandom {
pub fn new() -> Self {
Self {
seed: std::f64::consts::PI / 4.0,
}
}
pub fn skip(&mut self, n: usize) {
for _ in 0..n {
self.next();
}
}
#[allow(clippy::should_implement_trait)]
pub fn next(&mut self) -> f64 {
let x = self.seed.sin() * 10000.0;
self.seed += 1.0;
x - x.floor()
}
}
pub fn pin_time(ms: Option<f64>) {
crate::utils::time::pin(ms);
}
pub fn write_png(path: &str, width: u32, height: u32, pixels: &[u8]) {
if let Some(parent) = std::path::Path::new(path).parent() {
std::fs::create_dir_all(parent).ok();
}
let file = std::fs::File::create(path).expect("three-rs: cannot create PNG");
let mut encoder = png::Encoder::new(std::io::BufWriter::new(file), width, height);
encoder.set_color(png::ColorType::Rgba);
encoder.set_depth(png::BitDepth::Eight);
let mut writer = encoder.write_header().expect("three-rs: PNG header");
writer.write_image_data(pixels).expect("three-rs: PNG data");
}
pub fn composite_over_page(pixels: &mut [u8], page_background: u32) {
let page = [
(page_background >> 16) & 0xff,
(page_background >> 8) & 0xff,
page_background & 0xff,
];
for pixel in pixels.as_chunks_mut::<4>().0 {
let transparency = 255 - u32::from(pixel[3]);
for (channel, &behind) in pixel.iter_mut().zip(&page) {
let over = u32::from(*channel) + (behind * transparency + 127) / 255;
*channel = over.min(255) as u8;
}
pixel[3] = 255;
}
}
#[cfg(not(target_arch = "wasm32"))]
pub fn vendor_dir(env_var: &str, name: &str) -> std::path::PathBuf {
match std::env::var(env_var) {
Ok(dir) => std::path::PathBuf::from(dir),
Err(_) => std::path::PathBuf::from(std::env::var("HOME").expect("HOME"))
.join("src/vendor")
.join(name),
}
}
#[cfg(target_arch = "wasm32")]
pub fn vendor_dir(_env_var: &str, name: &str) -> std::path::PathBuf {
std::path::PathBuf::from("/vendor").join(name)
}
pub fn three_js_dir() -> std::path::PathBuf {
vendor_dir("THREE_JS_DIR", "three.js")
}
#[derive(Debug, Clone, PartialEq)]
pub struct Comparison {
pub width: u32,
pub height: u32,
pub num_different_pixels: u64,
pub different_pixels: f64,
pub max_different_pixels: f64,
pub pass: bool,
}
#[cfg(not(target_arch = "wasm32"))]
pub fn compare(actual: &Path, expected: &Path, out: &Path) -> Comparison {
assert!(
expected.exists(),
"reference screenshot missing: {}",
expected.display()
);
std::fs::create_dir_all(out).expect("three-rs: cannot create the comparison directory");
let script = out.join("compare.mjs");
std::fs::write(&script, COMPARE_MJS).expect("three-rs: cannot write compare.mjs");
let output = Command::new("node")
.arg(&script)
.arg(three_js_dir())
.arg(actual)
.arg(expected)
.arg(out)
.output()
.expect("three-rs: failed to run node");
assert!(
output.status.success(),
"comparator failed:\n{}",
String::from_utf8_lossy(&output.stderr)
);
let stdout = String::from_utf8_lossy(&output.stdout);
let json: serde_json::Value =
serde_json::from_str(stdout.trim()).expect("three-rs: the comparator's JSON");
let get = |key: &str| {
json.get(key)
.and_then(serde_json::Value::as_f64)
.unwrap_or_else(|| panic!("three-rs: the comparator's JSON has no numeric `{key}`"))
};
Comparison {
width: get("width") as u32,
height: get("height") as u32,
num_different_pixels: get("numDifferentPixels") as u64,
different_pixels: get("differentPixels"),
max_different_pixels: get("maxDifferentPixels"),
pass: json.get("pass").and_then(serde_json::Value::as_bool) == Some(true),
}
}
#[cfg(not(target_arch = "wasm32"))]
const COMPARE_MJS: &str = include_str!("testing/compare.mjs");