2017-07-02 86 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; 
    } 
}