vpenades / SharpGLTF

glTF reader and writer for .NET Standard
MIT License
454 stars 72 forks source link

Animation #173

Open Omega1975 opened 1 year ago

Omega1975 commented 1 year ago

The program I'm writing merges several glb files, now I would like to be able to read the animation data from the original files and write them into the new file. Below the simplified code

ModelRoot model = SharpGLTF.Schema2.ModelRoot.Load(glb.FileName);

foreach (Node nd in model.LogicalNodes){ if (nd.Mesh != null){

    //duplicate mesh -> NewMesh and merged rootNode.WithLocalTransform(afm).WithMesh(NewMesh);
   .....
   //

   foreach (Animation animation in model.LogicalAnimations){
       foreach (AnimationChannel ch in animation.Channels){
          if (ch.TargetNode == nd){
              IAnimationSampler<Vector3> AS=ch.GetTranslationSampler();
              ICurveSampler<Vector3> CS = AS.CreateCurveSampler(true);

              rootNode.WithTranslationAnimation(name, CS);
          }
       }
   }
}                                           

}

the line rootNode.WithTranslationAnimation(name, CS); returns an error at runtime: Message: "Must implement IConvertibleCurve\r\nNome parametro: sampler" ParamName: "sampler" Source: "SharpGLTF.Toolkit" StackTrace: " in SharpGLTF.Schema2.Toolkit.WithTranslationAnimation(Node node, String animationName, ICurveSampler1 sampler) in D:\\Sorgenti\\SharpGLTF\\src\\SharpGLTF.Toolkit\\Schema2\\AnimationExtensions.cs:riga 56" TargetSite: {SharpGLTF.Schema2.Node WithTranslationAnimation(SharpGLTF.Schema2.Node, System.String, SharpGLTF.Animations.ICurveSampler1[System.Numerics.Vector3])}

Where am i going wrong? Which instructions should I use?

Thanks in advance for the time you will dedicate to me

vpenades commented 1 year ago

Without having a reproducible example, my guess is you're not using the right object for that property.

I suspect you need to use CurveBuilder

Generally speaking, ICurverSampler is an interface used to "sample values along the curve" but does not state how the curve was built in the first place.

So you need to use an object that implements both ICurveSampler and IConvertibleCurve so it can be written back to a file.

Also, there's a number of incompatible curves: you cannot mix STEP, LINEAR, or SPLINE curves,