代码之家  ›  专栏  ›  技术社区  ›  Dmitriy Puchkov Robert

LibGDX:向现有三维模型添加新动画

  •  2
  • Dmitriy Puchkov Robert  · 技术社区  · 7 年前

    我使用libgdx渲染三维模型。 首先,我从资产中加载模型:

    assetManager.load(name, Model.class);
    assetManager.finishLoading();
    Model model = assets.get(name, Model.class)
    

    ModelInstance modelInstance = new ModelInstance(model);
    AnimationController controller = new AnimationController(modelInstance);   
    controller.setAnimation(modelInstance.animations.first().id, -1);
    

    渲染方法

    controller.update(Gdx.graphics.getDeltaTime());
    modelBatch.begin(cam);
    modelBatch.render(model, environment);
    modelBatch.end();
    

    它工作得很好,动画播放。

    当我尝试加载并运行下一个动画时,问题就开始了。

    assetManager.load(animation, Model.class);
    assetManager.finishLoading();
    Model animModel = assets.get(animation, Model.class)
    
    modelInstance.model.animations.add(animModel.animations.first());
    System.out.println("animation size "+modelInstance.model.animations.size); // prints 2
    
    controller.setAnimation(modelInstance.model.animations.get(1).id, -1);
    

    然后发生错误

    com.badlogic.gdx.utils.GdxRuntimeException: Unknown animation: acrobat
    W/System.err: atcom.badlogic.gdx.graphics.g3d.utils.AnimationController.obtain(AnimationController.java:158)atcom.badlogic.gdx.graphics.g3d.utils.AnimationController.animate(AnimationController.java:349)
    at com.badlogic.gdx.graphics.g3d.utils.AnimationController.animate(AnimationController.java:331)
    at com.badlogic.gdx.graphics.g3d.utils.AnimationController.animate(AnimationController.java:303)
    at com.snaappy.ar.game.MyGameRenderer$5.run(MyGameRenderer.java:768)
    at com.badlogic.gdx.backends.android.AndroidGraphics.onDrawFrame(AndroidGraphics.java:488)
    at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1562)
    at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1262) 
    

    看起来该动画尚未添加到ModelInstance。

    接下来,我尝试将动画添加到 模型实例 直接地

    modelInstance.animations.add(animModel.animations.first());
    

    而不是

    modelInstance.model.animations.add(animModel.animations.first());
    

    错误不再出现,但动画未播放。
    模型有两个动画,但只能播放最初的第一个空闲动画。

    这个问题可以通过重新创建来解决 模型实例 动画控制器 . 每次加载动画后,我都必须重新创建这些对象。

    modelInstance.model.animations.add(animModel.animations.first());
    ModelInstance modelInstance = new ModelInstance(modelInstance.model);
    AnimationControllercontroller = new AnimationController(modelInstance);   
    

    我认为这不好。 它看起来像LibGDX中的一个bug。

    我想听听专家对这个问题的意见。

    1 回复  |  直到 7 年前
        1
  •  0
  •   Dmitriy Puchkov Robert    7 年前

    我找到了一个比每次添加动画后创建新的ModelInstance更好的解决方案。
    看看我对LibGDX的请求 https://github.com/libgdx/libgdx/pull/4972

    复制动画 将公开。

    然后,在从资源加载新动画后,我们可以直接将其添加到我们的ModelInstance。

    assetManager.load(animation, Model.class);
    assetManager.finishLoading();
    Model animModel = assets.get(animation, Model.class)
    
    modelInstance.copyAnimations(animModel.animations);