使用Rust+Bevy开发的体素地形生成器无法生成无缝地形求协助
Rust+Bevy体素引擎地形生成问题(Simplex Noise无缝地形失效)
我正在使用Rust和Bevy开发体素引擎,目前正进行地形生成工作。选择用Simplex Noise生成高度图,但当前4x1x4区块规模的世界效果完全达不到预期,地形存在明显的不连续/断层问题。
我查阅资料后了解到实现无缝地形需根据区块位置计算种子,已按此实现,但问题仍未解决,相关代码如下:
高度图生成代码
fn calculate_seed(&self, chunk_pos: &IVec3) -> u64 { let mut hasher = DefaultHasher::new(); chunk_pos.hash(&mut hasher); hasher.finish() } pub fn create_chunk_heightmap(&mut self, chunk_pos: IVec3) -> Vec<i32> { let mut heightmap: Vec<i32> = Vec::with_capacity((CHUNK_SIZE * CHUNK_SIZE) as usize); // 预分配向量空间 let seed = self.calculate_seed(&chunk_pos); let mut noise: FastNoise = FastNoise::seeded(seed); noise.set_noise_type(NoiseType::Simplex); noise.set_frequency(0.1); for x in 0..CHUNK_SIZE { for z in 0..CHUNK_SIZE { // 获取全局空间下的体素X和Z坐标 let voxel_x = chunk_pos.x * CHUNK_SIZE + x; let voxel_z = chunk_pos.z * CHUNK_SIZE + z; let noise_value = noise.get_noise(voxel_x as f32 / 32.0, voxel_z as f32 / 32.0); //println!("Noise Value: {}", noise_value); let normalized_noise_value = (noise_value + 1.0) / 2.0; let scaled_noise_value = normalized_noise_value * 32.0; let final_noise_value = scaled_noise_value as i32; // 写入高度图 heightmap.push(final_noise_value); } } return heightmap; }
体素与区块生成代码
pub fn create_chunk_voxels(&mut self, chunk_pos: IVec3, heightmap: Vec<i32>) -> Vec<Voxel> { let mut voxels: Vec<Voxel> = Vec::with_capacity((CHUNK_SIZE * CHUNK_SIZE * CHUNK_SIZE) as usize); // 预分配向量空间 for x in 0..CHUNK_SIZE { for z in 0..CHUNK_SIZE { for y in 0..CHUNK_SIZE { let voxel_id = x * CHUNK_SIZE * CHUNK_SIZE + y * CHUNK_SIZE + z; let voxel_y = chunk_pos.y * CHUNK_SIZE + y; let heightmap_index = (x * CHUNK_SIZE + z) as usize; let heightmap_value = heightmap[heightmap_index]; let is_solid = voxel_y <= heightmap_value; let voxel = Voxel { id: voxel_id, is_solid, }; voxels.push(voxel); } } } return voxels; } pub fn generate_chunk_v2(&mut self, chunk_pos: IVec3) -> Chunk { let heightmap = self.create_chunk_heightmap(chunk_pos); // println!("Heightmap: {:?}", heightmap); let voxels = self.create_chunk_voxels(chunk_pos, heightmap); let chunk = Chunk { voxels }; return chunk; } pub fn generate_terrain_v2(&mut self, world_size: IVec3) { for x in 0..world_size.x { for z in 0..world_size.z { for y in 0..world_size.y { let chunk_pos: IVec3 = IVec3::new(x, y, z); let chunk = self.generate_chunk_v2(chunk_pos); self.insert_chunk(chunk_pos, chunk); } } } }
我不确定问题出在实现细节还是遗漏了关键步骤,已为此困扰三天,请求协助解决,谢谢。
内容的提问来源于stack exchange,提问作者Kingdawnage
相关产品推荐
相关产品推荐

