Skip to content

Commit 5555a9b

Browse files
committed
microbrick support
1 parent aaa00b3 commit 5555a9b

File tree

8 files changed

+59
-31
lines changed

8 files changed

+59
-31
lines changed

.gitignore

+3-1
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,6 @@ Cargo.lock
1414
# These are backup files generated by rustfmt
1515
**/*.rs.bk
1616

17-
# End of https://www.toptal.com/developers/gitignore/api/rust
17+
# End of https://www.toptal.com/developers/gitignore/api/rust
18+
19+
/models/

Cargo.toml

+2-3
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,9 @@ include = ["blank.brs"]
1010

1111
[dependencies]
1212
structopt = { version = "0.3", default-features = false }
13-
14-
brs = "0.2"
13+
brs = { git = "https://github.com/Brickadia/brs", branch = "0.3" }
1514
tobj = "2.0"
1615
cgmath = "0.17"
1716
image = "0.23"
1817
uuid = "0.7"
19-
chrono = "0.4"
18+
chrono = "0.4"

blank.brs

-460 Bytes
Binary file not shown.

elf.brs

36.7 KB
Binary file not shown.

reference.brs

481 Bytes
Binary file not shown.

src/main.rs

+35-13
Original file line numberDiff line numberDiff line change
@@ -25,25 +25,25 @@ use structopt::StructOpt;
2525
struct Opt {
2626
#[structopt(parse(from_os_str))]
2727
file: PathBuf,
28-
2928
#[structopt(parse(from_os_str))]
3029
output: PathBuf,
31-
3230
#[structopt(long, possible_values = &["lossy", "lossless"], default_value = "lossy")]
3331
simplify: String,
34-
3532
#[structopt(short, long, default_value = "1")]
36-
scale: f32
33+
scale: f32,
34+
#[structopt(short, long, possible_values = &["micro", "normal"], default_value = "normal")]
35+
bricktype: String,
3736
}
3837

3938
fn main() {
4039
let opt = Opt::from_args();
40+
println!("{:?}", opt);
4141
let mut octree = generate_octree(&opt);
4242

4343
match opt.output.extension() {
4444
Some(extension) => {
4545
match extension.to_str() {
46-
Some("brs") => write_brs_data(&mut octree, opt.output, opt.simplify),
46+
Some("brs") => write_brs_data(&mut octree, opt.output, opt.simplify, opt.bricktype),
4747
// Implement new file types
4848
Some(extension) => panic!("Output file type {} is not supported", extension),
4949
None => panic!("Invalid output file type")
@@ -105,23 +105,45 @@ fn generate_octree(opt: &Opt) -> VoxelTree<Vector4<u8>> {
105105
}
106106

107107
println!("Voxelizing...");
108-
voxelize(&mut models, &material_images, opt.scale)
108+
voxelize(&mut models, &material_images, opt.scale, opt.bricktype.clone())
109109
}
110110

111-
fn write_brs_data(mut octree: &mut VoxelTree::<Vector4::<u8>>, output: PathBuf, simplify_algo: String) {
112-
let blank_data = match File::open("blank.brs") {
113-
Err(e) => panic!("Error encountered when loading blank.brs file: {:}", e.to_string()),
111+
fn write_brs_data(mut octree: &mut VoxelTree::<Vector4::<u8>>, output: PathBuf, simplify_algo: String, bricktype: String) {
112+
let reference_save = match File::open("reference.brs") {
113+
Err(e) => panic!("Error encountered when loading microbrick.brs file: {:}", e.to_string()),
114+
Ok(data) => data,
115+
};
116+
117+
let reference_save = match brs::Reader::new(reference_save) {
118+
Err(e) => panic!("Error encountered when reading microbrick.brs: {:}", e.to_string()),
114119
Ok(data) => data,
115120
};
116121

117-
let mut write_data = brs::Reader::new(blank_data).unwrap().read_header1().unwrap().read_header2().unwrap().into_write_data().unwrap();
118-
write_data.bricks.clear();
122+
let smallguy = brs::User {
123+
name: "Smallguy".to_string(),
124+
id: brs::uuid::Uuid::parse_str("8efaeb23-5e82-428e-b575-0dd30270146e").unwrap(),
125+
};
126+
127+
let mut write_data = brs::WriteData {
128+
author: smallguy.clone(),
129+
brick_assets: reference_save.brick_assets().to_vec(),
130+
brick_owners: vec![smallguy],
131+
bricks: vec![],
132+
colors: reference_save.colors().to_vec(),
133+
description: "generated with obj2brs".to_string(),
134+
map: reference_save.map().to_string(),
135+
materials: reference_save.materials().to_vec(),
136+
mods: vec![],
137+
save_time: brs::chrono::DateTime::from(std::time::SystemTime::now()),
138+
};
139+
140+
println!("{:?}", write_data.brick_assets);
119141

120142
println!("Simplifying {:?}...", simplify_algo);
121143
if simplify_algo == "lossless" {
122-
simplify_lossless(&mut octree, &mut write_data);
144+
simplify_lossless(&mut octree, &mut write_data, bricktype);
123145
} else {
124-
simplify(&mut octree, &mut write_data);
146+
simplify(&mut octree, &mut write_data, bricktype);
125147
}
126148

127149
// Write file

src/simplify.rs

+15-11
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::color::*;
33

44
use cgmath::{ Vector3, Vector4 };
55

6-
pub fn simplify(octree: &mut VoxelTree::<Vector4::<u8>>, write_data: &mut brs::WriteData) {
6+
pub fn simplify(octree: &mut VoxelTree::<Vector4::<u8>>, write_data: &mut brs::WriteData, bricktype: String) {
77
let colorset = convert_colorset_to_hsv(&write_data.colors);
88

99
loop {
@@ -89,15 +89,17 @@ pub fn simplify(octree: &mut VoxelTree::<Vector4::<u8>>, write_data: &mut brs::W
8989
let height = yp - y;
9090
let depth = zp - z;
9191

92+
let scales: (isize, isize, isize) = if bricktype == "micro" { (1, 1, 1) } else { (5, 5, 2) };
93+
9294
write_data.bricks.push(
9395
brs::Brick {
94-
asset_name_index: 0,
96+
asset_name_index: if bricktype == "micro" { 0 } else { 1 },
9597
// Coordinates are rotated
9698
size: (5*width as u32, 5*depth as u32, 2*height as u32),
9799
position: (
98-
(5*width + 10*x) as i32,
99-
(5*depth + 10*z) as i32,
100-
(2*height + 4*y) as i32
100+
(scales.0*width + 2*scales.0*x) as i32,
101+
(scales.1*depth + 2*scales.1*z) as i32,
102+
(scales.2*height + 2*scales.2*y) as i32
101103
),
102104
direction: brs::Direction::ZPositive,
103105
rotation: brs::Rotation::Deg0,
@@ -111,7 +113,7 @@ pub fn simplify(octree: &mut VoxelTree::<Vector4::<u8>>, write_data: &mut brs::W
111113
}
112114
}
113115

114-
pub fn simplify_lossless(octree: &mut VoxelTree::<Vector4::<u8>>, write_data: &mut brs::WriteData) {
116+
pub fn simplify_lossless(octree: &mut VoxelTree::<Vector4::<u8>>, write_data: &mut brs::WriteData, bricktype: String) {
115117
let d: isize = 1 << octree.size;
116118
let len = d + 1;
117119

@@ -204,15 +206,17 @@ pub fn simplify_lossless(octree: &mut VoxelTree::<Vector4::<u8>>, write_data: &m
204206
let height = yp - y;
205207
let depth = zp - z;
206208

209+
let scales: (isize, isize, isize) = if bricktype == "micro" { (1, 1, 1) } else { (5, 5, 2) };
210+
207211
write_data.bricks.push(
208212
brs::Brick {
209-
asset_name_index: 0,
213+
asset_name_index: if bricktype == "micro" { 0 } else { 1 },
210214
// Coordinates are rotated
211-
size: (5*width as u32, 5*depth as u32, 2*height as u32),
215+
size: ((scales.0*width) as u32, (scales.1*depth) as u32, (scales.2*height) as u32),
212216
position: (
213-
(5*width + 10*x) as i32,
214-
(5*depth + 10*z) as i32,
215-
(2*height + 4*y) as i32
217+
(scales.0*width + 2*scales.0*x) as i32,
218+
(scales.1*depth + 2*scales.1*z) as i32,
219+
(scales.2*height + 2*scales.2*y) as i32
216220
),
217221
direction: brs::Direction::ZPositive,
218222
rotation: brs::Rotation::Deg0,

src/voxelize.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,23 @@ struct Triangle {
1616
uvs: Option::<[Vector2::<f32>; 3]>
1717
}
1818

19-
pub fn voxelize(models: &mut Vec::<tobj::Model>, materials: &[RgbaImage], scale: f32) -> VoxelTree::<Vector4::<u8>> {
19+
pub fn voxelize(models: &mut Vec::<tobj::Model>, materials: &[RgbaImage], scale: f32, bricktype: String) -> VoxelTree::<Vector4::<u8>> {
2020
let mut octree = VoxelTree::<Vector4::<u8>>::new();
2121

2222
// Determine model AABB to expand triangle octree to final size
2323
// Multiply y-coordinate by 2.5 to take into account plates
24+
let yscale = if bricktype == "micro" { 1.0 } else { 2.5 };
2425

2526
let u = &models[0].mesh.positions; // Guess initial
26-
let mut min = Vector3::new(u[0] * scale, u[1] * 2.5 * scale, u[2] * scale);
27+
let mut min = Vector3::new(u[0] * scale, u[1] * yscale * scale, u[2] * scale);
2728
let mut max = min;
2829

2930
for m in models.iter_mut() {
3031
let p = &mut m.mesh.positions;
3132
for v in (0..p.len()).step_by(3) {
3233

3334
p[v] *= scale;
34-
p[v + 1] *= 2.5 * scale;
35+
p[v + 1] *= yscale * scale;
3536
p[v + 2] *= scale;
3637

3738
for m in 0 .. 3 {

0 commit comments

Comments
 (0)