| ... | @@ -1,12 +1,15 @@ | ... | @@ -1,12 +1,15 @@ |
| 1 | //! 3D scene graph traversal, material resolution, and 3D transform evaluation. | 1 | //! 3D scene graph traversal, material resolution, and 3D transform evaluation. |
| 2 | | 2 | |
| | 3 | use std::sync::Arc; |
| | 4 | |
| 3 | use super::fbx::load_mesh_cached; | 5 | use super::fbx::load_mesh_cached; |
| | 6 | use super::mesh::{build_cone, build_icosahedron, build_shape_mesh, build_torus, smooth_normals, triangle_soup}; |
| 4 | use super::math::{ | 7 | use super::math::{ |
| 5 | mat4_identity, mat4_inverse_rigid, mat4_mul, mat4_rot_x, mat4_rot_y, mat4_rot_z, mat4_scale, mat4_translate, | 8 | mat4_identity, mat4_inverse_rigid, mat4_mul, mat4_rot_x, mat4_rot_y, mat4_rot_z, mat4_scale, mat4_translate, |
| 6 | }; | 9 | }; |
| 7 | use super::{ | 10 | use super::{ |
| 8 | CameraInstance, DirLight, Displace3DSpec, Mat4, MaterialFx, MeshInstance, PointLight, SceneLights, | 11 | CameraInstance, DirLight, Displace3DSpec, Mat4, MaterialFx, MeshInstance, PointLight, SceneLights, |
| 9 | ShapeKind, SpecModel, SpotLight, Text3DSpec, UvAffine, CUBE_FACES, | 12 | ShapeKind, SpecModel, SpotLight, Text3DSpec, UvAffine, Vertex3D, CUBE_FACES, |
| 10 | }; | 13 | }; |
| 11 | use model::{Comp, ExprEngine, Node, NodeId}; | 14 | use model::{Comp, ExprEngine, Node, NodeId}; |
| 12 | | 15 | |
| ... | @@ -16,6 +19,8 @@ struct SceneOverrides { | ... | @@ -16,6 +19,8 @@ struct SceneOverrides { |
| 16 | affected_by_lights: Option<bool>, | 19 | affected_by_lights: Option<bool>, |
| 17 | casts_shadow: Option<bool>, | 20 | casts_shadow: Option<bool>, |
| 18 | flip_normals: bool, | 21 | flip_normals: bool, |
| | 22 | /// Smoothing angle of an enclosing `ReplaceNormals3D` that recomputes normals. |
| | 23 | smooth_normals_deg: Option<f32>, |
| 19 | displace: Option<Displace3DSpec>, | 24 | displace: Option<Displace3DSpec>, |
| 20 | cull_back: bool, | 25 | cull_back: bool, |
| 21 | cull_front: bool, | 26 | cull_front: bool, |
| ... | @@ -162,9 +167,12 @@ fn collect_scene_inner( | ... | @@ -162,9 +167,12 @@ fn collect_scene_inner( |
| 162 | } | 167 | } |
| 163 | "ReplaceNormals3D" => { | 168 | "ReplaceNormals3D" => { |
| 164 | let mut next_ovr = ovr.clone(); | 169 | let mut next_ovr = ovr.clone(); |
| 165 | if comp.eval_number(node, "FlipNormals", time, 0.0, expr) > 0.5 { | 170 | if comp.eval_number(node, "Normals.Flip", time, 0.0, expr) > 0.5 { |
| 166 | next_ovr.flip_normals = !next_ovr.flip_normals; | 171 | next_ovr.flip_normals = !next_ovr.flip_normals; |
| 167 | } | 172 | } |
| | 173 | if comp.eval_number(node, "Normals.Recompute", time, 0.0, expr).round() as i32 == 0 { |
| | 174 | next_ovr.smooth_normals_deg = Some(comp.eval_number(node, "Normals.SmoothingAngle", time, 45.0, expr) as f32); |
| | 175 | } |
| 168 | if let Some(src_id) = node.connected_node("SceneInput").and_then(|s| comp.find_node(s)) { | 176 | if let Some(src_id) = node.connected_node("SceneInput").and_then(|s| comp.find_node(s)) { |
| 169 | collect_scene_inner(comp, src_id, time, expr, world, render_aspect, &next_ovr, cameras, meshes, lights, visited, depth + 1); | 177 | collect_scene_inner(comp, src_id, time, expr, world, render_aspect, &next_ovr, cameras, meshes, lights, visited, depth + 1); |
| 170 | } | 178 | } |
| ... | @@ -257,12 +265,20 @@ fn collect_scene_inner( | ... | @@ -257,12 +265,20 @@ fn collect_scene_inner( |
| 257 | let vfov_rad = 2.0 * half_v.atan(); | 265 | let vfov_rad = 2.0 * half_v.atan(); |
| 258 | let near = comp.eval_number(node, "PlaneOfFocus", time, 0.05, expr).min(0.05).max(0.005) as f32; | 266 | let near = comp.eval_number(node, "PlaneOfFocus", time, 0.05, expr).min(0.05).max(0.005) as f32; |
| 259 | let far = 5000.0_f32; | 267 | let far = 5000.0_f32; |
| 260 | cameras.push(CameraInstance { world, vfov_rad, near, far }); | 268 | cameras.push(CameraInstance { |
| | 269 | name: node.name.clone(), |
| | 270 | orthographic: node.static_str("ProjectionType") == Some("Orthographic"), |
| | 271 | world, |
| | 272 | vfov_rad, |
| | 273 | near, |
| | 274 | far, |
| | 275 | }); |
| 261 | if let Some(src_id) = node.connected_node("SceneInput").and_then(|s| comp.find_node(s)) { | 276 | if let Some(src_id) = node.connected_node("SceneInput").and_then(|s| comp.find_node(s)) { |
| 262 | collect_scene_inner(comp, src_id, time, expr, world, render_aspect, ovr, cameras, meshes, lights, visited, depth + 1); | 277 | collect_scene_inner(comp, src_id, time, expr, world, render_aspect, ovr, cameras, meshes, lights, visited, depth + 1); |
| 263 | } | 278 | } |
| 264 | } | 279 | } |
| 265 | "LightAmbient" => { | 280 | "LightAmbient" => { |
| | 281 | lights.count += (comp.eval_number(node, "Enabled", time, 1.0, expr) > 0.5) as u32; |
| 266 | if let Some(color) = light_color(comp, node, time, expr) { | 282 | if let Some(color) = light_color(comp, node, time, expr) { |
| 267 | for (acc, c) in lights.ambient.iter_mut().zip(color) { | 283 | for (acc, c) in lights.ambient.iter_mut().zip(color) { |
| 268 | *acc += c; | 284 | *acc += c; |
| ... | @@ -273,6 +289,7 @@ fn collect_scene_inner( | ... | @@ -273,6 +289,7 @@ fn collect_scene_inner( |
| 273 | } | 289 | } |
| 274 | } | 290 | } |
| 275 | "LightDirectional" => { | 291 | "LightDirectional" => { |
| | 292 | lights.count += (comp.eval_number(node, "Enabled", time, 1.0, expr) > 0.5) as u32; |
| 276 | if let Some(color) = light_color(comp, node, time, expr) { | 293 | if let Some(color) = light_color(comp, node, time, expr) { |
| 277 | // Default Fusion LightDirectional shines along -Z => direction TO light is +Z transformed by world rotation! | 294 | // Default Fusion LightDirectional shines along -Z => direction TO light is +Z transformed by world rotation! |
| 278 | let dx = world[8]; | 295 | let dx = world[8]; |
| ... | @@ -286,6 +303,7 @@ fn collect_scene_inner( | ... | @@ -286,6 +303,7 @@ fn collect_scene_inner( |
| 286 | } | 303 | } |
| 287 | } | 304 | } |
| 288 | "LightPoint" => { | 305 | "LightPoint" => { |
| | 306 | lights.count += (comp.eval_number(node, "Enabled", time, 1.0, expr) > 0.5) as u32; |
| 289 | if let Some(color) = light_color(comp, node, time, expr) { | 307 | if let Some(color) = light_color(comp, node, time, expr) { |
| 290 | let decay_type = comp.eval_number(node, "DecayType", time, 0.0, expr) as f32; | 308 | let decay_type = comp.eval_number(node, "DecayType", time, 0.0, expr) as f32; |
| 291 | lights.points.push(PointLight { | 309 | lights.points.push(PointLight { |
| ... | @@ -302,6 +320,7 @@ fn collect_scene_inner( | ... | @@ -302,6 +320,7 @@ fn collect_scene_inner( |
| 302 | } | 320 | } |
| 303 | } | 321 | } |
| 304 | "LightSpot" => { | 322 | "LightSpot" => { |
| | 323 | lights.count += (comp.eval_number(node, "Enabled", time, 1.0, expr) > 0.5) as u32; |
| 305 | if let Some(color) = light_color(comp, node, time, expr) { | 324 | if let Some(color) = light_color(comp, node, time, expr) { |
| 306 | let cone_deg = comp.eval_number(node, "ConeAngle", time, 40.0, expr) as f32; | 325 | let cone_deg = comp.eval_number(node, "ConeAngle", time, 40.0, expr) as f32; |
| 307 | let penumbra_deg = comp.eval_number(node, "PenumbraAngle", time, 0.0, expr) as f32; | 326 | let penumbra_deg = comp.eval_number(node, "PenumbraAngle", time, 0.0, expr) as f32; |
| ... | @@ -359,8 +378,37 @@ fn collect_scene_inner( | ... | @@ -359,8 +378,37 @@ fn collect_scene_inner( |
| 359 | start_sweep_deg: start_sw, | 378 | start_sweep_deg: start_sw, |
| 360 | end_sweep_deg: if (end_sw - start_sw).abs() < 1e-3 { start_sw + 360.0 } else { end_sw }, | 379 | end_sweep_deg: if (end_sw - start_sw).abs() < 1e-3 { start_sw + 360.0 } else { end_sw }, |
| 361 | sub_base: comp.eval_number(node, "SurfaceCylinderInputs.SubdivisionLevelBase", time, 20.0, expr).round() as u32, | 380 | sub_base: comp.eval_number(node, "SurfaceCylinderInputs.SubdivisionLevelBase", time, 20.0, expr).round() as u32, |
| | 381 | top_cap: comp.eval_number(node, "SurfaceCylinderInputs.TopCap", time, 0.0, expr) > 0.5, |
| | 382 | bottom_cap: comp.eval_number(node, "SurfaceCylinderInputs.BottomCap", time, 0.0, expr) > 0.5, |
| 362 | } | 383 | } |
| 363 | } | 384 | } |
| | 385 | "SurfaceConeInputs" => { |
| | 386 | let f = |k: &str, d: f64| comp.eval_number(node, &format!("SurfaceConeInputs.{k}"), time, d, expr); |
| | 387 | let sweep = [f("StartSweep", 0.0) as f32, f("EndSweep", 360.0) as f32]; |
| | 388 | custom_shape(build_cone( |
| | 389 | f("Radius", 1.0) as f32, |
| | 390 | f("Height", 2.0) as f32, |
| | 391 | sweep, |
| | 392 | f("SubdivisionLevelBase", 20.0).round() as u32, |
| | 393 | f("SubdivisionLevelHeight", 1.0).round() as u32, |
| | 394 | f("BottomCap", 0.0) > 0.5, |
| | 395 | )) |
| | 396 | } |
| | 397 | "SurfaceTorusInputs" => { |
| | 398 | let f = |k: &str, d: f64| comp.eval_number(node, &format!("SurfaceTorusInputs.{k}"), time, d, expr); |
| | 399 | let sweep = [f("StartSweep", 0.0) as f32, f("EndSweep", 360.0) as f32]; |
| | 400 | custom_shape(build_torus( |
| | 401 | f("Radius", 1.0) as f32, |
| | 402 | f("Section", 0.5) as f32, |
| | 403 | sweep, |
| | 404 | f("SubdivisionLevelBase", 20.0).round() as u32, |
| | 405 | f("SubdivisionLevelHeight", 20.0).round() as u32, |
| | 406 | )) |
| | 407 | } |
| | 408 | "SurfaceIcoInputs" => { |
| | 409 | let f = |k: &str, d: f64| comp.eval_number(node, &format!("SurfaceIcoInputs.{k}"), time, d, expr); |
| | 410 | custom_shape(build_icosahedron(f("Size", 1.0) as f32, f("SubdivisionLevel", 0.0).round() as u32)) |
| | 411 | } |
| 364 | "SurfaceCubeInputs" => ShapeKind::Cube { | 412 | "SurfaceCubeInputs" => ShapeKind::Cube { |
| 365 | width: comp.eval_number(node, "SurfaceCubeInputs.Width", time, 1.0, expr) as f32, | 413 | width: comp.eval_number(node, "SurfaceCubeInputs.Width", time, 1.0, expr) as f32, |
| 366 | sub: cube_subdivisions(comp, node, time, expr), | 414 | sub: cube_subdivisions(comp, node, time, expr), |
| ... | @@ -518,6 +566,10 @@ fn make_mesh_instance( | ... | @@ -518,6 +566,10 @@ fn make_mesh_instance( |
| 518 | text3d: Option<Text3DSpec>, | 566 | text3d: Option<Text3DSpec>, |
| 519 | ovr: &SceneOverrides, | 567 | ovr: &SceneOverrides, |
| 520 | ) -> MeshInstance { | 568 | ) -> MeshInstance { |
| | 569 | let shape = match (ovr.smooth_normals_deg, &shape) { |
| | 570 | (Some(deg), s) if !matches!(s, ShapeKind::ImagePlane | ShapeKind::TextPlate { .. }) => ShapeKind::CustomMesh(Arc::new(smooth_normals(build_shape_mesh(s, 1.0), deg))), |
| | 571 | _ => shape, |
| | 572 | }; |
| 521 | MeshInstance { | 573 | MeshInstance { |
| 522 | world, | 574 | world, |
| 523 | shape: ShapeKind::WithMaterialFx { | 575 | shape: ShapeKind::WithMaterialFx { |
| ... | @@ -549,11 +601,13 @@ fn surface_scope( | ... | @@ -549,11 +601,13 @@ fn surface_scope( |
| 549 | expr: &ExprEngine, | 601 | expr: &ExprEngine, |
| 550 | ) -> Option<SceneOverrides> { | 602 | ) -> Option<SceneOverrides> { |
| 551 | let flag = |k: &str, d: f64| comp.eval_number(node, &format!("{group}.{k}"), time, d, expr) > 0.5; | 603 | let flag = |k: &str, d: f64| comp.eval_number(node, &format!("{group}.{k}"), time, d, expr) > 0.5; |
| | 604 | let unseen = flag("Visibility.IsUnseenByCameras", 0.0); |
| 552 | flag("Visibility.IsVisible", 1.0).then(|| SceneOverrides { | 605 | flag("Visibility.IsVisible", 1.0).then(|| SceneOverrides { |
| 553 | affected_by_lights: ovr.affected_by_lights.or(Some(flag("Lighting.IsAffectedByLights", 1.0))), | 606 | affected_by_lights: ovr.affected_by_lights.or(Some(flag("Lighting.IsAffectedByLights", 1.0))), |
| 554 | casts_shadow: Some(flag("Lighting.IsShadowCaster", 1.0)), | 607 | casts_shadow: Some(flag("Lighting.IsShadowCaster", 1.0)), |
| 555 | cull_back: flag("Visibility.CullBackFace", 0.0), | 608 | // Unseen-by-cameras surfaces skip the camera pass but still cast shadows. |
| 556 | cull_front: flag("Visibility.CullFrontFace", 0.0), | 609 | cull_back: flag("Visibility.CullBackFace", 0.0) || unseen, |
| | 610 | cull_front: flag("Visibility.CullFrontFace", 0.0) || unseen, |
| 557 | ..ovr.clone() | 611 | ..ovr.clone() |
| 558 | }) | 612 | }) |
| 559 | } | 613 | } |
| ... | @@ -568,6 +622,10 @@ fn light_color(comp: &Comp, node: &Node, time: f64, expr: &ExprEngine) -> Option | ... | @@ -568,6 +622,10 @@ fn light_color(comp: &Comp, node: &Node, time: f64, expr: &ExprEngine) -> Option |
| 568 | (color != [0.0; 3]).then_some(color) | 622 | (color != [0.0; 3]).then_some(color) |
| 569 | } | 623 | } |
| 570 | | 624 | |
| | 625 | fn custom_shape(mesh: (Vec<Vertex3D>, Vec<u32>)) -> ShapeKind { |
| | 626 | ShapeKind::CustomMesh(Arc::new(triangle_soup(mesh))) |
| | 627 | } |
| | 628 | |
| 571 | /// Cells per cube edge; Fusion's cube keeps all three subdivision counts equal. | 629 | /// Cells per cube edge; Fusion's cube keeps all three subdivision counts equal. |
| 572 | fn cube_subdivisions(comp: &Comp, node: &Node, time: f64, expr: &ExprEngine) -> u32 { | 630 | fn cube_subdivisions(comp: &Comp, node: &Node, time: f64, expr: &ExprEngine) -> u32 { |
| 573 | comp.eval_number(node, "SurfaceCubeInputs.SubdivisionWidth", time, 10.0, expr).round() as u32 | 631 | comp.eval_number(node, "SurfaceCubeInputs.SubdivisionWidth", time, 10.0, expr).round() as u32 |
| ... | @@ -582,6 +640,13 @@ fn apply_mtl_std(mat: &mut ResolvedMaterial, comp: &Comp, node: &Node, prefix: & | ... | @@ -582,6 +640,13 @@ fn apply_mtl_std(mat: &mut ResolvedMaterial, comp: &Comp, node: &Node, prefix: & |
| 582 | mat.specular_intensity = f("Specular.Intensity", 1.0).clamp(0.0, 4.0); | 640 | mat.specular_intensity = f("Specular.Intensity", 1.0).clamp(0.0, 4.0); |
| 583 | mat.specular_exponent = f("Specular.Exponent", 50.0).max(1.0); | 641 | mat.specular_exponent = f("Specular.Exponent", 50.0).max(1.0); |
| 584 | mat.receives_shadows = f("ReceivesShadows", 1.0) > 0.5; | 642 | mat.receives_shadows = f("ReceivesShadows", 1.0) > 0.5; |
| | 643 | if f("ReceivesLighting", 1.0) < 0.5 { |
| | 644 | mat.unlit = true; |
| | 645 | mat.fx.per_light_unlit = true; |
| | 646 | } |
| | 647 | if f("EnablePBRMaterial", 0.0) > 0.5 { |
| | 648 | mat.fx.spec_model = SpecModel::Pbr; |
| | 649 | } |
| 585 | } | 650 | } |
| 586 | | 651 | |
| 587 | fn parse_wrap_mode(comp: &Comp, node: &Node, time: f64, expr: &ExprEngine) -> Option<u32> { | 652 | fn parse_wrap_mode(comp: &Comp, node: &Node, time: f64, expr: &ExprEngine) -> Option<u32> { |
| ... | @@ -603,15 +668,16 @@ fn parse_wrap_mode(comp: &Comp, node: &Node, time: f64, expr: &ExprEngine) -> Op | ... | @@ -603,15 +668,16 @@ fn parse_wrap_mode(comp: &Comp, node: &Node, time: f64, expr: &ExprEngine) -> Op |
| 603 | None | 668 | None |
| 604 | } | 669 | } |
| 605 | | 670 | |
| 606 | /// Mesh-UV -> texture-UV map for a `TextureTransformOperator`. The content is moved by | 671 | /// Mesh-UV -> texture-UV map for a UVW content transform `m` (V up, W dropped when sampling). |
| 607 | /// translate * rotate * scale about the pivot in V-up image space, so sampling applies the inverse. | 672 | /// `m` moves the content, so sampling applies its inverse; `invert` swaps the two. |
| 608 | fn texture_transform_uv(translate: [f32; 2], rot_rad: f32, scale: [f32; 2], pivot: [f32; 2]) -> UvAffine { | 673 | fn texture_transform_uv(m: Mat4, invert: bool) -> UvAffine { |
| 609 | let inv = |v: f32| 1.0 / if v.abs() < 1e-4 { 1e-4 } else { v }; | 674 | let sampling = if invert { m } else { mat4_inverse(m) }; |
| 610 | let (sn, cs) = rot_rad.sin_cos(); | | |
| 611 | let sample = |uv: [f32; 2]| { | 675 | let sample = |uv: [f32; 2]| { |
| 612 | let q = [uv[0] - translate[0] - pivot[0], (1.0 - uv[1]) - translate[1] - pivot[1]]; | 676 | let (x, y) = (uv[0], 1.0 - uv[1]); |
| 613 | let r = [cs * q[0] + sn * q[1], -sn * q[0] + cs * q[1]]; | 677 | [ |
| 614 | [pivot[0] + r[0] * inv(scale[0]), 1.0 - (pivot[1] + r[1] * inv(scale[1]))] | 678 | sampling[0] * x + sampling[4] * y + sampling[12], |
| | 679 | 1.0 - (sampling[1] * x + sampling[5] * y + sampling[13]), |
| | 680 | ] |
| 615 | }; | 681 | }; |
| 616 | let (o, eu, ev) = (sample([0.0, 0.0]), sample([1.0, 0.0]), sample([0.0, 1.0])); | 682 | let (o, eu, ev) = (sample([0.0, 0.0]), sample([1.0, 0.0]), sample([0.0, 1.0])); |
| 617 | UvAffine { m: [eu[0] - o[0], eu[1] - o[1], ev[0] - o[0], ev[1] - o[1]], t: o } | 683 | UvAffine { m: [eu[0] - o[0], eu[1] - o[1], ev[0] - o[0], ev[1] - o[1]], t: o } |
| ... | @@ -649,9 +715,14 @@ fn resolve_material_inner( | ... | @@ -649,9 +715,14 @@ fn resolve_material_inner( |
| 649 | match n.kind.as_str() { | 715 | match n.kind.as_str() { |
| 650 | "Texture2DOperator" => { | 716 | "Texture2DOperator" => { |
| 651 | let tex_src = n.connected_node("Input").and_then(|s| comp.find_node(s)); | 717 | let tex_src = n.connected_node("Input").and_then(|s| comp.find_node(s)); |
| | 718 | let f = |k: &str, d: f64| comp.eval_number(n, k, time, d, expr) as f32; |
| 652 | ResolvedMaterial { | 719 | ResolvedMaterial { |
| 653 | tex_node: tex_src, | 720 | tex_node: tex_src, |
| 654 | fx: MaterialFx { | 721 | fx: MaterialFx { |
| | 722 | uv: texture_transform_uv( |
| | 723 | trs_matrix([f("UOffset", 0.0), f("VOffset", 0.0), 0.0], [0.0; 3], "XYZ", [f("UScale", 1.0), f("VScale", 1.0), 1.0], [0.0; 3]), |
| | 724 | false, |
| | 725 | ), |
| 655 | uv_wrap: parse_wrap_mode(comp, n, time, expr).unwrap_or(0), | 726 | uv_wrap: parse_wrap_mode(comp, n, time, expr).unwrap_or(0), |
| 656 | ..default_mat.fx | 727 | ..default_mat.fx |
| 657 | }, | 728 | }, |
| ... | @@ -663,12 +734,10 @@ fn resolve_material_inner( | ... | @@ -663,12 +734,10 @@ fn resolve_material_inner( |
| 663 | let inner_id = n.connected_node("MaterialInput").and_then(|s| comp.find_node(s)); | 734 | let inner_id = n.connected_node("MaterialInput").and_then(|s| comp.find_node(s)); |
| 664 | let mut mat = resolve_material_inner(comp, inner_id, time, expr, visited, depth + 1); | 735 | let mut mat = resolve_material_inner(comp, inner_id, time, expr, visited, depth + 1); |
| 665 | let f = |k: &str, d: f64| comp.eval_number(n, k, time, d, expr) as f32; | 736 | let f = |k: &str, d: f64| comp.eval_number(n, k, time, d, expr) as f32; |
| 666 | let xf = texture_transform_uv( | 737 | let uvw = |prefix: &str, d: f64| ["U", "V", "W"].map(|a| f(&format!("{prefix}.{a}"), d)); |
| 667 | [f("Translation.U", 0.0), f("Translation.V", 0.0)], | 738 | let order = n.static_str("Rotation.RotOrder").unwrap_or("UVW").replace('U', "X").replace('V', "Y").replace('W', "Z"); |
| 668 | f("Rotation.W", 0.0).to_radians(), | 739 | let m = trs_matrix(uvw("Translation", 0.0), uvw("Rotation", 0.0), &order, uvw("Scale", 1.0), uvw("Pivot", 0.0)); |
| 669 | [f("Scale.U", 1.0), f("Scale.V", 1.0)], | 740 | let xf = texture_transform_uv(m, f("InvertTransform", 0.0) > 0.5); |
| 670 | [f("Pivot.U", 0.0), f("Pivot.V", 0.0)], | | |
| 671 | ); | | |
| 672 | mat.fx.uv = xf.then(&mat.fx.uv); | 741 | mat.fx.uv = xf.then(&mat.fx.uv); |
| 673 | if let Some(w) = parse_wrap_mode(comp, n, time, expr) { | 742 | if let Some(w) = parse_wrap_mode(comp, n, time, expr) { |
| 674 | mat.fx.uv_wrap = w; | 743 | mat.fx.uv_wrap = w; |
| ... | @@ -728,12 +797,14 @@ fn resolve_material_inner( | ... | @@ -728,12 +797,14 @@ fn resolve_material_inner( |
| 728 | _ => (comp.eval_number(n, "Specular.Exponent", time, 50.0, expr).max(1.0) as f32, SpecModel::Blinn), | 797 | _ => (comp.eval_number(n, "Specular.Exponent", time, 50.0, expr).max(1.0) as f32, SpecModel::Blinn), |
| 729 | }; | 798 | }; |
| 730 | | 799 | |
| | 800 | let spec_model = if comp.eval_number(n, "EnablePBRMaterial", time, 0.0, expr) > 0.5 { SpecModel::Pbr } else { spec_model }; |
| | 801 | let lit = comp.eval_number(n, "ReceivesLighting", time, 1.0, expr) > 0.5; |
| 731 | let mut tex_node = diff_mat.tex_node; | 802 | let mut tex_node = diff_mat.tex_node; |
| 732 | let mut fx = MaterialFx { spec_model, ..diff_mat.fx }; | 803 | let mut fx = MaterialFx { spec_model, per_light_unlit: !lit, ..diff_mat.fx }; |
| 733 | if let Some(bm) = bump_mat { | 804 | if let Some(bm) = bump_mat { |
| 734 | if tex_node.is_none() { | 805 | if tex_node.is_none() { |
| 735 | tex_node = bm.tex_node; | 806 | tex_node = bm.tex_node; |
| 736 | fx = MaterialFx { bump_only: true, spec_model, ..bm.fx }; | 807 | fx = MaterialFx { bump_only: true, spec_model, per_light_unlit: !lit, ..bm.fx }; |
| 737 | } | 808 | } |
| 738 | fx.bump_scale = if bm.fx.bump_scale.abs() > 1e-6 { bm.fx.bump_scale } else { 1.0 }; | 809 | fx.bump_scale = if bm.fx.bump_scale.abs() > 1e-6 { bm.fx.bump_scale } else { 1.0 }; |
| 739 | fx.bump_mode = bm.fx.bump_mode; | 810 | fx.bump_mode = bm.fx.bump_mode; |
| ... | @@ -746,7 +817,7 @@ fn resolve_material_inner( | ... | @@ -746,7 +817,7 @@ fn resolve_material_inner( |
| 746 | specular_intensity: spec_int.clamp(0.0, 4.0), | 817 | specular_intensity: spec_int.clamp(0.0, 4.0), |
| 747 | specular_exponent: spec_exp, | 818 | specular_exponent: spec_exp, |
| 748 | fx, | 819 | fx, |
| 749 | unlit: false, | 820 | unlit: !lit, |
| 750 | receives_shadows: comp.eval_number(n, "ReceivesShadows", time, 1.0, expr) > 0.5, | 821 | receives_shadows: comp.eval_number(n, "ReceivesShadows", time, 1.0, expr) > 0.5, |
| 751 | has_material_shader: true, | 822 | has_material_shader: true, |
| 752 | } | 823 | } |
| ... | @@ -813,44 +884,36 @@ pub fn eval_transform3d(comp: &Comp, node: &Node, time: f64, expr: &ExprEngine) | ... | @@ -813,44 +884,36 @@ pub fn eval_transform3d(comp: &Comp, node: &Node, time: f64, expr: &ExprEngine) |
| 813 | let ty = comp.eval_number(node, "Transform3DOp.Translate.Y", time, 0.0, expr) as f32; | 884 | let ty = comp.eval_number(node, "Transform3DOp.Translate.Y", time, 0.0, expr) as f32; |
| 814 | let tz = comp.eval_number(node, "Transform3DOp.Translate.Z", time, 0.0, expr) as f32; | 885 | let tz = comp.eval_number(node, "Transform3DOp.Translate.Z", time, 0.0, expr) as f32; |
| 815 | | 886 | |
| 816 | let rx = (comp.eval_number(node, "Transform3DOp.Rotate.X", time, 0.0, expr) as f32).to_radians(); | 887 | let r = ["X", "Y", "Z"].map(|a| comp.eval_number(node, &format!("Transform3DOp.Rotate.{a}"), time, 0.0, expr) as f32); |
| 817 | let ry = (comp.eval_number(node, "Transform3DOp.Rotate.Y", time, 0.0, expr) as f32).to_radians(); | | |
| 818 | let rz = (comp.eval_number(node, "Transform3DOp.Rotate.Z", time, 0.0, expr) as f32).to_radians(); | | |
| 819 | let rot_order = node.static_str("Transform3DOp.Rotate.RotOrder").unwrap_or("XYZ"); | 888 | let rot_order = node.static_str("Transform3DOp.Rotate.RotOrder").unwrap_or("XYZ"); |
| 820 | | 889 | |
| 821 | let scale_lock = comp.eval_number(node, "Transform3DOp.ScaleLock", time, 1.0, expr) > 0.5; | 890 | let scale_lock = comp.eval_number(node, "Transform3DOp.ScaleLock", time, 1.0, expr) > 0.5; |
| 822 | let sx = comp.eval_number(node, "Transform3DOp.Scale.X", time, 1.0, expr) as f32; | 891 | let sx = comp.eval_number(node, "Transform3DOp.Scale.X", time, 1.0, expr) as f32; |
| 823 | let sy = if scale_lock { | 892 | let (sy, sz) = if scale_lock { |
| 824 | sx | 893 | (sx, sx) |
| 825 | } else { | | |
| 826 | comp.eval_number(node, "Transform3DOp.Scale.Y", time, 1.0, expr) as f32 | | |
| 827 | }; | | |
| 828 | let sz = if scale_lock { | | |
| 829 | sx | | |
| 830 | } else { | 894 | } else { |
| 831 | comp.eval_number(node, "Transform3DOp.Scale.Z", time, 1.0, expr) as f32 | 895 | ( |
| | 896 | comp.eval_number(node, "Transform3DOp.Scale.Y", time, 1.0, expr) as f32, |
| | 897 | comp.eval_number(node, "Transform3DOp.Scale.Z", time, 1.0, expr) as f32, |
| | 898 | ) |
| 832 | }; | 899 | }; |
| 833 | | 900 | |
| 834 | let px = comp.eval_number(node, "Transform3DOp.Pivot.X", time, 0.0, expr) as f32; | 901 | let p = ["X", "Y", "Z"].map(|a| comp.eval_number(node, &format!("Transform3DOp.Pivot.{a}"), time, 0.0, expr) as f32); |
| 835 | let py = comp.eval_number(node, "Transform3DOp.Pivot.Y", time, 0.0, expr) as f32; | 902 | let m = trs_matrix([tx, ty, tz], r, rot_order, [sx, sy, sz], p); |
| 836 | let pz = comp.eval_number(node, "Transform3DOp.Pivot.Z", time, 0.0, expr) as f32; | 903 | if comp.eval_number(node, "Transform3DOp.InvertTransform", time, 0.0, expr) > 0.5 { mat4_inverse(m) } else { m } |
| 837 | | 904 | } |
| 838 | let mx = mat4_rot_x(rx); | | |
| 839 | let my = mat4_rot_y(ry); | | |
| 840 | let mz = mat4_rot_z(rz); | | |
| 841 | let rot = match rot_order { | | |
| 842 | "XZY" => mat4_mul(my, mat4_mul(mz, mx)), | | |
| 843 | "YXZ" => mat4_mul(mz, mat4_mul(mx, my)), | | |
| 844 | "YZX" => mat4_mul(mx, mat4_mul(mz, my)), | | |
| 845 | "ZXY" => mat4_mul(my, mat4_mul(mx, mz)), | | |
| 846 | "ZYX" => mat4_mul(mx, mat4_mul(my, mz)), | | |
| 847 | _ => mat4_mul(mz, mat4_mul(my, mx)), // XYZ default | | |
| 848 | }; | | |
| 849 | | 905 | |
| 850 | let t = mat4_translate(tx + px, ty + py, tz + pz); | 906 | /// `T(translate + pivot) * R * S * T(-pivot)`; `rot_deg` is per-axis, composed in `order` (`"XYZ"` applies X first). |
| 851 | let s = mat4_scale(sx, sy, sz); | 907 | fn trs_matrix(translate: [f32; 3], rot_deg: [f32; 3], order: &str, scale: [f32; 3], pivot: [f32; 3]) -> Mat4 { |
| 852 | let inv_p = mat4_translate(-px, -py, -pz); | 908 | let [rx, ry, rz] = rot_deg.map(f32::to_radians); |
| 853 | mat4_mul(t, mat4_mul(rot, mat4_mul(s, inv_p))) | 909 | let axes = [mat4_rot_x(rx), mat4_rot_y(ry), mat4_rot_z(rz)]; |
| | 910 | let rot = order |
| | 911 | .bytes() |
| | 912 | .filter_map(|b| "XYZ".find(b as char)) |
| | 913 | .fold(mat4_identity(), |acc, a| mat4_mul(axes[a], acc)); |
| | 914 | let t = mat4_translate(translate[0] + pivot[0], translate[1] + pivot[1], translate[2] + pivot[2]); |
| | 915 | let s = mat4_scale(scale[0], scale[1], scale[2]); |
| | 916 | mat4_mul(t, mat4_mul(rot, mat4_mul(s, mat4_translate(-pivot[0], -pivot[1], -pivot[2])))) |
| 854 | } | 917 | } |
| 855 | | 918 | |
| 856 | #[cfg(test)] | 919 | #[cfg(test)] |
| ... | @@ -904,7 +967,7 @@ mod tests { | ... | @@ -904,7 +967,7 @@ mod tests { |
| 904 | RepNorm = ReplaceNormals3D { | 967 | RepNorm = ReplaceNormals3D { |
| 905 | Inputs = { | 968 | Inputs = { |
| 906 | SceneInput = Input { SourceOp = "RepMat", Source = "Output", }, | 969 | SceneInput = Input { SourceOp = "RepMat", Source = "Output", }, |
| 907 | FlipNormals = Input { Value = 1, }, | 970 | ["Normals.Flip"] = Input { Value = 1, }, |
| 908 | }, | 971 | }, |
| 909 | }, | 972 | }, |
| 910 | Disp1 = Displace3D { | 973 | Disp1 = Displace3D { |
| ... | @@ -1122,6 +1185,73 @@ mod tests { | ... | @@ -1122,6 +1185,73 @@ mod tests { |
| 1122 | format!("Composition {{ CurrentTime = 0, RenderRange = {{ 0, 1 }}, GlobalRange = {{ 0, 1 }}, Tools = ordered() {{ {tools} }}, }}") | 1185 | format!("Composition {{ CurrentTime = 0, RenderRange = {{ 0, 1 }}, GlobalRange = {{ 0, 1 }}, Tools = ordered() {{ {tools} }}, }}") |
| 1123 | } | 1186 | } |
| 1124 | | 1187 | |
| | 1188 | #[test] |
| | 1189 | fn cameras_carry_names_for_renderer_selection() { |
| | 1190 | let src = comp_src( |
| | 1191 | r#"A = Camera3D { }, B = Camera3D { Inputs = { ProjectionType = Input { Value = FuID { "Orthographic" }, }, }, }, |
| | 1192 | M = Merge3D { Inputs = { SceneInput1 = Input { SourceOp = "A", Source = "Output", }, SceneInput2 = Input { SourceOp = "B", Source = "Output", }, }, },"#, |
| | 1193 | ); |
| | 1194 | let (cams, ..) = collect(&src, "M"); |
| | 1195 | let found: Vec<_> = cams.iter().map(|c| (c.name.as_str(), c.orthographic)).collect(); |
| | 1196 | assert_eq!(found, [("A", false), ("B", true)]); |
| | 1197 | } |
| | 1198 | |
| | 1199 | #[test] |
| | 1200 | fn texture2d_offset_and_scale_move_content_like_a_texture_transform() { |
| | 1201 | let src = comp_src( |
| | 1202 | r#"Bg = Background { }, Tex = Texture2DOperator { Inputs = { UOffset = Input { Value = 0.25, }, UScale = Input { Value = 2, }, Input = Input { SourceOp = "Bg", Source = "Output", }, }, }, |
| | 1203 | S = Shape3D { Inputs = { MaterialInput = Input { SourceOp = "Tex", Source = "MaterialOutput", }, }, },"#, |
| | 1204 | ); |
| | 1205 | let (_, meshes, _) = collect(&src, "S"); |
| | 1206 | let (_, fx) = meshes[0].shape.split_fx(); |
| | 1207 | // Content scales 2x about the origin then moves right by 0.25: mesh u=0.75 samples texture u=0.25. |
| | 1208 | assert!((fx.uv.apply([0.75, 0.0])[0] - 0.25).abs() < 1e-5); |
| | 1209 | } |
| | 1210 | |
| | 1211 | #[test] |
| | 1212 | fn receives_lighting_off_is_unlit_and_lights_are_counted() { |
| | 1213 | let src = comp_src( |
| | 1214 | r#"Mtl = MtlBlinn { Inputs = { ReceivesLighting = Input { Value = 0, }, EnablePBRMaterial = Input { Value = 1, }, }, }, |
| | 1215 | S = Shape3D { Inputs = { MaterialInput = Input { SourceOp = "Mtl", Source = "MaterialOutput", }, }, }, |
| | 1216 | A = LightAmbient { }, P = LightPoint { }, Off = LightPoint { Inputs = { Enabled = Input { Value = 0, }, }, }, |
| | 1217 | M = Merge3D { Inputs = { SceneInput1 = Input { SourceOp = "S", Source = "Output", }, SceneInput2 = Input { SourceOp = "A", Source = "Output", }, SceneInput3 = Input { SourceOp = "P", Source = "Output", }, SceneInput4 = Input { SourceOp = "Off", Source = "Output", }, }, },"#, |
| | 1218 | ); |
| | 1219 | let (_, meshes, lights) = collect(&src, "M"); |
| | 1220 | let (_, fx) = meshes[0].shape.split_fx(); |
| | 1221 | assert!(fx.per_light_unlit && !meshes[0].affected_by_lights); |
| | 1222 | assert_eq!(fx.spec_model, SpecModel::Pbr); |
| | 1223 | assert_eq!(lights.count, 2); |
| | 1224 | } |
| | 1225 | |
| | 1226 | #[test] |
| | 1227 | fn invert_transform_inverts_the_node_matrix() { |
| | 1228 | let src = comp_src( |
| | 1229 | r#"Cam = Camera3D { Inputs = { ["Transform3DOp.Translate.X"] = Input { Value = 3, }, ["Transform3DOp.InvertTransform"] = Input { Value = 1, }, }, }, |
| | 1230 | M = Merge3D { Inputs = { SceneInput1 = Input { SourceOp = "Cam", Source = "Output", }, }, },"#, |
| | 1231 | ); |
| | 1232 | let (cams, ..) = collect(&src, "M"); |
| | 1233 | assert!((cams[0].world[12] + 3.0).abs() < 1e-5); |
| | 1234 | } |
| | 1235 | |
| | 1236 | #[test] |
| | 1237 | fn replace_normals_recomputes_by_smoothing_angle() { |
| | 1238 | let tris = |angle: f64| { |
| | 1239 | let src = comp_src(&format!( |
| | 1240 | r#"S = Shape3D {{ Inputs = {{ Shape = Input {{ Value = FuID {{ "SurfaceIcoInputs" }}, }}, }}, }}, |
| | 1241 | N = ReplaceNormals3D {{ Inputs = {{ ["Normals.SmoothingAngle"] = Input {{ Value = {angle}, }}, SceneInput = Input {{ SourceOp = "S", Source = "Output", }}, }}, }},"# |
| | 1242 | )); |
| | 1243 | let (_, meshes, _) = collect(&src, "N"); |
| | 1244 | let (ShapeKind::CustomMesh(v), _) = meshes[0].shape.split_fx() else { panic!("expected a mesh") }; |
| | 1245 | v.clone() |
| | 1246 | }; |
| | 1247 | // Adjacent icosahedron faces differ by ~41.8 degrees: below that every face is flat. |
| | 1248 | let flat = tris(30.0); |
| | 1249 | let close = |a: [f32; 3], b: [f32; 3]| a.iter().zip(b).all(|(x, y)| (x - y).abs() < 1e-4); |
| | 1250 | assert!(flat.chunks_exact(3).all(|t| close(t[0].normal, t[1].normal) && close(t[1].normal, t[2].normal))); |
| | 1251 | let smooth = tris(90.0); |
| | 1252 | assert!(smooth.chunks_exact(3).any(|t| !close(t[0].normal, t[1].normal))); |
| | 1253 | } |
| | 1254 | |
| 1125 | #[test] | 1255 | #[test] |
| 1126 | fn camera_vertical_aov_comes_from_default_35mm_focal_length() { | 1256 | fn camera_vertical_aov_comes_from_default_35mm_focal_length() { |
| 1127 | // Serialized AoV is ignored: 35mm on a 1" gate gives 2*atan(12.7/35). | 1257 | // Serialized AoV is ignored: 35mm on a 1" gate gives 2*atan(12.7/35). |
| ... | @@ -1174,20 +1304,27 @@ mod tests { | ... | @@ -1174,20 +1304,27 @@ mod tests { |
| 1174 | | 1304 | |
| 1175 | #[test] | 1305 | #[test] |
| 1176 | fn texture_transform_moves_content_about_a_bottom_left_pivot() { | 1306 | fn texture_transform_moves_content_about_a_bottom_left_pivot() { |
| | 1307 | let uvw = |t, r, s, invert| texture_transform_uv(trs_matrix(t, r, "XYZ", s, [0.0; 3]), invert); |
| 1177 | // Scale 2 about the (0, 0) pivot zooms toward the bottom-left corner (V up). | 1308 | // Scale 2 about the (0, 0) pivot zooms toward the bottom-left corner (V up). |
| 1178 | let zoom = texture_transform_uv([0.0, 0.0], 0.0, [2.0, 2.0], [0.0, 0.0]); | 1309 | let zoom = uvw([0.0; 3], [0.0; 3], [2.0, 2.0, 1.0], false); |
| 1179 | let (bl, tr) = (zoom.apply([0.0, 1.0]), zoom.apply([1.0, 0.0])); | 1310 | let (bl, tr) = (zoom.apply([0.0, 1.0]), zoom.apply([1.0, 0.0])); |
| 1180 | assert!(bl[0].abs() < 1e-5 && (bl[1] - 1.0).abs() < 1e-5); | 1311 | assert!(bl[0].abs() < 1e-5 && (bl[1] - 1.0).abs() < 1e-5); |
| 1181 | assert!((tr[0] - 0.5).abs() < 1e-5 && (tr[1] - 0.5).abs() < 1e-5); | 1312 | assert!((tr[0] - 0.5).abs() < 1e-5 && (tr[1] - 0.5).abs() < 1e-5); |
| 1182 | | 1313 | |
| 1183 | // Content moved right and up samples from smaller u and larger raw v (V down). | 1314 | // Content moved right and up samples from smaller u and larger raw v (V down). |
| 1184 | let shift = texture_transform_uv([0.25, 0.1], 0.0, [1.0, 1.0], [0.0, 0.0]).apply([0.5, 0.5]); | 1315 | let shift = uvw([0.25, 0.1, 0.0], [0.0; 3], [1.0; 3], false).apply([0.5, 0.5]); |
| 1185 | assert!((shift[0] - 0.25).abs() < 1e-5 && (shift[1] - 0.6).abs() < 1e-5); | 1316 | assert!((shift[0] - 0.25).abs() < 1e-5 && (shift[1] - 0.6).abs() < 1e-5); |
| 1186 | | 1317 | |
| 1187 | // Counter-clockwise content rotation about the pivot. | 1318 | // Counter-clockwise content rotation about the pivot. |
| 1188 | let quarter = texture_transform_uv([0.0, 0.0], std::f32::consts::FRAC_PI_2, [1.0, 1.0], [0.0, 0.0]); | 1319 | let quarter = uvw([0.0; 3], [0.0, 0.0, 90.0], [1.0; 3], false); |
| 1189 | let p = quarter.apply([0.0, 0.0]); // top-left corner samples from the content to its right | 1320 | let p = quarter.apply([0.0, 0.0]); // top-left corner samples from the content to its right |
| 1190 | assert!((p[0] - 1.0).abs() < 1e-5 && (p[1] - 1.0).abs() < 1e-5, "{p:?}"); | 1321 | assert!((p[0] - 1.0).abs() < 1e-5 && (p[1] - 1.0).abs() < 1e-5, "{p:?}"); |
| | 1322 | |
| | 1323 | // Tilting about U drops W, magnifying content in V by 1/cos; InvertTransform undoes a move. |
| | 1324 | let tilt = uvw([0.0; 3], [60.0, 0.0, 0.0], [1.0; 3], false).apply([0.0, 0.5]); |
| | 1325 | assert!((tilt[1] - 0.75).abs() < 1e-5, "{tilt:?}"); |
| | 1326 | let back = uvw([0.2, 0.0, 0.0], [0.0; 3], [1.0; 3], true).apply([0.5, 0.5]); |
| | 1327 | assert!((back[0] - 0.7).abs() < 1e-5, "{back:?}"); |
| 1191 | } | 1328 | } |
| 1192 | | 1329 | |
| 1193 | #[test] | 1330 | #[test] |