| 1 | //! Position evaluators: `VolumeFog`, `VolumeMask`, `ZtoWorldPos`. |
| 2 | |
| 3 | use super::*; |
| 4 | use crate::scene3d::{Mat4, SceneLights, collect_scene, eval_transform3d, mat4_identity, mat4_inverse}; |
| 5 | |
| 6 | const SHAPES: [&str; 3] = ["Sphere", "Box", "Cylinder"]; |
| 7 | |
| 8 | /// Camera used to unproject eye-space Z into world space. |
| 9 | pub(super) struct Rig { |
| 10 | world: Mat4, |
| 11 | pub(super) tan_half: f32, |
| 12 | ortho: bool, |
| 13 | near: f32, |
| 14 | far: f32, |
| 15 | } |
| 16 | |
| 17 | /// Where a world-position kernel reads positions from: `code` 0 = `aux.z` + camera, |
| 18 | /// 1 = `WorldPosImage` RGB, 2 = nothing usable. |
| 19 | struct WorldSource { |
| 20 | code: f32, |
| 21 | view: Option<Arc<wgpu::TextureView>>, |
| 22 | hash: u64, |
| 23 | image: Option<CachedTexture>, |
| 24 | } |
| 25 | |
| 26 | fn pack_columns(u: &mut UniformParams, first_slot: usize, m: &Mat4) { |
| 27 | for i in 0..4 { |
| 28 | u.p[first_slot + i] = [m[i * 4], m[i * 4 + 1], m[i * 4 + 2], m[i * 4 + 3]]; |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | impl Engine { |
| 33 | /// A connected `Camera` (`Camera3D` scene) or the explicit `FOV` / `NearClip` / `FarClip` inputs; |
| 34 | /// `own_transform` additionally reads the node's own `Transform3DOp` as the camera pose. |
| 35 | pub(super) fn camera_rig(&self, comp: &Comp, node: &Node, time: f64, own_transform: bool) -> Rig { |
| 36 | if let Some(id) = conn_any(comp, node, &["Camera"]) { |
| 37 | let (mut cams, mut meshes, mut lights) = (Vec::new(), Vec::new(), SceneLights::default()); |
| 38 | let aspect = comp.width as f32 / comp.height.max(1) as f32; |
| 39 | collect_scene(comp, id, time, &self.expr, mat4_identity(), aspect, &mut cams, &mut meshes, &mut lights, 0); |
| 40 | if let Some(c) = cams.first() { |
| 41 | return Rig { |
| 42 | world: c.world, |
| 43 | tan_half: (c.vfov_rad * 0.5).tan(), |
| 44 | ortho: c.orthographic, |
| 45 | near: c.near, |
| 46 | far: c.far, |
| 47 | }; |
| 48 | } |
| 49 | } |
| 50 | let fov = num_any(comp, node, &["FOV", "AoV", "AngleOfView"], time, 24.302, &self.expr).clamp(0.01, 179.0); |
| 51 | Rig { |
| 52 | world: if own_transform { eval_transform3d(comp, node, time, &self.expr) } else { mat4_identity() }, |
| 53 | tan_half: (fov.to_radians() * 0.5).tan() as f32, |
| 54 | ortho: false, |
| 55 | near: num_any(comp, node, &["NearClip", "Near"], time, 0.0, &self.expr) as f32, |
| 56 | far: num_any(comp, node, &["FarClip", "Far"], time, 0.0, &self.expr) as f32, |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | #[allow(clippy::too_many_arguments)] |
| 61 | fn world_source( |
| 62 | &mut self, |
| 63 | comp: &Comp, |
| 64 | node: &Node, |
| 65 | time: f64, |
| 66 | src: Option<&CachedTexture>, |
| 67 | enc: &mut wgpu::CommandEncoder, |
| 68 | visiting: &mut HashSet<(NodeId, i64)>, |
| 69 | stats: &mut EvalStats, |
| 70 | ) -> WorldSource { |
| 71 | if let Some(img) = self.df_input(comp, node, &["WorldPosImage", "WorldPos"], time, enc, visiting, stats) { |
| 72 | return WorldSource { code: 1.0, view: Some(img.view.clone()), hash: img.content_hash, image: Some(img) }; |
| 73 | } |
| 74 | match src.and_then(|s| s.aux.as_ref()?.z.as_ref().map(|z| (z.view.clone(), s.content_hash))) { |
| 75 | Some((view, hash)) => WorldSource { code: 0.0, view: Some(view), hash, image: None }, |
| 76 | None => WorldSource { code: 2.0, view: None, hash: 0, image: None }, |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | fn pack_world(u: &mut UniformParams, rig: &Rig, size: [u32; 2], code: f32) { |
| 81 | pack_columns(u, 0, &rig.world); |
| 82 | u.p[4] = [rig.tan_half, size[0] as f32 / size[1].max(1) as f32, b2f(rig.ortho), code]; |
| 83 | u.p[5] = [rig.near, rig.far, 0.0, 0.0]; |
| 84 | } |
| 85 | |
| 86 | /// World-to-local matrix of the node's own `Transform3DOp` (unit shape in local space) and the `Shape` index. |
| 87 | fn pack_shape(&self, u: &mut UniformParams, comp: &Comp, node: &Node, time: f64) { |
| 88 | pack_columns(u, 6, &mat4_inverse(eval_transform3d(comp, node, time, &self.expr))); |
| 89 | u.p[11][0] = enum_idx(comp, node, "Shape", &SHAPES, time, 0, &self.expr) as f32; |
| 90 | } |
| 91 | |
| 92 | /// `VolumeFog`: fog density inside a transformed Sphere / Box / Cylinder volume in world space, |
| 93 | /// with optional fBm turbulence. World positions come from `WorldPosImage` or `aux.z` + `Camera`. |
| 94 | #[allow(clippy::too_many_arguments)] |
| 95 | pub(in crate::eval) fn eval_volume_fog( |
| 96 | &mut self, |
| 97 | comp: &Comp, |
| 98 | node: &Node, |
| 99 | time: f64, |
| 100 | frame_i: i64, |
| 101 | enc: &mut wgpu::CommandEncoder, |
| 102 | visiting: &mut HashSet<(NodeId, i64)>, |
| 103 | stats: &mut EvalStats, |
| 104 | ) -> CachedTexture { |
| 105 | let Some(src) = self.df_input(comp, node, &INPUT_PORTS, time, enc, visiting, stats) else { |
| 106 | return self.dummy.clone(); |
| 107 | }; |
| 108 | let blend = self.df_blend(comp, node, time); |
| 109 | let world = self.world_source(comp, node, time, Some(&src), enc, visiting, stats); |
| 110 | if (blend.0 <= 1e-6 && !blend.1) || world.code > 1.5 { |
| 111 | return src; |
| 112 | } |
| 113 | let fin = self.df_fin(comp, node, time, blend, src.size, enc, visiting, stats); |
| 114 | |
| 115 | let mut u = UniformParams::default(); |
| 116 | Self::pack_world(&mut u, &self.camera_rig(comp, node, time, false), src.size, world.code); |
| 117 | self.pack_shape(&mut u, comp, node, time); |
| 118 | let n = |keys: &[&str], d: f64| num_any(comp, node, keys, time, d, &self.expr) as f32; |
| 119 | u.p[10] = [n(&["FogRed"], 1.0), n(&["FogGreen"], 1.0), n(&["FogBlue"], 1.0), n(&["FogAlpha"], 1.0)]; |
| 120 | u.p[11][1] = n(&["Density"], 1.0).max(0.0); |
| 121 | u.p[11][2] = n(&["Falloff"], 0.25).max(0.0); |
| 122 | u.p[11][3] = n(&["Noise", "Turbulence"], 0.0).clamp(0.0, 1.0); |
| 123 | u.p[12][0] = n(&["NoiseScale", "Detail"], 1.0).max(1e-3); |
| 124 | u.p[12][1] = n(&["Seed", "NoiseSeed"], 0.0) * 13.7; |
| 125 | |
| 126 | let depth = self.node_depth(comp, node, time, Some(src.depth)); |
| 127 | let pass = DfPass { |
| 128 | tag: "VolumeFog", |
| 129 | op: DeepFlowOp::VolumeFog, |
| 130 | srcs: [Some(&src.view), world.view.as_deref(), None, None], |
| 131 | hashes: &[src.content_hash, world.hash], |
| 132 | size: src.size, |
| 133 | depth, |
| 134 | }; |
| 135 | self.df_run(node, frame_i, pass, &fin, &u, src.aux.clone(), enc, stats) |
| 136 | } |
| 137 | |
| 138 | /// `VolumeMask`: matte of a transformed Sphere / Box / Cylinder volume in world space, written to |
| 139 | /// the alpha of `Input` (`Output = Alpha`) or to an RGBA matte (`Output = Mask`). |
| 140 | #[allow(clippy::too_many_arguments)] |
| 141 | pub(in crate::eval) fn eval_volume_mask( |
| 142 | &mut self, |
| 143 | comp: &Comp, |
| 144 | node: &Node, |
| 145 | time: f64, |
| 146 | frame_i: i64, |
| 147 | enc: &mut wgpu::CommandEncoder, |
| 148 | visiting: &mut HashSet<(NodeId, i64)>, |
| 149 | stats: &mut EvalStats, |
| 150 | ) -> CachedTexture { |
| 151 | let input = self.df_input(comp, node, &INPUT_PORTS, time, enc, visiting, stats); |
| 152 | let world = self.world_source(comp, node, time, input.as_ref(), enc, visiting, stats); |
| 153 | let Some(src) = input.or_else(|| world.image.clone()) else { |
| 154 | return self.dummy.clone(); |
| 155 | }; |
| 156 | let blend = self.df_blend(comp, node, time); |
| 157 | if blend.0 <= 1e-6 && !blend.1 { |
| 158 | return src; |
| 159 | } |
| 160 | let fin = self.df_fin(comp, node, time, blend, src.size, enc, visiting, stats); |
| 161 | |
| 162 | let mut u = UniformParams::default(); |
| 163 | Self::pack_world(&mut u, &self.camera_rig(comp, node, time, false), src.size, world.code); |
| 164 | self.pack_shape(&mut u, comp, node, time); |
| 165 | u.p[12][2] = num_any(comp, node, &["Softness"], time, 0.0, &self.expr).max(0.0) as f32; |
| 166 | u.p[12][3] = b2f(num_any(comp, node, &["Invert"], time, 0.0, &self.expr) > 0.5); |
| 167 | u.p[13][0] = enum_idx(comp, node, "Output", &["Alpha", "Mask"], time, 0, &self.expr) as f32; |
| 168 | |
| 169 | let depth = self.node_depth(comp, node, time, Some(src.depth)); |
| 170 | let pass = DfPass { |
| 171 | tag: "VolumeMask", |
| 172 | op: DeepFlowOp::VolumeMask, |
| 173 | srcs: [Some(&src.view), world.view.as_deref(), None, None], |
| 174 | hashes: &[src.content_hash, world.hash], |
| 175 | size: src.size, |
| 176 | depth, |
| 177 | }; |
| 178 | self.df_run(node, frame_i, pass, &fin, &u, src.aux.clone(), enc, stats) |
| 179 | } |
| 180 | |
| 181 | /// `ZtoWorldPos`: unprojects `aux.z` into world-space XYZ (RGB, A = 1 where depth is valid) using a |
| 182 | /// connected `Camera` or the node's own `FOV` / `NearClip` / `FarClip` / `Transform3DOp` inputs. |
| 183 | #[allow(clippy::too_many_arguments)] |
| 184 | pub(in crate::eval) fn eval_z_to_world_pos( |
| 185 | &mut self, |
| 186 | comp: &Comp, |
| 187 | node: &Node, |
| 188 | time: f64, |
| 189 | frame_i: i64, |
| 190 | enc: &mut wgpu::CommandEncoder, |
| 191 | visiting: &mut HashSet<(NodeId, i64)>, |
| 192 | stats: &mut EvalStats, |
| 193 | ) -> CachedTexture { |
| 194 | let Some(src) = self.df_input(comp, node, &INPUT_PORTS, time, enc, visiting, stats) else { |
| 195 | return self.dummy.clone(); |
| 196 | }; |
| 197 | let blend = self.df_blend(comp, node, time); |
| 198 | let Some(z) = src.aux.as_ref().and_then(|a| a.z.as_ref()).map(|z| z.view.clone()) else { |
| 199 | return src; |
| 200 | }; |
| 201 | if blend.0 <= 1e-6 && !blend.1 { |
| 202 | return src; |
| 203 | } |
| 204 | let fin = self.df_fin(comp, node, time, blend, src.size, enc, visiting, stats); |
| 205 | |
| 206 | let mut u = UniformParams::default(); |
| 207 | Self::pack_world(&mut u, &self.camera_rig(comp, node, time, true), src.size, 0.0); |
| 208 | |
| 209 | // World coordinates exceed [0, 1], so integer depths are promoted. |
| 210 | let depth = self.node_depth(comp, node, time, Some(src.depth)); |
| 211 | let pass = DfPass { |
| 212 | tag: "ZtoWorldPos", |
| 213 | op: DeepFlowOp::ZToWorld, |
| 214 | srcs: [Some(&src.view), Some(&z), None, None], |
| 215 | hashes: &[src.content_hash], |
| 216 | size: src.size, |
| 217 | depth: if depth.is_float() { depth } else { BitDepth::Float16 }, |
| 218 | }; |
| 219 | self.df_run(node, frame_i, pass, &fin, &u, src.aux.clone(), enc, stats) |
| 220 | } |
| 221 | } |