Libgdx禁用纹理并添加着色

时间:2017-07-02 14:36:32

标签: android libgdx

有没有办法在运行时禁用纹理并启用颜色化模型? 然后再次启用纹理?

现在我正在使用它:

@Override
public void enableTexture(boolean enable, Vector3 colorize) {
    enableTexture(enable, colorize.x, colorize.y, colorize.z);
}

@Override
public void enableTexture(boolean enable, float r, float g, float b) {
    if (enable) objModel.getModelInstance().materials.get(0).set(TextureAttribute.createDiffuse(objModel.getTexture()));
    else {
        objModel.getModelInstance().materials.get(0).set(ColorAttribute.createDiffuse(r, g, b, 1));
    }
}

但这对性能不是很好,因为它总是在运行时创建一个新对象。我需要这个用于光轴

最终工作代码:

@Override
public void enableTexture(boolean enable, float r, float g, float b) {
    if (enable) {
        if (diffuse == null) diffuse = TextureAttribute.createDiffuse(objModel.getTexture());
        objModel.getModelInstance().materials.get(0).clear();
        objModel.getModelInstance().materials.get(0).set(diffuse);
    }
    else {
        if (color == null) color = ColorAttribute.createDiffuse(r, g, b, 1);
        objModel.getModelInstance().materials.get(0).clear();
        objModel.getModelInstance().materials.get(0).set(color);
    }
}

1 个答案:

答案 0 :(得分:1)

使用Attributes类获取特定的Attribute。 在游戏运行之前加载时创建属性。

private Attributes attributes

public void create() {

    this.attributes = new Attributes();

    this.attributes.set(
            TextureAttribute.createDiffuse( this.region ),
            ColorAttribute.createDiffuse( Color.WHITE )
    );
}

当游戏运行时,您现在可以使用get方法。

public Attribute getAttribute( boolean enable, float r, float g, float b ){

    if ( enable ) {
        TextureAttribute attribute = (TextureAttribute) attributes.get( TextureAttribute.Diffuse );
        attribute.set( this.region );

        return attribute;
    } else {
        ColorAttribute attribute = (ColorAttribute) attributes.get( ColorAttribute.Diffuse );
        attribute.color.set( r, g, b, 1 );

        return attribute;
    }
}