Skip to content

Commit

Permalink
reuse meshes & materials
Browse files Browse the repository at this point in the history
  • Loading branch information
mockersf committed Aug 1, 2023
1 parent 3957165 commit e4c27af
Showing 1 changed file with 29 additions and 23 deletions.
52 changes: 29 additions & 23 deletions examples/3d/spotlight.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,19 @@ fn setup(

// cubes
let mut rng = StdRng::seed_from_u64(19878367467713);
let cube_mesh = meshes.add(Mesh::from(shape::Cube { size: 0.5 }));
let blue = materials.add(StandardMaterial {
base_color: Color::BLUE,
..default()
});
for _ in 0..40 {
let x = rng.gen_range(-5.0..5.0);
let y = rng.gen_range(0.0..3.0);
let z = rng.gen_range(-5.0..5.0);
commands.spawn((
PbrBundle {
mesh: meshes.add(Mesh::from(shape::Cube { size: 0.5 })),
material: materials.add(StandardMaterial {
base_color: Color::BLUE,
..default()
}),
mesh: cube_mesh.clone(),
material: blue.clone(),
transform: Transform::from_xyz(x, y, z),
..default()
},
Expand All @@ -65,6 +67,24 @@ fn setup(
brightness: 0.14,
});

let sphere_mesh = meshes.add(Mesh::from(shape::UVSphere {
radius: 0.05,
..default()
}));
let sphere_mesh_direction = meshes.add(Mesh::from(shape::UVSphere {
radius: 0.1,
..default()
}));
let red_emissive = materials.add(StandardMaterial {
base_color: Color::RED,
emissive: Color::rgba_linear(1.0, 0.0, 0.0, 0.0),
..default()
});
let maroon_emissive = materials.add(StandardMaterial {
base_color: Color::MAROON,
emissive: Color::rgba_linear(0.369, 0.0, 0.0, 0.0),
..default()
});
for x in 0..4 {
for z in 0..4 {
let x = x as f32 - 2.0;
Expand All @@ -86,29 +106,15 @@ fn setup(
})
.with_children(|builder| {
builder.spawn(PbrBundle {
mesh: meshes.add(Mesh::from(shape::UVSphere {
radius: 0.05,
..default()
})),
material: materials.add(StandardMaterial {
base_color: Color::RED,
emissive: Color::rgba_linear(1.0, 0.0, 0.0, 0.0),
..default()
}),
mesh: sphere_mesh.clone(),
material: red_emissive.clone(),
..default()
});
builder.spawn((
PbrBundle {
transform: Transform::from_translation(Vec3::Z * -0.1),
mesh: meshes.add(Mesh::from(shape::UVSphere {
radius: 0.1,
..default()
})),
material: materials.add(StandardMaterial {
base_color: Color::MAROON,
emissive: Color::rgba_linear(0.369, 0.0, 0.0, 0.0),
..default()
}),
mesh: sphere_mesh_direction.clone(),
material: maroon_emissive.clone(),
..default()
},
NotShadowCaster,
Expand Down

0 comments on commit e4c27af

Please sign in to comment.