particles System

This commit is contained in:
Able 2022-04-02 15:11:40 -05:00
parent b9a162c4a4
commit 6b699e942f
9 changed files with 208 additions and 129 deletions

37
Cargo.lock generated
View file

@ -1068,6 +1068,12 @@ dependencies = [
"miniz_oxide 0.5.1",
]
[[package]]
name = "ppv-lite86"
version = "0.2.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "eb9f9e6e233e5c4a35559a617bf40a4ec447db2e84c20b55a6f83167b7e57872"
[[package]]
name = "proc-macro-crate"
version = "1.1.3"
@ -1096,6 +1102,36 @@ dependencies = [
"proc-macro2",
]
[[package]]
name = "rand"
version = "0.8.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404"
dependencies = [
"libc",
"rand_chacha",
"rand_core",
]
[[package]]
name = "rand_chacha"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88"
dependencies = [
"ppv-lite86",
"rand_core",
]
[[package]]
name = "rand_core"
version = "0.6.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d34f1408f55294453790c48b2f1ebbb1c5b4b7563eb1f418bcfcfdbb06ebb4e7"
dependencies = [
"getrandom",
]
[[package]]
name = "raw-window-handle"
version = "0.4.2"
@ -1297,6 +1333,7 @@ version = "0.1.0"
dependencies = [
"glium",
"image",
"rand",
]
[[package]]

View file

@ -7,4 +7,5 @@ edition = "2021"
[dependencies]
glium = "0.31"
image = "0.24.1"
image = "0.24.1"
rand = "0.8.5"

View file

@ -0,0 +1,9 @@
#version 140
out vec4 color;
in vec4 v_color;
void main() {
color = v_color;
}

View file

@ -0,0 +1,12 @@
#version 140
in vec3 position;
out vec4 v_color;
uniform vec4 color;
void main() {
v_color = color;
gl_Position = vec4(position, 1.0);
}

View file

@ -3,3 +3,4 @@
pub const CHUNK_SIZE: usize = 32;
pub const PLAYER_HEIGHT: f64 = 1.7;
pub const PARTICLE_SCALE: f32 = 0.004;

View file

@ -2,123 +2,7 @@
#![allow(non_upper_case_globals)]
#![allow(unused_mut)]
#[derive(Debug, Copy, Clone)]
pub struct Hex {
pub s: f64,
pub r: f64,
pub q: f64,
pub y: f64,
}
pub enum HexDirection {
SPos,
SNeg,
RPos,
RNeg,
QPos,
QNeg,
YPos,
YNeg,
}
impl Hex {
pub fn distance_to(&self, other: Hex) -> f64 {
let ret = *self - other;
ret.length()
}
pub fn length(&self) -> f64 {
self.q.abs() + self.r.abs() + self.s.abs() / 2.0
}
pub fn round(mut self) -> Hex {
let q = self.q.round();
let r = self.r.round();
let s = self.s.round();
let y = self.y.round();
Hex { q, r, s, y }
}
pub fn scale(self, k: f64) -> Hex {
Hex {
q: self.q * k,
r: self.r * k,
s: self.s * k,
y: self.y * k,
}
}
pub fn rotate_left(&mut self) {
self.s = -self.s;
self.q = -self.q;
self.r = -self.r;
}
pub fn rotate_right(&mut self) {
self.s = -self.s;
self.q = -self.q;
self.r = -self.r;
}
pub fn lerp(&self, other: Hex, time: f64) -> Hex {
return Hex {
q: self.q * (1.0 - time) + other.q * time,
r: self.r * (1.0 - time) + other.r * time,
s: self.s * (1.0 - time) + other.s * time,
y: self.y * (1.0 - time) + other.y * time,
};
}
}
impl Add for Hex {
type Output = Self;
fn add(self, other: Self) -> Self {
Self {
s: self.s + other.s,
r: self.r + other.r,
q: self.q + other.q,
y: self.y + other.y,
}
}
}
impl Sub for Hex {
type Output = Self;
fn sub(self, other: Self) -> Self {
Self {
s: self.s - other.s,
r: self.r - other.r,
q: self.q - other.q,
y: self.y - other.y,
}
}
}
impl Div for Hex {
type Output = Self;
fn div(self, other: Self) -> Self {
Self {
s: self.s / other.s,
r: self.r / other.r,
q: self.q / other.q,
y: self.y / other.y,
}
}
}
impl Mul for Hex {
type Output = Self;
fn mul(self, other: Self) -> Self {
Self {
s: self.s * other.s,
r: self.r * other.r,
q: self.q * other.q,
y: self.y * other.y,
}
}
}
use std::ops::{Add, Div, Mul, Sub};
// use std::ops::{Add, Div, Mul, Sub};
#[derive(Debug, Copy, Clone, Default)]
pub struct Coordinates {
pub x: f32,

View file

@ -9,15 +9,25 @@ use glium::{
glutin::{self, event::KeyboardInput},
Surface,
};
use rand::Rng;
use crate::{
config::PARTICLE_SCALE,
particle_system::{generate_particle_vertexs, Particle},
};
pub mod chunk;
pub mod config;
pub mod coordinates;
pub mod particle_system;
pub mod textures;
pub mod vertex;
pub mod voxel;
fn main() {
let chunk = chunk::Chunk::half_chunk_filled();
println!("{:?}", chunk);
let _chunk = chunk::Chunk::half_chunk_filled();
// println!("{:?}", chunk);
bruh();
}
mod teapot;
@ -41,17 +51,23 @@ pub fn bruh() {
glium::Program::from_source(&display, vertex_shader_src, fragment_shader_src, None)
.unwrap();
let water_vertex_shader_src = include_str!("../assets/shaders/water/water.vert");
let water_fragment_shader_src = include_str!("../assets/shaders/water/water.frag");
let particles_vertex_shader_src = include_str!("../assets/shaders/particles/solid_color.vert");
let particles_fragment_shader_src =
include_str!("../assets/shaders/particles/solid_color.frag");
let water_program = glium::Program::from_source(
let particles_program = glium::Program::from_source(
&display,
water_vertex_shader_src,
water_fragment_shader_src,
particles_vertex_shader_src,
particles_fragment_shader_src,
None,
)
.unwrap();
let mut particles_vec: Vec<Particle> = Vec::new();
for _ in 0..=10000 {
particles_vec.push(Particle::random_particle());
}
let mut render_particles = true;
event_loop.run(move |event, _, control_flow| {
use glutin::event::Event::*;
match event {
@ -89,6 +105,7 @@ pub fn bruh() {
..
} => {
println!("{:?}", virtual_keycode);
render_particles = !render_particles;
return;
}
_ => return,
@ -150,8 +167,29 @@ pub fn bruh() {
.unwrap();
}
{
// Water Stage
if render_particles {
// Particle Stage
let particle_group_color: [f32; 4] = [1.0, 0.0, 1.0, 1.0];
let shape = generate_particle_vertexs(&mut particles_vec);
let vertex_buffer = glium::VertexBuffer::new(&display, &shape).unwrap();
let indices = glium::index::NoIndices(glium::index::PrimitiveType::TrianglesList);
let uniforms = uniform! {
color: particle_group_color,
};
target
.draw(
&vertex_buffer,
&indices,
&particles_program,
&uniforms,
&Default::default(),
)
.unwrap();
}
target.finish().unwrap();

88
src/particle_system.rs Normal file
View file

@ -0,0 +1,88 @@
use rand::Rng;
use crate::{config::PARTICLE_SCALE, vertex};
#[derive(Debug, Copy, Clone, Default)]
pub struct Particle {
pub position: [f32; 3],
pub velocity: [f32; 3],
pub lifetime: f32,
}
impl Particle {
pub fn new(position: [f32; 3], velocity: [f32; 3], lifetime: f32) -> Self {
Self {
position,
velocity,
lifetime,
}
}
pub fn random_particle() -> Self {
let mut rng = rand::thread_rng();
let x = rng.gen_range(-1.0..1.0);
let y = rng.gen_range(-1.0..1.0);
let z = rng.gen_range(-1.0..1.0);
let x_vel = rng.gen_range(-0.001..0.001);
let y_vel = rng.gen_range(-0.001..0.001);
let z_vel = rng.gen_range(-0.001..0.001);
let lifetime = rng.gen_range(0.0..100000000000.0);
Self {
position: [x, y, z],
velocity: [x_vel, y_vel, z_vel],
lifetime,
}
}
}
pub fn generate_particle_vertexs(particle_list: &mut Vec<Particle>) -> Vec<vertex::Vertex> {
let mut shape = Vec::new();
for particle in particle_list.iter_mut() {
if particle.lifetime <= 0.0 {
drop(particle);
break;
}
let vert_1 = vertex::Vertex {
position: (
particle.position[0] - PARTICLE_SCALE,
particle.position[1] + PARTICLE_SCALE,
particle.position[2],
),
};
let vert_2 = vertex::Vertex {
position: (
particle.position[0] + PARTICLE_SCALE,
particle.position[1] + PARTICLE_SCALE,
particle.position[2],
),
};
let vert_3 = vertex::Vertex {
position: (
particle.position[0] - PARTICLE_SCALE,
particle.position[1] - PARTICLE_SCALE,
particle.position[2],
),
};
let vert_4 = vertex::Vertex {
position: (
particle.position[0] + PARTICLE_SCALE,
particle.position[1] - PARTICLE_SCALE,
particle.position[2],
),
};
shape.append(&mut vec![vert_1, vert_2, vert_3, vert_3, vert_4, vert_2]);
particle.lifetime -= 0.1;
particle.position[0] += particle.velocity[0];
particle.position[1] += particle.velocity[1];
particle.position[2] += particle.velocity[2];
}
shape
}

View file

@ -1,3 +1,12 @@
pub struct Vertex {
position: (f32, f32, f32),
#[derive(Debug, Copy, Clone, Default)]
pub struct ColoredVertex {
pub position: (f32, f32, f32),
pub color: (f32, f32, f32, f32),
}
implement_vertex!(ColoredVertex, position, color);
#[derive(Debug, Copy, Clone, Default)]
pub struct Vertex {
pub position: (f32, f32, f32),
}
implement_vertex!(Vertex, position);