Search in sources :

Example 21 with Statement

use of com.jme3.util.blockparser.Statement in project jmonkeyengine by jMonkeyEngine.

the class J3MLoader method readRenderState.

private void readRenderState(List<Statement> renderStates) throws IOException {
    renderState = new RenderState();
    for (Statement statement : renderStates) {
        readRenderStateStatement(statement);
    }
    technique.setRenderState(renderState);
    renderState = null;
}
Also used : Statement(com.jme3.util.blockparser.Statement)

Example 22 with Statement

use of com.jme3.util.blockparser.Statement 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 23 with Statement

use of com.jme3.util.blockparser.Statement in project jmonkeyengine by jMonkeyEngine.

the class J3MLoader method readParam.

// <TYPE> <NAME> [ "(" <FFBINDING> ")" ] [-LINEAR] [ ":" <DEFAULTVAL> ]
private void readParam(String statement) throws IOException {
    String name;
    String defaultVal = null;
    ColorSpace colorSpace = null;
    String[] split = statement.split(":");
    // Parse default val
    if (split.length == 1) {
    // Doesn't contain default value
    } else {
        if (split.length != 2) {
            throw new IOException("Parameter statement syntax incorrect");
        }
        statement = split[0].trim();
        defaultVal = split[1].trim();
    }
    if (statement.endsWith("-LINEAR")) {
        colorSpace = ColorSpace.Linear;
        statement = statement.substring(0, statement.length() - "-LINEAR".length());
    }
    // Parse ffbinding
    int startParen = statement.indexOf("(");
    if (startParen != -1) {
        // get content inside parentheses
        int endParen = statement.indexOf(")", startParen);
        String bindingStr = statement.substring(startParen + 1, endParen).trim();
        // don't care about bindingStr
        statement = statement.substring(0, startParen);
    }
    // Parse type + name
    split = statement.split(whitespacePattern);
    if (split.length != 2) {
        throw new IOException("Parameter statement syntax incorrect");
    }
    VarType type;
    if (split[0].equals("Color")) {
        type = VarType.Vector4;
    } else {
        type = VarType.valueOf(split[0]);
    }
    name = split[1];
    Object defaultValObj = null;
    if (defaultVal != null) {
        defaultValObj = readValue(type, defaultVal);
    }
    if (type.isTextureType()) {
        materialDef.addMaterialParamTexture(type, name, colorSpace);
    } else {
        materialDef.addMaterialParam(type, name, defaultValObj);
    }
}
Also used : ColorSpace(com.jme3.texture.image.ColorSpace) IOException(java.io.IOException)

Example 24 with Statement

use of com.jme3.util.blockparser.Statement in project jmonkeyengine by jMonkeyEngine.

the class J3MLoader method readAdditionalRenderState.

private void readAdditionalRenderState(List<Statement> renderStates) throws IOException {
    renderState = material.getAdditionalRenderState();
    for (Statement statement : renderStates) {
        readRenderStateStatement(statement);
    }
    renderState = null;
}
Also used : Statement(com.jme3.util.blockparser.Statement)

Example 25 with Statement

use of com.jme3.util.blockparser.Statement in project jmonkeyengine by jMonkeyEngine.

the class ShaderNodeLoaderDelegate method updateRightFromUniforms.

/**
     * updates the right variable of the given mapping from a MatParam (a
     * WorldParam) it checks if the uniform hasn't already been loaded, add it
     * to the maps if not.
     *
     * @param param the MatParam
     * @param mapping the mapping
     * @param map the map of uniforms to search into
     * @return true if the param was added to the map
     */
public boolean updateRightFromUniforms(MatParam param, VariableMapping mapping, Map<String, DeclaredVariable> map, Statement statement) throws MatParseException {
    ShaderNodeVariable right = mapping.getRightVariable();
    DeclaredVariable dv = map.get(param.getPrefixedName());
    if (dv == null) {
        right.setType(param.getVarType().getGlslType());
        right.setName(param.getPrefixedName());
        if (mapping.getLeftVariable().getMultiplicity() != null) {
            if (!param.getVarType().name().endsWith("Array")) {
                throw new MatParseException(param.getName() + " is not of Array type", statement);
            }
            String multiplicity = mapping.getLeftVariable().getMultiplicity();
            try {
                Integer.parseInt(multiplicity);
            } catch (NumberFormatException nfe) {
                //multiplicity is not an int attempting to find for a material parameter.
                MatParam mp = findMatParam(multiplicity);
                if (mp != null) {
                    addDefine(multiplicity, VarType.Int);
                    multiplicity = multiplicity.toUpperCase();
                } else {
                    throw new MatParseException("Wrong multiplicity for variable" + mapping.getLeftVariable().getName() + ". " + multiplicity + " should be an int or a declared material parameter.", statement);
                }
            }
            right.setMultiplicity(multiplicity);
        }
        dv = new DeclaredVariable(right);
        map.put(right.getName(), dv);
        dv.addNode(shaderNode);
        mapping.setRightVariable(right);
        return true;
    }
    dv.addNode(shaderNode);
    mapping.setRightVariable(dv.var);
    return false;
}
Also used : MatParam(com.jme3.material.MatParam) ShaderNodeVariable(com.jme3.shader.ShaderNodeVariable)

Aggregations

Statement (com.jme3.util.blockparser.Statement)16 IOException (java.io.IOException)10 AssetNotFoundException (com.jme3.asset.AssetNotFoundException)4 ShaderNodeVariable (com.jme3.shader.ShaderNodeVariable)4 VariableMapping (com.jme3.shader.VariableMapping)4 ShaderNodeDefinitionKey (com.jme3.asset.ShaderNodeDefinitionKey)3 Material (com.jme3.material.Material)3 MaterialList (com.jme3.material.MaterialList)3 ShaderNodeDefinition (com.jme3.shader.ShaderNodeDefinition)3 MatParam (com.jme3.material.MatParam)2 ShaderNode (com.jme3.shader.ShaderNode)2 Image (com.jme3.texture.Image)2 Texture2D (com.jme3.texture.Texture2D)2 InputStream (java.io.InputStream)2 AssetKey (com.jme3.asset.AssetKey)1 AssetLoadException (com.jme3.asset.AssetLoadException)1 TextureKey (com.jme3.asset.TextureKey)1 ShaderGenerationInfo (com.jme3.material.ShaderGenerationInfo)1 LightMode (com.jme3.material.TechniqueDef.LightMode)1 ShadowMode (com.jme3.material.TechniqueDef.ShadowMode)1