Generate random points

This commit is contained in:
Niklas Korz 2025-03-30 16:10:16 +02:00
parent ff438eae2c
commit 5cb9409723
3 changed files with 117 additions and 33 deletions

View file

@ -1,7 +1,15 @@
use bevy::prelude::*;
use bevy_vello::{VelloPlugin, prelude::*};
use bevy_vello::{VelloPlugin, prelude::*, vello::kurbo::Point};
use rand::Rng;
use std::ops::DerefMut;
const JITTER: f64 = 0.5;
#[derive(Component)]
struct Grid {
points: Vec<Point>,
}
fn main() {
App::new()
.add_plugins(DefaultPlugins)
@ -14,31 +22,39 @@ fn main() {
fn setup_vector_graphics(mut commands: Commands) {
commands.spawn((Camera2d, VelloView));
commands.spawn(VelloScene::new());
let mut rng = rand::rng();
let mut points = Vec::new();
for x in -20..=20 {
for y in -10..=10 {
points.push(Point::new(
(x as f64) + rng.random_range(-JITTER..=JITTER),
(y as f64) + rng.random_range(-JITTER..=JITTER),
));
}
}
commands.spawn(Grid { points });
}
fn simple_animation(mut query_scene: Single<(&mut Transform, &mut VelloScene)>, time: Res<Time>) {
let sin_time = time.elapsed_secs().sin().mul_add(0.5, 0.5);
fn simple_animation(
mut query_scene: Single<(&mut Transform, &mut VelloScene)>,
query_grid: Query<&Grid>,
) {
let (transform, scene) = query_scene.deref_mut();
// Reset scene every frame
scene.reset();
// Animate color green to blue
let c = Vec3::lerp(
Vec3::new(-1.0, 1.0, -1.0),
Vec3::new(-1.0, 1.0, 1.0),
sin_time + 0.5,
);
for point in &query_grid.single().points {
scene.fill(
peniko::Fill::NonZero,
kurbo::Affine::default(),
peniko::Color::WHITE,
None,
&kurbo::Circle::new(*point, 0.1),
);
}
// Animate the corner radius
scene.fill(
peniko::Fill::NonZero,
kurbo::Affine::default(),
peniko::Color::new([c.x, c.y, c.z, 1.]),
None,
&kurbo::RoundedRect::new(-50.0, -50.0, 50.0, 50.0, (sin_time as f64) * 50.0),
);
transform.scale = Vec3::lerp(Vec3::ONE * 0.5, Vec3::ONE * 1.0, sin_time);
transform.translation = Vec3::lerp(Vec3::X * -100.0, Vec3::X * 100.0, sin_time);
transform.rotation = Quat::from_rotation_z(-std::f32::consts::TAU * sin_time);
transform.scale = Vec3::ONE * 25.0;
//transform.translation = Vec3::lerp(Vec3::X * -100.0, Vec3::X * 100.0, sin_time);
//transform.rotation = Quat::from_rotation_z(-std::f32::consts::TAU * sin_time);
}