2020-08-21 16:53:43 +00:00
|
|
|
//! Converts graphics primitives into textured triangles.
|
|
|
|
//!
|
2020-12-27 13:16:37 +00:00
|
|
|
//! This module converts lines, circles, text and more represented by [`PaintCmd`]
|
|
|
|
//! into textured triangles represented by [`Triangles`].
|
2020-08-21 16:53:43 +00:00
|
|
|
|
2020-07-30 10:30:20 +00:00
|
|
|
#![allow(clippy::identity_op)]
|
|
|
|
|
2020-05-19 20:28:57 +00:00
|
|
|
use {
|
|
|
|
super::{
|
2020-08-31 20:56:24 +00:00
|
|
|
color::{self, srgba, Rgba, Srgba, TRANSPARENT},
|
2020-05-19 20:28:57 +00:00
|
|
|
fonts::Fonts,
|
2020-09-01 21:54:21 +00:00
|
|
|
PaintCmd, Stroke,
|
2020-05-19 20:28:57 +00:00
|
|
|
},
|
|
|
|
crate::math::*,
|
2020-12-27 10:24:08 +00:00
|
|
|
std::f32::consts::TAU,
|
2020-04-21 08:33:33 +00:00
|
|
|
};
|
2019-01-04 13:14:32 +00:00
|
|
|
|
2020-12-27 13:16:37 +00:00
|
|
|
/// What texture to use in a [`Triangles`] mesh.
|
2020-09-10 14:48:01 +00:00
|
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
|
|
|
|
pub enum TextureId {
|
|
|
|
/// The Egui font texture.
|
2020-12-27 13:16:37 +00:00
|
|
|
/// If you don't want to use a texture, pick this and the [`WHITE_UV`] for uv-coord.
|
2020-09-10 14:48:01 +00:00
|
|
|
Egui,
|
|
|
|
|
|
|
|
/// Your own texture, defined in any which way you want.
|
|
|
|
/// Egui won't care. The backend renderer will presumably use this to look up what texture to use.
|
|
|
|
User(u64),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for TextureId {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self::Egui
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-09 15:24:32 +00:00
|
|
|
/// The UV coordinate of a white region of the texture mesh.
|
2020-09-09 13:24:09 +00:00
|
|
|
/// The default Egui texture has the top-left corner pixel fully white.
|
|
|
|
/// You need need use a clamping texture sampler for this to work
|
|
|
|
/// (so it doesn't do bilinear blending with bottom right corner).
|
2020-09-09 15:14:42 +00:00
|
|
|
pub const WHITE_UV: Pos2 = pos2(0.0, 0.0);
|
2020-04-16 21:10:05 +00:00
|
|
|
|
2020-08-21 16:53:43 +00:00
|
|
|
/// The vertex type.
|
|
|
|
///
|
|
|
|
/// Should be friendly to send to GPU as is.
|
|
|
|
#[repr(C)]
|
2020-05-30 09:04:40 +00:00
|
|
|
#[derive(Clone, Copy, Debug, Default)]
|
2019-01-04 13:14:32 +00:00
|
|
|
pub struct Vertex {
|
2020-08-21 16:53:43 +00:00
|
|
|
/// Logical pixel coordinates (points).
|
|
|
|
/// (0,0) is the top left corner of the screen.
|
|
|
|
pub pos: Pos2, // 64 bit
|
2020-09-09 13:24:09 +00:00
|
|
|
|
2020-09-09 15:14:42 +00:00
|
|
|
/// Normalized texture coordinates.
|
2020-09-09 13:24:09 +00:00
|
|
|
/// (0, 0) is the top left corner of the texture.
|
2020-09-09 15:14:42 +00:00
|
|
|
/// (1, 1) is the bottom right corner of the texture.
|
|
|
|
pub uv: Pos2, // 64 bit
|
2020-09-09 13:24:09 +00:00
|
|
|
|
2020-08-09 15:24:32 +00:00
|
|
|
/// sRGBA with premultiplied alpha
|
2020-08-29 14:58:01 +00:00
|
|
|
pub color: Srgba, // 32 bit
|
2019-01-04 13:14:32 +00:00
|
|
|
}
|
|
|
|
|
2020-08-21 16:53:43 +00:00
|
|
|
/// Textured triangles.
|
2020-05-30 09:04:40 +00:00
|
|
|
#[derive(Clone, Debug, Default)]
|
2020-05-19 18:54:02 +00:00
|
|
|
pub struct Triangles {
|
2020-08-09 15:24:32 +00:00
|
|
|
/// Draw as triangles (i.e. the length is always multiple of three).
|
2019-01-04 13:14:32 +00:00
|
|
|
pub indices: Vec<u32>,
|
2020-09-10 14:48:01 +00:00
|
|
|
|
2020-08-09 15:24:32 +00:00
|
|
|
/// The vertex data indexed by `indices`.
|
2019-01-04 13:14:32 +00:00
|
|
|
pub vertices: Vec<Vertex>,
|
2020-09-10 14:48:01 +00:00
|
|
|
|
|
|
|
/// The texture to use when drawing these triangles
|
|
|
|
pub texture_id: TextureId,
|
2019-01-04 13:14:32 +00:00
|
|
|
}
|
|
|
|
|
2020-08-09 15:24:32 +00:00
|
|
|
/// A clip triangle and some textured triangles.
|
2020-07-18 22:01:13 +00:00
|
|
|
pub type PaintJob = (Rect, Triangles);
|
|
|
|
|
2020-04-20 21:33:16 +00:00
|
|
|
/// Grouped by clip rectangles, in pixel coordinates
|
2020-07-18 22:01:13 +00:00
|
|
|
pub type PaintJobs = Vec<PaintJob>;
|
2020-04-20 21:33:16 +00:00
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2020-08-21 16:53:43 +00:00
|
|
|
/// ## Helpers for adding
|
2020-05-19 18:54:02 +00:00
|
|
|
impl Triangles {
|
2020-09-10 14:48:01 +00:00
|
|
|
pub fn with_texture(texture_id: TextureId) -> Self {
|
|
|
|
Self {
|
|
|
|
texture_id,
|
|
|
|
..Default::default()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-17 08:57:25 +00:00
|
|
|
pub fn bytes_used(&self) -> usize {
|
|
|
|
std::mem::size_of::<Self>()
|
|
|
|
+ self.vertices.len() * std::mem::size_of::<Vertex>()
|
|
|
|
+ self.indices.len() * std::mem::size_of::<u32>()
|
|
|
|
}
|
|
|
|
|
2020-08-21 16:53:43 +00:00
|
|
|
/// Are all indices within the bounds of the contained vertices?
|
|
|
|
pub fn is_valid(&self) -> bool {
|
|
|
|
let n = self.vertices.len() as u32;
|
|
|
|
self.indices.iter().all(|&i| i < n)
|
|
|
|
}
|
|
|
|
|
2020-09-10 14:48:01 +00:00
|
|
|
pub fn is_empty(&self) -> bool {
|
|
|
|
self.indices.is_empty() && self.vertices.is_empty()
|
|
|
|
}
|
|
|
|
|
2020-08-21 16:53:43 +00:00
|
|
|
/// Append all the indices and vertices of `other` to `self`.
|
2020-10-21 09:09:42 +00:00
|
|
|
pub fn append(&mut self, other: Triangles) {
|
2020-10-21 09:04:22 +00:00
|
|
|
debug_assert!(other.is_valid());
|
|
|
|
|
2020-09-10 14:48:01 +00:00
|
|
|
if self.is_empty() {
|
2020-10-21 09:09:42 +00:00
|
|
|
*self = other;
|
2020-09-10 14:48:01 +00:00
|
|
|
} else {
|
|
|
|
assert_eq!(
|
|
|
|
self.texture_id, other.texture_id,
|
|
|
|
"Can't merge Triangles using different textures"
|
|
|
|
);
|
|
|
|
|
2020-10-21 09:09:42 +00:00
|
|
|
let index_offset = self.vertices.len() as u32;
|
|
|
|
for index in &other.indices {
|
|
|
|
self.indices.push(index_offset + index);
|
|
|
|
}
|
|
|
|
self.vertices.extend(other.vertices.iter());
|
2019-01-17 17:03:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-02 19:52:43 +00:00
|
|
|
pub fn colored_vertex(&mut self, pos: Pos2, color: Srgba) {
|
2020-09-10 14:48:01 +00:00
|
|
|
debug_assert!(self.texture_id == TextureId::Egui);
|
2020-09-02 19:52:43 +00:00
|
|
|
self.vertices.push(Vertex {
|
|
|
|
pos,
|
|
|
|
uv: WHITE_UV,
|
|
|
|
color,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-08-21 16:53:43 +00:00
|
|
|
/// Add a triangle.
|
|
|
|
pub fn add_triangle(&mut self, a: u32, b: u32, c: u32) {
|
2019-01-05 19:14:16 +00:00
|
|
|
self.indices.push(a);
|
|
|
|
self.indices.push(b);
|
|
|
|
self.indices.push(c);
|
|
|
|
}
|
|
|
|
|
2020-08-21 16:53:43 +00:00
|
|
|
/// Make room for this many additional triangles (will reserve 3x as many indices).
|
|
|
|
/// See also `reserve_vertices`.
|
2020-05-20 19:22:53 +00:00
|
|
|
pub fn reserve_triangles(&mut self, additional_triangles: usize) {
|
|
|
|
self.indices.reserve(3 * additional_triangles);
|
|
|
|
}
|
|
|
|
|
2020-08-21 16:53:43 +00:00
|
|
|
/// Make room for this many additional vertices.
|
|
|
|
/// See also `reserve_triangles`.
|
2020-05-20 19:22:53 +00:00
|
|
|
pub fn reserve_vertices(&mut self, additional: usize) {
|
|
|
|
self.vertices.reserve(additional);
|
|
|
|
}
|
|
|
|
|
2020-09-10 14:48:01 +00:00
|
|
|
/// Rectangle with a texture and color.
|
|
|
|
pub fn add_rect_with_uv(&mut self, pos: Rect, uv: Rect, color: Srgba) {
|
2019-01-05 14:28:07 +00:00
|
|
|
let idx = self.vertices.len() as u32;
|
2020-08-21 16:53:43 +00:00
|
|
|
self.add_triangle(idx + 0, idx + 1, idx + 2);
|
|
|
|
self.add_triangle(idx + 2, idx + 1, idx + 3);
|
2019-01-05 14:28:07 +00:00
|
|
|
|
2020-09-10 14:48:01 +00:00
|
|
|
let right_top = Vertex {
|
|
|
|
pos: pos.right_top(),
|
|
|
|
uv: uv.right_top(),
|
|
|
|
color,
|
2019-01-05 14:28:07 +00:00
|
|
|
};
|
2020-09-10 14:48:01 +00:00
|
|
|
let left_top = Vertex {
|
|
|
|
pos: pos.left_top(),
|
|
|
|
uv: uv.left_top(),
|
|
|
|
color,
|
|
|
|
};
|
|
|
|
let left_bottom = Vertex {
|
|
|
|
pos: pos.left_bottom(),
|
|
|
|
uv: uv.left_bottom(),
|
|
|
|
color,
|
2019-01-05 14:28:07 +00:00
|
|
|
};
|
2020-09-10 14:48:01 +00:00
|
|
|
let right_bottom = Vertex {
|
|
|
|
pos: pos.right_bottom(),
|
|
|
|
uv: uv.right_bottom(),
|
|
|
|
color,
|
|
|
|
};
|
|
|
|
self.vertices.push(left_top);
|
|
|
|
self.vertices.push(right_top);
|
|
|
|
self.vertices.push(left_bottom);
|
|
|
|
self.vertices.push(right_bottom);
|
2019-01-05 14:28:07 +00:00
|
|
|
}
|
2019-03-12 13:43:50 +00:00
|
|
|
|
2020-09-06 19:30:52 +00:00
|
|
|
/// Uniformly colored rectangle.
|
|
|
|
pub fn add_colored_rect(&mut self, rect: Rect, color: Srgba) {
|
2020-09-10 14:48:01 +00:00
|
|
|
debug_assert!(self.texture_id == TextureId::Egui);
|
|
|
|
self.add_rect_with_uv(rect, [WHITE_UV, WHITE_UV].into(), color)
|
2020-09-06 19:30:52 +00:00
|
|
|
}
|
|
|
|
|
2020-08-09 15:24:32 +00:00
|
|
|
/// This is for platforms that only support 16-bit index buffers.
|
2020-08-21 16:53:43 +00:00
|
|
|
///
|
|
|
|
/// Splits this mesh into many smaller meshes (if needed).
|
2020-05-19 18:54:02 +00:00
|
|
|
/// All the returned meshes will have indices that fit into a `u16`.
|
|
|
|
pub fn split_to_u16(self) -> Vec<Triangles> {
|
2019-03-12 13:43:50 +00:00
|
|
|
const MAX_SIZE: u32 = 1 << 16;
|
|
|
|
|
|
|
|
if self.vertices.len() < MAX_SIZE as usize {
|
|
|
|
return vec![self]; // Common-case optimization
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut output = vec![];
|
|
|
|
let mut index_cursor = 0;
|
|
|
|
|
|
|
|
while index_cursor < self.indices.len() {
|
|
|
|
let span_start = index_cursor;
|
|
|
|
let mut min_vindex = self.indices[index_cursor];
|
|
|
|
let mut max_vindex = self.indices[index_cursor];
|
|
|
|
|
|
|
|
while index_cursor < self.indices.len() {
|
|
|
|
let (mut new_min, mut new_max) = (min_vindex, max_vindex);
|
|
|
|
for i in 0..3 {
|
|
|
|
let idx = self.indices[index_cursor + i];
|
|
|
|
new_min = new_min.min(idx);
|
|
|
|
new_max = new_max.max(idx);
|
|
|
|
}
|
|
|
|
|
|
|
|
if new_max - new_min < MAX_SIZE {
|
|
|
|
// Triangle fits
|
|
|
|
min_vindex = new_min;
|
|
|
|
max_vindex = new_max;
|
|
|
|
index_cursor += 3;
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assert!(
|
|
|
|
index_cursor > span_start,
|
|
|
|
"One triangle spanned more than {} vertices",
|
|
|
|
MAX_SIZE
|
|
|
|
);
|
|
|
|
|
2020-05-19 18:54:02 +00:00
|
|
|
output.push(Triangles {
|
2019-03-12 13:43:50 +00:00
|
|
|
indices: self.indices[span_start..index_cursor]
|
|
|
|
.iter()
|
|
|
|
.map(|vi| vi - min_vindex)
|
|
|
|
.collect(),
|
|
|
|
vertices: self.vertices[(min_vindex as usize)..=(max_vindex as usize)].to_vec(),
|
2020-09-10 14:48:01 +00:00
|
|
|
texture_id: self.texture_id,
|
2019-03-12 13:43:50 +00:00
|
|
|
});
|
|
|
|
}
|
2020-04-24 16:47:14 +00:00
|
|
|
output
|
2019-03-12 13:43:50 +00:00
|
|
|
}
|
2020-11-02 16:41:52 +00:00
|
|
|
|
|
|
|
/// Translate location by this much, in-place
|
|
|
|
pub fn translate(&mut self, delta: Vec2) {
|
|
|
|
for v in &mut self.vertices {
|
|
|
|
v.pos += delta;
|
|
|
|
}
|
|
|
|
}
|
2019-02-24 16:18:30 +00:00
|
|
|
}
|
|
|
|
|
2020-04-18 22:27:25 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2020-04-19 21:34:34 +00:00
|
|
|
#[derive(Clone, Debug, Default)]
|
2020-04-18 22:27:25 +00:00
|
|
|
pub struct PathPoint {
|
2020-04-18 23:05:49 +00:00
|
|
|
pos: Pos2,
|
2020-04-18 22:27:25 +00:00
|
|
|
|
2020-09-01 21:54:21 +00:00
|
|
|
/// For filled paths the normal is used for anti-aliasing (both strokes and filled areas).
|
2020-08-21 16:53:43 +00:00
|
|
|
///
|
2020-09-01 21:54:21 +00:00
|
|
|
/// For strokes the normal is also used for giving thickness to the path
|
2020-04-18 22:27:25 +00:00
|
|
|
/// (i.e. in what direction to expand).
|
2020-08-21 16:53:43 +00:00
|
|
|
///
|
2020-04-18 22:27:25 +00:00
|
|
|
/// The normal could be estimated by differences between successive points,
|
|
|
|
/// but that would be less accurate (and in some cases slower).
|
2020-08-21 16:53:43 +00:00
|
|
|
///
|
|
|
|
/// Normals are normally unit-length.
|
2020-04-18 22:27:25 +00:00
|
|
|
normal: Vec2,
|
|
|
|
}
|
|
|
|
|
2020-08-29 14:58:01 +00:00
|
|
|
/// A connected line (without thickness or gaps) which can be tessellated
|
2020-09-01 21:54:21 +00:00
|
|
|
/// to either to a stroke (with thickness) or a filled convex area.
|
2020-12-28 23:51:27 +00:00
|
|
|
/// Used as a scratch-pad during tessellation.
|
2020-04-19 21:34:34 +00:00
|
|
|
#[derive(Clone, Debug, Default)]
|
2020-09-01 18:03:50 +00:00
|
|
|
struct Path(Vec<PathPoint>);
|
2020-04-18 22:27:25 +00:00
|
|
|
|
|
|
|
impl Path {
|
|
|
|
pub fn clear(&mut self) {
|
|
|
|
self.0.clear();
|
|
|
|
}
|
|
|
|
|
2020-05-23 20:10:39 +00:00
|
|
|
pub fn reserve(&mut self, additional: usize) {
|
|
|
|
self.0.reserve(additional)
|
|
|
|
}
|
|
|
|
|
2020-05-11 11:11:01 +00:00
|
|
|
#[inline(always)]
|
2020-04-18 23:05:49 +00:00
|
|
|
pub fn add_point(&mut self, pos: Pos2, normal: Vec2) {
|
2020-04-18 22:27:25 +00:00
|
|
|
self.0.push(PathPoint { pos, normal });
|
|
|
|
}
|
|
|
|
|
2020-04-18 23:05:49 +00:00
|
|
|
pub fn add_circle(&mut self, center: Pos2, radius: f32) {
|
2020-05-17 15:44:29 +00:00
|
|
|
let n = (radius * 4.0).round() as i32; // TODO: tweak a bit more
|
2020-05-17 07:44:09 +00:00
|
|
|
let n = clamp(n, 4..=64);
|
2020-05-23 20:10:39 +00:00
|
|
|
self.reserve(n as usize);
|
2020-04-18 22:27:25 +00:00
|
|
|
for i in 0..n {
|
2020-04-25 09:11:44 +00:00
|
|
|
let angle = remap(i as f32, 0.0..=n as f32, 0.0..=TAU);
|
2020-04-18 22:27:25 +00:00
|
|
|
let normal = vec2(angle.cos(), angle.sin());
|
|
|
|
self.add_point(center + radius * normal, normal);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-11 11:11:01 +00:00
|
|
|
pub fn add_line_segment(&mut self, points: [Pos2; 2]) {
|
2020-05-23 20:10:39 +00:00
|
|
|
self.reserve(2);
|
2020-05-11 11:11:01 +00:00
|
|
|
let normal = (points[1] - points[0]).normalized().rot90();
|
|
|
|
self.add_point(points[0], normal);
|
|
|
|
self.add_point(points[1], normal);
|
|
|
|
}
|
|
|
|
|
2020-05-23 12:14:36 +00:00
|
|
|
pub fn add_open_points(&mut self, points: &[Pos2]) {
|
2020-04-18 22:27:25 +00:00
|
|
|
let n = points.len();
|
|
|
|
assert!(n >= 2);
|
|
|
|
|
2020-05-11 11:11:01 +00:00
|
|
|
if n == 2 {
|
|
|
|
// Common case optimization:
|
|
|
|
self.add_line_segment([points[0], points[1]]);
|
|
|
|
} else {
|
2020-09-01 18:03:50 +00:00
|
|
|
// TODO: optimize
|
2020-05-23 20:10:39 +00:00
|
|
|
self.reserve(n);
|
2020-05-11 11:11:01 +00:00
|
|
|
self.add_point(points[0], (points[1] - points[0]).normalized().rot90());
|
|
|
|
for i in 1..n - 1 {
|
2020-09-01 18:29:00 +00:00
|
|
|
let mut n0 = (points[i] - points[i - 1]).normalized().rot90();
|
|
|
|
let mut n1 = (points[i + 1] - points[i]).normalized().rot90();
|
|
|
|
|
|
|
|
// Handle duplicated points (but not triplicated...):
|
|
|
|
if n0 == Vec2::zero() {
|
|
|
|
n0 = n1;
|
|
|
|
} else if n1 == Vec2::zero() {
|
|
|
|
n1 = n0;
|
|
|
|
}
|
|
|
|
|
2020-05-11 11:11:01 +00:00
|
|
|
let v = (n0 + n1) / 2.0;
|
2020-09-01 18:03:50 +00:00
|
|
|
let normal = v / v.length_sq(); // TODO: handle VERY sharp turns better
|
|
|
|
self.add_point(points[i], normal);
|
2020-05-11 11:11:01 +00:00
|
|
|
}
|
|
|
|
self.add_point(
|
|
|
|
points[n - 1],
|
|
|
|
(points[n - 1] - points[n - 2]).normalized().rot90(),
|
|
|
|
);
|
2020-04-18 22:27:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-17 07:44:09 +00:00
|
|
|
pub fn add_line_loop(&mut self, points: &[Pos2]) {
|
|
|
|
let n = points.len();
|
|
|
|
assert!(n >= 2);
|
2020-05-23 20:10:39 +00:00
|
|
|
self.reserve(n);
|
2020-05-17 07:44:09 +00:00
|
|
|
|
|
|
|
// TODO: optimize
|
|
|
|
for i in 0..n {
|
2020-09-01 18:29:00 +00:00
|
|
|
let mut n0 = (points[i] - points[(i + n - 1) % n]).normalized().rot90();
|
|
|
|
let mut n1 = (points[(i + 1) % n] - points[i]).normalized().rot90();
|
|
|
|
|
|
|
|
// Handle duplicated points (but not triplicated...):
|
|
|
|
if n0 == Vec2::zero() {
|
|
|
|
n0 = n1;
|
|
|
|
} else if n1 == Vec2::zero() {
|
|
|
|
n1 = n0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if n1 == Vec2::zero() {
|
|
|
|
// continue
|
|
|
|
// }
|
2020-05-17 07:44:09 +00:00
|
|
|
let v = (n0 + n1) / 2.0;
|
2020-09-01 18:03:50 +00:00
|
|
|
let normal = v / v.length_sq(); // TODO: handle VERY sharp turns better
|
|
|
|
self.add_point(points[i], normal);
|
2020-05-17 07:44:09 +00:00
|
|
|
}
|
|
|
|
}
|
2020-09-01 18:03:50 +00:00
|
|
|
}
|
2020-05-17 07:44:09 +00:00
|
|
|
|
2020-09-01 18:03:50 +00:00
|
|
|
pub(crate) mod path {
|
|
|
|
//! Helpers for constructing paths
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
/// overwrites existing points
|
|
|
|
pub fn rounded_rectangle(path: &mut Vec<Pos2>, rect: Rect, corner_radius: f32) {
|
|
|
|
path.clear();
|
2020-04-18 22:27:25 +00:00
|
|
|
|
2020-04-25 13:45:38 +00:00
|
|
|
let min = rect.min;
|
|
|
|
let max = rect.max;
|
2020-04-18 22:27:25 +00:00
|
|
|
|
|
|
|
let cr = corner_radius
|
|
|
|
.min(rect.width() * 0.5)
|
|
|
|
.min(rect.height() * 0.5);
|
|
|
|
|
|
|
|
if cr <= 0.0 {
|
2020-09-01 18:03:50 +00:00
|
|
|
let min = rect.min;
|
|
|
|
let max = rect.max;
|
|
|
|
path.reserve(4);
|
|
|
|
path.push(pos2(min.x, min.y));
|
|
|
|
path.push(pos2(max.x, min.y));
|
|
|
|
path.push(pos2(max.x, max.y));
|
|
|
|
path.push(pos2(min.x, max.y));
|
2020-04-18 22:27:25 +00:00
|
|
|
} else {
|
2020-09-01 18:03:50 +00:00
|
|
|
add_circle_quadrant(path, pos2(max.x - cr, max.y - cr), cr, 0.0);
|
|
|
|
add_circle_quadrant(path, pos2(min.x + cr, max.y - cr), cr, 1.0);
|
|
|
|
add_circle_quadrant(path, pos2(min.x + cr, min.y + cr), cr, 2.0);
|
|
|
|
add_circle_quadrant(path, pos2(max.x - cr, min.y + cr), cr, 3.0);
|
2020-04-18 22:27:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-21 16:53:43 +00:00
|
|
|
/// Add one quadrant of a circle
|
|
|
|
///
|
|
|
|
/// * quadrant 0: right bottom
|
|
|
|
/// * quadrant 1: left bottom
|
|
|
|
/// * quadrant 2: left top
|
|
|
|
/// * quadrant 3: right top
|
|
|
|
//
|
|
|
|
// Derivation:
|
|
|
|
//
|
|
|
|
// * angle 0 * TAU / 4 = right
|
|
|
|
// - quadrant 0: right bottom
|
|
|
|
// * angle 1 * TAU / 4 = bottom
|
|
|
|
// - quadrant 1: left bottom
|
|
|
|
// * angle 2 * TAU / 4 = left
|
|
|
|
// - quadrant 2: left top
|
|
|
|
// * angle 3 * TAU / 4 = top
|
|
|
|
// - quadrant 3: right top
|
|
|
|
// * angle 4 * TAU / 4 = right
|
2020-09-01 18:03:50 +00:00
|
|
|
pub fn add_circle_quadrant(path: &mut Vec<Pos2>, center: Pos2, radius: f32, quadrant: f32) {
|
2020-06-03 08:59:02 +00:00
|
|
|
// TODO: optimize with precalculated vertices for some radii ranges
|
|
|
|
|
|
|
|
let n = (radius * 0.75).round() as i32; // TODO: tweak a bit more
|
2020-05-17 07:44:09 +00:00
|
|
|
let n = clamp(n, 2..=32);
|
2020-04-18 22:27:25 +00:00
|
|
|
const RIGHT_ANGLE: f32 = TAU / 4.0;
|
2020-09-01 18:03:50 +00:00
|
|
|
path.reserve(n as usize + 1);
|
2020-04-18 22:27:25 +00:00
|
|
|
for i in 0..=n {
|
|
|
|
let angle = remap(
|
|
|
|
i as f32,
|
2020-04-25 09:11:44 +00:00
|
|
|
0.0..=n as f32,
|
|
|
|
quadrant * RIGHT_ANGLE..=(quadrant + 1.0) * RIGHT_ANGLE,
|
2020-04-18 22:27:25 +00:00
|
|
|
);
|
2020-09-01 18:03:50 +00:00
|
|
|
path.push(center + radius * Vec2::angled(angle));
|
2020-04-18 22:27:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2019-02-24 16:18:30 +00:00
|
|
|
#[derive(Clone, Copy, PartialEq)]
|
|
|
|
pub enum PathType {
|
|
|
|
Open,
|
|
|
|
Closed,
|
|
|
|
}
|
2020-05-07 08:47:03 +00:00
|
|
|
use self::PathType::{Closed, Open};
|
2019-02-24 16:18:30 +00:00
|
|
|
|
2020-12-28 23:51:27 +00:00
|
|
|
/// Tessellation quality options
|
2020-10-12 06:37:56 +00:00
|
|
|
#[derive(Clone, Copy, Debug)]
|
2020-12-28 23:51:27 +00:00
|
|
|
pub struct TessellationOptions {
|
2020-05-11 15:57:11 +00:00
|
|
|
/// Size of a pixel in points, e.g. 0.5
|
2019-02-24 16:18:30 +00:00
|
|
|
pub aa_size: f32,
|
2020-10-10 05:21:45 +00:00
|
|
|
/// Anti-aliasing makes shapes appear smoother, but requires more triangles and is therefore slower.
|
2020-11-07 13:06:14 +00:00
|
|
|
/// By default this is enabled in release builds and disabled in debug builds.
|
2020-10-10 05:21:45 +00:00
|
|
|
pub anti_alias: bool,
|
|
|
|
/// If `true` (default) cull certain primitives before tessellating them
|
|
|
|
pub coarse_tessellation_culling: bool,
|
2020-08-09 15:24:32 +00:00
|
|
|
/// Output the clip rectangles to be painted?
|
2020-04-21 08:33:33 +00:00
|
|
|
pub debug_paint_clip_rects: bool,
|
2020-10-10 09:39:39 +00:00
|
|
|
/// If true, no clipping will be done
|
|
|
|
pub debug_ignore_clip_rects: bool,
|
2020-04-21 08:33:33 +00:00
|
|
|
}
|
|
|
|
|
2020-12-28 23:51:27 +00:00
|
|
|
impl Default for TessellationOptions {
|
2020-04-21 08:33:33 +00:00
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
|
|
|
aa_size: 1.0,
|
2020-12-19 14:11:43 +00:00
|
|
|
anti_alias: true,
|
2020-11-07 10:44:32 +00:00
|
|
|
coarse_tessellation_culling: true,
|
2020-04-21 08:33:33 +00:00
|
|
|
debug_paint_clip_rects: false,
|
2020-10-10 09:39:39 +00:00
|
|
|
debug_ignore_clip_rects: false,
|
2020-04-21 08:33:33 +00:00
|
|
|
}
|
|
|
|
}
|
2019-02-24 16:18:30 +00:00
|
|
|
}
|
|
|
|
|
2020-12-28 23:51:27 +00:00
|
|
|
/// Tessellate the given convex area into a polygon.
|
2020-11-07 10:44:32 +00:00
|
|
|
fn fill_closed_path(
|
|
|
|
path: &[PathPoint],
|
|
|
|
color: Srgba,
|
2020-12-28 23:51:27 +00:00
|
|
|
options: TessellationOptions,
|
2020-11-07 10:44:32 +00:00
|
|
|
out: &mut Triangles,
|
|
|
|
) {
|
2020-05-11 15:57:11 +00:00
|
|
|
if color == color::TRANSPARENT {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-04-18 22:27:25 +00:00
|
|
|
let n = path.len() as u32;
|
|
|
|
if options.anti_alias {
|
2020-08-21 16:53:43 +00:00
|
|
|
out.reserve_triangles(3 * n as usize);
|
|
|
|
out.reserve_vertices(2 * n as usize);
|
2020-05-11 15:57:11 +00:00
|
|
|
let color_outer = color::TRANSPARENT;
|
2020-08-21 16:53:43 +00:00
|
|
|
let idx_inner = out.vertices.len() as u32;
|
2020-04-18 22:27:25 +00:00
|
|
|
let idx_outer = idx_inner + 1;
|
|
|
|
for i in 2..n {
|
2020-08-21 16:53:43 +00:00
|
|
|
out.add_triangle(idx_inner + 2 * (i - 1), idx_inner, idx_inner + 2 * i);
|
2020-04-18 22:27:25 +00:00
|
|
|
}
|
|
|
|
let mut i0 = n - 1;
|
|
|
|
for i1 in 0..n {
|
|
|
|
let p1 = &path[i1 as usize];
|
|
|
|
let dm = p1.normal * options.aa_size * 0.5;
|
2020-09-02 19:52:43 +00:00
|
|
|
out.colored_vertex(p1.pos - dm, color);
|
|
|
|
out.colored_vertex(p1.pos + dm, color_outer);
|
2020-08-21 16:53:43 +00:00
|
|
|
out.add_triangle(idx_inner + i1 * 2, idx_inner + i0 * 2, idx_outer + 2 * i0);
|
|
|
|
out.add_triangle(idx_outer + i0 * 2, idx_outer + i1 * 2, idx_inner + 2 * i1);
|
2020-04-18 22:27:25 +00:00
|
|
|
i0 = i1;
|
|
|
|
}
|
|
|
|
} else {
|
2020-08-21 16:53:43 +00:00
|
|
|
out.reserve_triangles(n as usize);
|
|
|
|
let idx = out.vertices.len() as u32;
|
2020-09-02 19:52:43 +00:00
|
|
|
out.vertices.extend(path.iter().map(|p| Vertex {
|
|
|
|
pos: p.pos,
|
|
|
|
uv: WHITE_UV,
|
|
|
|
color,
|
|
|
|
}));
|
2020-04-18 22:27:25 +00:00
|
|
|
for i in 2..n {
|
2020-08-21 16:53:43 +00:00
|
|
|
out.add_triangle(idx, idx + i - 1, idx + i);
|
2019-02-24 16:18:30 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-18 22:27:25 +00:00
|
|
|
}
|
2019-01-05 14:28:07 +00:00
|
|
|
|
2020-12-28 23:51:27 +00:00
|
|
|
/// Tessellate the given path as a stroke with thickness.
|
2020-09-01 21:54:21 +00:00
|
|
|
fn stroke_path(
|
2020-04-18 22:27:25 +00:00
|
|
|
path: &[PathPoint],
|
2020-08-21 16:53:43 +00:00
|
|
|
path_type: PathType,
|
2020-09-01 21:54:21 +00:00
|
|
|
stroke: Stroke,
|
2020-12-28 23:51:27 +00:00
|
|
|
options: TessellationOptions,
|
2020-08-21 16:53:43 +00:00
|
|
|
out: &mut Triangles,
|
2020-04-18 22:27:25 +00:00
|
|
|
) {
|
2020-09-01 21:54:21 +00:00
|
|
|
if stroke.width <= 0.0 || stroke.color == color::TRANSPARENT {
|
2020-05-11 15:57:11 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-04-18 22:27:25 +00:00
|
|
|
let n = path.len() as u32;
|
2020-08-21 16:53:43 +00:00
|
|
|
let idx = out.vertices.len() as u32;
|
2020-04-18 22:27:25 +00:00
|
|
|
|
|
|
|
if options.anti_alias {
|
2020-09-01 21:54:21 +00:00
|
|
|
let color_inner = stroke.color;
|
2020-05-11 15:57:11 +00:00
|
|
|
let color_outer = color::TRANSPARENT;
|
|
|
|
|
2020-09-01 21:54:21 +00:00
|
|
|
let thin_line = stroke.width <= options.aa_size;
|
2020-04-18 22:27:25 +00:00
|
|
|
if thin_line {
|
2020-05-11 15:57:11 +00:00
|
|
|
/*
|
|
|
|
We paint the line using three edges: outer, inner, outer.
|
|
|
|
|
|
|
|
. o i o outer, inner, outer
|
|
|
|
. |---| aa_size (pixel width)
|
|
|
|
*/
|
|
|
|
|
2020-04-18 22:27:25 +00:00
|
|
|
// Fade out as it gets thinner:
|
2020-09-01 21:54:21 +00:00
|
|
|
let color_inner = mul_color(color_inner, stroke.width / options.aa_size);
|
2020-05-11 15:57:11 +00:00
|
|
|
if color_inner == color::TRANSPARENT {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-08-21 16:53:43 +00:00
|
|
|
out.reserve_triangles(4 * n as usize);
|
|
|
|
out.reserve_vertices(3 * n as usize);
|
2020-05-20 19:22:53 +00:00
|
|
|
|
2020-05-11 15:57:11 +00:00
|
|
|
let mut i0 = n - 1;
|
|
|
|
for i1 in 0..n {
|
|
|
|
let connect_with_previous = path_type == PathType::Closed || i1 > 0;
|
2020-04-18 22:27:25 +00:00
|
|
|
let p1 = &path[i1 as usize];
|
|
|
|
let p = p1.pos;
|
|
|
|
let n = p1.normal;
|
2020-09-02 19:52:43 +00:00
|
|
|
out.colored_vertex(p + n * options.aa_size, color_outer);
|
|
|
|
out.colored_vertex(p, color_inner);
|
|
|
|
out.colored_vertex(p - n * options.aa_size, color_outer);
|
2020-04-18 22:27:25 +00:00
|
|
|
|
|
|
|
if connect_with_previous {
|
2020-08-21 16:53:43 +00:00
|
|
|
out.add_triangle(idx + 3 * i0 + 0, idx + 3 * i0 + 1, idx + 3 * i1 + 0);
|
|
|
|
out.add_triangle(idx + 3 * i0 + 1, idx + 3 * i1 + 0, idx + 3 * i1 + 1);
|
2020-04-18 22:27:25 +00:00
|
|
|
|
2020-08-21 16:53:43 +00:00
|
|
|
out.add_triangle(idx + 3 * i0 + 1, idx + 3 * i0 + 2, idx + 3 * i1 + 1);
|
|
|
|
out.add_triangle(idx + 3 * i0 + 2, idx + 3 * i1 + 1, idx + 3 * i1 + 2);
|
2020-04-18 22:27:25 +00:00
|
|
|
}
|
2020-05-11 15:57:11 +00:00
|
|
|
i0 = i1;
|
|
|
|
}
|
|
|
|
} else {
|
2020-08-09 15:24:32 +00:00
|
|
|
// thick line
|
2020-05-11 15:57:11 +00:00
|
|
|
// TODO: line caps for really thick lines?
|
|
|
|
|
|
|
|
/*
|
|
|
|
We paint the line using four edges: outer, inner, inner, outer
|
|
|
|
|
|
|
|
. o i p i o outer, inner, point, inner, outer
|
|
|
|
. |---| aa_size (pixel width)
|
|
|
|
. |--------------| width
|
|
|
|
. |---------| outer_rad
|
|
|
|
. |-----| inner_rad
|
|
|
|
*/
|
|
|
|
|
2020-08-21 16:53:43 +00:00
|
|
|
out.reserve_triangles(6 * n as usize);
|
|
|
|
out.reserve_vertices(4 * n as usize);
|
2020-05-20 19:22:53 +00:00
|
|
|
|
2020-05-11 15:57:11 +00:00
|
|
|
let mut i0 = n - 1;
|
|
|
|
for i1 in 0..n {
|
|
|
|
let connect_with_previous = path_type == PathType::Closed || i1 > 0;
|
2020-09-01 21:54:21 +00:00
|
|
|
let inner_rad = 0.5 * (stroke.width - options.aa_size);
|
|
|
|
let outer_rad = 0.5 * (stroke.width + options.aa_size);
|
2020-04-18 22:27:25 +00:00
|
|
|
let p1 = &path[i1 as usize];
|
|
|
|
let p = p1.pos;
|
|
|
|
let n = p1.normal;
|
2020-09-02 19:52:43 +00:00
|
|
|
out.colored_vertex(p + n * outer_rad, color_outer);
|
|
|
|
out.colored_vertex(p + n * inner_rad, color_inner);
|
|
|
|
out.colored_vertex(p - n * inner_rad, color_inner);
|
|
|
|
out.colored_vertex(p - n * outer_rad, color_outer);
|
2020-04-18 22:27:25 +00:00
|
|
|
|
|
|
|
if connect_with_previous {
|
2020-08-21 16:53:43 +00:00
|
|
|
out.add_triangle(idx + 4 * i0 + 0, idx + 4 * i0 + 1, idx + 4 * i1 + 0);
|
|
|
|
out.add_triangle(idx + 4 * i0 + 1, idx + 4 * i1 + 0, idx + 4 * i1 + 1);
|
2020-04-18 22:27:25 +00:00
|
|
|
|
2020-08-21 16:53:43 +00:00
|
|
|
out.add_triangle(idx + 4 * i0 + 1, idx + 4 * i0 + 2, idx + 4 * i1 + 1);
|
|
|
|
out.add_triangle(idx + 4 * i0 + 2, idx + 4 * i1 + 1, idx + 4 * i1 + 2);
|
2020-04-18 22:27:25 +00:00
|
|
|
|
2020-08-21 16:53:43 +00:00
|
|
|
out.add_triangle(idx + 4 * i0 + 2, idx + 4 * i0 + 3, idx + 4 * i1 + 2);
|
|
|
|
out.add_triangle(idx + 4 * i0 + 3, idx + 4 * i1 + 2, idx + 4 * i1 + 3);
|
2020-04-18 22:27:25 +00:00
|
|
|
}
|
2020-05-11 15:57:11 +00:00
|
|
|
i0 = i1;
|
2019-01-05 19:14:16 +00:00
|
|
|
}
|
2020-04-18 22:27:25 +00:00
|
|
|
}
|
|
|
|
} else {
|
2020-08-21 16:53:43 +00:00
|
|
|
out.reserve_triangles(2 * n as usize);
|
|
|
|
out.reserve_vertices(2 * n as usize);
|
2020-05-20 19:22:53 +00:00
|
|
|
|
2020-04-18 22:27:25 +00:00
|
|
|
let last_index = if path_type == Closed { n } else { n - 1 };
|
|
|
|
for i in 0..last_index {
|
2020-08-21 16:53:43 +00:00
|
|
|
out.add_triangle(
|
2020-04-18 22:27:25 +00:00
|
|
|
idx + (2 * i + 0) % (2 * n),
|
|
|
|
idx + (2 * i + 1) % (2 * n),
|
|
|
|
idx + (2 * i + 2) % (2 * n),
|
|
|
|
);
|
2020-08-21 16:53:43 +00:00
|
|
|
out.add_triangle(
|
2020-04-18 22:27:25 +00:00
|
|
|
idx + (2 * i + 2) % (2 * n),
|
|
|
|
idx + (2 * i + 1) % (2 * n),
|
|
|
|
idx + (2 * i + 3) % (2 * n),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-09-01 21:54:21 +00:00
|
|
|
let thin_line = stroke.width <= options.aa_size;
|
2020-05-11 15:57:11 +00:00
|
|
|
if thin_line {
|
|
|
|
// Fade out thin lines rather than making them thinner
|
|
|
|
let radius = options.aa_size / 2.0;
|
2020-09-01 21:54:21 +00:00
|
|
|
let color = mul_color(stroke.color, stroke.width / options.aa_size);
|
2020-05-11 15:57:11 +00:00
|
|
|
if color == color::TRANSPARENT {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
for p in path {
|
2020-09-02 19:52:43 +00:00
|
|
|
out.colored_vertex(p.pos + radius * p.normal, color);
|
|
|
|
out.colored_vertex(p.pos - radius * p.normal, color);
|
2020-05-11 15:57:11 +00:00
|
|
|
}
|
|
|
|
} else {
|
2020-09-01 21:54:21 +00:00
|
|
|
let radius = stroke.width / 2.0;
|
2020-05-11 15:57:11 +00:00
|
|
|
for p in path {
|
2020-09-02 19:52:43 +00:00
|
|
|
out.colored_vertex(p.pos + radius * p.normal, stroke.color);
|
|
|
|
out.colored_vertex(p.pos - radius * p.normal, stroke.color);
|
2020-05-11 15:57:11 +00:00
|
|
|
}
|
2019-01-05 14:28:07 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-18 22:27:25 +00:00
|
|
|
}
|
2019-01-05 14:28:07 +00:00
|
|
|
|
2020-08-29 14:58:01 +00:00
|
|
|
fn mul_color(color: Srgba, factor: f32) -> Srgba {
|
2020-05-11 15:57:11 +00:00
|
|
|
debug_assert!(0.0 <= factor && factor <= 1.0);
|
2020-08-29 14:58:01 +00:00
|
|
|
// sRGBA correct fading requires conversion to linear space and back again because of premultiplied alpha
|
|
|
|
Rgba::from(color).multiply(factor).into()
|
2020-05-11 15:57:11 +00:00
|
|
|
}
|
|
|
|
|
2020-04-18 22:27:25 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
2019-01-05 19:14:16 +00:00
|
|
|
|
2020-12-28 23:51:27 +00:00
|
|
|
/// Tessellate a single [`PaintCmd`] into a [`Triangles`].
|
2020-08-21 16:53:43 +00:00
|
|
|
///
|
2020-12-28 23:51:27 +00:00
|
|
|
/// * `command`: the command to tessellate
|
|
|
|
/// * `options`: tessellation quality
|
2020-08-29 14:58:01 +00:00
|
|
|
/// * `fonts`: font source when tessellating text
|
2020-08-21 16:53:43 +00:00
|
|
|
/// * `out`: where the triangles are put
|
|
|
|
/// * `scratchpad_path`: if you plan to run `tessellate_paint_command`
|
|
|
|
/// many times, pass it a reference to the same `Path` to avoid excessive allocations.
|
2020-09-01 18:03:50 +00:00
|
|
|
fn tessellate_paint_command(
|
2020-12-28 23:51:27 +00:00
|
|
|
options: TessellationOptions,
|
2020-05-11 11:11:01 +00:00
|
|
|
fonts: &Fonts,
|
2020-12-16 20:59:33 +00:00
|
|
|
clip_rect: Rect,
|
|
|
|
command: PaintCmd,
|
2020-05-19 18:54:02 +00:00
|
|
|
out: &mut Triangles,
|
2020-09-01 18:03:50 +00:00
|
|
|
scratchpad_points: &mut Vec<Pos2>,
|
2020-08-21 16:53:43 +00:00
|
|
|
scratchpad_path: &mut Path,
|
2020-05-11 11:11:01 +00:00
|
|
|
) {
|
2020-08-21 16:53:43 +00:00
|
|
|
let path = scratchpad_path;
|
2020-05-11 11:11:01 +00:00
|
|
|
path.clear();
|
|
|
|
|
2020-04-20 21:33:16 +00:00
|
|
|
match command {
|
2020-07-30 12:11:09 +00:00
|
|
|
PaintCmd::Noop => {}
|
2020-04-20 21:33:16 +00:00
|
|
|
PaintCmd::Circle {
|
|
|
|
center,
|
2020-08-21 16:53:43 +00:00
|
|
|
radius,
|
2020-05-23 12:17:40 +00:00
|
|
|
fill,
|
2020-09-01 21:54:21 +00:00
|
|
|
stroke,
|
2020-04-20 21:33:16 +00:00
|
|
|
} => {
|
2020-10-10 05:21:45 +00:00
|
|
|
if radius <= 0.0 {
|
|
|
|
return;
|
2020-04-20 21:33:16 +00:00
|
|
|
}
|
2020-10-10 05:21:45 +00:00
|
|
|
|
|
|
|
if options.coarse_tessellation_culling
|
|
|
|
&& !clip_rect.expand(radius + stroke.width).contains(center)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
path.add_circle(center, radius);
|
|
|
|
fill_closed_path(&path.0, fill, options, out);
|
|
|
|
stroke_path(&path.0, Closed, stroke, options, out);
|
2019-01-05 14:28:07 +00:00
|
|
|
}
|
2020-05-19 18:54:02 +00:00
|
|
|
PaintCmd::Triangles(triangles) => {
|
2020-10-21 09:04:22 +00:00
|
|
|
if triangles.is_valid() {
|
2020-10-21 09:09:42 +00:00
|
|
|
out.append(triangles);
|
2020-10-21 09:04:22 +00:00
|
|
|
} else {
|
2020-11-02 16:41:52 +00:00
|
|
|
debug_assert!(false, "Invalid Triangles in PaintCmd::Triangles");
|
2020-10-21 09:04:22 +00:00
|
|
|
}
|
2020-04-20 21:33:16 +00:00
|
|
|
}
|
2020-09-01 21:54:21 +00:00
|
|
|
PaintCmd::LineSegment { points, stroke } => {
|
2020-05-11 11:11:01 +00:00
|
|
|
path.add_line_segment(points);
|
2020-09-01 21:54:21 +00:00
|
|
|
stroke_path(&path.0, Open, stroke, options, out);
|
2020-05-11 11:11:01 +00:00
|
|
|
}
|
2020-04-20 21:33:16 +00:00
|
|
|
PaintCmd::Path {
|
2020-09-01 18:03:50 +00:00
|
|
|
points,
|
2020-04-20 21:33:16 +00:00
|
|
|
closed,
|
2020-05-23 12:17:40 +00:00
|
|
|
fill,
|
2020-09-01 21:54:21 +00:00
|
|
|
stroke,
|
2020-04-20 21:33:16 +00:00
|
|
|
} => {
|
2020-09-01 18:03:50 +00:00
|
|
|
if points.len() >= 2 {
|
|
|
|
if closed {
|
|
|
|
path.add_line_loop(&points);
|
|
|
|
} else {
|
|
|
|
path.add_open_points(&points);
|
|
|
|
}
|
|
|
|
|
2020-08-31 20:56:24 +00:00
|
|
|
if fill != TRANSPARENT {
|
2020-05-23 09:28:21 +00:00
|
|
|
debug_assert!(
|
|
|
|
closed,
|
|
|
|
"You asked to fill a path that is not closed. That makes no sense."
|
|
|
|
);
|
2020-08-21 16:53:43 +00:00
|
|
|
fill_closed_path(&path.0, fill, options, out);
|
2020-05-23 09:28:21 +00:00
|
|
|
}
|
2020-08-31 20:56:24 +00:00
|
|
|
let typ = if closed { Closed } else { Open };
|
2020-09-01 21:54:21 +00:00
|
|
|
stroke_path(&path.0, typ, stroke, options, out);
|
2020-04-20 21:33:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
PaintCmd::Rect {
|
2020-08-21 16:53:43 +00:00
|
|
|
mut rect,
|
2020-04-20 21:33:16 +00:00
|
|
|
corner_radius,
|
2020-05-23 12:17:40 +00:00
|
|
|
fill,
|
2020-09-01 21:54:21 +00:00
|
|
|
stroke,
|
2020-04-20 21:33:16 +00:00
|
|
|
} => {
|
2020-10-10 05:21:45 +00:00
|
|
|
if rect.is_empty() {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if options.coarse_tessellation_culling
|
|
|
|
&& !rect.expand(stroke.width).intersects(clip_rect)
|
|
|
|
{
|
|
|
|
return;
|
2020-04-20 21:33:16 +00:00
|
|
|
}
|
2020-10-10 05:21:45 +00:00
|
|
|
|
|
|
|
// It is common to (sometimes accidentally) create an infinitely sized rectangle.
|
|
|
|
// Make sure we can handle that:
|
|
|
|
rect.min = rect.min.at_least(pos2(-1e7, -1e7));
|
|
|
|
rect.max = rect.max.at_most(pos2(1e7, 1e7));
|
|
|
|
|
|
|
|
path::rounded_rectangle(scratchpad_points, rect, corner_radius);
|
|
|
|
path.add_line_loop(scratchpad_points);
|
|
|
|
fill_closed_path(&path.0, fill, options, out);
|
|
|
|
stroke_path(&path.0, Closed, stroke, options, out);
|
2020-04-20 21:33:16 +00:00
|
|
|
}
|
|
|
|
PaintCmd::Text {
|
|
|
|
pos,
|
2020-05-16 16:17:35 +00:00
|
|
|
galley,
|
2020-04-20 21:33:16 +00:00
|
|
|
text_style,
|
2020-05-16 16:17:35 +00:00
|
|
|
color,
|
2020-04-20 21:33:16 +00:00
|
|
|
} => {
|
2020-12-28 23:51:27 +00:00
|
|
|
tessellate_text(
|
2020-12-16 20:59:33 +00:00
|
|
|
options, fonts, clip_rect, pos, &galley, text_style, color, out,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[allow(clippy::too_many_arguments)]
|
2020-12-28 23:51:27 +00:00
|
|
|
fn tessellate_text(
|
|
|
|
options: TessellationOptions,
|
2020-12-16 20:59:33 +00:00
|
|
|
fonts: &Fonts,
|
|
|
|
clip_rect: Rect,
|
|
|
|
pos: Pos2,
|
|
|
|
galley: &super::Galley,
|
|
|
|
text_style: super::TextStyle,
|
|
|
|
color: Srgba,
|
|
|
|
out: &mut Triangles,
|
|
|
|
) {
|
|
|
|
if color == TRANSPARENT {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
galley.sanity_check();
|
|
|
|
|
|
|
|
let num_chars = galley.text.chars().count();
|
|
|
|
out.reserve_triangles(num_chars * 2);
|
|
|
|
out.reserve_vertices(num_chars * 4);
|
|
|
|
|
|
|
|
let tex_w = fonts.texture().width as f32;
|
|
|
|
let tex_h = fonts.texture().height as f32;
|
|
|
|
|
|
|
|
let text_offset = vec2(0.0, 1.0); // Eye-balled for buttons. TODO: why is this needed?
|
|
|
|
|
|
|
|
let clip_rect = clip_rect.expand(2.0); // Some fudge to handle letters that are slightly larger than expected.
|
|
|
|
|
|
|
|
let font = &fonts[text_style];
|
|
|
|
let mut chars = galley.text.chars();
|
|
|
|
for line in &galley.rows {
|
|
|
|
let line_min_y = pos.y + line.y_min + text_offset.x;
|
|
|
|
let line_max_y = line_min_y + font.row_height();
|
|
|
|
let is_line_visible = line_max_y >= clip_rect.min.y && line_min_y <= clip_rect.max.y;
|
|
|
|
|
|
|
|
for x_offset in line.x_offsets.iter().take(line.x_offsets.len() - 1) {
|
|
|
|
let c = chars.next().unwrap();
|
|
|
|
|
|
|
|
if options.coarse_tessellation_culling && !is_line_visible {
|
|
|
|
// culling individual lines of text is important, since a single `PaintCmd::Text`
|
|
|
|
// can span hundreds of lines.
|
|
|
|
continue;
|
2020-08-31 20:56:24 +00:00
|
|
|
}
|
2020-12-16 20:59:33 +00:00
|
|
|
|
|
|
|
if let Some(glyph) = font.uv_rect(c) {
|
|
|
|
let mut left_top = pos + glyph.offset + vec2(*x_offset, line.y_min) + text_offset;
|
|
|
|
left_top.x = font.round_to_pixel(left_top.x); // Pixel-perfection.
|
|
|
|
left_top.y = font.round_to_pixel(left_top.y); // Pixel-perfection.
|
|
|
|
|
|
|
|
let pos = Rect::from_min_max(left_top, left_top + glyph.size);
|
|
|
|
let uv = Rect::from_min_max(
|
|
|
|
pos2(glyph.min.0 as f32 / tex_w, glyph.min.1 as f32 / tex_h),
|
|
|
|
pos2(glyph.max.0 as f32 / tex_w, glyph.max.1 as f32 / tex_h),
|
|
|
|
);
|
|
|
|
out.add_rect_with_uv(pos, uv, color);
|
2019-01-04 13:14:32 +00:00
|
|
|
}
|
2020-12-16 20:59:33 +00:00
|
|
|
}
|
|
|
|
if line.ends_with_newline {
|
|
|
|
let newline = chars.next().unwrap();
|
|
|
|
debug_assert_eq!(newline, '\n');
|
2019-01-04 13:14:32 +00:00
|
|
|
}
|
|
|
|
}
|
2020-12-16 20:59:33 +00:00
|
|
|
assert_eq!(chars.next(), None);
|
2019-01-04 13:14:32 +00:00
|
|
|
}
|
2020-04-20 21:33:16 +00:00
|
|
|
|
2020-12-27 13:16:37 +00:00
|
|
|
/// Turns [`PaintCmd`]:s into sets of triangles.
|
2020-08-21 16:53:43 +00:00
|
|
|
///
|
|
|
|
/// The given commands will be painted back-to-front (painters algorithm).
|
|
|
|
/// They will be batched together by clip rectangle.
|
|
|
|
///
|
2020-12-28 23:51:27 +00:00
|
|
|
/// * `commands`: the command to tessellate
|
|
|
|
/// * `options`: tessellation quality
|
2020-08-29 14:58:01 +00:00
|
|
|
/// * `fonts`: font source when tessellating text
|
2020-08-21 16:53:43 +00:00
|
|
|
///
|
|
|
|
/// ## Returns
|
2020-12-27 13:16:37 +00:00
|
|
|
/// A list of clip rectangles with matching [`Triangles`].
|
2020-07-23 08:27:21 +00:00
|
|
|
pub fn tessellate_paint_commands(
|
2020-08-21 16:53:43 +00:00
|
|
|
commands: Vec<(Rect, PaintCmd)>,
|
2020-12-28 23:51:27 +00:00
|
|
|
options: TessellationOptions,
|
2020-04-20 21:33:16 +00:00
|
|
|
fonts: &Fonts,
|
2020-05-19 18:54:02 +00:00
|
|
|
) -> Vec<(Rect, Triangles)> {
|
2020-09-01 18:03:50 +00:00
|
|
|
let mut scratchpad_points = Vec::new();
|
2020-08-21 16:53:43 +00:00
|
|
|
let mut scratchpad_path = Path::default();
|
2020-05-11 11:11:01 +00:00
|
|
|
|
2020-07-18 22:01:13 +00:00
|
|
|
let mut jobs = PaintJobs::default();
|
2020-04-20 21:33:16 +00:00
|
|
|
for (clip_rect, cmd) in commands {
|
2020-10-21 09:09:42 +00:00
|
|
|
let start_new_job = match jobs.last() {
|
|
|
|
None => true,
|
|
|
|
Some(job) => job.0 != clip_rect || job.1.texture_id != cmd.texture_id(),
|
|
|
|
};
|
2020-09-10 14:48:01 +00:00
|
|
|
|
2020-10-21 09:09:42 +00:00
|
|
|
if start_new_job {
|
2020-07-18 22:01:13 +00:00
|
|
|
jobs.push((clip_rect, Triangles::default()));
|
2020-04-20 21:33:16 +00:00
|
|
|
}
|
|
|
|
|
2020-07-18 22:01:13 +00:00
|
|
|
let out = &mut jobs.last_mut().unwrap().1;
|
2020-09-01 18:03:50 +00:00
|
|
|
tessellate_paint_command(
|
|
|
|
options,
|
|
|
|
fonts,
|
2020-12-16 20:59:33 +00:00
|
|
|
clip_rect,
|
|
|
|
cmd,
|
2020-09-01 18:03:50 +00:00
|
|
|
out,
|
|
|
|
&mut scratchpad_points,
|
|
|
|
&mut scratchpad_path,
|
|
|
|
);
|
2020-04-20 21:33:16 +00:00
|
|
|
}
|
|
|
|
|
2020-05-11 16:14:02 +00:00
|
|
|
if options.debug_paint_clip_rects {
|
2020-07-18 22:01:13 +00:00
|
|
|
for (clip_rect, triangles) in &mut jobs {
|
2020-07-23 08:27:21 +00:00
|
|
|
tessellate_paint_command(
|
2020-12-16 20:59:33 +00:00
|
|
|
options,
|
|
|
|
fonts,
|
2020-10-10 05:21:45 +00:00
|
|
|
Rect::everything(),
|
2020-05-11 16:14:02 +00:00
|
|
|
PaintCmd::Rect {
|
|
|
|
rect: *clip_rect,
|
|
|
|
corner_radius: 0.0,
|
2020-08-31 20:56:24 +00:00
|
|
|
fill: Default::default(),
|
2020-09-01 21:54:21 +00:00
|
|
|
stroke: Stroke::new(2.0, srgba(150, 255, 150, 255)),
|
2020-05-11 16:14:02 +00:00
|
|
|
},
|
2020-05-19 18:54:02 +00:00
|
|
|
triangles,
|
2020-09-01 18:03:50 +00:00
|
|
|
&mut scratchpad_points,
|
2020-08-21 16:53:43 +00:00
|
|
|
&mut scratchpad_path,
|
2020-05-11 16:14:02 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-10 09:39:39 +00:00
|
|
|
if options.debug_ignore_clip_rects {
|
|
|
|
for (clip_rect, _) in &mut jobs {
|
|
|
|
*clip_rect = Rect::everything();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-21 09:04:22 +00:00
|
|
|
for (_, triangles) in &jobs {
|
|
|
|
debug_assert!(
|
|
|
|
triangles.is_valid(),
|
2020-12-28 23:51:27 +00:00
|
|
|
"Tessellator generated invalid Triangles"
|
2020-10-21 09:04:22 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-07-18 22:01:13 +00:00
|
|
|
jobs
|
2020-04-20 21:33:16 +00:00
|
|
|
}
|