LoadAnimation creates an Animation for the specified animation index from the GLTF Animations array.
(animIdx int)
| 326 | // LoadAnimation creates an Animation for the specified |
| 327 | // animation index from the GLTF Animations array. |
| 328 | func (g *GLTF) LoadAnimation(animIdx int) (*animation.Animation, error) { |
| 329 | |
| 330 | // Check if provided animation index is valid |
| 331 | if animIdx < 0 || animIdx >= len(g.Animations) { |
| 332 | return nil, fmt.Errorf("invalid animation index") |
| 333 | } |
| 334 | log.Debug("Loading Animation %d", animIdx) |
| 335 | animData := g.Animations[animIdx] |
| 336 | |
| 337 | anim := animation.NewAnimation() |
| 338 | anim.SetName(animData.Name) |
| 339 | for i := 0; i < len(animData.Channels); i++ { |
| 340 | |
| 341 | chData := animData.Channels[i] |
| 342 | target := chData.Target |
| 343 | sampler := animData.Samplers[chData.Sampler] |
| 344 | node, err := g.LoadNode(target.Node) |
| 345 | if err != nil { |
| 346 | return nil, err |
| 347 | } |
| 348 | |
| 349 | var validTypes []string |
| 350 | var validComponentTypes []int |
| 351 | |
| 352 | var ch animation.IChannel |
| 353 | if target.Path == "translation" { |
| 354 | validTypes = []string{VEC3} |
| 355 | validComponentTypes = []int{FLOAT} |
| 356 | ch = animation.NewPositionChannel(node) |
| 357 | } else if target.Path == "rotation" { |
| 358 | validTypes = []string{VEC4} |
| 359 | validComponentTypes = []int{FLOAT, BYTE, UNSIGNED_BYTE, SHORT, UNSIGNED_SHORT} |
| 360 | ch = animation.NewRotationChannel(node) |
| 361 | } else if target.Path == "scale" { |
| 362 | validTypes = []string{VEC3} |
| 363 | validComponentTypes = []int{FLOAT} |
| 364 | ch = animation.NewScaleChannel(node) |
| 365 | } else if target.Path == "weights" { |
| 366 | validTypes = []string{SCALAR} |
| 367 | validComponentTypes = []int{FLOAT, BYTE, UNSIGNED_BYTE, SHORT, UNSIGNED_SHORT} |
| 368 | children := node.GetNode().Children() |
| 369 | if len(children) > 1 { |
| 370 | return nil, fmt.Errorf("animating meshes with more than a single primitive is not supported") |
| 371 | } |
| 372 | morphGeom := children[0].(graphic.IGraphic).IGeometry().(*geometry.MorphGeometry) |
| 373 | ch = animation.NewMorphChannel(morphGeom) |
| 374 | } |
| 375 | |
| 376 | // TODO what if Input and Output accessors are interleaved? probably de-interleave in these 2 cases |
| 377 | |
| 378 | keyframes, err := g.loadAccessorF32(sampler.Input, "Input", []string{SCALAR}, []int{FLOAT}) |
| 379 | if err != nil { |
| 380 | return nil, err |
| 381 | } |
| 382 | values, err := g.loadAccessorF32(sampler.Output, "Output", validTypes, validComponentTypes) |
| 383 | if err != nil { |
| 384 | return nil, err |
| 385 | } |
no test coverage detected