1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
|
use clap::Parser;
use std::{
fs::File,
io::{BufRead, BufReader, BufWriter, Write},
path::PathBuf,
};
#[derive(Parser)]
struct Args {
atlas_out: PathBuf,
atlas_meta_out: PathBuf,
inputs: Vec<PathBuf>,
}
fn main() {
let Args {
inputs,
atlas_meta_out,
atlas_out,
} = Args::parse();
let atlas_size = 1024;
let mut cursor_x = 0;
let mut cursor_y = 0;
let mut row_height = 0;
let mut texels = vec![vec![' '; atlas_size]; atlas_size];
let mut metadata = Vec::new();
for path in inputs {
let file = BufReader::new(File::open(&path).unwrap());
let tex = file.lines().map(Result::unwrap).collect::<Vec<String>>();
let (width, height) = (tex[0].len(), tex.len());
if cursor_x + width > atlas_size {
cursor_y += row_height;
row_height = 0;
cursor_x = 0;
}
if cursor_y + height > atlas_size {
panic!("texture too big or atlas full")
}
row_height = row_height.max(atlas_size);
let texcoord = [cursor_x, cursor_y];
for (y, line) in tex.iter().enumerate() {
if line.is_empty() {
continue;
}
for (x, char) in line.chars().enumerate() {
texels[cursor_y + y][cursor_x + x] = char;
}
}
metadata.push((
texcoord,
path.file_stem().unwrap().to_str().unwrap().to_string(),
));
cursor_x += atlas_size;
}
let mut atlas_out = BufWriter::new(File::create(atlas_out).unwrap());
let mut atlas_meta_out = BufWriter::new(File::create(atlas_meta_out).unwrap());
for line in texels {
for char in line {
write!(atlas_out, "{char}").unwrap()
}
writeln!(atlas_out).unwrap();
}
for ([x, y], name) in metadata {
writeln!(atlas_meta_out, "{x},{y},{name}").unwrap();
}
}
|