【问题标题】:How to animate textures in a 3d model?如何为 3d 模型中的纹理设置动画?
【发布时间】:2014-10-22 22:59:20
【问题描述】:

我希望在我的 LibGDX 代码中拥有一个动画 3d 纹理,但我正在努力寻找如何做到这一点。

我认为这个“应该”是怎么做的;

a) 直接访问和修改模型上的纹理。 (通过像素图?ByteBuffer?)

b) 预渲染包含所有帧(例如 20 帧)的大图像,然后移动 UV 坐标以创建动画的错觉。 (类似于 2d/webdesign 中的 ImageStrips)。

我确实想出了如何每次都完全替换材料,但这似乎是一种更糟糕的方法。因此,如果有人可以显示我需要执行 a) 或 b) (或类似的最佳方法)的命令,我会非常失败。 数学我很好。 OpenGLES 或 GDX 的复杂性我不是:)

(该解决方案至少应该适用于 HTML/Android 编译,理想情况下是所有内容)

【问题讨论】:

    标签: libgdx textures bytebuffer


    【解决方案1】:

    自从最新版本以来,在 3d 表面上播放 2d 动画非常容易。首先确保熟悉 2d 动画概念,如下所述:https://github.com/libgdx/libgdx/wiki/2D-Animation。然后,您可以使用TextureRegionAnimation#getKeyFrame 返回)来设置曲面的材质,而不是使用 spritebatch,如下所示:https://github.com/libgdx/libgdx/blob/master/tests/gdx-tests/src/com/badlogic/gdx/tests/g3d/TextureRegion3DTest.java。所以基本上你会得到你的渲染方法:

    attribute.set(animation.getKeyFrame(stateTime, true));
    

    或者,如果您想要更通用的方法:

    instance.getMaterial("<name of material>").get(TextureAttribute.class, TextureAttribute.Diffuse).set(animation.getKeyFrame(stateTime, true));
    

    或者,如果 ModelInstance 中只有一种材质:

    instance.materials.get(0).get(TextureAttribute.class, TextureAttribute.Diffuse).set(animation.getKeyFrame(stateTime, true));
    

    【讨论】:

    • 最新的意思是 1.4.1?还是一些夜间的东西?该方法看起来确实很简单,但我似乎在 TextureAttribute 上没有“Set”方法。
    • TextureAttribute 属性 = instance3.materials.get(0).get(TextureAttribute.class, TextureAttribute.Diffuse); “属性”将没有“设置”方法。
    • 最新版本是1.4.1,其中存在TextureAttribute#set(TextureRegion)。见:github.com/libgdx/libgdx/blob/1.4.1/gdx/src/com/badlogic/gdx/…
    • 啊,那我没更新对吧。正如我认为的那样。我的 .gradle 文件显示“gdxVersion = '1.4.1'”,但可能还有其他问题。 Libgdx 的新手
    • 没有刷新 Gradle >_
    【解决方案2】:

    如果你有足够的内存,我肯定会选择 b),它在处理器上更容易。此外,您只会更改制服的价值。但是,由于预处理,打开应用程序可能需要一些时间。

    获取统一变量,在其中编译着色器,animationPos 应该是全局的。

    Gluint animationPos = glGetUniformLocation(shaderProgram, "nameoftheuniform");
    

    您的主循环应该将 animationPos 值传递给着色器:

    Gluniform1i ( animationPos, curentAnimationIndex);
    

    添加您的片段着色器变量:

    uniform int animationPos;
    

    片段着色器主:

    float texCoordY = texCoord.y; //texture coordinates should be passed from vertex shader
    float texCoordX = texCoord.x/20.0f; //we are dividing it with 20 since it is the amount of textures that we have and if we use it directly it would try to use all the texture. Whereas the texture stores at 20 different textures.
    
    float textureIndex = 1.0f*animationPos/20.0f; //Pointer to the start of the animation texture.
    gl_fragColor = texture2D ( yourTexture, vec2( textureIndex + texCoordX, texCoordY));
    

    上面的代码假设你在x方向扩展了你的纹理,你也可以尝试像矩阵一样扩展它,那么你需要改变texCoord计算部分。此外,我们使用了 20 种纹理。

    选项 a) 对处理器的负担更大,并且您每次都将更改纹理,因此它会更多地使用 pci,但更容易占用内存。这个问题更像是一个设计决策,但我想可以处理 20 张图片,所以请选择选项 b)。

    编辑:添加代码。

    【讨论】:

      猜你喜欢
      • 2013-08-25
      • 2017-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-22
      • 1970-01-01
      • 2010-10-28
      相关资源
      最近更新 更多