Search in sources :

Example 6 with MaterialDef

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);
            }
        }
    }
}
Also used : Statement(com.jme3.util.blockparser.Statement) IOException(java.io.IOException)

Example 7 with MaterialDef

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));
        }
    }
}
Also used : J3MLoader(com.jme3.material.plugins.J3MLoader) J3mdExporter(com.jme3.material.plugin.export.materialdef.J3mdExporter)

Example 8 with MaterialDef

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();
}
Also used : DefineList(com.jme3.shader.DefineList) MaterialDef(com.jme3.material.MaterialDef) Shader(com.jme3.shader.Shader) Caps(com.jme3.renderer.Caps) TechniqueDef(com.jme3.material.TechniqueDef)

Example 9 with MaterialDef

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());
}
Also used : MaterialDef(com.jme3.material.MaterialDef) Test(org.junit.Test)

Aggregations

MaterialDef (com.jme3.material.MaterialDef)6 Test (org.junit.Test)3 Material (com.jme3.material.Material)2 AssetKey (com.jme3.asset.AssetKey)1 BitmapCharacter (com.jme3.font.BitmapCharacter)1 BitmapCharacterSet (com.jme3.font.BitmapCharacterSet)1 BitmapFont (com.jme3.font.BitmapFont)1 LightList (com.jme3.light.LightList)1 RenderState (com.jme3.material.RenderState)1 Technique (com.jme3.material.Technique)1 TechniqueDef (com.jme3.material.TechniqueDef)1 J3mdExporter (com.jme3.material.plugin.export.materialdef.J3mdExporter)1 J3MLoader (com.jme3.material.plugins.J3MLoader)1 Caps (com.jme3.renderer.Caps)1 DefineList (com.jme3.shader.DefineList)1 Shader (com.jme3.shader.Shader)1 Texture (com.jme3.texture.Texture)1 ListMap (com.jme3.util.ListMap)1 Statement (com.jme3.util.blockparser.Statement)1 BufferedReader (java.io.BufferedReader)1