makepad/libs/model/tests/authoring_rig_export.rs
Admin d2130e6550 libs: model, mesh_edit, csg, scene, sim, render, xr
Squash of 25 work commits (Sep 1–12):
  237da90  render: the sprite lane hands the screen draw back the way it found it
  895b9a7  particles: an emitter can ride a body's own frame
  914471f  ai-hub: a feed session whose last socket left ends on its idle timeout; skin: parent, skinned centroid and a nodes-only rig for retargets
  3fcccf3  sim: the whole world is implicitly editable, and one seam says where the ground is
  5692fab  sim: the landform world proves itself — walker through the tunnel included
  f4af6df  sim: terrain knows who changed it — a plan layer over player history
  adbb078  render: a water volume can be physics without a picture
  c17480f  render: a non-rigid body may carry its own orientation
  acad401  sim: an agent with no route holds and retries instead of walking into the wall
  22b2bf9  sim + chat: the composed world surface takes a map floor; the chat gets plan tools
  faf8112  web path: the tessellator's lap timer, the trace span and the fusion cycle timer have no clock on the web
  d3472eb  tsdf: the clock-taking XR helpers are native-only — the browser has no depth camera and no Instant
  4946c9e  sim + render: a repaint is not a world edit — colour and glow restyles never rebake the lightmap
  4317f58  sim + render + chat: walk decks as a surface, the filmed body is no obstruction, the brief never asks
  9791279  render: support rigged models and custom materials across viewers
  3ce2792  sim: use deck geometry for collision and sensing
  c8b79ff  Add portable PBR, rig and soft-body authoring support
  108d423  Add transactional polygon modeling and editable asset documents
  38f4d3b  Refine editable modeling and firm yarn character behavior
  08a2637  sim: add entity-owned lights and vehicle headlights
  00d96a7  render: add clustered lights, local shadows and incremental GI
  cf916af  render: import glTF asset extensions and wire clustered GI
  c6d2cca  model: cut transaction memory and raise capacity limits
  41af47a  raytrace: add a CPU probe-ray BVH budget example
  c963d0a  libs: the game sim splits into makepad-scene and makepad-soft-body; render, model, fab and the asset importer retarget

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-09-15 13:40:31 +02:00

35 lines
5.2 KiB
Rust

use makepad_model::*;
use makepad_gltf::{ParsedGlb,JsonValue};
use std::collections::BTreeMap;
fn apply(d:&mut Document,id:&str,ops:Vec<Operation>){d.apply(Transaction{request_id:id.into(),expected:d.head(),operations:ops},None).unwrap();}
fn values(g:&ParsedGlb,index:usize,lanes:usize)->Vec<f32>{let a=&g.document.accessors_slice()[index];let v=&g.document.buffer_views_slice()[a.buffer_view.unwrap()];let at=v.byte_offset.unwrap_or(0)+a.byte_offset.unwrap_or(0);g.bin_chunk.as_ref().unwrap()[at..at+a.count*lanes*4].chunks_exact(4).map(|b|f32::from_le_bytes(b.try_into().unwrap())).collect()}
fn arr(v:&JsonValue)->&[JsonValue]{let JsonValue::Array(a)=v else{panic!("array")};a}
fn idx(v:&JsonValue)->usize{match v{JsonValue::U64(i)=>*i as usize,JsonValue::I64(i)=>*i as usize,_=>panic!("index")}}
fn field<'a>(v:&'a JsonValue,key:&str)->&'a JsonValue{let JsonValue::Object(v)=v else{panic!("object")}; &v[key]}
#[test]
fn rotated_scaled_rest_inverse_bind_and_joint_lamp_are_portable(){
let mut d=Document::new(Limits::default()).unwrap();let rest=Transform{translation:[3.,2.,1.],rotation:transform::quat_axis_angle([0.,0.,1.],0.7).unwrap(),scale:[2.,1.,0.5]};
apply(&mut d,"rig",vec![Operation::Cube{object:"body".into(),size:[1.;3]},Operation::SetSkeleton{skeleton:Skeleton{joints:vec![Joint{name:"root".into(),parent:None,translation:[0.;3]}]}},Operation::AutoWeights{object:"body".into()},Operation::Rig(RigOperation::Rest{joint:0,transform:rest}),Operation::Scene(SceneOperation::Light(LightEmitter{name:"lamp".into(),attachment:Attachment::Joint(0),transform:Transform::default(),kind:LightKind::Point,color:[1.;3],intensity:10.,range:5.}))]);
let glb=makepad_gltf::parse_glb_bytes(&d.compile(None).unwrap().glb).unwrap();let skin=&glb.document.skins.as_ref().unwrap()[0];let joint=idx(&arr(field(skin,"joints"))[0]);let node=&glb.document.nodes_slice()[joint];
assert_eq!(node.translation,Some([3.,2.,1.]));assert_eq!(node.scale,Some([2.,1.,0.5]));
let ibm=values(&glb,idx(field(skin,"inverseBindMatrices")),16);let inverse=transform::inverse(rest.matrix().unwrap()).unwrap();for r in 0..4{for c in 0..4{assert!((ibm[c*4+r]as f64-inverse[r][c]).abs()<1e-6);}}
let lamp=glb.document.nodes_slice().iter().position(|n|n.name.as_deref()==Some("lamp")).unwrap();assert!(node.children.as_ref().unwrap().contains(&lamp));
}
#[test]
fn static_shape_key_streams_and_defaults_follow_object_partitions(){
let mut d=Document::new(Limits::default()).unwrap();apply(&mut d,"mesh",vec![Operation::Cube{object:"body".into(),size:[2.;3]},Operation::Scene(SceneOperation::Instance{object:"copy".into(),source:"body".into(),transform:Transform{translation:[4.,0.,0.],..Default::default()}})]);
let deltas=d.object("body").unwrap().vertices().iter().map(|v|(v.id,[0.,0.25,0.])).collect();
apply(&mut d,"shape",vec![Operation::Rig(RigOperation::Morph{name:"raise".into(),object:"body".into(),deltas,weight:0.4})]);
let bytes=d.to_bytes(None).unwrap();let round=Document::from_bytes(&bytes,Limits::default(),None).unwrap();assert_eq!(d.head(),round.head());
let glb=makepad_gltf::parse_glb_bytes(&round.compile(None).unwrap().glb).unwrap();
for n in glb.document.nodes_slice().iter().filter(|n|n.mesh.is_some()){
let m=&glb.document.meshes_slice()[n.mesh.unwrap()];assert_eq!(m.weights,Some(vec![0.4]));for p in &m.primitives{let delta=values(&glb,p.targets.as_ref().unwrap()[0]["POSITION"],3);assert!(delta.chunks_exact(3).all(|p|p==[0.,0.25,0.]));let normal=values(&glb,p.targets.as_ref().unwrap()[0]["NORMAL"],3);assert!(normal.iter().all(|v|v.abs()<1e-6));}
}
}
#[test]
fn shape_weight_keys_step_and_events_export_as_real_animation(){
let mut d=Document::new(Limits::default()).unwrap();apply(&mut d,"mesh",vec![Operation::Cube{object:"body".into(),size:[1.;3]},Operation::SetSkeleton{skeleton:Skeleton{joints:vec![Joint{name:"root".into(),parent:None,translation:[0.;3]}]}},Operation::AutoWeights{object:"body".into()},Operation::SetClip{clip:AnimationClip{name:"act".into(),channels:vec![AnimationChannel{joint:0,path:AnimationPath::Translation,keys:vec![Keyframe{time:0.,value:[0.;4]},Keyframe{time:1.,value:[1.,0.,0.,0.]}]}]}}]);
let deltas=d.object("body").unwrap().vertices().iter().map(|v|(v.id,[0.,0.2,0.])).collect();
apply(&mut d,"shape",vec![Operation::Rig(RigOperation::Morph{name:"raise".into(),object:"body".into(),deltas,weight:0.}),Operation::Rig(RigOperation::ClipOptions{name:"act".into(),options:ClipOptions{interpolation:Interpolation::Step,root_motion:true,events:vec![ClipEvent{time:0.5,name:"footstep".into(),payload:"left".into()}],morph_keys:BTreeMap::from([("raise".into(),vec![MorphKey{time:0.,weight:0.},MorphKey{time:1.,weight:1.}])])}})]);
let glb=makepad_gltf::parse_glb_bytes(&d.compile(None).unwrap().glb).unwrap();let a=&glb.document.animations.as_ref().unwrap()[0];let channel=arr(field(a,"channels")).iter().find(|c|matches!(field(field(c,"target"),"path"),JsonValue::String(p)if p=="weights")).unwrap();let sampler=&arr(field(a,"samplers"))[idx(field(channel,"sampler"))];assert!(matches!(field(sampler,"interpolation"),JsonValue::String(s)if s=="STEP"));assert_eq!(values(&glb,idx(field(sampler,"output")),1),vec![0.,1.]);assert!(matches!(field(field(field(a,"extras"),"MAKEPAD_animation"),"root_motion"),JsonValue::Bool(true)));
}