M13.8: Advanced 3D iris depth of field, collapsed precomps, extruded strokes
Depth of field in Advanced 3D comps uses the camera's iris (Fast Rectangle, Triangle to Decagon, rotation, roundness, aspect, diffraction fringe) and highlight gain/threshold/ saturation through the Classic 3D bokeh kernel. Collapsed precomps in an Advanced 3D comp add their nested layers as real geometry (meshes, extrusions, cards) lit by the parent; 2D collapsed precomps draw nested 3D layers with the parent's renderer. Text and shape strokes extrude as bevelled meshes stacked in paint order (model extrude_stacked). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
553a51316c
commit
07da394ccb
@@ -6,9 +6,10 @@
|
||||
//! is rasterised with a depth buffer on the GPU when an [`crate::Accelerator`] implements
|
||||
//! [`crate::Accelerator::raster_3d`], otherwise by the software rasteriser ([`raster`]), at 2×2
|
||||
//! supersampling. The resolve (box filter), depth of field (a gather blur by each pixel's
|
||||
//! circle of confusion) and the conversion back to the working encoding run on the CPU for
|
||||
//! circle of confusion, shaped by the camera's iris: [`dof`]) and the conversion back to the working encoding run on the CPU for
|
||||
//! both paths, and the result is composited over the layers below the run.
|
||||
|
||||
pub mod dof;
|
||||
pub mod raster;
|
||||
pub mod scene;
|
||||
pub mod shade;
|
||||
@@ -16,13 +17,19 @@ pub mod shade;
|
||||
use effectcraft_project::Layer;
|
||||
use rayon::prelude::*;
|
||||
|
||||
pub use dof::depth_of_field;
|
||||
pub use raster::Target;
|
||||
pub use scene::{EnvInfo, Light, Material, Scene, ShadowMap, TexInfo, Vertex};
|
||||
|
||||
use super::camera::Dof;
|
||||
use super::compose::camera_for;
|
||||
use crate::{EvalCtx, Image, Renderer};
|
||||
|
||||
/// Whether a comp draws its 3D layers with the Advanced 3D renderer: its own setting, or for a
|
||||
/// collapsed precomp's layers, the setting of the outermost comp they collapse into.
|
||||
pub(crate) fn active(r: &Renderer, ctx: &EvalCtx) -> bool {
|
||||
r.collapse3d.map_or(ctx.comp, |c| c.parent.comp).renderer == effectcraft_project::Renderer::Advanced3D
|
||||
}
|
||||
|
||||
/// Box-filter a supersampled target down by `k`: (premultiplied colour, mean depth of the
|
||||
/// covered subsamples, ∞ where none).
|
||||
pub fn resolve(t: &Target, k: u32) -> (Image, Vec<f32>) {
|
||||
@@ -56,68 +63,6 @@ pub fn resolve(t: &Target, k: u32) -> (Image, Vec<f32>) {
|
||||
(img, depth)
|
||||
}
|
||||
|
||||
/// Depth of field: a gather blur where each pixel collects the neighbours whose circle of
|
||||
/// confusion (from their depth, in output pixels) reaches it; nearer in-focus surfaces are not
|
||||
/// smeared by the blur of what is behind them.
|
||||
pub fn depth_of_field(img: &Image, depth: &[f32], dof: &Dof, scale: f64) -> Image {
|
||||
let (w, h) = (img.width as i64, img.height as i64);
|
||||
let max_r = 48.0f32;
|
||||
let radius: Vec<f32> = depth
|
||||
.iter()
|
||||
.map(|&z| {
|
||||
let z = if z.is_finite() { z as f64 } else { 1.0e9 };
|
||||
((dof.coc(z) * scale * 0.5) as f32).min(max_r)
|
||||
})
|
||||
.collect();
|
||||
let rmax = radius.iter().fold(0.0f32, |a, &b| a.max(b));
|
||||
if rmax < 0.5 {
|
||||
return img.clone();
|
||||
}
|
||||
// Disk samples (golden-angle spiral) over the largest radius.
|
||||
const N: usize = 64;
|
||||
let golden = std::f32::consts::PI * (3.0 - 5.0f32.sqrt());
|
||||
let taps: Vec<(f32, f32, f32)> = (1..=N)
|
||||
.map(|i| {
|
||||
let r = (i as f32 / N as f32).sqrt() * rmax;
|
||||
let a = i as f32 * golden;
|
||||
(r * a.cos(), r * a.sin(), r)
|
||||
})
|
||||
.collect();
|
||||
let mut out = Image::new(img.width, img.height);
|
||||
out.data.par_chunks_mut(w as usize).enumerate().for_each(|(y, row)| {
|
||||
for x in 0..w as usize {
|
||||
let i = y * w as usize + x;
|
||||
let (rp, zp) = (radius[i], depth[i]);
|
||||
let cw = 1.0 / (std::f32::consts::PI * rp * rp).max(1.0);
|
||||
let c = img.data[i];
|
||||
let mut acc = [c[0] * cw, c[1] * cw, c[2] * cw, c[3] * cw];
|
||||
let mut wsum = cw;
|
||||
for &(dx, dy, d) in &taps {
|
||||
let (sx, sy) = (x as i64 + dx.round() as i64, y as i64 + dy.round() as i64);
|
||||
if sx < 0 || sy < 0 || sx >= w || sy >= h {
|
||||
continue;
|
||||
}
|
||||
let j = (sy * w + sx) as usize;
|
||||
// A sample behind this pixel blurs no further than this pixel's own circle.
|
||||
let rq = if depth[j] > zp { radius[j].min(rp) } else { radius[j] };
|
||||
let cov = (rq - d + 0.5).clamp(0.0, 1.0);
|
||||
if cov <= 0.0 {
|
||||
continue;
|
||||
}
|
||||
// Each tap stands for an annulus of area ≈ π·rmax²/N.
|
||||
let wq = cov / (std::f32::consts::PI * rq * rq).max(1.0) * (std::f32::consts::PI * rmax * rmax / N as f32);
|
||||
let q = img.data[j];
|
||||
for k in 0..4 {
|
||||
acc[k] += q[k] * wq;
|
||||
}
|
||||
wsum += wq;
|
||||
}
|
||||
row[x] = acc.map(|v| v / wsum);
|
||||
}
|
||||
});
|
||||
out
|
||||
}
|
||||
|
||||
/// Linear → sRGB-encoded premultiplied pixels (the working encoding of non-linear projects).
|
||||
pub fn encode(img: &mut Image) {
|
||||
img.data.par_iter_mut().for_each(|p| {
|
||||
|
||||
Reference in New Issue
Block a user