From 028a966856785e316e02246d7dea08016845830f Mon Sep 17 00:00:00 2001 From: Elias Stepanik <40958815+eliasstepanik@users.noreply.github.com> Date: Sat, 14 Jun 2025 01:32:37 +0200 Subject: [PATCH] Randomize voxel side textures --- .../plugins/environment/systems/voxel_system.rs | 10 +++++----- .../plugins/environment/systems/voxels/atlas.rs | 12 ++++++------ .../environment/systems/voxels/structure.rs | 16 ++++++++++++++++ client/src/plugins/input/systems/voxels.rs | 4 ++-- 4 files changed, 29 insertions(+), 13 deletions(-) diff --git a/client/src/plugins/environment/systems/voxel_system.rs b/client/src/plugins/environment/systems/voxel_system.rs index 117b284..0cee037 100644 --- a/client/src/plugins/environment/systems/voxel_system.rs +++ b/client/src/plugins/environment/systems/voxel_system.rs @@ -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::>() }) @@ -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); } } diff --git a/client/src/plugins/environment/systems/voxels/atlas.rs b/client/src/plugins/environment/systems/voxels/atlas.rs index 71b9eb9..7ae4f28 100644 --- a/client/src/plugins/environment/systems/voxels/atlas.rs +++ b/client/src/plugins/environment/systems/voxels/atlas.rs @@ -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; diff --git a/client/src/plugins/environment/systems/voxels/structure.rs b/client/src/plugins/environment/systems/voxels/structure.rs index befe22d..215ee09 100644 --- a/client/src/plugins/environment/systems/voxels/structure.rs +++ b/client/src/plugins/environment/systems/voxels/structure.rs @@ -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] = [ diff --git a/client/src/plugins/input/systems/voxels.rs b/client/src/plugins/input/systems/voxels.rs index bcae7dd..151f578 100644 --- a/client/src/plugins/input/systems/voxels.rs +++ b/client/src/plugins/input/systems/voxels.rs @@ -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()); } } }