| ... | ... | @@ -5,8 +5,8 @@ use super::math::{ |
| 5 | 5 | mat4_identity, mat4_mul, mat4_rot_x, mat4_rot_y, mat4_rot_z, mat4_scale, mat4_translate, |
| 6 | 6 | }; |
| 7 | 7 | use super::{ |
| 8 | | CameraInstance, Displace3DSpec, Mat4, MeshInstance, PointLight, SceneLights, ShapeKind, |
| 9 | | SpotLight, Text3DSpec, |
| 8 | CameraInstance, Displace3DSpec, Mat4, MaterialFx, MeshInstance, PointLight, SceneLights, |
| 9 | ShapeKind, SpecModel, SpotLight, Text3DSpec, UvAffine, CUBE_FACES, |
| 10 | 10 | }; |
| 11 | 11 | use model::{Comp, ExprEngine, Node, NodeId}; |
| 12 | 12 | |
| ... | ... | @@ -16,6 +16,8 @@ struct SceneOverrides { |
| 16 | 16 | affected_by_lights: Option<bool>, |
| 17 | 17 | flip_normals: bool, |
| 18 | 18 | displace: Option<Displace3DSpec>, |
| 19 | cull_back: bool, |
| 20 | cull_front: bool, |
| 19 | 21 | } |
| 20 | 22 | |
| 21 | 23 | fn splitmix64(mut x: u64) -> f32 { |
| ... | ... | @@ -135,7 +137,7 @@ fn collect_scene_inner( |
| 135 | 137 | if let Some(in_id) = node.connected_node("Input").and_then(|s| comp.find_node(s)) { |
| 136 | 138 | let scale = comp.eval_number(node, "Scale", time, 1.0, expr) as f32; |
| 137 | 139 | let bias = comp.eval_number(node, "Bias", time, 0.0, expr) as f32; |
| 138 | | let channel = comp.eval_number(node, "Channel", time, 4.0, expr).round() as i32; |
| 140 | let channel = comp.eval_number(node, "Channel", time, 0.0, expr).round() as i32; |
| 139 | 141 | next_ovr.displace = Some(Displace3DSpec { |
| 140 | 142 | input_node: in_id, |
| 141 | 143 | scale, |
| ... | ... | @@ -190,18 +192,19 @@ fn collect_scene_inner( |
| 190 | 192 | "Camera3D" => { |
| 191 | 193 | let ap_w = comp.eval_number(node, "ApertureW", time, 1.4173236, expr) as f32; |
| 192 | 194 | let ap_h = comp.eval_number(node, "ApertureH", time, 0.9448824, expr) as f32; |
| 193 | | // In Fusion .comp files, serialized AoV is always vertical degrees (AoVType only changes UI display). |
| 194 | | // When AoV is omitted, compute from FLength if present, else Fusion 21's default Camera3D vertical AoV (~24.302°). |
| 195 | | let has_aov = node.inputs.contains_key("AoV"); |
| 196 | | let has_fl = node.inputs.contains_key("FLength"); |
| 197 | | let aov_deg = if has_aov { |
| 198 | | comp.eval_number(node, "AoV", time, 24.302, expr) as f32 |
| 199 | | } else if has_fl { |
| 200 | | let fl_mm = comp.eval_number(node, "FLength", time, 24.0, expr).max(0.5) as f32; |
| 201 | | let sensor_h_mm = ap_h * 25.4; |
| 202 | | 2.0 * (sensor_h_mm * 0.5 / fl_mm).atan().to_degrees() |
| 195 | // Fusion derives the vertical AoV from FLength (default 35mm) and the film-gate height; |
| 196 | // a serialized AoV is only an echo of that, so it is the fallback when neither FLength |
| 197 | // nor ApertureH is serialized (default Camera3D vertical AoV ~24.302°). |
| 198 | let fl_mm = if node.inputs.contains_key("FLength") { |
| 199 | Some(comp.eval_number(node, "FLength", time, 35.0, expr).max(0.5) as f32) |
| 200 | } else if node.inputs.contains_key("ApertureH") { |
| 201 | Some(35.0) |
| 203 | 202 | } else { |
| 204 | | 24.302 |
| 203 | None |
| 204 | }; |
| 205 | let aov_deg = match fl_mm { |
| 206 | Some(fl) => 2.0 * (ap_h * 25.4 * 0.5 / fl).atan().to_degrees(), |
| 207 | None => comp.eval_number(node, "AoV", time, 24.302, expr) as f32, |
| 205 | 208 | }; |
| 206 | 209 | // Fusion Camera3D defaults ResolutionGateFit to "Height" (vertical AoV is preserved on square/tall renders) |
| 207 | 210 | let gate_fit = node.static_str("ResolutionGateFit").unwrap_or("Height"); |
| ... | ... | @@ -220,47 +223,34 @@ fn collect_scene_inner( |
| 220 | 223 | cameras.push(CameraInstance { world, vfov_rad, near, far }); |
| 221 | 224 | } |
| 222 | 225 | "LightAmbient" => { |
| 223 | | let inten = comp.eval_number(node, "Intensity", time, 1.0, expr) as f32; |
| 224 | | let r = comp.eval_number(node, "Red", time, 1.0, expr) as f32; |
| 225 | | let g = comp.eval_number(node, "Green", time, 1.0, expr) as f32; |
| 226 | | let b = comp.eval_number(node, "Blue", time, 1.0, expr) as f32; |
| 227 | | lights.ambient[0] += r * inten; |
| 228 | | lights.ambient[1] += g * inten; |
| 229 | | lights.ambient[2] += b * inten; |
| 226 | let Some(color) = light_color(comp, node, time, expr) else { return }; |
| 227 | for (acc, c) in lights.ambient.iter_mut().zip(color) { |
| 228 | *acc += c; |
| 229 | } |
| 230 | 230 | } |
| 231 | 231 | "LightDirectional" => { |
| 232 | | let inten = comp.eval_number(node, "Intensity", time, 1.0, expr) as f32; |
| 233 | | let r = comp.eval_number(node, "Red", time, 1.0, expr) as f32; |
| 234 | | let g = comp.eval_number(node, "Green", time, 1.0, expr) as f32; |
| 235 | | let b = comp.eval_number(node, "Blue", time, 1.0, expr) as f32; |
| 232 | let Some(color) = light_color(comp, node, time, expr) else { return }; |
| 236 | 233 | // Default Fusion LightDirectional shines along -Z => direction TO light is +Z transformed by world rotation! |
| 237 | 234 | let dx = world[8]; |
| 238 | 235 | let dy = world[9]; |
| 239 | 236 | let dz = world[10]; |
| 240 | 237 | let len = (dx * dx + dy * dy + dz * dz).sqrt().max(1e-5); |
| 241 | 238 | lights.dir_to_light = [dx / len, dy / len, dz / len]; |
| 242 | | lights.dir_color = [r * inten, g * inten, b * inten]; |
| 239 | lights.dir_color = color; |
| 243 | 240 | lights.has_dir = true; |
| 244 | 241 | } |
| 245 | 242 | "LightPoint" => { |
| 246 | | let inten = comp.eval_number(node, "Intensity", time, 1.0, expr) as f32; |
| 247 | | let r = comp.eval_number(node, "Red", time, 1.0, expr) as f32; |
| 248 | | let g = comp.eval_number(node, "Green", time, 1.0, expr) as f32; |
| 249 | | let b = comp.eval_number(node, "Blue", time, 1.0, expr) as f32; |
| 243 | let Some(color) = light_color(comp, node, time, expr) else { return }; |
| 250 | 244 | let decay_type = comp.eval_number(node, "DecayType", time, 0.0, expr) as f32; |
| 251 | | let decay_rate = comp.eval_number(node, "DecayRate", time, 1.0, expr) as f32; |
| 252 | 245 | lights.points.push(PointLight { |
| 253 | 246 | pos: [world[12], world[13], world[14]], |
| 254 | | color: [r * inten, g * inten, b * inten], |
| 247 | color, |
| 255 | 248 | decay_type, |
| 256 | | decay_rate, |
| 249 | decay_rate: 1.0, |
| 257 | 250 | }); |
| 258 | 251 | } |
| 259 | 252 | "LightSpot" => { |
| 260 | | let inten = comp.eval_number(node, "Intensity", time, 1.0, expr) as f32; |
| 261 | | let r = comp.eval_number(node, "Red", time, 1.0, expr) as f32; |
| 262 | | let g = comp.eval_number(node, "Green", time, 1.0, expr) as f32; |
| 263 | | let b = comp.eval_number(node, "Blue", time, 1.0, expr) as f32; |
| 253 | let Some(color) = light_color(comp, node, time, expr) else { return }; |
| 264 | 254 | let cone_deg = comp.eval_number(node, "ConeAngle", time, 40.0, expr) as f32; |
| 265 | 255 | let penumbra_deg = comp.eval_number(node, "PenumbraAngle", time, 0.0, expr) as f32; |
| 266 | 256 | let decay_type = comp.eval_number(node, "DecayType", time, 0.0, expr) as f32; |
| ... | ... | @@ -275,59 +265,53 @@ fn collect_scene_inner( |
| 275 | 265 | lights.spots.push(SpotLight { |
| 276 | 266 | pos: [world[12], world[13], world[14]], |
| 277 | 267 | dir: [dx / len, dy / len, dz / len], |
| 278 | | color: [r * inten, g * inten, b * inten], |
| 268 | color, |
| 279 | 269 | cos_inner: inner_deg.to_radians().cos(), |
| 280 | 270 | cos_outer: outer_deg.to_radians().cos(), |
| 281 | 271 | decay_type, |
| 282 | 272 | }); |
| 283 | 273 | } |
| 284 | 274 | "ImagePlane3D" => { |
| 275 | let Some(own) = surface_scope(comp, node, "SurfacePlaneInputs", ovr, time, expr) else { return }; |
| 285 | 276 | let mat_node = ovr.material_override.or_else(|| { |
| 286 | 277 | node.connected_node("MaterialInput") |
| 287 | 278 | .or_else(|| node.connected_node("SceneInput")) |
| 288 | 279 | .and_then(|s| comp.find_node(s)) |
| 289 | 280 | }); |
| 290 | 281 | let mat = resolve_material(comp, mat_node, time, expr); |
| 291 | | meshes.push(make_mesh_instance(world, ShapeKind::ImagePlane, mat, None, ovr)); |
| 282 | meshes.push(make_mesh_instance(world, ShapeKind::ImagePlane, mat, None, &own)); |
| 292 | 283 | } |
| 293 | 284 | "Shape3D" => { |
| 294 | 285 | let shape_name = node.static_str("Shape").unwrap_or("SurfacePlaneInputs"); |
| 295 | | let disp_sub = if ovr.displace.is_some() { 32 } else { 1 }; |
| 286 | let Some(own) = surface_scope(comp, node, shape_name, ovr, time, expr) else { return }; |
| 296 | 287 | let shape = match shape_name { |
| 297 | 288 | "SurfaceSphereInputs" => ShapeKind::Sphere { |
| 298 | 289 | // Fusion 21.1 Shape3D Sphere default Radius is 1.0 (verified via probe_moon_3d f00) |
| 299 | 290 | radius: comp.eval_number(node, "SurfaceSphereInputs.Radius", time, 1.0, expr) as f32, |
| 300 | | sub_base: comp.eval_number(node, "SurfaceSphereInputs.SubdivisionLevelBase", time, 32.0, expr).round() as u32, |
| 301 | | sub_height: comp.eval_number(node, "SurfaceSphereInputs.SubdivisionLevelHeight", time, 24.0, expr).round() as u32, |
| 291 | sub_base: comp.eval_number(node, "SurfaceSphereInputs.SubdivisionLevelBase", time, 20.0, expr).round() as u32, |
| 292 | sub_height: comp.eval_number(node, "SurfaceSphereInputs.SubdivisionLevelHeight", time, 20.0, expr).round() as u32, |
| 302 | 293 | }, |
| 303 | 294 | "SurfaceCylinderInputs" => { |
| 304 | 295 | let start_sw = comp.eval_number(node, "SurfaceCylinderInputs.StartSweep", time, 0.0, expr) as f32; |
| 305 | 296 | let end_sw = comp.eval_number(node, "SurfaceCylinderInputs.EndSweep", time, 360.0, expr) as f32; |
| 306 | 297 | ShapeKind::Cylinder { |
| 307 | | radius: comp.eval_number(node, "SurfaceCylinderInputs.Radius", time, 0.5, expr) as f32, |
| 308 | | height: comp.eval_number(node, "SurfaceCylinderInputs.Height", time, 1.0, expr) as f32, |
| 298 | radius: comp.eval_number(node, "SurfaceCylinderInputs.Radius", time, 1.0, expr) as f32, |
| 299 | height: comp.eval_number(node, "SurfaceCylinderInputs.Height", time, 2.0, expr) as f32, |
| 309 | 300 | start_sweep_deg: start_sw, |
| 310 | 301 | end_sweep_deg: if (end_sw - start_sw).abs() < 1e-3 { start_sw + 360.0 } else { end_sw }, |
| 311 | | sub_base: comp.eval_number(node, "SurfaceCylinderInputs.SubdivisionLevelBase", time, 48.0, expr).round() as u32, |
| 312 | | } |
| 313 | | } |
| 314 | | "SurfaceCubeInputs" => { |
| 315 | | let sz = comp.eval_number(node, "SurfaceCubeInputs.Size", time, 1.0, expr); |
| 316 | | ShapeKind::Cube { |
| 317 | | width: comp.eval_number(node, "SurfaceCubeInputs.Width", time, sz, expr) as f32, |
| 318 | | height: comp.eval_number(node, "SurfaceCubeInputs.Height", time, sz, expr) as f32, |
| 319 | | depth: comp.eval_number(node, "SurfaceCubeInputs.Depth", time, sz, expr) as f32, |
| 302 | sub_base: comp.eval_number(node, "SurfaceCylinderInputs.SubdivisionLevelBase", time, 20.0, expr).round() as u32, |
| 320 | 303 | } |
| 321 | 304 | } |
| 305 | "SurfaceCubeInputs" => ShapeKind::Cube { |
| 306 | width: comp.eval_number(node, "SurfaceCubeInputs.Width", time, 1.0, expr) as f32, |
| 307 | sub: cube_subdivisions(comp, node, time, expr), |
| 308 | faces: 0b11_1111, |
| 309 | }, |
| 322 | 310 | _ => { |
| 323 | | let sw = comp.eval_number(node, "SurfacePlaneInputs.SubdivisionWidth", time, disp_sub as f64, expr).round().max(disp_sub as f64) as u32; |
| 324 | | let sh = comp.eval_number(node, "SurfacePlaneInputs.SubdivisionHeight", time, disp_sub as f64, expr).round().max(disp_sub as f64) as u32; |
| 325 | | ShapeKind::Plane { |
| 326 | | width: comp.eval_number(node, "SurfacePlaneInputs.Width", time, 1.0, expr) as f32, |
| 327 | | height: comp.eval_number(node, "SurfacePlaneInputs.Height", time, 1.0, expr) as f32, |
| 328 | | sub_w: sw, |
| 329 | | sub_h: sh, |
| 330 | | } |
| 311 | // Fusion's plane is square: Height and SubdivisionHeight follow Width. |
| 312 | let sub = comp.eval_number(node, "SurfacePlaneInputs.SubdivisionWidth", time, 10.0, expr).round() as u32; |
| 313 | let width = comp.eval_number(node, "SurfacePlaneInputs.Width", time, 1.0, expr) as f32; |
| 314 | ShapeKind::Plane { width, height: width, sub_w: sub, sub_h: sub } |
| 331 | 315 | } |
| 332 | 316 | }; |
| 333 | 317 | let mat_node = ovr.material_override.or_else(|| { |
| ... | ... | @@ -336,83 +320,67 @@ fn collect_scene_inner( |
| 336 | 320 | }); |
| 337 | 321 | let mut mat = resolve_material(comp, mat_node, time, expr); |
| 338 | 322 | if mat_node.is_none() { |
| 339 | | mat.diffuse_col = [ |
| 340 | | comp.eval_number(node, "MtlStdInputs.Diffuse.Color.Red", time, 1.0, expr) as f32, |
| 341 | | comp.eval_number(node, "MtlStdInputs.Diffuse.Color.Green", time, 1.0, expr) as f32, |
| 342 | | comp.eval_number(node, "MtlStdInputs.Diffuse.Color.Blue", time, 1.0, expr) as f32, |
| 343 | | comp.eval_number(node, "MtlStdInputs.Diffuse.Opacity", time, 1.0, expr) as f32, |
| 344 | | ]; |
| 323 | apply_mtl_std(&mut mat, comp, node, "", time, expr); |
| 345 | 324 | } |
| 346 | | meshes.push(make_mesh_instance(world, shape, mat, None, ovr)); |
| 325 | mat.fx.wireframe = comp.eval_number(node, &format!("{shape_name}.Wireframe"), time, 0.0, expr) > 0.5; |
| 326 | meshes.push(make_mesh_instance(world, shape, mat, None, &own)); |
| 347 | 327 | } |
| 348 | 328 | "Cube3D" => { |
| 349 | | let sz = comp.eval_number(node, "SurfaceCubeInputs.Size", time, 1.0, expr); |
| 350 | | let shape = ShapeKind::Cube { |
| 351 | | width: comp.eval_number(node, "SurfaceCubeInputs.Width", time, sz, expr) as f32, |
| 352 | | height: comp.eval_number(node, "SurfaceCubeInputs.Height", time, sz, expr) as f32, |
| 353 | | depth: comp.eval_number(node, "SurfaceCubeInputs.Depth", time, sz, expr) as f32, |
| 354 | | }; |
| 355 | | let mat_node = ovr.material_override.or_else(|| { |
| 356 | | [ |
| 357 | | "MaterialInput", |
| 358 | | "FrontMaterialInput", |
| 359 | | "BackMaterialInput", |
| 360 | | "RightMaterialInput", |
| 361 | | "LeftMaterialInput", |
| 362 | | "TopMaterialInput", |
| 363 | | "BottomMaterialInput", |
| 364 | | ] |
| 365 | | .iter() |
| 366 | | .find_map(|k| node.connected_node(k)) |
| 367 | | .and_then(|s| comp.find_node(s)) |
| 368 | | }); |
| 369 | | let mut mat = resolve_material(comp, mat_node, time, expr); |
| 370 | | if mat_node.is_none() { |
| 371 | | let r_def = comp.eval_number(node, "Front.MtlStdInputs.Diffuse.Color.Red", time, 1.0, expr); |
| 372 | | let g_def = comp.eval_number(node, "Front.MtlStdInputs.Diffuse.Color.Green", time, 1.0, expr); |
| 373 | | let b_def = comp.eval_number(node, "Front.MtlStdInputs.Diffuse.Color.Blue", time, 1.0, expr); |
| 374 | | let a_def = comp.eval_number(node, "Front.MtlStdInputs.Diffuse.Opacity", time, 1.0, expr); |
| 375 | | mat.diffuse_col = [ |
| 376 | | comp.eval_number(node, "MtlStdInputs.Diffuse.Color.Red", time, r_def, expr) as f32, |
| 377 | | comp.eval_number(node, "MtlStdInputs.Diffuse.Color.Green", time, g_def, expr) as f32, |
| 378 | | comp.eval_number(node, "MtlStdInputs.Diffuse.Color.Blue", time, b_def, expr) as f32, |
| 379 | | comp.eval_number(node, "MtlStdInputs.Diffuse.Opacity", time, a_def, expr) as f32, |
| 380 | | ]; |
| 329 | if let Some(own) = surface_scope(comp, node, "SurfaceCubeInputs", ovr, time, expr) { |
| 330 | // Fusion's Cube3D only honors Width (all edges) and takes one material per face. |
| 331 | let width = comp.eval_number(node, "SurfaceCubeInputs.Width", time, 1.0, expr) as f32; |
| 332 | let wireframe = comp.eval_number(node, "SurfaceCubeInputs.Wireframe", time, 0.0, expr) > 0.5; |
| 333 | let sub = cube_subdivisions(comp, node, time, expr); |
| 334 | let mut groups: Vec<(Option<NodeId>, ResolvedMaterial, u8)> = Vec::new(); |
| 335 | for (i, face) in CUBE_FACES.iter().enumerate() { |
| 336 | let mat_node = ovr.material_override.or_else(|| { |
| 337 | node.connected_node(&format!("{face}MaterialInput")) |
| 338 | .and_then(|s| comp.find_node(s)) |
| 339 | }); |
| 340 | let mut mat = resolve_material(comp, mat_node, time, expr); |
| 341 | if mat_node.is_none() { |
| 342 | apply_mtl_std(&mut mat, comp, node, &format!("{face}."), time, expr); |
| 343 | } |
| 344 | mat.fx.wireframe = wireframe; |
| 345 | match groups.iter_mut().find(|(n, m, _)| *n == mat_node && *m == mat) { |
| 346 | Some((_, _, mask)) => *mask |= 1 << i, |
| 347 | None => groups.push((mat_node, mat, 1 << i)), |
| 348 | } |
| 349 | } |
| 350 | for (_, mat, faces) in groups { |
| 351 | meshes.push(make_mesh_instance(world, ShapeKind::Cube { width, sub, faces }, mat, None, &own)); |
| 352 | } |
| 381 | 353 | } |
| 382 | | meshes.push(make_mesh_instance(world, shape, mat, None, ovr)); |
| 383 | 354 | if let Some(src_id) = node.connected_node("SceneInput").and_then(|s| comp.find_node(s)) { |
| 384 | 355 | collect_scene_inner(comp, src_id, time, expr, world, render_aspect, ovr, cameras, meshes, lights, depth + 1); |
| 385 | 356 | } |
| 386 | 357 | } |
| 387 | 358 | "SurfaceFBXMesh" => { |
| 388 | | let import_file = node.static_str("ImportFile").unwrap_or(""); |
| 389 | | let obj_name = node.static_str("ObjName"); |
| 390 | | let scale_units = comp.eval_number(node, "ScaleFileUnitsBy", time, 1.0, expr) as f32; |
| 391 | | let verts = load_mesh_cached(import_file, comp.path.as_deref(), obj_name, scale_units); |
| 392 | | let shape = ShapeKind::CustomMesh(verts); |
| 393 | | let mat_node = ovr.material_override.or_else(|| { |
| 394 | | node.connected_node("MaterialInput") |
| 395 | | .or_else(|| { |
| 396 | | let mut slots: Vec<&String> = node |
| 397 | | .inputs |
| 398 | | .keys() |
| 399 | | .filter(|k| k.starts_with("Materials.")) |
| 400 | | .collect(); |
| 401 | | slots.sort(); |
| 402 | | slots.into_iter().find_map(|k| node.connected_node(k)) |
| 403 | | }) |
| 404 | | .and_then(|s| comp.find_node(s)) |
| 405 | | }); |
| 406 | | let mut mat = resolve_material(comp, mat_node, time, expr); |
| 407 | | if mat_node.is_none() { |
| 408 | | mat.diffuse_col = [ |
| 409 | | comp.eval_number(node, "MtlStdInputs.Diffuse.Color.Red", time, 1.0, expr) as f32, |
| 410 | | comp.eval_number(node, "MtlStdInputs.Diffuse.Color.Green", time, 1.0, expr) as f32, |
| 411 | | comp.eval_number(node, "MtlStdInputs.Diffuse.Color.Blue", time, 1.0, expr) as f32, |
| 412 | | comp.eval_number(node, "MtlStdInputs.Diffuse.Opacity", time, 1.0, expr) as f32, |
| 413 | | ]; |
| 359 | if let Some(own) = surface_scope(comp, node, "SurfaceFBXInputs", ovr, time, expr) { |
| 360 | let import_file = node.static_str("ImportFile").unwrap_or(""); |
| 361 | let obj_name = node.static_str("ObjName"); |
| 362 | let scale_units = comp.eval_number(node, "ScaleFileUnitsBy", time, 1.0, expr) as f32; |
| 363 | let verts = load_mesh_cached(import_file, comp.path.as_deref(), obj_name, scale_units); |
| 364 | let shape = ShapeKind::CustomMesh(verts); |
| 365 | let mat_node = ovr.material_override.or_else(|| { |
| 366 | node.connected_node("MaterialInput") |
| 367 | .or_else(|| { |
| 368 | let mut slots: Vec<&String> = node |
| 369 | .inputs |
| 370 | .keys() |
| 371 | .filter(|k| k.starts_with("Materials.")) |
| 372 | .collect(); |
| 373 | slots.sort(); |
| 374 | slots.into_iter().find_map(|k| node.connected_node(k)) |
| 375 | }) |
| 376 | .and_then(|s| comp.find_node(s)) |
| 377 | }); |
| 378 | let mut mat = resolve_material(comp, mat_node, time, expr); |
| 379 | if mat_node.is_none() { |
| 380 | apply_mtl_std(&mut mat, comp, node, "", time, expr); |
| 381 | } |
| 382 | meshes.push(make_mesh_instance(world, shape, mat, None, &own)); |
| 414 | 383 | } |
| 415 | | meshes.push(make_mesh_instance(world, shape, mat, None, ovr)); |
| 416 | 384 | if let Some(src_id) = node.connected_node("SceneInput").and_then(|s| comp.find_node(s)) { |
| 417 | 385 | collect_scene_inner(comp, src_id, time, expr, world, render_aspect, ovr, cameras, meshes, lights, depth + 1); |
| 418 | 386 | } |
| ... | ... | @@ -445,18 +413,16 @@ fn collect_scene_inner( |
| 445 | 413 | } |
| 446 | 414 | } |
| 447 | 415 | |
| 416 | #[derive(PartialEq)] |
| 448 | 417 | struct ResolvedMaterial { |
| 449 | 418 | tex_node: Option<NodeId>, |
| 450 | 419 | diffuse_col: [f32; 4], |
| 451 | 420 | specular_col: [f32; 3], |
| 452 | 421 | specular_intensity: f32, |
| 453 | 422 | specular_exponent: f32, |
| 454 | | uv_offset: [f32; 2], |
| 455 | | uv_scale: [f32; 2], |
| 456 | | uv_rot: f32, |
| 457 | | uv_wrap: u32, |
| 458 | | bump_scale: f32, |
| 459 | | bump_mode: u32, |
| 423 | fx: MaterialFx, |
| 424 | /// A bare texture operator used as a surface's material renders flat, ignoring lights. |
| 425 | unlit: bool, |
| 460 | 426 | } |
| 461 | 427 | |
| 462 | 428 | impl Default for ResolvedMaterial { |
| ... | ... | @@ -465,14 +431,10 @@ impl Default for ResolvedMaterial { |
| 465 | 431 | tex_node: None, |
| 466 | 432 | diffuse_col: [1.0, 1.0, 1.0, 1.0], |
| 467 | 433 | specular_col: [1.0, 1.0, 1.0], |
| 468 | | specular_intensity: 0.15, |
| 469 | | specular_exponent: 25.0, |
| 470 | | uv_offset: [0.0, 0.0], |
| 471 | | uv_scale: [1.0, 1.0], |
| 472 | | uv_rot: 0.0, |
| 473 | | uv_wrap: 0, |
| 474 | | bump_scale: 0.0, |
| 475 | | bump_mode: 0, |
| 434 | specular_intensity: 1.0, |
| 435 | specular_exponent: 50.0, |
| 436 | fx: MaterialFx::default(), |
| 437 | unlit: false, |
| 476 | 438 | } |
| 477 | 439 | } |
| 478 | 440 | } |
| ... | ... | @@ -484,54 +446,66 @@ fn make_mesh_instance( |
| 484 | 446 | text3d: Option<Text3DSpec>, |
| 485 | 447 | ovr: &SceneOverrides, |
| 486 | 448 | ) -> MeshInstance { |
| 487 | | let has_fx = mat.uv_offset[0].abs() > 1e-6 |
| 488 | | || mat.uv_offset[1].abs() > 1e-6 |
| 489 | | || (mat.uv_scale[0] - 1.0).abs() > 1e-6 |
| 490 | | || (mat.uv_scale[1] - 1.0).abs() > 1e-6 |
| 491 | | || mat.uv_rot.abs() > 1e-6 |
| 492 | | || mat.uv_wrap != 0 |
| 493 | | || mat.bump_scale.abs() > 1e-6; |
| 494 | | let wrapped_shape = if has_fx { |
| 495 | | ShapeKind::WithMaterialFx { |
| 496 | | inner: Box::new(shape), |
| 497 | | uv_offset: mat.uv_offset, |
| 498 | | uv_scale: mat.uv_scale, |
| 499 | | uv_rot: mat.uv_rot, |
| 500 | | uv_wrap: mat.uv_wrap, |
| 501 | | bump_scale: mat.bump_scale, |
| 502 | | bump_mode: mat.bump_mode, |
| 503 | | } |
| 504 | | } else { |
| 505 | | shape |
| 506 | | }; |
| 507 | 449 | MeshInstance { |
| 508 | 450 | world, |
| 509 | | shape: wrapped_shape, |
| 451 | shape: ShapeKind::WithMaterialFx { |
| 452 | inner: Box::new(shape), |
| 453 | fx: MaterialFx { cull_back: ovr.cull_back, cull_front: ovr.cull_front, ..mat.fx }, |
| 454 | }, |
| 510 | 455 | material_node: mat.tex_node, |
| 511 | 456 | text3d, |
| 512 | 457 | diffuse_col: mat.diffuse_col, |
| 513 | 458 | specular_col: mat.specular_col, |
| 514 | 459 | specular_intensity: mat.specular_intensity, |
| 515 | 460 | specular_exponent: mat.specular_exponent, |
| 516 | | affected_by_lights: ovr.affected_by_lights.unwrap_or(true), |
| 461 | affected_by_lights: !mat.unlit && ovr.affected_by_lights.unwrap_or(true), |
| 517 | 462 | flip_normals: ovr.flip_normals, |
| 518 | 463 | displace: ovr.displace.clone(), |
| 519 | | uv_offset: mat.uv_offset, |
| 520 | | uv_scale: mat.uv_scale, |
| 521 | | uv_rot: mat.uv_rot, |
| 522 | | uv_wrap: mat.uv_wrap, |
| 523 | | bump_scale: mat.bump_scale, |
| 524 | | bump_mode: mat.bump_mode, |
| 525 | 464 | } |
| 526 | 465 | } |
| 527 | 466 | |
| 528 | | fn eval_first_present(comp: &Comp, node: &Node, keys: &[&str], time: f64, default: f64, expr: &ExprEngine) -> f32 { |
| 529 | | for &k in keys { |
| 530 | | if node.inputs.contains_key(k) { |
| 531 | | return comp.eval_number(node, k, time, default, expr) as f32; |
| 532 | | } |
| 467 | /// Applies a surface's own `<group>.Visibility.*` / `<group>.Lighting.IsAffectedByLights` |
| 468 | /// inputs on top of inherited overrides; `None` when the surface is hidden. |
| 469 | fn surface_scope( |
| 470 | comp: &Comp, |
| 471 | node: &Node, |
| 472 | group: &str, |
| 473 | ovr: &SceneOverrides, |
| 474 | time: f64, |
| 475 | expr: &ExprEngine, |
| 476 | ) -> Option<SceneOverrides> { |
| 477 | let flag = |k: &str, d: f64| comp.eval_number(node, &format!("{group}.{k}"), time, d, expr) > 0.5; |
| 478 | flag("Visibility.IsVisible", 1.0).then(|| SceneOverrides { |
| 479 | affected_by_lights: ovr.affected_by_lights.or(Some(flag("Lighting.IsAffectedByLights", 1.0))), |
| 480 | cull_back: flag("Visibility.CullBackFace", 0.0), |
| 481 | cull_front: flag("Visibility.CullFrontFace", 0.0), |
| 482 | ..ovr.clone() |
| 483 | }) |
| 484 | } |
| 485 | |
| 486 | /// `Intensity * RGB` of a light, or `None` when it is disabled or contributes nothing. |
| 487 | fn light_color(comp: &Comp, node: &Node, time: f64, expr: &ExprEngine) -> Option<[f32; 3]> { |
| 488 | if comp.eval_number(node, "Enabled", time, 1.0, expr) < 0.5 { |
| 489 | return None; |
| 533 | 490 | } |
| 534 | | default as f32 |
| 491 | let intensity = comp.eval_number(node, "Intensity", time, 1.0, expr) as f32; |
| 492 | let color = ["Red", "Green", "Blue"].map(|k| comp.eval_number(node, k, time, 1.0, expr) as f32 * intensity); |
| 493 | (color != [0.0; 3]).then_some(color) |
| 494 | } |
| 495 | |
| 496 | /// Cells per cube edge; Fusion's cube keeps all three subdivision counts equal. |
| 497 | fn cube_subdivisions(comp: &Comp, node: &Node, time: f64, expr: &ExprEngine) -> u32 { |
| 498 | comp.eval_number(node, "SurfaceCubeInputs.SubdivisionWidth", time, 10.0, expr).round() as u32 |
| 499 | } |
| 500 | |
| 501 | /// Reads the built-in `MtlStdInputs` material (diffuse and Blinn specular) under an optional |
| 502 | /// per-face `prefix` such as `"Front."` into `mat`. |
| 503 | fn apply_mtl_std(mat: &mut ResolvedMaterial, comp: &Comp, node: &Node, prefix: &str, time: f64, expr: &ExprEngine) { |
| 504 | let f = |k: &str, d: f64| comp.eval_number(node, &format!("{prefix}MtlStdInputs.{k}"), time, d, expr) as f32; |
| 505 | mat.diffuse_col = [f("Diffuse.Color.Red", 1.0), f("Diffuse.Color.Green", 1.0), f("Diffuse.Color.Blue", 1.0), f("Diffuse.Opacity", 1.0)]; |
| 506 | mat.specular_col = [f("Specular.Color.Red", 1.0), f("Specular.Color.Green", 1.0), f("Specular.Color.Blue", 1.0)]; |
| 507 | mat.specular_intensity = f("Specular.Intensity", 1.0).clamp(0.0, 4.0); |
| 508 | mat.specular_exponent = f("Specular.Exponent", 50.0).max(1.0); |
| 535 | 509 | } |
| 536 | 510 | |
| 537 | 511 | fn parse_wrap_mode(comp: &Comp, node: &Node, time: f64, expr: &ExprEngine) -> Option<u32> { |
| ... | ... | @@ -553,6 +527,20 @@ fn parse_wrap_mode(comp: &Comp, node: &Node, time: f64, expr: &ExprEngine) -> Op |
| 553 | 527 | None |
| 554 | 528 | } |
| 555 | 529 | |
| 530 | /// Mesh-UV -> texture-UV map for a `TextureTransformOperator`. The content is moved by |
| 531 | /// translate * rotate * scale about the pivot in V-up image space, so sampling applies the inverse. |
| 532 | fn texture_transform_uv(translate: [f32; 2], rot_rad: f32, scale: [f32; 2], pivot: [f32; 2]) -> UvAffine { |
| 533 | let inv = |v: f32| 1.0 / if v.abs() < 1e-4 { 1e-4 } else { v }; |
| 534 | let (sn, cs) = rot_rad.sin_cos(); |
| 535 | let sample = |uv: [f32; 2]| { |
| 536 | let q = [uv[0] - translate[0] - pivot[0], (1.0 - uv[1]) - translate[1] - pivot[1]]; |
| 537 | let r = [cs * q[0] + sn * q[1], -sn * q[0] + cs * q[1]]; |
| 538 | [pivot[0] + r[0] * inv(scale[0]), 1.0 - (pivot[1] + r[1] * inv(scale[1]))] |
| 539 | }; |
| 540 | let (o, eu, ev) = (sample([0.0, 0.0]), sample([1.0, 0.0]), sample([0.0, 1.0])); |
| 541 | UvAffine { m: [eu[0] - o[0], eu[1] - o[1], ev[0] - o[0], ev[1] - o[1]], t: o } |
| 542 | } |
| 543 | |
| 556 | 544 | /// Resolves a 3D material input (`Texture2DOperator`, `TextureTransformOperator`, `BumpMap`, |
| 557 | 545 | /// `MtlBlinn`, `MtlCookTorrance`, `MtlPhong`, or a direct 2D image node). |
| 558 | 546 | fn resolve_material( |
| ... | ... | @@ -582,36 +570,29 @@ fn resolve_material_inner( |
| 582 | 570 | match n.kind.as_str() { |
| 583 | 571 | "Texture2DOperator" => { |
| 584 | 572 | let tex_src = n.connected_node("Input").and_then(|s| comp.find_node(s)); |
| 585 | | let uv_wrap = parse_wrap_mode(comp, n, time, expr).unwrap_or(0); |
| 586 | 573 | ResolvedMaterial { |
| 587 | 574 | tex_node: tex_src, |
| 588 | | uv_wrap, |
| 575 | fx: MaterialFx { |
| 576 | uv_wrap: parse_wrap_mode(comp, n, time, expr).unwrap_or(0), |
| 577 | ..default_mat.fx |
| 578 | }, |
| 579 | unlit: true, |
| 589 | 580 | ..default_mat |
| 590 | 581 | } |
| 591 | 582 | } |
| 592 | 583 | "TextureTransformOperator" => { |
| 593 | | let inner_id = n |
| 594 | | .connected_node("MaterialInput") |
| 595 | | .or_else(|| n.connected_node("Input")) |
| 596 | | .and_then(|s| comp.find_node(s)); |
| 584 | let inner_id = n.connected_node("MaterialInput").and_then(|s| comp.find_node(s)); |
| 597 | 585 | let mut mat = resolve_material_inner(comp, inner_id, time, expr, depth + 1); |
| 598 | | let tx = eval_first_present(comp, n, &["TranslationOffset.X", "Translation.U", "Translation.X"], time, 0.0, expr); |
| 599 | | let ty = eval_first_present(comp, n, &["TranslationOffset.Y", "Translation.V", "Translation.Y"], time, 0.0, expr); |
| 600 | | let sx = eval_first_present(comp, n, &["Scale.X", "Scale.U"], time, 1.0, expr); |
| 601 | | let sy = eval_first_present(comp, n, &["Scale.Y", "Scale.V"], time, 1.0, expr); |
| 602 | | let rot_deg = eval_first_present( |
| 603 | | comp, |
| 604 | | n, |
| 605 | | &["Rotation.Z", "RotationOffset", "Rotation.W", "Rotation.U"], |
| 606 | | time, |
| 607 | | 0.0, |
| 608 | | expr, |
| 586 | let f = |k: &str, d: f64| comp.eval_number(n, k, time, d, expr) as f32; |
| 587 | let xf = texture_transform_uv( |
| 588 | [f("Translation.U", 0.0), f("Translation.V", 0.0)], |
| 589 | f("Rotation.W", 0.0).to_radians(), |
| 590 | [f("Scale.U", 1.0), f("Scale.V", 1.0)], |
| 591 | [f("Pivot.U", 0.0), f("Pivot.V", 0.0)], |
| 609 | 592 | ); |
| 610 | | mat.uv_offset = [mat.uv_offset[0] + tx, mat.uv_offset[1] + ty]; |
| 611 | | mat.uv_scale = [mat.uv_scale[0] * sx, mat.uv_scale[1] * sy]; |
| 612 | | mat.uv_rot += rot_deg.to_radians(); |
| 593 | mat.fx.uv = xf.then(&mat.fx.uv); |
| 613 | 594 | if let Some(w) = parse_wrap_mode(comp, n, time, expr) { |
| 614 | | mat.uv_wrap = w; |
| 595 | mat.fx.uv_wrap = w; |
| 615 | 596 | } |
| 616 | 597 | mat |
| 617 | 598 | } |
| ... | ... | @@ -625,12 +606,14 @@ fn resolve_material_inner( |
| 625 | 606 | .round() |
| 626 | 607 | .clamp(0.0, 1.0) as u32 |
| 627 | 608 | }; |
| 628 | | let uv_wrap = parse_wrap_mode(comp, n, time, expr).unwrap_or(0); |
| 629 | 609 | ResolvedMaterial { |
| 630 | 610 | tex_node: src, |
| 631 | | uv_wrap, |
| 632 | | bump_scale: height_scale, |
| 633 | | bump_mode, |
| 611 | fx: MaterialFx { |
| 612 | uv_wrap: parse_wrap_mode(comp, n, time, expr).unwrap_or(0), |
| 613 | bump_scale: height_scale, |
| 614 | bump_mode, |
| 615 | ..default_mat.fx |
| 616 | }, |
| 634 | 617 | ..default_mat |
| 635 | 618 | } |
| 636 | 619 | } |
| ... | ... | @@ -657,45 +640,34 @@ fn resolve_material_inner( |
| 657 | 640 | comp.eval_number(n, "Specular.Color.Blue", time, 1.0, expr) as f32, |
| 658 | 641 | ]; |
| 659 | 642 | let spec_int = comp.eval_number(n, "Specular.Intensity", time, 1.0, expr) as f32; |
| 660 | | let (spec_exp, spec_gain) = if n.kind == "MtlCookTorrance" { |
| 661 | | let rough = comp.eval_number(n, "Specular.Roughness", time, 0.4, expr).clamp(0.05, 1.0) as f32; |
| 662 | | ((2.0 / (rough * rough) - 2.0).max(2.0), 0.15) |
| 663 | | } else { |
| 664 | | (comp.eval_number(n, "Specular.Exponent", time, 25.0, expr).max(1.0) as f32, 1.0) |
| 643 | let (spec_exp, spec_model) = match n.kind.as_str() { |
| 644 | "MtlCookTorrance" => ( |
| 645 | comp.eval_number(n, "Specular.Roughness", time, 0.6, expr).clamp(0.05, 1.0) as f32, |
| 646 | SpecModel::CookTorrance, |
| 647 | ), |
| 648 | "MtlPhong" => (comp.eval_number(n, "Specular.Exponent", time, 50.0, expr).max(1.0) as f32, SpecModel::Phong), |
| 649 | _ => (comp.eval_number(n, "Specular.Exponent", time, 50.0, expr).max(1.0) as f32, SpecModel::Blinn), |
| 665 | 650 | }; |
| 666 | 651 | |
| 667 | 652 | let mut tex_node = diff_mat.tex_node; |
| 668 | | let mut uv_offset = diff_mat.uv_offset; |
| 669 | | let mut uv_scale = diff_mat.uv_scale; |
| 670 | | let mut uv_rot = diff_mat.uv_rot; |
| 671 | | let mut uv_wrap = diff_mat.uv_wrap; |
| 672 | | let mut bump_scale = diff_mat.bump_scale; |
| 673 | | let mut bump_mode = diff_mat.bump_mode; |
| 674 | | |
| 653 | let mut fx = MaterialFx { spec_model, ..diff_mat.fx }; |
| 675 | 654 | if let Some(bm) = bump_mat { |
| 676 | | bump_scale = if bm.bump_scale.abs() > 1e-6 { bm.bump_scale } else { 1.0 }; |
| 677 | | bump_mode = bm.bump_mode; |
| 678 | 655 | if tex_node.is_none() { |
| 679 | 656 | tex_node = bm.tex_node; |
| 680 | | uv_offset = bm.uv_offset; |
| 681 | | uv_scale = bm.uv_scale; |
| 682 | | uv_rot = bm.uv_rot; |
| 683 | | uv_wrap = bm.uv_wrap; |
| 657 | fx = MaterialFx { bump_only: true, spec_model, ..bm.fx }; |
| 684 | 658 | } |
| 659 | fx.bump_scale = if bm.fx.bump_scale.abs() > 1e-6 { bm.fx.bump_scale } else { 1.0 }; |
| 660 | fx.bump_mode = bm.fx.bump_mode; |
| 685 | 661 | } |
| 686 | 662 | |
| 687 | 663 | ResolvedMaterial { |
| 688 | 664 | tex_node, |
| 689 | 665 | diffuse_col: col, |
| 690 | 666 | specular_col: spec_col, |
| 691 | | specular_intensity: spec_int.clamp(0.0, 4.0) * spec_gain, |
| 667 | specular_intensity: spec_int.clamp(0.0, 4.0), |
| 692 | 668 | specular_exponent: spec_exp, |
| 693 | | uv_offset, |
| 694 | | uv_scale, |
| 695 | | uv_rot, |
| 696 | | uv_wrap, |
| 697 | | bump_scale, |
| 698 | | bump_mode, |
| 669 | fx, |
| 670 | unlit: false, |
| 699 | 671 | } |
| 700 | 672 | } |
| 701 | 673 | _ => ResolvedMaterial { |
| ... | ... | @@ -911,67 +883,70 @@ mod tests { |
| 911 | 883 | |
| 912 | 884 | #[test] |
| 913 | 885 | fn test_collect_fbx_cube_tex_transform_and_bump() { |
| 914 | | let src = r#"Composition { |
| 886 | let faces: String = CUBE_FACES |
| 887 | .iter() |
| 888 | .map(|f| format!(r#"{f}MaterialInput = Input {{ SourceOp = "BlinnBump", Source = "MaterialOutput", }},"#)) |
| 889 | .collect(); |
| 890 | let src = format!( |
| 891 | r#"Composition {{ |
| 915 | 892 | CurrentTime = 0, |
| 916 | | RenderRange = { 0, 10 }, |
| 917 | | GlobalRange = { 0, 10 }, |
| 918 | | Tools = ordered() { |
| 919 | | BG1 = Background { |
| 920 | | Inputs = { |
| 921 | | Width = Input { Value = 64, }, |
| 922 | | Height = Input { Value = 64, }, |
| 923 | | }, |
| 924 | | }, |
| 925 | | Bump1 = BumpMap { |
| 926 | | Inputs = { |
| 927 | | Input = Input { SourceOp = "BG1", Source = "Output", }, |
| 928 | | HeightScale = Input { Value = 4.5, }, |
| 929 | | SourceImageType = Input { Value = 1, }, |
| 930 | | WrapMode = Input { Value = FuID { "Wrap" }, }, |
| 931 | | }, |
| 932 | | }, |
| 933 | | TexXf = TextureTransformOperator { |
| 934 | | Inputs = { |
| 935 | | MaterialInput = Input { SourceOp = "Bump1", Source = "MaterialOutput", }, |
| 936 | | ["TranslationOffset.X"] = Input { Value = 0.25, }, |
| 937 | | ["TranslationOffset.Y"] = Input { Value = -0.5, }, |
| 938 | | ["Scale.U"] = Input { Value = 2.0, }, |
| 939 | | ["Scale.V"] = Input { Value = 3.0, }, |
| 940 | | ["Rotation.Z"] = Input { Value = 90.0, }, |
| 941 | | }, |
| 942 | | }, |
| 943 | | BlinnBump = MtlBlinn { |
| 944 | | Inputs = { |
| 945 | | ["Diffuse.Color.Red"] = Input { Value = 0.7, }, |
| 946 | | ["Bumpmap.Material"] = Input { SourceOp = "TexXf", Source = "MaterialOutput", }, |
| 947 | | }, |
| 948 | | }, |
| 949 | | CubeNode = Cube3D { |
| 950 | | Inputs = { |
| 951 | | ["SurfaceCubeInputs.Size"] = Input { Value = 2.0, }, |
| 952 | | ["SurfaceCubeInputs.Height"] = Input { Value = 4.0, }, |
| 953 | | ["Transform3DOp.Translate.Y"] = Input { Value = 1.5, }, |
| 954 | | MaterialInput = Input { SourceOp = "BlinnBump", Source = "MaterialOutput", }, |
| 955 | | }, |
| 956 | | }, |
| 957 | | FbxNode = SurfaceFBXMesh { |
| 958 | | Inputs = { |
| 959 | | ImportFile = Input { Value = "/does/not/exist/model.fbx", }, |
| 960 | | ObjName = Input { Value = "HeroMesh", }, |
| 961 | | ScaleFileUnitsBy = Input { Value = 0.01, }, |
| 962 | | ["Transform3DOp.Translate.X"] = Input { Value = -2.0, }, |
| 963 | | ["Materials.Mat0_mat0"] = Input { SourceOp = "TexXf", Source = "MaterialOutput", }, |
| 964 | | }, |
| 965 | | }, |
| 966 | | Merge1 = Merge3D { |
| 967 | | Inputs = { |
| 968 | | SceneInput1 = Input { SourceOp = "CubeNode", Source = "Output", }, |
| 969 | | SceneInput2 = Input { SourceOp = "FbxNode", Source = "Output", }, |
| 970 | | }, |
| 971 | | }, |
| 972 | | }, |
| 973 | | }"#; |
| 974 | | let comp = Comp::from_fusion_str(src).expect("parse comp"); |
| 893 | RenderRange = {{ 0, 10 }}, |
| 894 | GlobalRange = {{ 0, 10 }}, |
| 895 | Tools = ordered() {{ |
| 896 | BG1 = Background {{ |
| 897 | Inputs = {{ |
| 898 | Width = Input {{ Value = 64, }}, |
| 899 | Height = Input {{ Value = 64, }}, |
| 900 | }}, |
| 901 | }}, |
| 902 | Bump1 = BumpMap {{ |
| 903 | Inputs = {{ |
| 904 | Input = Input {{ SourceOp = "BG1", Source = "Output", }}, |
| 905 | HeightScale = Input {{ Value = 4.5, }}, |
| 906 | SourceImageType = Input {{ Value = 1, }}, |
| 907 | WrapMode = Input {{ Value = FuID {{ "Wrap" }}, }}, |
| 908 | }}, |
| 909 | }}, |
| 910 | TexXf = TextureTransformOperator {{ |
| 911 | Inputs = {{ |
| 912 | MaterialInput = Input {{ SourceOp = "Bump1", Source = "MaterialOutput", }}, |
| 913 | ["Translation.U"] = Input {{ Value = 0.25, }}, |
| 914 | ["Translation.V"] = Input {{ Value = -0.5, }}, |
| 915 | }}, |
| 916 | }}, |
| 917 | BlinnBump = MtlBlinn {{ |
| 918 | Inputs = {{ |
| 919 | ["Diffuse.Color.Red"] = Input {{ Value = 0.7, }}, |
| 920 | ["Bumpmap.Material"] = Input {{ SourceOp = "TexXf", Source = "MaterialOutput", }}, |
| 921 | }}, |
| 922 | }}, |
| 923 | CubeNode = Cube3D {{ |
| 924 | Inputs = {{ |
| 925 | ["SurfaceCubeInputs.Width"] = Input {{ Value = 2.0, }}, |
| 926 | ["SurfaceCubeInputs.Height"] = Input {{ Value = 4.0, }}, |
| 927 | ["Transform3DOp.Translate.Y"] = Input {{ Value = 1.5, }}, |
| 928 | {faces} |
| 929 | }}, |
| 930 | }}, |
| 931 | FbxNode = SurfaceFBXMesh {{ |
| 932 | Inputs = {{ |
| 933 | ImportFile = Input {{ Value = "/does/not/exist/model.fbx", }}, |
| 934 | ObjName = Input {{ Value = "HeroMesh", }}, |
| 935 | ScaleFileUnitsBy = Input {{ Value = 0.01, }}, |
| 936 | ["Transform3DOp.Translate.X"] = Input {{ Value = -2.0, }}, |
| 937 | ["Materials.Mat0_mat0"] = Input {{ SourceOp = "TexXf", Source = "MaterialOutput", }}, |
| 938 | }}, |
| 939 | }}, |
| 940 | Merge1 = Merge3D {{ |
| 941 | Inputs = {{ |
| 942 | SceneInput1 = Input {{ SourceOp = "CubeNode", Source = "Output", }}, |
| 943 | SceneInput2 = Input {{ SourceOp = "FbxNode", Source = "Output", }}, |
| 944 | }}, |
| 945 | }}, |
| 946 | }}, |
| 947 | }}"# |
| 948 | ); |
| 949 | let comp = Comp::from_fusion_str(&src).expect("parse comp"); |
| 975 | 950 | let expr = ExprEngine::new(); |
| 976 | 951 | let merge_id = comp.find_node("Merge1").expect("Merge1"); |
| 977 | 952 | let bg_id = comp.find_node("BG1").expect("BG1"); |
| ... | ... | @@ -990,32 +965,184 @@ mod tests { |
| 990 | 965 | &mut lights, |
| 991 | 966 | 0, |
| 992 | 967 | ); |
| 968 | // Six identical face materials collapse into one cube; Height is ignored. |
| 993 | 969 | assert_eq!(meshes.len(), 2); |
| 994 | 970 | |
| 995 | | // Cube3D with Blinn + Bumpmap (TextureTransformOperator -> BumpMap) |
| 996 | 971 | let cube = &meshes[0]; |
| 997 | 972 | assert!(matches!( |
| 998 | 973 | cube.shape.base_shape(), |
| 999 | | ShapeKind::Cube { width, height, depth } |
| 1000 | | if (*width - 2.0).abs() < 1e-4 && (*height - 4.0).abs() < 1e-4 && (*depth - 2.0).abs() < 1e-4 |
| 974 | ShapeKind::Cube { width, faces: 0b11_1111, .. } if (*width - 2.0).abs() < 1e-4 |
| 1001 | 975 | )); |
| 1002 | 976 | assert!((cube.world[13] - 1.5).abs() < 1e-4); |
| 1003 | 977 | assert_eq!(cube.material_node, Some(bg_id)); |
| 1004 | 978 | assert!((cube.diffuse_col[0] - 0.7).abs() < 1e-4); |
| 1005 | | assert!((cube.bump_scale - 4.5).abs() < 1e-4); |
| 1006 | | assert_eq!(cube.bump_mode, 1); |
| 1007 | | assert!((cube.uv_offset[0] - 0.25).abs() < 1e-4); |
| 1008 | | assert!((cube.uv_offset[1] + 0.5).abs() < 1e-4); |
| 1009 | | assert!((cube.uv_scale[0] - 2.0).abs() < 1e-4); |
| 1010 | | assert!((cube.uv_scale[1] - 3.0).abs() < 1e-4); |
| 1011 | | assert!((cube.uv_rot - std::f32::consts::FRAC_PI_2).abs() < 1e-4); |
| 1012 | | assert_eq!(cube.uv_wrap, 1); |
| 1013 | | |
| 1014 | | // SurfaceFBXMesh fallback unit box + Materials.* slot resolution |
| 979 | let fx = cube.shape.split_fx().1; |
| 980 | assert!((fx.bump_scale - 4.5).abs() < 1e-4); |
| 981 | assert_eq!(fx.bump_mode, 1); |
| 982 | assert!(!fx.bump_only || cube.material_node.is_some()); |
| 983 | assert_eq!(fx.uv_wrap, 1); |
| 984 | // Content moved +0.25 in U and -0.5 in V (V up) samples from u-0.25 and v-0.5. |
| 985 | let sampled = fx.uv.apply([0.5, 0.5]); |
| 986 | assert!((sampled[0] - 0.25).abs() < 1e-4 && sampled[1].abs() < 1e-4, "{sampled:?}"); |
| 987 | |
| 1015 | 988 | let fbx = &meshes[1]; |
| 1016 | 989 | assert!(matches!(fbx.shape.base_shape(), ShapeKind::CustomMesh(v) if v.len() == 36)); |
| 1017 | 990 | assert!((fbx.world[12] + 2.0).abs() < 1e-4); |
| 1018 | 991 | assert_eq!(fbx.material_node, Some(bg_id)); |
| 1019 | | assert!((fbx.bump_scale - 4.5).abs() < 1e-4); |
| 992 | assert!((fbx.shape.split_fx().1.bump_scale - 4.5).abs() < 1e-4); |
| 993 | } |
| 994 | |
| 995 | fn collect(src: &str, root: &str) -> (Vec<CameraInstance>, Vec<MeshInstance>, SceneLights) { |
| 996 | let comp = Comp::from_fusion_str(src).expect("parse comp"); |
| 997 | let expr = ExprEngine::new(); |
| 998 | let id = comp.find_node(root).expect("root node"); |
| 999 | let (mut cams, mut meshes, mut lights) = (Vec::new(), Vec::new(), SceneLights::default()); |
| 1000 | collect_scene(&comp, id, 0.0, &expr, mat4_identity(), 1.0, &mut cams, &mut meshes, &mut lights, 0); |
| 1001 | (cams, meshes, lights) |
| 1002 | } |
| 1003 | |
| 1004 | fn comp_src(tools: &str) -> String { |
| 1005 | format!("Composition {{ CurrentTime = 0, RenderRange = {{ 0, 1 }}, GlobalRange = {{ 0, 1 }}, Tools = ordered() {{ {tools} }}, }}") |
| 1006 | } |
| 1007 | |
| 1008 | #[test] |
| 1009 | fn camera_vertical_aov_comes_from_default_35mm_focal_length() { |
| 1010 | // Serialized AoV is ignored: 35mm on a 1" gate gives 2*atan(12.7/35). |
| 1011 | let src = comp_src( |
| 1012 | r#"Cam = Camera3D { Inputs = { AoV = Input { Value = 35.0, }, ApertureW = Input { Value = 1.0, }, ApertureH = Input { Value = 1.0, }, }, }, |
| 1013 | M = Merge3D { Inputs = { SceneInput1 = Input { SourceOp = "Cam", Source = "Output", }, }, },"#, |
| 1014 | ); |
| 1015 | let (cams, ..) = collect(&src, "M"); |
| 1016 | let expected = 2.0 * (12.7_f32 / 35.0).atan(); |
| 1017 | assert!((cams[0].vfov_rad - expected).abs() < 1e-4, "{} vs {expected}", cams[0].vfov_rad); |
| 1018 | |
| 1019 | let src = comp_src( |
| 1020 | r#"Cam = Camera3D { Inputs = { AoV = Input { Value = 35.0, }, ApertureH = Input { Value = 1.0, }, FLength = Input { Value = 50.0, }, }, }, |
| 1021 | M = Merge3D { Inputs = { SceneInput1 = Input { SourceOp = "Cam", Source = "Output", }, }, },"#, |
| 1022 | ); |
| 1023 | let (cams, ..) = collect(&src, "M"); |
| 1024 | assert!((cams[0].vfov_rad - 2.0 * (12.7_f32 / 50.0).atan()).abs() < 1e-4); |
| 1025 | } |
| 1026 | |
| 1027 | #[test] |
| 1028 | fn plane_is_square_and_defaults_to_ten_cells() { |
| 1029 | let src = comp_src( |
| 1030 | r#"P = Shape3D { Inputs = { ["SurfacePlaneInputs.Width"] = Input { Value = 2.0, }, ["SurfacePlaneInputs.Height"] = Input { Value = 1.0, }, ["SurfacePlaneInputs.SubdivisionHeight"] = Input { Value = 3, }, }, }, |
| 1031 | M = Merge3D { Inputs = { SceneInput1 = Input { SourceOp = "P", Source = "Output", }, }, },"#, |
| 1032 | ); |
| 1033 | let (_, meshes, _) = collect(&src, "M"); |
| 1034 | assert!(matches!( |
| 1035 | meshes[0].shape.base_shape(), |
| 1036 | ShapeKind::Plane { width, height, sub_w: 10, sub_h: 10 } if *width == 2.0 && *height == 2.0 |
| 1037 | )); |
| 1038 | } |
| 1039 | |
| 1040 | #[test] |
| 1041 | fn cube3d_ignores_plain_material_input_and_splits_by_face_material() { |
| 1042 | let src = comp_src( |
| 1043 | r#"Bg = Background { Inputs = { Width = Input { Value = 8, }, Height = Input { Value = 8, }, }, }, |
| 1044 | Tex = Texture2DOperator { Inputs = { Input = Input { SourceOp = "Bg", Source = "Output", }, }, }, |
| 1045 | Plain = Cube3D { Inputs = { MaterialInput = Input { SourceOp = "Tex", Source = "MaterialOutput", }, }, }, |
| 1046 | Front = Cube3D { Inputs = { FrontMaterialInput = Input { SourceOp = "Tex", Source = "MaterialOutput", }, }, }, |
| 1047 | M = Merge3D { Inputs = { SceneInput1 = Input { SourceOp = "Plain", Source = "Output", }, SceneInput2 = Input { SourceOp = "Front", Source = "Output", }, }, },"#, |
| 1048 | ); |
| 1049 | let (_, meshes, _) = collect(&src, "M"); |
| 1050 | let tex = Comp::from_fusion_str(&src).unwrap().find_node("Bg"); |
| 1051 | assert_eq!(meshes.len(), 3); |
| 1052 | assert!(meshes[0].material_node.is_none(), "Cube3D has no plain MaterialInput"); |
| 1053 | assert!(matches!(meshes[1].shape.base_shape(), ShapeKind::Cube { faces: 0b00_0001, .. })); |
| 1054 | assert_eq!(meshes[1].material_node, tex); |
| 1055 | assert!(matches!(meshes[2].shape.base_shape(), ShapeKind::Cube { faces: 0b11_1110, .. })); |
| 1056 | } |
| 1057 | |
| 1058 | #[test] |
| 1059 | fn texture_transform_moves_content_about_a_bottom_left_pivot() { |
| 1060 | // Scale 2 about the (0, 0) pivot zooms toward the bottom-left corner (V up). |
| 1061 | let zoom = texture_transform_uv([0.0, 0.0], 0.0, [2.0, 2.0], [0.0, 0.0]); |
| 1062 | let (bl, tr) = (zoom.apply([0.0, 1.0]), zoom.apply([1.0, 0.0])); |
| 1063 | assert!(bl[0].abs() < 1e-5 && (bl[1] - 1.0).abs() < 1e-5); |
| 1064 | assert!((tr[0] - 0.5).abs() < 1e-5 && (tr[1] - 0.5).abs() < 1e-5); |
| 1065 | |
| 1066 | // Content moved right and up samples from smaller u and larger raw v (V down). |
| 1067 | let shift = texture_transform_uv([0.25, 0.1], 0.0, [1.0, 1.0], [0.0, 0.0]).apply([0.5, 0.5]); |
| 1068 | assert!((shift[0] - 0.25).abs() < 1e-5 && (shift[1] - 0.6).abs() < 1e-5); |
| 1069 | |
| 1070 | // Counter-clockwise content rotation about the pivot. |
| 1071 | let quarter = texture_transform_uv([0.0, 0.0], std::f32::consts::FRAC_PI_2, [1.0, 1.0], [0.0, 0.0]); |
| 1072 | let p = quarter.apply([0.0, 0.0]); // top-left corner samples from the content to its right |
| 1073 | assert!((p[0] - 1.0).abs() < 1e-5 && (p[1] - 1.0).abs() < 1e-5, "{p:?}"); |
| 1074 | } |
| 1075 | |
| 1076 | #[test] |
| 1077 | fn disabled_and_black_lights_do_not_take_slots() { |
| 1078 | let spot = |name: &str, extra: &str| format!("{name} = LightSpot {{ Inputs = {{ {extra} }}, }},"); |
| 1079 | let src = comp_src(&format!( |
| 1080 | "{} {} {} Live = LightSpot {{ }}, M = Merge3D {{ Inputs = {{ SceneInput1 = Input {{ SourceOp = \"Off\", Source = \"Output\", }}, SceneInput2 = Input {{ SourceOp = \"Dark\", Source = \"Output\", }}, SceneInput3 = Input {{ SourceOp = \"Live\", Source = \"Output\", }}, }}, }},", |
| 1081 | spot("Off", "Enabled = Input { Value = 0, },"), |
| 1082 | spot("Dark", "Intensity = Input { Value = 0, },"), |
| 1083 | "" |
| 1084 | )); |
| 1085 | let (_, _, lights) = collect(&src, "M"); |
| 1086 | assert_eq!(lights.spots.len(), 1); |
| 1087 | assert_eq!(lights.spots[0].color, [1.0, 1.0, 1.0]); |
| 1088 | } |
| 1089 | |
| 1090 | #[test] |
| 1091 | fn surface_visibility_lighting_and_bare_texture_flags() { |
| 1092 | let src = comp_src( |
| 1093 | r#"Bg = Background { Inputs = { Width = Input { Value = 8, }, Height = Input { Value = 8, }, }, }, |
| 1094 | Tex = Texture2DOperator { Inputs = { Input = Input { SourceOp = "Bg", Source = "Output", }, }, }, |
| 1095 | Blinn = MtlBlinn { Inputs = { ["Diffuse.Color.Material"] = Input { SourceOp = "Tex", Source = "MaterialOutput", }, }, }, |
| 1096 | Hidden = Shape3D { Inputs = { ["SurfacePlaneInputs.Visibility.IsVisible"] = Input { Value = 0, }, }, }, |
| 1097 | Unlit = Shape3D { Inputs = { ["SurfacePlaneInputs.Lighting.IsAffectedByLights"] = Input { Value = 0, }, }, }, |
| 1098 | Flat = Shape3D { Inputs = { MaterialInput = Input { SourceOp = "Tex", Source = "MaterialOutput", }, }, }, |
| 1099 | Shaded = Shape3D { Inputs = { MaterialInput = Input { SourceOp = "Blinn", Source = "MaterialOutput", }, }, }, |
| 1100 | Forced = Override3D { Inputs = { SceneInput = Input { SourceOp = "Unlit", Source = "Output", }, ["Lighting.DoAffectedByLights"] = Input { Value = 1, }, ["Lighting.IsAffectedByLights"] = Input { Value = 1, }, }, }, |
| 1101 | M = Merge3D { Inputs = { SceneInput1 = Input { SourceOp = "Hidden", Source = "Output", }, SceneInput2 = Input { SourceOp = "Unlit", Source = "Output", }, SceneInput3 = Input { SourceOp = "Flat", Source = "Output", }, SceneInput4 = Input { SourceOp = "Shaded", Source = "Output", }, SceneInput5 = Input { SourceOp = "Forced", Source = "Output", }, }, },"#, |
| 1102 | ); |
| 1103 | let (_, meshes, _) = collect(&src, "M"); |
| 1104 | let lit: Vec<bool> = meshes.iter().map(|m| m.affected_by_lights).collect(); |
| 1105 | assert_eq!(lit, [false, false, true, true], "hidden surface dropped; own flag, bare texture, Blinn, override"); |
| 1106 | } |
| 1107 | |
| 1108 | #[test] |
| 1109 | fn specular_models_and_defaults() { |
| 1110 | let src = comp_src( |
| 1111 | r#"Blinn = MtlBlinn { }, Phong = MtlPhong { }, Cook = MtlCookTorrance { }, |
| 1112 | A = Shape3D { Inputs = { MaterialInput = Input { SourceOp = "Blinn", Source = "MaterialOutput", }, }, }, |
| 1113 | B = Shape3D { Inputs = { MaterialInput = Input { SourceOp = "Phong", Source = "MaterialOutput", }, }, }, |
| 1114 | C = Shape3D { Inputs = { MaterialInput = Input { SourceOp = "Cook", Source = "MaterialOutput", }, }, }, |
| 1115 | M = Merge3D { Inputs = { SceneInput1 = Input { SourceOp = "A", Source = "Output", }, SceneInput2 = Input { SourceOp = "B", Source = "Output", }, SceneInput3 = Input { SourceOp = "C", Source = "Output", }, }, },"#, |
| 1116 | ); |
| 1117 | let (_, meshes, _) = collect(&src, "M"); |
| 1118 | let models: Vec<_> = meshes.iter().map(|m| (m.shape.split_fx().1.spec_model, m.specular_exponent)).collect(); |
| 1119 | assert_eq!(models, [(SpecModel::Blinn, 50.0), (SpecModel::Phong, 50.0), (SpecModel::CookTorrance, 0.6)]); |
| 1120 | } |
| 1121 | |
| 1122 | #[test] |
| 1123 | fn bump_only_material_does_not_color_the_surface_and_wireframe_follows_shape_input() { |
| 1124 | let src = comp_src( |
| 1125 | r#"Bg = Background { Inputs = { Width = Input { Value = 8, }, Height = Input { Value = 8, }, }, }, |
| 1126 | Bump = BumpMap { Inputs = { Input = Input { SourceOp = "Bg", Source = "Output", }, HeightScale = Input { Value = 2.0, }, }, }, |
| 1127 | Blinn = MtlBlinn { Inputs = { ["Bumpmap.Material"] = Input { SourceOp = "Bump", Source = "MaterialOutput", }, }, }, |
| 1128 | S = Shape3D { Inputs = { Shape = Input { Value = FuID { "SurfaceSphereInputs" }, }, ["SurfaceSphereInputs.Wireframe"] = Input { Value = 1, }, MaterialInput = Input { SourceOp = "Blinn", Source = "MaterialOutput", }, }, }, |
| 1129 | M = Merge3D { Inputs = { SceneInput1 = Input { SourceOp = "S", Source = "Output", }, }, },"#, |
| 1130 | ); |
| 1131 | let (_, meshes, _) = collect(&src, "M"); |
| 1132 | let fx = meshes[0].shape.split_fx().1; |
| 1133 | assert!(fx.bump_only && fx.wireframe); |
| 1134 | assert_eq!(fx.bump_scale, 2.0); |
| 1135 | } |
| 1136 | |
| 1137 | #[test] |
| 1138 | fn hidden_cube_still_passes_its_scene_input_through() { |
| 1139 | let src = comp_src( |
| 1140 | r#"Inner = Shape3D { }, |
| 1141 | C = Cube3D { Inputs = { ["SurfaceCubeInputs.Visibility.IsVisible"] = Input { Value = 0, }, SceneInput = Input { SourceOp = "Inner", Source = "Output", }, }, }, |
| 1142 | M = Merge3D { Inputs = { SceneInput1 = Input { SourceOp = "C", Source = "Output", }, }, },"#, |
| 1143 | ); |
| 1144 | let (_, meshes, _) = collect(&src, "M"); |
| 1145 | assert_eq!(meshes.len(), 1); |
| 1146 | assert!(matches!(meshes[0].shape.base_shape(), ShapeKind::Plane { .. })); |
| 1020 | 1147 | } |
| 1021 | 1148 | } |