Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add radius boost for depth clouds on outline #1713

Merged
merged 8 commits into from
Mar 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 16 additions & 13 deletions crates/re_renderer/examples/depth_cloud.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ use itertools::Itertools;
use macaw::IsoTransform;
use re_renderer::{
renderer::{
DepthCloud, DepthCloudDepthData, DepthCloudDrawData, DrawData, GenericSkyboxDrawData,
RectangleDrawData, TexturedRect,
DepthCloud, DepthCloudDepthData, DepthCloudDrawData, DepthClouds, DrawData,
GenericSkyboxDrawData, RectangleDrawData, TexturedRect,
},
resource_managers::{GpuTexture2DHandle, Texture2DCreationDesc},
view_builder::{self, Projection, ViewBuilder},
Expand Down Expand Up @@ -172,17 +172,20 @@ impl RenderDepthClouds {

let depth_cloud_draw_data = DepthCloudDrawData::new(
re_ctx,
&[DepthCloud {
world_from_obj,
depth_camera_intrinsics: *intrinsics,
world_depth_from_data_depth: 1.0,
point_radius_from_world_depth: *point_radius_from_world_depth,
max_depth_in_world: 5.0,
depth_dimensions: depth.dimensions,
depth_data: depth.data.clone(),
colormap: re_renderer::ColorMap::ColorMapTurbo,
outline_mask_id: Default::default(),
}],
&DepthClouds {
clouds: vec![DepthCloud {
world_from_obj,
depth_camera_intrinsics: *intrinsics,
world_depth_from_data_depth: 1.0,
point_radius_from_world_depth: *point_radius_from_world_depth,
max_depth_in_world: 5.0,
depth_dimensions: depth.dimensions,
depth_data: depth.data.clone(),
colormap: re_renderer::ColorMap::ColorMapTurbo,
outline_mask_id: Default::default(),
}],
radius_boost_in_ui_points_for_outlines: 2.5,
},
)
.unwrap();

Expand Down
70 changes: 37 additions & 33 deletions crates/re_renderer/shader/depth_cloud.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -19,40 +19,11 @@ struct PointData {
color: Vec4
}

// Backprojects the depth texture using the intrinsics passed in the uniform buffer.
fn compute_point_data(quad_idx: i32) -> PointData {
let wh = textureDimensions(depth_texture);
let texcoords = IVec2(quad_idx % wh.x, quad_idx / wh.x);

// TODO(cmc): expose knobs to linearize/normalize/flip/cam-to-plane depth.
let world_space_depth = depth_cloud_info.world_depth_from_texture_value * textureLoad(depth_texture, texcoords, 0).x;

// TODO(cmc): albedo textures
let color = Vec4(colormap_linear(depth_cloud_info.colormap, world_space_depth / depth_cloud_info.max_depth_in_world), 1.0);

// TODO(cmc): This assumes a pinhole camera; need to support other kinds at some point.
let intrinsics = depth_cloud_info.depth_camera_intrinsics;
let focal_length = Vec2(intrinsics[0][0], intrinsics[1][1]);
let offset = Vec2(intrinsics[2][0], intrinsics[2][1]);

let pos_in_obj = Vec3(
(Vec2(texcoords) - offset) * world_space_depth / focal_length,
world_space_depth,
);

let pos_in_world = depth_cloud_info.world_from_obj * Vec4(pos_in_obj, 1.0);

var data: PointData;
data.pos_in_world = pos_in_world.xyz;
data.unresolved_radius = depth_cloud_info.point_radius_from_world_depth * world_space_depth;
data.color = color;

return data;
}

// ---

/// Keep in sync with `DepthCloudInfoUBO` in `depth_cloud.rs`.
///
/// Same for all draw-phases.
struct DepthCloudInfo {
/// The extrinsincs of the camera used for the projection.
world_from_obj: Mat4,
Expand All @@ -76,6 +47,9 @@ struct DepthCloudInfo {

/// Configures color mapping mode, see `colormap.wgsl`.
colormap: u32,

/// Changes between the opaque and outline draw-phases.
radius_boost_in_ui_points: f32,
};

@group(1) @binding(0)
Expand All @@ -92,6 +66,37 @@ struct VertexOut {
@location(3) point_radius: f32,
};

// Backprojects the depth texture using the intrinsics passed in the uniform buffer.
fn compute_point_data(quad_idx: i32) -> PointData {
let wh = textureDimensions(depth_texture);
let texcoords = IVec2(quad_idx % wh.x, quad_idx / wh.x);

// TODO(cmc): expose knobs to linearize/normalize/flip/cam-to-plane depth.
let world_space_depth = depth_cloud_info.world_depth_from_texture_value * textureLoad(depth_texture, texcoords, 0).x;

// TODO(cmc): albedo textures
let color = Vec4(colormap_linear(depth_cloud_info.colormap, world_space_depth / depth_cloud_info.max_depth_in_world), 1.0);

// TODO(cmc): This assumes a pinhole camera; need to support other kinds at some point.
let intrinsics = depth_cloud_info.depth_camera_intrinsics;
let focal_length = Vec2(intrinsics[0][0], intrinsics[1][1]);
let offset = Vec2(intrinsics[2][0], intrinsics[2][1]);

let pos_in_obj = Vec3(
(Vec2(texcoords) - offset) * world_space_depth / focal_length,
world_space_depth,
);

let pos_in_world = depth_cloud_info.world_from_obj * Vec4(pos_in_obj, 1.0);

var data: PointData;
data.pos_in_world = pos_in_world.xyz;
data.unresolved_radius = depth_cloud_info.point_radius_from_world_depth * world_space_depth;
data.color = color;

return data;
}

@vertex
fn vs_main(@builtin(vertex_index) vertex_idx: u32) -> VertexOut {
let quad_idx = sphere_quad_index(vertex_idx);
Expand All @@ -100,8 +105,7 @@ fn vs_main(@builtin(vertex_index) vertex_idx: u32) -> VertexOut {
let point_data = compute_point_data(quad_idx);

// Span quad
// TODO(andreas): Implement outline-mask size boost for depth cloud as well.
let quad = sphere_quad_span(vertex_idx, point_data.pos_in_world, point_data.unresolved_radius, 0.0);
let quad = sphere_quad_span(vertex_idx, point_data.pos_in_world, point_data.unresolved_radius, depth_cloud_info.radius_boost_in_ui_points);

var out: VertexOut;
out.pos_in_clip = frame.projection_from_world * Vec4(quad.pos_in_world, 1.0);
Expand Down
6 changes: 3 additions & 3 deletions crates/re_renderer/shader/lines.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ var line_strip_texture: texture_2d<f32>;
var position_data_texture: texture_2d<u32>;

struct DrawDataUniformBuffer {
size_boost_in_points: f32,
radius_boost_in_ui_points: f32,
// In actuality there is way more padding than this since we align all our uniform buffers to
// 256bytes in order to allow them to be buffer-suballocations.
// However, wgpu doesn't know this at this point and therefore requires `DownlevelFlags::BUFFER_BINDINGS_NOT_16_BYTE_ALIGNED`
Expand Down Expand Up @@ -200,8 +200,8 @@ fn vs_main(@builtin(vertex_index) vertex_idx: u32) -> VertexOut {
let camera_ray = camera_ray_to_world_pos(center_position);
let camera_distance = distance(camera_ray.origin, center_position);
var strip_radius = unresolved_size_to_world(strip_data.unresolved_radius, camera_distance, frame.auto_size_lines);
if draw_data.size_boost_in_points > 0.0 {
let size_boost = world_size_from_point_size(draw_data.size_boost_in_points, camera_distance);
if draw_data.radius_boost_in_ui_points > 0.0 {
let size_boost = world_size_from_point_size(draw_data.radius_boost_in_ui_points, camera_distance);
strip_radius += size_boost;
// Push out positions as well along the quad dir.
// This is especially important if there's no miters on a line-strip (TODO(#829)),
Expand Down
4 changes: 2 additions & 2 deletions crates/re_renderer/shader/point_cloud.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ var position_data_texture: texture_2d<f32>;
var color_texture: texture_2d<f32>;

struct DrawDataUniformBuffer {
size_boost_in_points: f32,
radius_boost_in_ui_points: f32,
// In actuality there is way more padding than this since we align all our uniform buffers to
// 256bytes in order to allow them to be buffer-suballocations.
// However, wgpu doesn't know this at this point and therefore requires `DownlevelFlags::BUFFER_BINDINGS_NOT_16_BYTE_ALIGNED`
Expand Down Expand Up @@ -74,7 +74,7 @@ fn vs_main(@builtin(vertex_index) vertex_idx: u32) -> VertexOut {
let point_data = read_data(quad_idx);

// Span quad
let quad = sphere_quad_span(vertex_idx, point_data.pos, point_data.unresolved_radius, draw_data.size_boost_in_points);
let quad = sphere_quad_span(vertex_idx, point_data.pos, point_data.unresolved_radius, draw_data.radius_boost_in_ui_points);

// Output, transform to projection space and done.
var out: VertexOut;
Expand Down
4 changes: 2 additions & 2 deletions crates/re_renderer/shader/utils/sphere_quad.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,12 @@ struct SphereQuadData {
/// Span a quad onto which perspective correct spheres can be drawn.
///
/// Spanning is done in perspective or orthographically depending of the state of the global cam.
fn sphere_quad_span(vertex_idx: u32, point_pos: Vec3, point_unresolved_radius: f32, size_boost_in_points: f32) -> SphereQuadData {
fn sphere_quad_span(vertex_idx: u32, point_pos: Vec3, point_unresolved_radius: f32, radius_boost_in_ui_points: f32) -> SphereQuadData {
// Resolve radius to a world size. We need the camera distance for this, which is useful later on.
let to_camera = frame.camera_position - point_pos;
let camera_distance = length(to_camera);
let radius = unresolved_size_to_world(point_unresolved_radius, camera_distance, frame.auto_size_points) +
world_size_from_point_size(size_boost_in_points, camera_distance);
world_size_from_point_size(radius_boost_in_ui_points, camera_distance);

// Basic properties of the vertex we're at.
let local_idx = vertex_idx % 6u;
Expand Down
12 changes: 6 additions & 6 deletions crates/re_renderer/src/line_strip_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub struct LineStripSeriesBuilder<PerStripUserData> {

pub batches: Vec<LineBatchInfo>,

pub(crate) size_boost_in_points_for_outlines: f32,
pub(crate) radius_boost_in_ui_points_for_outlines: f32,
}

impl<PerStripUserData> LineStripSeriesBuilder<PerStripUserData>
Expand All @@ -39,16 +39,16 @@ where
strips: Vec::with_capacity(RESERVE_SIZE),
strip_user_data: Vec::with_capacity(RESERVE_SIZE),
batches: Vec::with_capacity(16),
size_boost_in_points_for_outlines: 0.0,
radius_boost_in_ui_points_for_outlines: 0.0,
}
}

/// Boosts the size of the points by the given amount of ui-points for the purpose of drawing outlines.
pub fn size_boost_in_points_for_outlines(
pub fn radius_boost_in_ui_points_for_outlines(
mut self,
size_boost_in_points_for_outlines: f32,
radius_boost_in_ui_points_for_outlines: f32,
) -> Self {
self.size_boost_in_points_for_outlines = size_boost_in_points_for_outlines;
self.radius_boost_in_ui_points_for_outlines = radius_boost_in_ui_points_for_outlines;
self
}

Expand Down Expand Up @@ -93,7 +93,7 @@ where
&self.vertices,
&self.strips,
&self.batches,
self.size_boost_in_points_for_outlines,
self.radius_boost_in_ui_points_for_outlines,
)
.unwrap()
}
Expand Down
10 changes: 5 additions & 5 deletions crates/re_renderer/src/point_cloud_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub struct PointCloudBuilder<PerPointUserData> {

pub(crate) batches: Vec<PointCloudBatchInfo>,

pub(crate) size_boost_in_points_for_outlines: f32,
pub(crate) radius_boost_in_ui_points_for_outlines: f32,
}

impl<PerPointUserData> PointCloudBuilder<PerPointUserData>
Expand All @@ -39,16 +39,16 @@ where
color_buffer,
user_data: Vec::with_capacity(RESERVE_SIZE),
batches: Vec::with_capacity(16),
size_boost_in_points_for_outlines: 0.0,
radius_boost_in_ui_points_for_outlines: 0.0,
}
}

/// Boosts the size of the points by the given amount of ui-points for the purpose of drawing outlines.
pub fn size_boost_in_points_for_outlines(
pub fn radius_boost_in_ui_points_for_outlines(
mut self,
size_boost_in_points_for_outlines: f32,
radius_boost_in_ui_points_for_outlines: f32,
) -> Self {
self.size_boost_in_points_for_outlines = size_boost_in_points_for_outlines;
self.radius_boost_in_ui_points_for_outlines = radius_boost_in_ui_points_for_outlines;
self
}

Expand Down
Loading