faster way to iterate through a chunk of voxels in rust?

63 views Asked by At

so i am making chunks of voxels (128x256x128) and need to iterate through those positions to first create the voxels:

for x in 0..=width {
    for z in 0..=depth {
        let noise_value1 = noise.get([
            (chunk_position.x + x as f32 * 0.01) as f64,
            (chunk_position.z + z as f32 * 0.01) as f64,
        ]) as f32;
        let mut y = ((noise_value1 * 20.0) + (CHUNK_DEPTH / 3) as f32).round() as usize;
        y = y.min(height);

        chunk_voxels.voxel_array.extend((0..y).map(|n| {
            (
                Position {
                    x: x as u8,
                    y: n as u8,
                    z: z as u8,
                },
                Voxel { voxel_id: 1 },
            )
        }));
    }
}

and after i am checking for neighbouring voxels to make sure i only render meshes where they are visable:

for voxel in voxel_array.iter() {
    if check_for_out_of_bounds(Vec3 {
        x: voxel.0.x as f32,
        y: voxel.0.y as f32 + 1 as f32,
        z: voxel.0.z as f32,
    }) {
        //check for up
        if !voxel_array.contains_key(&Position {
            x: voxel.0.x,
            y: voxel.0.y + 1,
            z: voxel.0.z,
        }) {
            generate_voxel_mesh(
                Vec3 {
                    x: voxel.0.x as f32 + chunk_pos.x,
                    y: voxel.0.y as f32 + chunk_pos.y,
                    z: voxel.0.z as f32 + chunk_pos.z,
                },
                1.0,
                voxel.1.voxel_id,
                mesh_builder,
                Vec3 {
                    x: 0.0,
                    y: 1.0,
                    z: 0.0,
                },
            )
        }
    }
    if check_for_out_of_bounds(Vec3 {
        x: voxel.0.x as f32,
        y: voxel.0.y as f32 - 1 as f32,
        z: voxel.0.z as f32,
    }) {
        //check for down
        if !voxel_array.contains_key(&Position {
            x: voxel.0.x,
            y: voxel.0.y - 1,
            z: voxel.0.z,
        }) {
            generate_voxel_mesh(
                Vec3 {
                    x: voxel.0.x as f32 + chunk_pos.x,
                    y: voxel.0.y as f32 + chunk_pos.y,
                    z: voxel.0.z as f32 + chunk_pos.z,
                },
                1.0,
                voxel.1.voxel_id,
                mesh_builder,
                Vec3 {
                    x: 0.0,
                    y: -1.0,
                    z: 0.0,
                },
            )
        }
    }
    if check_for_out_of_bounds(Vec3 {
        x: voxel.0.x as f32,
        y: voxel.0.y as f32,
        z: voxel.0.z as f32 - 1 as f32,
    }) {
        //check for left
        if !voxel_array.contains_key(&Position {
            x: voxel.0.x,
            y: voxel.0.y,
            z: voxel.0.z - 1,
        }) {
            generate_voxel_mesh(
                Vec3 {
                    x: voxel.0.x as f32 + chunk_pos.x,
                    y: voxel.0.y as f32 + chunk_pos.y,
                    z: voxel.0.z as f32 + chunk_pos.z,
                },
                1.0,
                voxel.1.voxel_id,
                mesh_builder,
                Vec3 {
                    x: 0.0,
                    y: 0.0,
                    z: -1.0,
                },
            )
        }
    }
    if check_for_out_of_bounds(Vec3 {
        x: voxel.0.x as f32,
        y: voxel.0.y as f32,
        z: voxel.0.z as f32 + 1 as f32,
    }) {
        //check for right
        if !voxel_array.contains_key(&Position {
            x: voxel.0.x,
            y: voxel.0.y,
            z: voxel.0.z + 1,
        }) {
            generate_voxel_mesh(
                Vec3 {
                    x: voxel.0.x as f32 + chunk_pos.x,
                    y: voxel.0.y as f32 + chunk_pos.y,
                    z: voxel.0.z as f32 + chunk_pos.z,
                },
                1.0,
                voxel.1.voxel_id,
                mesh_builder,
                Vec3 {
                    x: 0.0,
                    y: 0.0,
                    z: 1.0,
                },
            )
        }
    }
    if check_for_out_of_bounds(Vec3 {
        x: voxel.0.x as f32 - 1 as f32,
        y: voxel.0.y as f32,
        z: voxel.0.z as f32,
    }) {
        //check for forward
        if !voxel_array.contains_key(&Position {
            x: voxel.0.x - 1,
            y: voxel.0.y,
            z: voxel.0.z,
        }) {
            generate_voxel_mesh(
                Vec3 {
                    x: voxel.0.x as f32 + chunk_pos.x,
                    y: voxel.0.y as f32 + chunk_pos.y,
                    z: voxel.0.z as f32 + chunk_pos.z,
                },
                1.0,
                voxel.1.voxel_id,
                mesh_builder,
                Vec3 {
                    x: -1.0,
                    y: 0.0,
                    z: 0.0,
                },
            )
        }
    }
    if check_for_out_of_bounds(Vec3 {
        x: voxel.0.x as f32 + 1 as f32,
        y: voxel.0.y as f32,
        z: voxel.0.z as f32,
    }) {
        //check for back
        if !voxel_array.contains_key(&Position {
            x: voxel.0.x + 1,
            y: voxel.0.y,
            z: voxel.0.z,
        }) {
            generate_voxel_mesh(
                Vec3 {
                    x: voxel.0.x as f32 + chunk_pos.x,
                    y: voxel.0.y as f32 + chunk_pos.y,
                    z: voxel.0.z as f32 + chunk_pos.z,
                },
                1.0,
                voxel.1.voxel_id,
                mesh_builder,
                Vec3 {
                    x: 1.0,
                    y: 0.0,
                    z: 0.0,
                },
            )
        }
    }
}

all this is taking rather long (like 5 seconds per chunk) so any info on making this process faster would be appreciated :))

0

There are 0 answers