2

In my libgdx project I'm trying to add additional materials to model. Actually, I need to obtain texture combination - texture of the following material must be placed on the texture of the previous one. In some cases with transparency.

Something like that

    Model knight = assets.get("data/g3d/knight.g3db", Model.class);
    knightInstance = new ModelInstance(knight);

    Material additionalMaterial = new Material();
    knightInstance.materials.add(additionalMaterial);

    knightInstance.materials.get(0).clear();
    knightInstance.materials.get(0).set(TextureAttribute.createDiffuse(assets.get("data/g3d/checkboard.png", Texture.class)));
    BlendingAttribute blendingAttribute1 = new BlendingAttribute(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA, 0.4f);
    knightInstance.materials.get(0).set(blendingAttribute1);

    knightInstance.materials.get(1).clear();
    knightInstance.materials.get(1).set(TextureAttribute.createDiffuse(assets.get("data/g3d/tiles.png", Texture.class)));
    BlendingAttribute blendingAttribute2 = new BlendingAttribute(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA, 0.6f);
    knightInstance.materials.get(1).set(blendingAttribute2);

Offcorse, It doesn't work. Only first material is visible. Is that possible to do in LibGDX?

4

1 回答 1

2

每个 NodePart 只能有一种材质(这是正在渲染的实际网格/材质组合)。

如果您只想渲染模型两次,但第二次使用不同的纹理。您还可以像上面那样创建两个 ModelInstance 并在每个上设置纹理。

如果不想创建两个 ModelInstance,也可以在模型(或模型实例)中复制 NodePart。假设您的模型有一个节点和一个部分,可以这样完成:

Model knight = assets.get(...);
NodePart part1 = knight.nodes.get(0).parts.get(0);
NodePart part2 = new NodePart(part1.meshPart, new Material());
knight.nodes.get(0).parts.add(part2);
part1.material.set(TextureAttribute.createDiffuse(texture1));
part2.material.set(TextureAttribute.createDiffuse(texture2));

请注意,这将从第二个 NodePart 中删除任何蒙皮/骨骼信息。如果您的模型是动画/蒙皮的,您还需要复制骨骼数组并绑定姿势。

根据深度测试,此方法(渲染 NodePart 两次)可能不会像您预期的那样工作。如果它有效,我不会推荐它。相反,您应该使用着色器来组合这两个纹理。

每个材质属性基本上代表着色器的一个制服。如果要组合两个纹理,则必须创建一个实现实际渲染的着色器。接下来,您可以将两个 TextureAttributes(不同类型)添加到您的材质中以供您的着色器使用。看看:http ://blog.xoppa.com/using-materials-with-libgdx/详细解释了这个,包括一个例子。

于 2013-08-06T19:13:26.017 回答