mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-16 16:42:52 +00:00
Effect › 3D and Materials › 3D (Classic) › Revolve (Classic)… sweeps the profile round a vertical axis, as Revolve does, and draws every face with the Wireframe, No Shading, Diffuse and Plastic surfaces of Extrude & Bevel (Classic). - `effect.apply` with `threeD.classicRevolve`: Angle (0 to 360), Cap, Offset (0 to 1000 pt) from the left or right edge of the profile's bounds, the Position rotation and Perspective, and the classic lights, Ambient Light, highlight, Blend Steps and Shading Color. - Groups and layers revolve about one shared axis, and the effect also goes on a fill or stroke, as Revolve's does. - `effect.lightingPreset` refuses it, as it refuses Extrude & Bevel (Classic). - Revolve faces carry their view-space normal, and `three_d::revolve_facets` hands them to the classic surface code. - Unknown parameters, numbers out of range and bad flags, edges, surfaces and colors are refused with an error.
126 lines
6.2 KiB
Rust
126 lines
6.2 KiB
Rust
use crate::*;
|
|
|
|
const TOWARD: [f64; 3] = [0.0, 0.0, 1.0];
|
|
|
|
fn close(a: f64, b: f64) -> bool {
|
|
(a - b).abs() < 1e-9
|
|
}
|
|
|
|
fn one(light: [f64; 3], intensity: f64) -> [([f64; 3], f64); 1] {
|
|
[(light, intensity)]
|
|
}
|
|
|
|
#[test]
|
|
fn wireframe_and_no_shading_leave_every_face_unlit() {
|
|
for surface in [ClassicSurface::Wireframe, ClassicSurface::NoShading] {
|
|
let s = ClassicShading { surface, ..ClassicShading::default() };
|
|
for normal in [TOWARD, [1.0, 0.0, 0.0], [0.0, 0.0, -1.0]] {
|
|
assert_eq!(s.tone(normal, &one(TOWARD, 1.0)), (1.0, 0.0), "{surface:?}");
|
|
}
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn diffuse_adds_the_light_to_the_ambient_and_has_no_highlight() {
|
|
let s = ClassicShading { surface: ClassicSurface::Diffuse, ambient: 0.3, blend_steps: 256, ..ClassicShading::default() };
|
|
let (lit, spec) = s.tone(TOWARD, &one(TOWARD, 0.4));
|
|
assert!((lit - 0.7).abs() < 1.0 / 256.0, "{lit}");
|
|
assert_eq!(spec, 0.0);
|
|
// A face turned away from the light gets the ambient light only.
|
|
let (away, _) = s.tone(TOWARD, &one([1.0, 0.0, 0.0], 0.4));
|
|
assert!((away - 0.3).abs() < 1.0 / 256.0, "{away}");
|
|
// A face at 60° to the light gets half of it.
|
|
let (half, _) = s.tone(TOWARD, &one([3f64.sqrt() / 2.0, 0.0, 0.5], 0.4));
|
|
assert!((half - 0.5).abs() < 1.0 / 256.0, "{half}");
|
|
// The tone stays within full shadow and full light.
|
|
assert_eq!(ClassicShading { ambient: 1.0, ..s }.tone(TOWARD, &one(TOWARD, 1.0)).0, 1.0);
|
|
// Without lights a face gets the ambient light only.
|
|
assert!((s.tone(TOWARD, &[]).0 - 0.3).abs() < 1.0 / 256.0);
|
|
}
|
|
|
|
#[test]
|
|
fn lights_add_up() {
|
|
let s = ClassicShading { surface: ClassicSurface::Diffuse, ambient: 0.0, blend_steps: 256, ..ClassicShading::default() };
|
|
let left = [-0.6, 0.0, 0.8];
|
|
let right = [0.6, 0.0, 0.8];
|
|
let both = s.tone(TOWARD, &[(left, 0.3), (right, 0.3)]).0;
|
|
let single = s.tone(TOWARD, &one(left, 0.3)).0;
|
|
assert!((both - 2.0 * single).abs() < 2.0 / 256.0, "{both} {single}");
|
|
// A light behind the object lights the faces turned away from the viewer only.
|
|
assert_eq!(s.tone(TOWARD, &one([0.0, 0.0, -1.0], 1.0)).0, 0.0);
|
|
assert!(s.tone([0.0, 0.0, -1.0], &one([0.0, 0.0, -1.0], 1.0)).0 > 0.99);
|
|
}
|
|
|
|
#[test]
|
|
fn plastic_highlight_peaks_where_a_face_mirrors_the_light_to_the_eye() {
|
|
let s = ClassicShading { highlight_intensity: 0.8, blend_steps: 256, ..ClassicShading::default() };
|
|
let light = one([0.6, 0.0, 0.8], 1.0);
|
|
// The face halfway between the light and the eye mirrors the light straight to the viewer.
|
|
let length = 0.6f64.hypot(1.8);
|
|
let half = [0.6 / length, 0.0, 1.8 / length];
|
|
let (_, peak) = s.tone(half, &light);
|
|
assert!((peak - 0.8).abs() < 1.0 / 256.0, "{peak}");
|
|
let (_, aside) = s.tone([-0.6, 0.0, 0.8], &light);
|
|
assert!(aside < peak / 4.0, "{aside}");
|
|
// A smaller highlight falls off faster.
|
|
let tilted = [0.3, 0.0, 0.954];
|
|
let broad = s.tone(tilted, &light).1;
|
|
let narrow = ClassicShading { highlight_size: 0.1, ..s }.tone(tilted, &light).1;
|
|
assert!(narrow < broad, "{narrow} {broad}");
|
|
// No highlight on a face the light does not reach, without Highlight Intensity, or on Diffuse.
|
|
assert_eq!(s.tone(TOWARD, &one([0.0, 0.0, -1.0], 1.0)).1, 0.0);
|
|
assert_eq!(ClassicShading { highlight_intensity: 0.0, ..s }.tone(half, &light).1, 0.0);
|
|
assert_eq!(ClassicShading { surface: ClassicSurface::Diffuse, ..s }.tone(half, &light).1, 0.0);
|
|
}
|
|
|
|
#[test]
|
|
fn blend_steps_round_the_shading_to_that_many_levels() {
|
|
let s = ClassicShading { surface: ClassicSurface::Diffuse, ambient: 0.0, blend_steps: 2, ..ClassicShading::default() };
|
|
let tones: Vec<f64> = (0..=20).map(|k| f64::from(k) / 20.0).map(|c| s.tone(TOWARD, &one([(1.0 - c * c).sqrt(), 0.0, c], 0.5)).0).collect();
|
|
assert!(tones.iter().all(|t| close(*t, 0.0) || close(*t, 0.5)), "{tones:?}");
|
|
assert!(tones.iter().any(|t| close(*t, 0.0)) && tones.iter().any(|t| close(*t, 0.5)), "{tones:?}");
|
|
// Out-of-range step counts and intensities are kept within their ranges.
|
|
for steps in [0, 100_000] {
|
|
let t = ClassicShading { blend_steps: steps, ..s }.tone(TOWARD, &one(TOWARD, f64::NAN)).0;
|
|
assert!((0.0..=1.0).contains(&t));
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn surfaces_round_trip_through_their_ids() {
|
|
for surface in ClassicSurface::ALL {
|
|
assert_eq!(ClassicSurface::parse(surface.id()), Some(surface));
|
|
}
|
|
assert_eq!(ClassicSurface::parse("glossy"), None);
|
|
assert_eq!(ClassicSurface::default(), ClassicSurface::Plastic);
|
|
}
|
|
|
|
#[test]
|
|
fn revolve_facets_keep_each_face_normal_turned_to_the_viewer() {
|
|
// A cylinder of radius 30 about the left edge, with closed ends, tipped toward the viewer.
|
|
let mut b = BezPath::new();
|
|
b.move_to((100.0, 0.0));
|
|
b.line_to((130.0, 0.0));
|
|
b.line_to((130.0, 100.0));
|
|
b.line_to((100.0, 100.0));
|
|
let path = PathData::from_bezpath(&b);
|
|
let options = Revolve { rotation_x: -30.0, shade: false, smooth_shading: false, ..Revolve::default() };
|
|
let faces = revolve(&path, path.bounds().unwrap(), options).unwrap();
|
|
let facets = revolve_facets(&path, path.bounds().unwrap(), options).unwrap();
|
|
assert_eq!(facets.len(), faces.len());
|
|
for (facet, face) in facets.iter().zip(&faces) {
|
|
let [x, y, z] = facet.normal;
|
|
assert!(((x * x + y * y + z * z).sqrt() - 1.0).abs() < 1e-9, "unit length: {:?}", facet.normal);
|
|
assert!(z >= 0.0, "turned to the viewer: {:?}", facet.normal);
|
|
assert_eq!((facet.depth, facet.side), (face.depth, !face.cap));
|
|
}
|
|
// Tipped 30°, the wall faces nearest the viewer turn 30° down from it, and the flat top and bottom
|
|
// (the profile's horizontal edges swept round) face up and out at 30°.
|
|
let (s, c) = 30f64.to_radians().sin_cos();
|
|
assert!(facets.iter().any(|f| f.normal[2] > c - 0.01 && (f.normal[1] - s).abs() < 0.01), "{facets:?}");
|
|
assert!(facets.iter().any(|f| (f.normal[1] + c).abs() < 1e-6 && (f.normal[2] - s).abs() < 1e-6), "{facets:?}");
|
|
// Moved off the axis, the profile's open ends close with caps, which are ends rather than sides.
|
|
let ring = revolve_facets(&path, path.bounds().unwrap(), Revolve { offset: 10.0, ..options }).unwrap();
|
|
assert!(ring.iter().any(|f| !f.side && (f.normal[1] + c).abs() < 1e-6 && (f.normal[2] - s).abs() < 1e-6));
|
|
}
|