Skip to content

Commit

Permalink
Action buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
CharlesTaylor7 committed May 3, 2024
1 parent 93c29fc commit 2baedf6
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 24 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ Inverse twists are provided for convenience. These are located directly above th


## Future Ideas
- [ ] Use an action Buffer
- [ ] Use hexagonal cross sections for rotations
- [ ] outlines or gaps between pieces
- [ ] hot key to reset the camera to default orientation
Expand Down
40 changes: 17 additions & 23 deletions src/webgl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use gl_matrix::common::{Mat4, Vec3, PI};
use gl_matrix::{mat4, vec3};
use std::borrow::BorrowMut;
use std::cell::RefCell;
use std::collections::HashMap;
use std::collections::{HashMap, VecDeque};
use wasm_bindgen::prelude::*;
use web_sys::js_sys::{Float32Array, Uint32Array};
use web_sys::{
Expand Down Expand Up @@ -106,7 +106,7 @@ pub fn render(ms: f32) -> Result<()> {
STATE.with_borrow_mut(|p| {
let delta = ms - p.then;
p.then = ms;
if p.active_twist.is_some() {
if p.twist_buffer.len() > 0 {
p.frame += delta;
console_log(delta);
if p.frame > ANIMATION_DURATION {
Expand Down Expand Up @@ -172,9 +172,9 @@ pub fn on_key_down(event: &KeyboardEvent) {
}
}
Command::Twist { octant } => {
if state.active_twist.is_none() {
state.active_twist = Some(Twist::Center { octant: *octant });
}
state
.twist_buffer
.push_back(Twist::Center { octant: *octant });
}
}
}
Expand Down Expand Up @@ -360,7 +360,12 @@ impl Twist {
}
}

fn to_matrix(&self, angle: f32) -> Mat4 {
fn to_matrix(&self, frame: f32) -> Mat4 {
let mut angle = ((2. * PI) / 3.) * f32::min(1., frame / ANIMATION_DURATION);
if !self.positive() {
angle *= -1.;
}

let mut matrix = mat4::create();
mat4::from_rotation(&mut matrix, angle, &self.to_normal());
matrix
Expand Down Expand Up @@ -399,7 +404,7 @@ struct State {
frame: f32,
then: f32,
pieces: Vec<Piece>,
active_twist: Option<Twist>,
twist_buffer: VecDeque<Twist>,
}

impl State {
Expand All @@ -412,23 +417,22 @@ impl State {
camera_axis: vec3::create(),
frame: 0.0,
then: 0.0,
active_twist: None,
twist_buffer: VecDeque::new(),
pieces,
}
}

fn complete_twist(&mut self) {
if let Some(twist) = self.active_twist {
if let Some(twist) = self.twist_buffer.pop_front() {
let normal = twist.to_normal();
let twist = twist.to_matrix(self.twist_angle());
let twist = twist.to_matrix(self.frame);
for piece in self.pieces.iter_mut() {
if vec3::dot(&normal, &piece.normal) > 0. {
piece.transform(&twist)
}
}
}
self.frame = 0.;
self.active_twist = None;
}

fn facets(&self) -> impl Iterator<Item = &Facet> {
Expand Down Expand Up @@ -611,22 +615,12 @@ impl State {
array
}

fn twist_angle(&self) -> f32 {
let mut angle = ((2. * PI) / 3.) * f32::min(1., self.frame / ANIMATION_DURATION);
if let Some(twist) = self.active_twist {
if !twist.positive() {
angle *= -1.;
}
}
angle
}

fn get_vertex_positions(&self) -> Float32Array {
let mut vector = vec![0.0; self.get_vertex_count() as usize * 3];
let mut offset = 0;

// slow path
if let Some(twist) = self.active_twist {
if let Some(twist) = self.twist_buffer.front() {
let normal = twist.to_normal();
for piece in self.pieces.iter() {
for facet in piece.facets.iter() {
Expand All @@ -635,7 +629,7 @@ impl State {

if vec3::dot(&normal, &piece.normal) > 0. {
let mut mesh = Mesh { data };
mesh.transform(&twist.to_matrix(self.twist_angle()));
mesh.transform(&twist.to_matrix(self.frame));
}
offset += facet.mesh.len();
}
Expand Down

0 comments on commit 2baedf6

Please sign in to comment.