use of com.jme3.material.MaterialDef in project jmonkeyengine by jMonkeyEngine.
the class J3MLoader method loadFromRoot.
private void loadFromRoot(List<Statement> roots) throws IOException {
if (roots.size() == 2) {
Statement exception = roots.get(0);
String line = exception.getLine();
if (line.startsWith("Exception")) {
throw new AssetLoadException(line.substring("Exception ".length()));
} else {
throw new IOException("In multiroot material, expected first statement to be 'Exception'");
}
} else if (roots.size() != 1) {
throw new IOException("Too many roots in J3M/J3MD file");
}
boolean extending = false;
Statement materialStat = roots.get(0);
String materialName = materialStat.getLine();
if (materialName.startsWith("MaterialDef")) {
materialName = materialName.substring("MaterialDef ".length()).trim();
extending = false;
} else if (materialName.startsWith("Material")) {
materialName = materialName.substring("Material ".length()).trim();
extending = true;
} else {
throw new IOException("Specified file is not a Material file");
}
String[] split = materialName.split(":", 2);
if (materialName.equals("")) {
throw new MatParseException("Material name cannot be empty", materialStat);
}
if (split.length == 2) {
if (!extending) {
throw new MatParseException("Must use 'Material' when extending.", materialStat);
}
String extendedMat = split[1].trim();
MaterialDef def = (MaterialDef) assetManager.loadAsset(new AssetKey(extendedMat));
if (def == null) {
throw new MatParseException("Extended material " + extendedMat + " cannot be found.", materialStat);
}
material = new Material(def);
material.setKey(key);
material.setName(split[0].trim());
// material.setAssetName(fileName);
} else if (split.length == 1) {
if (extending) {
throw new MatParseException("Expected ':', got '{'", materialStat);
}
materialDef = new MaterialDef(assetManager, materialName);
// NOTE: pass file name for defs so they can be loaded later
materialDef.setAssetName(key.getName());
} else {
throw new MatParseException("Cannot use colon in material name/path", materialStat);
}
for (Statement statement : materialStat.getContents()) {
split = statement.getLine().split("[ \\{]");
String statType = split[0];
if (extending) {
if (statType.equals("MaterialParameters")) {
readExtendingMaterialParams(statement.getContents());
} else if (statType.equals("AdditionalRenderState")) {
readAdditionalRenderState(statement.getContents());
} else if (statType.equals("Transparent")) {
readTransparentStatement(statement.getLine());
}
} else {
if (statType.equals("Technique")) {
readTechnique(statement);
} else if (statType.equals("MaterialParameters")) {
readMaterialParams(statement.getContents());
} else {
throw new MatParseException("Expected material statement, got '" + statType + "'", statement);
}
}
}
}
use of com.jme3.material.MaterialDef in project jmonkeyengine by jMonkeyEngine.
the class TestMaterialDefWrite method testWriteMat.
@Test
public void testWriteMat() throws Exception {
Material mat = new Material(assetManager, "example.j3md");
final ByteArrayOutputStream stream = new ByteArrayOutputStream();
J3mdExporter exporter = new J3mdExporter();
try {
exporter.save(mat.getMaterialDef(), stream);
} catch (IOException e) {
e.printStackTrace();
}
// System.err.println(stream.toString());
J3MLoader loader = new J3MLoader();
AssetInfo info = new AssetInfo(assetManager, new AssetKey("test")) {
@Override
public InputStream openStream() {
return new ByteArrayInputStream(stream.toByteArray());
}
};
MaterialDef matDef = (MaterialDef) loader.load(info);
MaterialDef ref = mat.getMaterialDef();
for (MatParam refParam : ref.getMaterialParams()) {
MatParam matParam = matDef.getMaterialParam(refParam.getName());
assertTrue(refParam != null);
assertEquals(refParam, matParam);
}
for (String key : ref.getTechniqueDefsNames()) {
List<TechniqueDef> refDefs = ref.getTechniqueDefs(key);
List<TechniqueDef> defs = matDef.getTechniqueDefs(key);
assertNotNull(defs);
assertTrue(refDefs.size() == defs.size());
for (int i = 0; i < refDefs.size(); i++) {
assertEqualTechniqueDefs(refDefs.get(i), defs.get(i));
}
}
}
use of com.jme3.material.MaterialDef in project jmonkeyengine by jMonkeyEngine.
the class ShaderCheck method checkMatDef.
private static void checkMatDef(String matdefName) {
MaterialDef def = (MaterialDef) assetManager.loadAsset(matdefName);
EnumSet<Caps> rendererCaps = EnumSet.noneOf(Caps.class);
rendererCaps.add(Caps.GLSL100);
for (TechniqueDef techDef : def.getTechniqueDefs(TechniqueDef.DEFAULT_TECHNIQUE_NAME)) {
DefineList defines = techDef.createDefineList();
Shader shader = techDef.getShader(assetManager, rendererCaps, defines);
for (Validator validator : validators) {
StringBuilder sb = new StringBuilder();
validator.validate(shader, sb);
System.out.println("==== Validator: " + validator.getName() + " " + validator.getInstalledVersion() + " ====");
System.out.println(sb.toString());
}
}
throw new UnsupportedOperationException();
}
use of com.jme3.material.MaterialDef in project jmonkeyengine by jMonkeyEngine.
the class J3MLoaderTest method multipleSameNamedTechniques_shouldBeSupported.
@Test
public void multipleSameNamedTechniques_shouldBeSupported() throws IOException {
when(assetInfo.openStream()).thenReturn(J3MLoader.class.getResourceAsStream("/same-name-technique.j3md"));
MaterialDef def = (MaterialDef) j3MLoader.load(assetInfo);
assertEquals(2, def.getTechniqueDefs("Test").size());
assertEquals(EnumSet.of(Caps.GLSL150), def.getTechniqueDefs("Test").get(0).getRequiredCaps());
assertEquals(EnumSet.of(Caps.GLSL100), def.getTechniqueDefs("Test").get(1).getRequiredCaps());
}
Aggregations