-
In this code I am spawning chunks and rendering them. Even though the meshes are visible and rendered I cannot get them from the meshes resource. Maybe only once I can and then poof - not found #[derive(Resource, Default)]
struct ChunkManager {
chunks: HashMap<ChunkPosition2, Entity>,
}
pub fn plugin(app: &mut App) {
app.init_resource::<MapRenderCenter>()
.init_resource::<ChunkManager>()
.add_systems(
Update,
(
spawn_chunks,
register_chunks,
despawn_unregister_chunks,
render_chunks,
update_map_render_center,
draw_normals_system,
check_normal_meshes,
),
);
}
fn update_map_render_center(
query: Query<&Transform, With<ChunkOrigin>>,
mut map_render_center: ResMut<MapRenderCenter>,
) {
let Ok(transform) = query.get_single() else {
return;
};
let position = transform.translation;
let chunk_position = ChunkPosition3::from_world_pos(position);
if map_render_center.center != chunk_position {
*map_render_center = MapRenderCenter {
center: chunk_position,
};
info!(
"Map render center ({}, {})",
chunk_position.x.0, chunk_position.z.0
);
}
}
fn spawn_chunks(
mut commands: Commands,
center: Res<MapRenderCenter>,
chunk_manager: Res<ChunkManager>,
) {
if !center.is_changed() {
return;
}
let ChunkRect {
from_x,
to_x,
from_y,
to_y,
} = ChunkRect::from_circle_outside(center.center, CHUNK_SPAWN_RADIUS.into());
let mut bundles = Vec::new();
for x in from_x.0..=to_x.0 {
for y in from_y.0..=to_y.0 {
let chunk_position = chunk_position_3(x, 0, y);
if chunk_manager.chunks.contains_key(&chunk_position.to_2d()) {
continue;
}
bundles.push((
Chunk,
Name::new(format!("Chunk {}x{}", x, y)),
chunk_position,
));
}
}
info!("Spawning {} chunks", bundles.len());
commands.spawn_batch(bundles);
}
fn despawn_unregister_chunks(
mut commands: Commands,
center: Res<MapRenderCenter>,
mut chunk_manager: ResMut<ChunkManager>,
) {
if !center.is_changed() {
return;
}
let chunk_rect = ChunkRect::from_circle_outside(center.center, CHUNK_SPAWN_RADIUS.into());
let removed_chunks = chunk_manager
.chunks
.extract_if(|pos, _| !chunk_rect.contains(pos));
let mut count = 0;
for (_, chunk) in removed_chunks {
count += 1;
commands.entity(chunk).despawn();
}
info!("Despawned {} chunks", count);
}
fn register_chunks(
mut chunk_manager: ResMut<ChunkManager>,
chunks: Query<(Entity, &ChunkPosition3), Added<Chunk>>,
) {
for (chunk, chunk_position) in chunks.iter() {
chunk_manager.chunks.insert(chunk_position.to_2d(), chunk);
}
}
fn render_chunks(
mut commands: Commands,
asset_server: Res<AssetServer>,
center: Res<MapRenderCenter>,
chunks: Query<(Entity, &ChunkPosition3), (With<Chunk>, Without<Handle<Mesh>>)>,
) {
if !center.is_changed() {
return;
}
let center = center.center.to_ivec().xz();
let render_radius_squared = CHUNK_VISIBILITY_RADIUS as i32 * CHUNK_VISIBILITY_RADIUS as i32;
let mut count = 0;
for (chunk_entity, chunk_position) in chunks.iter() {
let distance_squared = center.distance_squared(chunk_position.to_ivec().xz());
if distance_squared > render_radius_squared {
continue;
}
let chunk_translation = chunk_position.to_2d().to_world_pos();
let mesh =
utils::primitives::create_subdivided_plane(CHUNK_SUBDIVISIONS, CHUNK_SIZE, |x, y| {
let pos = chunk_translation + vec2(x, y);
let mut layer_1 = utils::noise::value_noise_2d(pos / 100.0);
layer_1.value *= 10.0;
(layer_1.value, layer_1.get_normal().into())
});
let mesh_handle = asset_server.add(mesh);
let material_handle = asset_server.add(
StandardMaterial {
base_color: tailwind::GRAY_400.into(),
perceptual_roughness: 0.9,
..default()
});
commands.entity(chunk_entity).insert((
MaterialMeshBundle {
mesh: mesh_handle,
material: material_handle,
transform: Transform::from_translation(chunk_position.to_world_pos()),
..default()
},
DrawNormals,
));
count += 1;
}
info!("Rendered {} chunks", count);
}
fn check_normal_meshes(meshes: Res<Assets<Mesh>>, query: Query<&Handle<Mesh>, With<DrawNormals>>) {
for mesh_handle in query.iter() {
if meshes.get(mesh_handle).is_none() {
info!("Mesh is not here");
} else {
info!("Mesh is here");
}
}
} |
Beta Was this translation helpful? Give feedback.
Answered by
JMS55
Sep 12, 2024
Replies: 1 comment
-
When building your mesh, use RenderAssetUsages::all(). I bet you're using only RenderAssetUsages::RENDER_WORLD. |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
Stranget0
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
When building your mesh, use RenderAssetUsages::all(). I bet you're using only RenderAssetUsages::RENDER_WORLD.