Search in sources :

Example 1 with MaterialDef

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);
    }
}
Also used : AssetKey(com.jme3.asset.AssetKey) ListMap(com.jme3.util.ListMap)

Example 2 with MaterialDef

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

Example 3 with MaterialDef

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

Example 4 with MaterialDef

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);
    }
}
Also used : RenderState(com.jme3.material.RenderState) LightList(com.jme3.light.LightList) Material(com.jme3.material.Material) Technique(com.jme3.material.Technique) MaterialDef(com.jme3.material.MaterialDef)

Example 5 with MaterialDef

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;
}
Also used : InputStreamReader(java.io.InputStreamReader) BitmapCharacterSet(com.jme3.font.BitmapCharacterSet) Material(com.jme3.material.Material) MaterialDef(com.jme3.material.MaterialDef) Texture(com.jme3.texture.Texture) BufferedReader(java.io.BufferedReader) BitmapCharacter(com.jme3.font.BitmapCharacter) BitmapFont(com.jme3.font.BitmapFont)

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