Skip to content

Commit

Permalink
Faster sphere rendering (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
emilk authored Jun 7, 2022
1 parent de56ede commit ccaaee1
Show file tree
Hide file tree
Showing 2 changed files with 426 additions and 18 deletions.
32 changes: 14 additions & 18 deletions viewer/src/ui/view3d/rendering.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
mod sphere_renderer;

use super::{camera::Camera, mesh_cache::GpuMeshCache, scene::*};

type LineMaterial = three_d::ColorMaterial;
Expand All @@ -12,7 +14,7 @@ pub struct RenderingContext {
gpu_mesh_cache: GpuMeshCache,

/// So we don't need to re-allocate them.
points_cache: three_d::InstancedModel<three_d::PhysicalMaterial>,
points_cache: sphere_renderer::InstancedSpheres<three_d::PhysicalMaterial>,
lines_cache: three_d::InstancedModel<LineMaterial>,
}

Expand Down Expand Up @@ -41,20 +43,18 @@ impl RenderingContext {
)
.unwrap();

let sphere_mesh = three_d::CpuMesh::sphere(24);
let points_cache = three_d::InstancedModel::new_with_material(
let points_cache = sphere_renderer::InstancedSpheres::new_with_material(
&three_d,
&Default::default(),
&sphere_mesh,
Default::default(),
&three_d::CpuMesh::sphere(6),
default_material(),
)
.unwrap();

let line_mesh = three_d::CpuMesh::cylinder(10);
let lines_cache = three_d::InstancedModel::new_with_material(
&three_d,
&Default::default(),
&line_mesh,
&three_d::CpuMesh::cylinder(10),
Default::default(),
)
.unwrap();
Expand Down Expand Up @@ -154,26 +154,22 @@ fn default_material() -> three_d::PhysicalMaterial {
}
}

fn allocate_points(points: &[Point]) -> three_d::Instances {
fn allocate_points(points: &[Point]) -> sphere_renderer::SphereInstances {
crate::profile_function!();
use three_d::*;

let mut translations = vec![];
let mut scales = vec![];
let mut colors = vec![];
let mut translations_and_scale = Vec::with_capacity(points.len());
let mut colors = Vec::with_capacity(points.len());

for point in points {
let p = point.pos;
translations.push(vec3(p[0], p[1], p[2]));
scales.push(vec3(point.radius, point.radius, point.radius));
translations_and_scale.push(vec4(p[0], p[1], p[2], point.radius));
colors.push(color_to_three_d(point.color));
}

three_d::Instances {
translations,
scales: Some(scales),
sphere_renderer::SphereInstances {
translations_and_scale,
colors: Some(colors),
..Default::default()
}
}

Expand Down Expand Up @@ -333,7 +329,7 @@ pub fn paint_with_three_d(

rendering
.points_cache
.set_instances(&allocate_points(points))?;
.set_instances(allocate_points(points))?;
if rendering.points_cache.instance_count() > 0 {
objects.push(&rendering.points_cache);
}
Expand Down
Loading

0 comments on commit ccaaee1

Please sign in to comment.