Skip to content

Commit

Permalink
Merge pull request #12 from Davidster/optimization
Browse files Browse the repository at this point in the history
Optimization
  • Loading branch information
Davidster authored Dec 9, 2022
2 parents 501ef7c + a71605d commit d408897
Show file tree
Hide file tree
Showing 22 changed files with 1,680 additions and 1,378 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ log = "0.4"
pollster = "0.2"
puffin = "0.14"
puffin_http = "0.11"
twox-hash = "1.6.3"

# assets
gltf = "1"
Expand Down
1 change: 1 addition & 0 deletions src/animation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use super::*;

#[derive(Debug)]
pub struct Animation {
pub name: Option<String>,
pub length_seconds: f32,
pub speed: f32,
pub channels: Vec<Channel>,
Expand Down
2 changes: 1 addition & 1 deletion src/ball.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ impl BallComponent {
-1.0 + rand::random::<f32>() * 2.0,
-1.0 + rand::random::<f32>() * 2.0,
),
0.5 + (rand::random::<f32>() * 0.75),
0.05 + (rand::random::<f32>() * 0.2),
1.0 + (rand::random::<f32>() * 15.0),
)
}
Expand Down
4 changes: 2 additions & 2 deletions src/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub struct GpuBuffer {
impl GpuBuffer {
pub fn empty(
device: &wgpu::Device,
capacity: usize,
capacity: usize, // TODO: make this optional?
stride: usize,
usage: wgpu::BufferUsages,
) -> Self {
Expand Down Expand Up @@ -80,7 +80,7 @@ impl GpuBuffer {
&self.src
}

pub fn _stride(&self) -> usize {
pub fn stride(&self) -> usize {
self.stride
}

Expand Down
12 changes: 6 additions & 6 deletions src/camera.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ pub struct CameraUniform {
position: [f32; 4],
near_plane_distance: f32,
far_plane_distance: f32,
padding: [f32; 2], // TODO: remove
padding: [f32; 2],
}

impl CameraUniform {
Expand All @@ -58,7 +58,7 @@ impl CameraUniform {

impl ShaderCameraView {
pub fn from_transform(
transform: crate::transform::Transform,
transform: Matrix4<f32>,
aspect_ratio: f32,
near_plane_distance: f32,
far_plane_distance: f32,
Expand All @@ -72,10 +72,10 @@ impl ShaderCameraView {
aspect_ratio,
reverse_z,
);
let rotation_only_matrix = clear_translation_from_matrix(transform.matrix());
let rotation_only_matrix = clear_translation_from_matrix(transform);
let rotation_only_view = rotation_only_matrix.inverse_transform().unwrap();
let view = transform.matrix().inverse_transform().unwrap();
let position = get_translation_from_matrix(transform.matrix());
let view = transform.inverse_transform().unwrap();
let position = get_translation_from_matrix(transform);
Self {
proj,
view,
Expand Down Expand Up @@ -142,7 +142,7 @@ pub fn build_cubemap_face_camera_views(
)
.map(|camera| {
ShaderCameraView::from_transform(
camera.to_transform(),
camera.to_transform().matrix(),
1.0,
near_plane_distance,
far_plane_distance,
Expand Down
4 changes: 3 additions & 1 deletion src/character.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ impl Character {
}

pub fn update(&mut self, scene: &mut Scene, physics_state: &mut PhysicsState) {
let root_node_global_transform = scene.get_global_transform_for_node(self.root_node_id);
let root_node_global_transform: crate::transform::Transform = scene
.get_global_transform_for_node(self.root_node_id);
let should_fill_collision_boxes = self.collision_box_colliders.is_empty();
if let Some((skin_node_id, first_skin_bounding_box_transforms)) =
scene.skins.get(self.skin_index).map(|skin| {
Expand All @@ -55,6 +56,7 @@ impl Character {
{
let transform = {
let skin = &scene.skins[self.skin_index];
// TODO: optimize this like get_global_transform_for_node
let skeleton_space_transform = {
let node_ancestry_list = scene.get_skeleton_node_ancestry_list(
skin.bone_node_ids[bone_index],
Expand Down
51 changes: 51 additions & 0 deletions src/default_textures.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
use super::*;
use std::collections::hash_map::Entry;
use std::collections::HashMap;
use std::rc::Rc;

pub struct DefaultTextures {
textures: HashMap<DefaultTextureType, Rc<Texture>>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum DefaultTextureType {
BaseColor,
Normal,
MetallicRoughness,
MetallicRoughnessGLTF,
Emissive,
EmissiveGLTF,
AmbientOcclusion,
}

impl DefaultTextures {
pub fn new() -> Self {
Self {
textures: HashMap::new(),
}
}

pub fn get_default_texture(
&mut self,
device: &wgpu::Device,
queue: &wgpu::Queue,
default_texture_type: DefaultTextureType,
) -> anyhow::Result<Rc<Texture>> {
match self.textures.entry(default_texture_type) {
Entry::Occupied(texture) => Ok(texture.get().clone()),
Entry::Vacant(entry) => {
let color: [u8; 4] = match default_texture_type {
DefaultTextureType::BaseColor => [255, 255, 255, 255],
DefaultTextureType::Normal => [127, 127, 255, 255],
DefaultTextureType::MetallicRoughness => [255, 255, 255, 255],
DefaultTextureType::MetallicRoughnessGLTF => [255, 127, 0, 255],
DefaultTextureType::Emissive => [0, 0, 0, 255],
DefaultTextureType::EmissiveGLTF => [255, 255, 255, 255],
DefaultTextureType::AmbientOcclusion => [255, 255, 255, 255],
};
let texture = Rc::new(Texture::from_color(device, queue, color)?);
Ok(entry.insert(texture).clone())
}
}
}
}
Loading

0 comments on commit d408897

Please sign in to comment.