Skip to content

Commit

Permalink
Update three-d to 0.13 and image to 0.24 (#38)
Browse files Browse the repository at this point in the history
* Update to three-d 0.13

* Make sure we support loading jpegs and pngs in Glb files

* Update image crate from 0.23 to 0.24

* Fix compilation by handling all color formats

* update arboard to 3.1

* Use patched version of gltf crate
  • Loading branch information
emilk committed Sep 21, 2022
1 parent 44daf6e commit d8b95f2
Show file tree
Hide file tree
Showing 13 changed files with 217 additions and 312 deletions.
188 changes: 54 additions & 134 deletions Cargo.lock

Large diffs are not rendered by default.

13 changes: 8 additions & 5 deletions Cargo.toml
Expand Up @@ -14,8 +14,11 @@ exclude = ["examples/nyud"]
debug = true

[patch.crates-io]
# 2022-09-20
eframe = { git = "https://github.com/emilk/egui", rev = "311eb66cae5d0a029e2f19dc60b9010872aa25db" }
egui = { git = "https://github.com/emilk/egui", rev = "311eb66cae5d0a029e2f19dc60b9010872aa25db" }
egui_extras = { git = "https://github.com/emilk/egui", rev = "311eb66cae5d0a029e2f19dc60b9010872aa25db" }
egui_glow = { git = "https://github.com/emilk/egui", rev = "311eb66cae5d0a029e2f19dc60b9010872aa25db" }
# 2022-09-21 (updated arboard)
eframe = { git = "https://github.com/emilk/egui", rev = "12dc61ed0f1a23b45e336799005d62e0a6f47e4f" }
egui = { git = "https://github.com/emilk/egui", rev = "12dc61ed0f1a23b45e336799005d62e0a6f47e4f" }
egui_extras = { git = "https://github.com/emilk/egui", rev = "12dc61ed0f1a23b45e336799005d62e0a6f47e4f" }
egui_glow = { git = "https://github.com/emilk/egui", rev = "12dc61ed0f1a23b45e336799005d62e0a6f47e4f" }

# Because gltf hasn't published a new version: https://github.com/gltf-rs/gltf/issues/357
gltf = { git = "https://github.com/rerun-io/gltf", rev = "3c14ded73755d1ce9e47010edb06db63cb7e2cca" }
12 changes: 7 additions & 5 deletions crates/re_viewer/Cargo.toml
Expand Up @@ -39,7 +39,7 @@ egui_dock = { git = "https://github.com/rerun-io/egui_dock", rev = "096be40cc9fc
fixed = { version = "1.17", features = ["serde"] }
glam = { version = "0.20", features = ["mint"] } # can't update to 0.21 until a new version of `macaw` is released
glow = "0.11"
image = { version = "0.23", default-features = false, features = ["png"] }
image = { version = "0.24", default-features = false, features = ["jpeg", "png"] }
instant = { version = "0.1", features = ["wasm-bindgen"] }
itertools = "0.10"
macaw = { version = "0.17", features = ["with_serde"] }
Expand All @@ -49,18 +49,20 @@ rand = { version = "0.8", features = ["small_rng"] }
rfd = "0.10"
serde = { version = "1", features = ["derive"] }
serde_json = "1"
three-d = { version = "0.12.2", default-features = false }
typenum = "1.15"
three-d-asset = { version = "0.1.0", default-features = false, features = [
three-d = { version = "0.13.0", default-features = false }
three-d-asset = { version = "0.3.0", default-features = false, features = [
"gltf",
"image",
"jpeg",
"obj",
"png",
] }
typenum = "1.15"
vec1 = "1.8"

# native:
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
arboard = { version = "2.1", default-features = false, features = [
arboard = { version = "3.1", default-features = false, features = [
"image-data",
] }
ctrlc = { version = "3.0", features = ["termination"] }
Expand Down
5 changes: 0 additions & 5 deletions crates/re_viewer/src/app.rs
Expand Up @@ -107,11 +107,6 @@ impl App {
}

impl eframe::App for App {
#[cfg(target_arch = "wasm32")]
fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
&mut *self
}

fn save(&mut self, storage: &mut dyn eframe::Storage) {
eframe::set_value(storage, eframe::APP_KEY, &self.state);
}
Expand Down
7 changes: 3 additions & 4 deletions crates/re_viewer/src/misc/mesh_loader.rs
@@ -1,4 +1,4 @@
use anyhow::{anyhow, Context as _};
use anyhow::Context as _;
use re_log_types::{EncodedMesh3D, Mesh3D, MeshFormat, RawMesh3D};
use three_d::*;

Expand Down Expand Up @@ -134,7 +134,7 @@ impl CpuMesh {

let mut materials = Vec::new();
for m in &self.materials {
materials.push(PhysicalMaterial::new(three_d, m).map_err(|err| anyhow!("{}", err))?);
materials.push(PhysicalMaterial::new(three_d, m));
}

let mut meshes = Vec::new();
Expand All @@ -146,8 +146,7 @@ impl CpuMesh {
.clone();

let gm = Gm::new(
InstancedMesh::new(three_d, &Default::default(), mesh)
.map_err(|err| anyhow!("{}", err))?,
InstancedMesh::new(three_d, &Default::default(), mesh),
material,
);
meshes.push(gm);
Expand Down
5 changes: 0 additions & 5 deletions crates/re_viewer/src/remote_viewer_app.rs
Expand Up @@ -52,11 +52,6 @@ impl RemoteViewerApp {
}

impl eframe::App for RemoteViewerApp {
#[cfg(target_arch = "wasm32")]
fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
&mut *self
}

fn save(&mut self, storage: &mut dyn eframe::Storage) {
if let Some((_, app)) = &mut self.app {
app.save(storage);
Expand Down
19 changes: 14 additions & 5 deletions crates/re_viewer/src/ui/image_ui.rs
Expand Up @@ -76,7 +76,6 @@ fn show_zoomed_image_region(
meter: Option<f32>,
) {
use egui::*;
use image::GenericImageView as _;

let (_id, zoom_rect) = tooltip_ui.allocate_space(vec2(192.0, 192.0));
let w = dynamic_image.width() as _;
Expand Down Expand Up @@ -209,17 +208,27 @@ fn show_zoomed_image_region(
}

DynamicImage::ImageRgb8(_)
| DynamicImage::ImageBgr8(_)
| DynamicImage::ImageRgb16(_) => {
| DynamicImage::ImageRgb16(_)
| DynamicImage::ImageRgb32F(_) => {
// TODO(emilk): show 16-bit and 32f values differently
format!(
"R: {}\nG: {}\nB: {}\n\n#{:02X}{:02X}{:02X}",
r, g, b, r, g, b
)
}

DynamicImage::ImageRgba8(_)
| DynamicImage::ImageBgra8(_)
| DynamicImage::ImageRgba16(_) => {
| DynamicImage::ImageRgba16(_)
| DynamicImage::ImageRgba32F(_) => {
// TODO(emilk): show 16-bit and 32f values differently
format!(
"R: {}\nG: {}\nB: {}\nA: {}\n\n#{:02X}{:02X}{:02X}{:02X}",
r, g, b, a, r, g, b, a
)
}

_ => {
re_log::warn_once!("Unknown image color type: {:?}", dynamic_image.color());
format!(
"R: {}\nG: {}\nB: {}\nA: {}\n\n#{:02X}{:02X}{:02X}{:02X}",
r, g, b, a, r, g, b, a
Expand Down
4 changes: 3 additions & 1 deletion crates/re_viewer/src/ui/view3d.rs
Expand Up @@ -419,7 +419,9 @@ pub(crate) fn view_3d(
rect,
callback: std::sync::Arc::new(egui_glow::CallbackFn::new(move |info, painter| {
with_three_d_context(painter.gl(), |rendering| {
paint_with_three_d(rendering, &eye, &info, &scene, dark_mode, show_axes).unwrap();
paint_with_three_d(
rendering, &eye, &info, &scene, dark_mode, show_axes, painter,
);
});
})),
};
Expand Down
9 changes: 2 additions & 7 deletions crates/re_viewer/src/ui/view3d/mesh_cache.rs
Expand Up @@ -64,17 +64,12 @@ impl GpuMeshCache {
});
}

pub fn set_instances(
&mut self,
mesh_id: u64,
instances: &three_d::Instances,
) -> three_d::ThreeDResult<()> {
pub fn set_instances(&mut self, mesh_id: u64, instances: &three_d::Instances) {
if let Some(Some(gpu_mesh)) = self.0.get_mut(&mesh_id) {
for model in &mut gpu_mesh.meshes {
model.set_instances(instances)?;
model.set_instances(instances);
}
}
Ok(())
}

pub fn get(&self, mesh_id: u64) -> Option<&GpuMesh> {
Expand Down
56 changes: 29 additions & 27 deletions crates/re_viewer/src/ui/view3d/rendering.rs
Expand Up @@ -4,6 +4,8 @@ use super::{eye::Eye, mesh_cache::GpuMeshCache, scene::*};

type LineMaterial = three_d::ColorMaterial;

type ThreeDResult<T> = Result<T, three_d::CoreError>;

pub struct RenderingContext {
three_d: three_d::Context,
skybox_dark: three_d::Skybox,
Expand All @@ -15,27 +17,25 @@ pub struct RenderingContext {
}

impl RenderingContext {
pub fn new(gl: &std::sync::Arc<glow::Context>) -> three_d::ThreeDResult<Self> {
pub fn new(gl: &std::sync::Arc<glow::Context>) -> ThreeDResult<Self> {
let three_d = three_d::Context::from_gl_context(gl.clone())?;

let skybox_dark = load_skybox_texture(&three_d, skybox_dark).unwrap();
let skybox_light = load_skybox_texture(&three_d, skybox_light).unwrap();
let skybox_dark = load_skybox_texture(&three_d, skybox_dark);
let skybox_light = load_skybox_texture(&three_d, skybox_light);

let ambient_light_intensity = 5.0;
let ambient_dark = three_d::AmbientLight::new_with_environment(
&three_d,
ambient_light_intensity,
three_d::Color::WHITE,
skybox_dark.texture(),
)
.unwrap();
);
let ambient_light = three_d::AmbientLight::new_with_environment(
&three_d,
ambient_light_intensity,
three_d::Color::WHITE,
skybox_light.texture(),
)
.unwrap();
);

let gpu_scene = GpuScene::new(&three_d);

Expand Down Expand Up @@ -65,16 +65,14 @@ impl GpuScene {
Default::default(),
&three_d::CpuMesh::sphere(3), // fast
default_material(),
)
.unwrap();
);

let lines_cache = three_d::Gm::new(
three_d::InstancedMesh::new(
three_d,
&Default::default(),
&three_d::CpuMesh::cylinder(10),
)
.unwrap(),
),
Default::default(),
);

Expand All @@ -98,11 +96,10 @@ impl GpuScene {
line_radius_from_distance: _,
} = scene;

self.points.set_instances(allocate_points(points)).unwrap();
self.points.set_instances(allocate_points(points));

self.lines
.set_instances(&allocate_line_segments(line_segments))
.unwrap();
.set_instances(&allocate_line_segments(line_segments));

self.mesh_instances.clear();

Expand All @@ -127,7 +124,7 @@ impl GpuScene {
}

for (mesh_id, instances) in &self.mesh_instances {
self.gpu_meshes.set_instances(*mesh_id, instances).unwrap();
self.gpu_meshes.set_instances(*mesh_id, instances);
}
}

Expand Down Expand Up @@ -157,7 +154,7 @@ impl GpuScene {
fn load_skybox_texture(
three_d: &three_d::Context,
color_from_dir: fn(glam::Vec3) -> [u8; 3],
) -> three_d::ThreeDResult<three_d::Skybox> {
) -> three_d::Skybox {
crate::profile_function!();

let resolution = 64;
Expand Down Expand Up @@ -314,7 +311,8 @@ pub fn paint_with_three_d(
scene: &Scene,
dark_mode: bool,
show_axes: bool, // TODO(emilk): less bool arguments
) -> three_d::ThreeDResult<()> {
painter: &egui_glow::Painter,
) {
crate::profile_function!();
use three_d::*;
let three_d = &rendering.three_d;
Expand All @@ -329,28 +327,27 @@ pub fn paint_with_three_d(

// Respect the egui clip region (e.g. if we are inside an `egui::ScrollArea`).
let clip_rect = info.clip_rect_in_pixels();
three_d.set_scissor(ScissorBox {
let scissor_box = ScissorBox {
x: clip_rect.left_px.round() as _,
y: clip_rect.from_bottom_px.round() as _,
width: clip_rect.width_px.round() as _,
height: clip_rect.height_px.round() as _,
});
};
three_d.set_blend(three_d::Blend::TRANSPARENCY);

let position = eye.world_from_view.translation();
let target = eye.world_from_view.transform_point3(-glam::Vec3::Z);
let up = eye.world_from_view.transform_vector3(glam::Vec3::Y);
let far = 100_000.0; // TODO(emilk): infinity (https://github.com/rustgd/cgmath/pull/547)
let three_d_camera = three_d::Camera::new_perspective(
three_d,
viewport,
mint::Vector3::from(position).into(),
mint::Vector3::from(target).into(),
mint::Vector3::from(up).into(),
radians(eye.fov_y),
eye.near(),
far,
)?;
);

// -------------------

Expand All @@ -359,8 +356,8 @@ pub fn paint_with_three_d(
} else {
&rendering.ambient_light
};
let directional0 = DirectionalLight::new(three_d, 5.0, Color::WHITE, &vec3(-1.0, -1.0, -1.0))?;
let directional1 = DirectionalLight::new(three_d, 5.0, Color::WHITE, &vec3(1.0, 1.0, 1.0))?;
let directional0 = DirectionalLight::new(three_d, 5.0, Color::WHITE, &vec3(-1.0, -1.0, -1.0));
let directional1 = DirectionalLight::new(three_d, 5.0, Color::WHITE, &vec3(1.0, 1.0, 1.0));
let lights: &[&dyn Light] = &[ambient, &directional0, &directional1];

// -------------------
Expand All @@ -377,17 +374,22 @@ pub fn paint_with_three_d(
objects.push(&rendering.skybox_light);
}

let axes = three_d::Axes::new(three_d, 0.01, 1.0).unwrap();
let axes = three_d::Axes::new(three_d, 0.01, 1.0);
if show_axes {
objects.push(&axes);
}

rendering.gpu_scene.collect_objects(&mut objects);

crate::profile_scope!("render_pass");
render_pass(&three_d_camera, &objects, lights)?;
let (width, height) = (info.viewport.width() as u32, info.viewport.height() as u32);

let render_target = painter.intermediate_fbo().map_or_else(
|| RenderTarget::screen(three_d, width, height),
|fbo| RenderTarget::from_framebuffer(three_d, width, height, fbo),
);

Ok(())
crate::profile_scope!("render");
render_target.render_partially(scissor_box, &three_d_camera, &objects, lights);
}

fn color_to_three_d([r, g, b, a]: [u8; 4]) -> three_d::Color {
Expand Down

0 comments on commit d8b95f2

Please sign in to comment.