Randomize voxel side textures

This commit is contained in:
Elias Stepanik 2025-06-14 01:32:37 +02:00
parent 496c5bf673
commit 028a966856
4 changed files with 29 additions and 13 deletions

View File

@ -77,7 +77,7 @@ pub fn generate_voxel_sphere_parallel(octree: &mut SparseVoxelOctree, center: Ve
center.y + iy as f32 * step,
center.z + iz as f32 * step,
);
(pos, Voxel::new([0; 6]))
(pos, Voxel::random_sides())
})
.collect::<Vec<_>>()
})
@ -115,7 +115,7 @@ fn generate_voxel_sphere(octree: &mut SparseVoxelOctree, planet_radius: i32) {
let position = Vec3::new(wx, wy, wz);
// Insert the voxel
let voxel = Voxel::new([0; 6]);
let voxel = Voxel::random_sides();
octree.insert(position, voxel);
}
}
@ -150,7 +150,7 @@ fn generate_voxel_rect(octree: &mut SparseVoxelOctree) {
let position = Vec3::new(wx, wy, wz);
// Insert the voxel
let voxel = Voxel::new([0; 6]);
let voxel = Voxel::random_sides();
octree.insert(position, voxel);
}
}
@ -178,7 +178,7 @@ fn generate_large_plane(octree: &mut SparseVoxelOctree, width: usize, depth: usi
let position = Vec3::new(wx, wy, wz);
// Insert the voxel
let voxel = Voxel::new([0; 6]);
let voxel = Voxel::random_sides();
octree.insert(position, voxel);
}
}
@ -214,7 +214,7 @@ pub fn generate_solid_plane_with_noise(
for iy in 0..=max_layer {
let position = Vec3::new(x * step, iy as f32 * step, z * step);
let voxel = Voxel::new([0; 6]);
let voxel = Voxel::random_sides();
octree.insert(position, voxel);
}
}

View File

@ -20,12 +20,12 @@ impl VoxelTextureAtlas {
let height = tile_size * rows as u32;
let mut data = vec![0u8; (width * height * 4) as usize];
let colors = [
[255, 0, 0, 255], // red
[0, 255, 0, 255], // green
[0, 0, 255, 255], // blue
[255, 255, 0, 255], // yellow
[255, 0, 255, 255], // magenta
[0, 255, 255, 255], // cyan
[255, 0, 0, 255], // 0: red
[0, 0, 0, 255], // 1: black
[0, 255, 0, 255], // 2: green
[0, 0, 255, 255], // 3: blue
[255, 255, 0, 255], // 4: yellow
[255, 0, 255, 255], // 5: magenta
];
for (i, col) in colors.iter().enumerate() {
let cx = (i % columns) as u32 * tile_size;

View File

@ -1,4 +1,5 @@
use bevy::prelude::*;
use rand::Rng;
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, HashSet, VecDeque};
@ -68,6 +69,21 @@ impl Voxel {
pub fn new(textures: [usize; 6]) -> Self {
Self { textures }
}
/// Generate a voxel with a red top, black bottom and random colors on
/// all remaining faces. Assumes the atlas uses index 0 for red, index 1
/// for black and indices >=2 for random colors.
pub fn random_sides() -> Self {
let mut rng = rand::thread_rng();
let mut textures = [0usize; 6];
// Face order: left, right, bottom, top, back, front
textures[3] = 0; // top is red
textures[2] = 1; // bottom is black
for &i in &[0usize, 1usize, 4usize, 5usize] {
textures[i] = rng.gen_range(2..6);
}
Self { textures }
}
}
pub const NEIGHBOR_OFFSETS: [(f32, f32, f32); 6] = [

View File

@ -36,7 +36,7 @@ pub fn voxel_system(
if keyboard_input.just_pressed(KeyCode::KeyQ) && window.cursor_options.visible == false {
for mut octree in octree_query.iter_mut() {
octree.insert(transform.translation, Voxel::new([0; 6]));
octree.insert(transform.translation, Voxel::random_sides());
}
}
if keyboard_input.just_pressed(KeyCode::F4) {
@ -106,7 +106,7 @@ pub fn voxel_system(
+ (normal * Vec3::new(epsilon as f32, epsilon as f32, epsilon as f32));
// Insert the new voxel
octree.insert(offset_position, Voxel::new([0; 6]));
octree.insert(offset_position, Voxel::random_sides());
}
}
}