use of com.jme3.material.MaterialDef in project jmonkeyengine by jMonkeyEngine.
the class Material method read.
public void read(JmeImporter im) throws IOException {
InputCapsule ic = im.getCapsule(this);
name = ic.readString("name", null);
additionalState = (RenderState) ic.readSavable("render_state", null);
transparent = ic.readBoolean("is_transparent", false);
// Load the material def
String defName = ic.readString("material_def", null);
HashMap<String, MatParam> params = (HashMap<String, MatParam>) ic.readStringSavableMap("parameters", null);
boolean enableVcolor = false;
boolean separateTexCoord = false;
boolean applyDefaultValues = false;
boolean guessRenderStateApply = false;
int ver = ic.getSavableVersion(Material.class);
if (ver < 1) {
applyDefaultValues = true;
}
if (ver < 2) {
guessRenderStateApply = true;
}
if (im.getFormatVersion() == 0) {
// Enable compatibility with old models
if (defName.equalsIgnoreCase("Common/MatDefs/Misc/VertexColor.j3md")) {
// Using VertexColor, switch to Unshaded and set VertexColor=true
enableVcolor = true;
defName = "Common/MatDefs/Misc/Unshaded.j3md";
} else if (defName.equalsIgnoreCase("Common/MatDefs/Misc/SimpleTextured.j3md") || defName.equalsIgnoreCase("Common/MatDefs/Misc/SolidColor.j3md")) {
// Using SimpleTextured/SolidColor, just switch to Unshaded
defName = "Common/MatDefs/Misc/Unshaded.j3md";
} else if (defName.equalsIgnoreCase("Common/MatDefs/Misc/WireColor.j3md")) {
// Using WireColor, set wireframe renderstate = true and use Unshaded
getAdditionalRenderState().setWireframe(true);
defName = "Common/MatDefs/Misc/Unshaded.j3md";
} else if (defName.equalsIgnoreCase("Common/MatDefs/Misc/Unshaded.j3md")) {
// Uses unshaded, ensure that the proper param is set
MatParam value = params.get("SeperateTexCoord");
if (value != null && ((Boolean) value.getValue()) == true) {
params.remove("SeperateTexCoord");
separateTexCoord = true;
}
}
assert applyDefaultValues && guessRenderStateApply;
}
def = (MaterialDef) im.getAssetManager().loadAsset(new AssetKey(defName));
paramValues = new ListMap<String, MatParam>();
// load the textures and update nextTexUnit
for (Map.Entry<String, MatParam> entry : params.entrySet()) {
MatParam param = entry.getValue();
if (param instanceof MatParamTexture) {
MatParamTexture texVal = (MatParamTexture) param;
// do not add to param values
if (texVal.getTextureValue() == null || texVal.getTextureValue().getImage() == null) {
continue;
}
}
if (im.getFormatVersion() == 0 && param.getName().startsWith("m_")) {
// Ancient version of jME3 ...
param.setName(param.getName().substring(2));
}
if (def.getMaterialParam(param.getName()) == null) {
logger.log(Level.WARNING, "The material parameter is not defined: {0}. Ignoring..", param.getName());
} else {
checkSetParam(param.getVarType(), param.getName());
paramValues.put(param.getName(), param);
}
}
if (applyDefaultValues) {
// not available
for (MatParam param : def.getMaterialParams()) {
if (param.getValue() != null && paramValues.get(param.getName()) == null) {
setParam(param.getName(), param.getVarType(), param.getValue());
}
}
}
if (guessRenderStateApply && additionalState != null) {
// Try to guess values of "apply" render state based on defaults
// if value != default then set apply to true
additionalState.applyPolyOffset = additionalState.offsetEnabled;
additionalState.applyBlendMode = additionalState.blendMode != BlendMode.Off;
additionalState.applyColorWrite = !additionalState.colorWrite;
additionalState.applyCullMode = additionalState.cullMode != FaceCullMode.Back;
additionalState.applyDepthTest = !additionalState.depthTest;
additionalState.applyDepthWrite = !additionalState.depthWrite;
additionalState.applyStencilTest = additionalState.stencilTest;
additionalState.applyWireFrame = additionalState.wireframe;
}
if (enableVcolor) {
setBoolean("VertexColor", true);
}
if (separateTexCoord) {
setBoolean("SeparateTexCoord", true);
}
}
use of com.jme3.material.MaterialDef in project jmonkeyengine by jMonkeyEngine.
the class J3MLoaderTest method fixedPipelineTechnique_shouldBeIgnored.
@Test
public void fixedPipelineTechnique_shouldBeIgnored() throws IOException {
when(assetInfo.openStream()).thenReturn(J3MLoader.class.getResourceAsStream("/no-shader-specified.j3md"));
MaterialDef def = (MaterialDef) j3MLoader.load(assetInfo);
assertEquals(null, def.getTechniqueDefs("A"));
assertEquals(1, def.getTechniqueDefs("B").size());
}
use of com.jme3.material.MaterialDef in project jmonkeyengine by jMonkeyEngine.
the class J3MLoaderTest method noDefaultTechnique_shouldBeSupported.
@Test
public void noDefaultTechnique_shouldBeSupported() throws IOException {
when(assetInfo.openStream()).thenReturn(J3MLoader.class.getResourceAsStream("/no-default-technique.j3md"));
MaterialDef def = (MaterialDef) j3MLoader.load(assetInfo);
assertEquals(1, def.getTechniqueDefs("Test").size());
}
use of com.jme3.material.MaterialDef in project jmonkeyengine by jMonkeyEngine.
the class RenderManager method renderGeometry.
/**
* Renders the given geometry.
* <p>
* First the proper world matrix is set, if
* the geometry's {@link Geometry#setIgnoreTransform(boolean) ignore transform}
* feature is enabled, the identity world matrix is used, otherwise, the
* geometry's {@link Geometry#getWorldMatrix() world transform matrix} is used.
* <p>
* Once the world matrix is applied, the proper material is chosen for rendering.
* If a {@link #setForcedMaterial(com.jme3.material.Material) forced material} is
* set on this RenderManager, then it is used for rendering the geometry,
* otherwise, the {@link Geometry#getMaterial() geometry's material} is used.
* <p>
* If a {@link #setForcedTechnique(java.lang.String) forced technique} is
* set on this RenderManager, then it is selected automatically
* on the geometry's material and is used for rendering. Otherwise, one
* of the {@link MaterialDef#getDefaultTechniques() default techniques} is
* used.
* <p>
* If a {@link #setForcedRenderState(com.jme3.material.RenderState) forced
* render state} is set on this RenderManager, then it is used
* for rendering the material, and the material's own render state is ignored.
* Otherwise, the material's render state is used as intended.
*
* @param geom The geometry to render
*
* @see Technique
* @see RenderState
* @see Material#selectTechnique(java.lang.String, com.jme3.renderer.RenderManager)
* @see Material#render(com.jme3.scene.Geometry, com.jme3.renderer.RenderManager)
*/
public void renderGeometry(Geometry geom) {
if (geom.isIgnoreTransform()) {
setWorldMatrix(Matrix4f.IDENTITY);
} else {
setWorldMatrix(geom.getWorldMatrix());
}
// Perform light filtering if we have a light filter.
LightList lightList = geom.getWorldLightList();
if (lightFilter != null) {
filteredLightList.clear();
lightFilter.filterLights(geom, filteredLightList);
lightList = filteredLightList;
}
Material material = geom.getMaterial();
//else the geom is not rendered
if (forcedTechnique != null) {
MaterialDef matDef = material.getMaterialDef();
if (matDef.getTechniqueDefs(forcedTechnique) != null) {
Technique activeTechnique = material.getActiveTechnique();
String previousTechniqueName = activeTechnique != null ? activeTechnique.getDef().getName() : TechniqueDef.DEFAULT_TECHNIQUE_NAME;
geom.getMaterial().selectTechnique(forcedTechnique, this);
//saving forcedRenderState for future calls
RenderState tmpRs = forcedRenderState;
if (geom.getMaterial().getActiveTechnique().getDef().getForcedRenderState() != null) {
//forcing forced technique renderState
forcedRenderState = geom.getMaterial().getActiveTechnique().getDef().getForcedRenderState();
}
// use geometry's material
material.render(geom, lightList, this);
material.selectTechnique(previousTechniqueName, this);
//restoring forcedRenderState
forcedRenderState = tmpRs;
//Reverted this part from revision 6197
//If forcedTechnique does not exists, and forcedMaterial is not set, the geom MUST NOT be rendered
} else if (forcedMaterial != null) {
// use forced material
forcedMaterial.render(geom, lightList, this);
}
} else if (forcedMaterial != null) {
// use forced material
forcedMaterial.render(geom, lightList, this);
} else {
material.render(geom, lightList, this);
}
}
use of com.jme3.material.MaterialDef in project jmonkeyengine by jMonkeyEngine.
the class BitmapFontLoader method load.
private BitmapFont load(AssetManager assetManager, String folder, InputStream in) throws IOException {
MaterialDef spriteMat = (MaterialDef) assetManager.loadAsset(new AssetKey("Common/MatDefs/Misc/Unshaded.j3md"));
BitmapCharacterSet charSet = new BitmapCharacterSet();
Material[] matPages = null;
BitmapFont font = new BitmapFont();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String regex = "[\\s=]+";
font.setCharSet(charSet);
String line;
while ((line = reader.readLine()) != null) {
String[] tokens = line.split(regex);
if (tokens[0].equals("info")) {
// Get rendered size
for (int i = 1; i < tokens.length; i++) {
if (tokens[i].equals("size")) {
charSet.setRenderedSize(Integer.parseInt(tokens[i + 1]));
}
}
} else if (tokens[0].equals("common")) {
// Fill out BitmapCharacterSet fields
for (int i = 1; i < tokens.length; i++) {
String token = tokens[i];
if (token.equals("lineHeight")) {
charSet.setLineHeight(Integer.parseInt(tokens[i + 1]));
} else if (token.equals("base")) {
charSet.setBase(Integer.parseInt(tokens[i + 1]));
} else if (token.equals("scaleW")) {
charSet.setWidth(Integer.parseInt(tokens[i + 1]));
} else if (token.equals("scaleH")) {
charSet.setHeight(Integer.parseInt(tokens[i + 1]));
} else if (token.equals("pages")) {
// number of texture pages
matPages = new Material[Integer.parseInt(tokens[i + 1])];
font.setPages(matPages);
}
}
} else if (tokens[0].equals("page")) {
int index = -1;
Texture tex = null;
for (int i = 1; i < tokens.length; i++) {
String token = tokens[i];
if (token.equals("id")) {
index = Integer.parseInt(tokens[i + 1]);
} else if (token.equals("file")) {
String file = tokens[i + 1];
if (file.startsWith("\"")) {
file = file.substring(1, file.length() - 1);
}
TextureKey key = new TextureKey(folder + file, true);
key.setGenerateMips(false);
tex = assetManager.loadTexture(key);
tex.setMagFilter(Texture.MagFilter.Bilinear);
tex.setMinFilter(Texture.MinFilter.BilinearNoMipMaps);
}
}
// set page
if (index >= 0 && tex != null) {
Material mat = new Material(spriteMat);
mat.setTexture("ColorMap", tex);
mat.setBoolean("VertexColor", true);
mat.getAdditionalRenderState().setBlendMode(BlendMode.Alpha);
matPages[index] = mat;
}
} else if (tokens[0].equals("char")) {
// New BitmapCharacter
BitmapCharacter ch = null;
for (int i = 1; i < tokens.length; i++) {
String token = tokens[i];
if (token.equals("id")) {
int index = Integer.parseInt(tokens[i + 1]);
ch = new BitmapCharacter();
charSet.addCharacter(index, ch);
} else if (token.equals("x")) {
ch.setX(Integer.parseInt(tokens[i + 1]));
} else if (token.equals("y")) {
ch.setY(Integer.parseInt(tokens[i + 1]));
} else if (token.equals("width")) {
ch.setWidth(Integer.parseInt(tokens[i + 1]));
} else if (token.equals("height")) {
ch.setHeight(Integer.parseInt(tokens[i + 1]));
} else if (token.equals("xoffset")) {
ch.setXOffset(Integer.parseInt(tokens[i + 1]));
} else if (token.equals("yoffset")) {
ch.setYOffset(Integer.parseInt(tokens[i + 1]));
} else if (token.equals("xadvance")) {
ch.setXAdvance(Integer.parseInt(tokens[i + 1]));
} else if (token.equals("page")) {
ch.setPage(Integer.parseInt(tokens[i + 1]));
}
}
} else if (tokens[0].equals("kerning")) {
// Build kerning list
int index = 0;
int second = 0;
int amount = 0;
for (int i = 1; i < tokens.length; i++) {
if (tokens[i].equals("first")) {
index = Integer.parseInt(tokens[i + 1]);
} else if (tokens[i].equals("second")) {
second = Integer.parseInt(tokens[i + 1]);
} else if (tokens[i].equals("amount")) {
amount = Integer.parseInt(tokens[i + 1]);
}
}
BitmapCharacter ch = charSet.getCharacter(index);
ch.addKerning(second, amount);
}
}
return font;
}
Aggregations