2017-07-02 45 views
0

有没有什么办法可以在运行时禁用纹理并启用着色模型? 然后再次启用纹理?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

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

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; 
    } 
}