Ok, for last project I was doing with JME3, I found out that its really useful for me to extend Material with some other functions, to make its use easier. This is because I mostly like to play with dynamic materials and change material properties in code. Also I like to abstract all the stuff that can change in the future of the engine, so I have only one place to change this (was useful for example when you omitted m_ prefix before shader variables). So here are my helper classes, which I used, donāt know if they should be included in the engine or not, its just my proposition to make use of the materials easier. These classes arenāt complete in any way!! Lots of other functions could be added, but probably they should be added to base Material class or to subclasses of it, like LightingMaterial and UnshadedMaterial, which are imho most commonly used materials.
The code:
[java]
package net.marbley.data.material;
import com.jme3.asset.AssetManager;
import com.jme3.material.Material;
import com.jme3.material.RenderState.BlendMode;
import com.jme3.material.RenderState.FaceCullMode;
import com.jme3.texture.Texture;
import com.jme3.texture.Texture.WrapMode;
public abstract class AbstractMaterialExt extends Material
{
private static AssetManager ASSET_MANAGER;
public AbstractMaterialExt(String materialPath)
{
super(ASSET_MANAGER, materialPath);
}
/**
- This function must be called before using any subclasses of this abstract
- class!
*
-
@param assetManager
*/
public static void setAssetManager(AssetManager assetManager)
{
ASSET_MANAGER = assetManager;
}
public void enableAlphaBlendMode()
{
getAdditionalRenderState().setBlendMode(BlendMode.Alpha);
}
public void disableBackFaceCulling()
{
getAdditionalRenderState().setFaceCullMode(FaceCullMode.Off);
}
protected Texture loadTexture(String texName)
{
Texture tex = ASSET_MANAGER.loadTexture(texName);
tex.setWrap(WrapMode.Repeat);
return tex;
}
}
[/java]
[java]
package net.marbley.data.material;
import com.jme3.math.ColorRGBA;
public final class LightingMaterial extends AbstractMaterialExt
{
private static final String matPath = "Common/MatDefs/Light/Lighting.j3md";
public LightingMaterial()
{
super(matPath);
setBoolean("UseMaterialColors", true);
setBoolean("UseAlpha", true);
}
public void setLowQuality(boolean lQuality)
{
setBoolean("LowQuality", lQuality);
}
public void setHighQuality(boolean hQuality)
{
setBoolean("HighQuality", hQuality);
}
public void setAmbientColor(ColorRGBA color)
{
setColor("Ambient", color);
}
public void setDiffuseColor(ColorRGBA color)
{
setColor("Diffuse", color);
}
public void setSpecularColor(ColorRGBA color)
{
setColor("Specular", color);
}
public void setShininess(float shininess)
{
setFloat("Shininess", shininess);
}
public void setGlowColor(ColorRGBA color)
{
setColor("GlowColor", color);
}
public void setDiffuseMap(String texName)
{
setTexture("DiffuseMap", loadTexture(texName));
}
public void setNormalMap(String texName)
{
setTexture("NormalMap", loadTexture(texName));
}
public void setSpecularMap(String texName)
{
setTexture("SpecularMap", loadTexture(texName));
}
public void setParallaxMap(String texName)
{
setTexture("ParallaxMap", loadTexture(texName));
}
public void setGlowMap(String texName)
{
setTexture("GlowMap", loadTexture(texName));
}
}
[/java]
[java]
package net.marbley.data.material;
import com.jme3.math.ColorRGBA;
import com.jme3.texture.Texture;
public final class UnshadedMaterial extends AbstractMaterialExt
{
private static final String matPath = "Common/MatDefs/Misc/Unshaded.j3md";
public UnshadedMaterial()
{
super(matPath);
}
public void setColor(ColorRGBA color)
{
setColor("Color", color);
}
public void setColorMap(String texName)
{
setTexture("ColorMap", loadTexture(texName));
}
public void setColorMap(Texture texture)
{
setTexture("ColorMap", texture);
}
public void setLightMap(String texName)
{
setTexture("LightMap", loadTexture(texName));
}
public void setGlowMap(String texName)
{
setTexture("GlowMap", loadTexture(texName));
}
public void setGlowColor(ColorRGBA color)
{
setColor("GlowColor", color);
}
}
[/java]
Again, these classes are in no way complete, but I would really appreciate your opinion on this matter, and if such classes would be considered for including in the engine, I would be more than happy to enrich them and make them completeā¦