Search in sources :

Example 1 with ShaderNodeDefinition

use of com.jme3.shader.ShaderNodeDefinition in project jmonkeyengine by jMonkeyEngine.

the class ShaderNodeDefinitionLoader method load.

@Override
public Object load(AssetInfo assetInfo) throws IOException {
    AssetKey k = assetInfo.getKey();
    if (!(k instanceof ShaderNodeDefinitionKey)) {
        throw new IOException("ShaderNodeDefinition file must be loaded via ShaderNodeDefinitionKey");
    }
    ShaderNodeDefinitionKey key = (ShaderNodeDefinitionKey) k;
    loaderDelegate = new ShaderNodeLoaderDelegate();
    InputStream in = assetInfo.openStream();
    List<Statement> roots = BlockLanguageParser.parse(in);
    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 MatParseException("In multiroot shader node definition, expected first statement to be 'Exception'", exception);
        }
    } else if (roots.size() != 1) {
        throw new MatParseException("Too many roots in J3SN file", roots.get(0));
    }
    return loaderDelegate.readNodesDefinitions(roots.get(0).getContents(), key);
}
Also used : AssetKey(com.jme3.asset.AssetKey) ShaderNodeDefinitionKey(com.jme3.asset.ShaderNodeDefinitionKey) InputStream(java.io.InputStream) Statement(com.jme3.util.blockparser.Statement) IOException(java.io.IOException) AssetLoadException(com.jme3.asset.AssetLoadException)

Example 2 with ShaderNodeDefinition

use of com.jme3.shader.ShaderNodeDefinition in project jmonkeyengine by jMonkeyEngine.

the class ShaderNodeLoaderDelegate method readNodesDefinitions.

/**
     * Read the ShaderNodesDefinitions block and returns a list of
     * ShaderNodesDefinition This method is used by the j3sn loader
     *
     * note that the order of the definitions in the list is not guaranteed.
     *
     * @param statements the list statements to parse
     * @param key the ShaderNodeDefinitionKey
     * @return a list of ShaderNodesDefinition
     * @throws IOException
     */
public List<ShaderNodeDefinition> readNodesDefinitions(List<Statement> statements, ShaderNodeDefinitionKey key) throws IOException {
    for (Statement statement : statements) {
        String[] split = statement.getLine().split("[ \\{]");
        if (statement.getLine().startsWith("ShaderNodeDefinition")) {
            String name = statement.getLine().substring("ShaderNodeDefinition".length()).trim();
            if (!getNodeDefinitions().containsKey(name)) {
                shaderNodeDefinition = new ShaderNodeDefinition();
                getNodeDefinitions().put(name, shaderNodeDefinition);
                shaderNodeDefinition.setName(name);
                readShaderNodeDefinition(statement.getContents(), key);
            }
        } else {
            throw new MatParseException("ShaderNodeDefinition", split[0], statement);
        }
    }
    return new ArrayList<ShaderNodeDefinition>(getNodeDefinitions().values());
}
Also used : Statement(com.jme3.util.blockparser.Statement) ArrayList(java.util.ArrayList) ShaderNodeDefinition(com.jme3.shader.ShaderNodeDefinition)

Example 3 with ShaderNodeDefinition

use of com.jme3.shader.ShaderNodeDefinition in project jmonkeyengine by jMonkeyEngine.

the class ShaderNode method read.

/**
     * jme serialization 
     *
     * @param im the importer
     * @throws IOException
     */
@Override
public void read(JmeImporter im) throws IOException {
    InputCapsule ic = (InputCapsule) im.getCapsule(this);
    name = ic.readString("name", "");
    definition = (ShaderNodeDefinition) ic.readSavable("definition", null);
    condition = ic.readString("condition", null);
    inputMapping = (List<VariableMapping>) ic.readSavableArrayList("inputMapping", new ArrayList<VariableMapping>());
    outputMapping = (List<VariableMapping>) ic.readSavableArrayList("outputMapping", new ArrayList<VariableMapping>());
}
Also used : InputCapsule(com.jme3.export.InputCapsule)

Example 4 with ShaderNodeDefinition

use of com.jme3.shader.ShaderNodeDefinition in project jmonkeyengine by jMonkeyEngine.

the class ShaderNodeLoaderDelegate method findDefinition.

/**
     * find the definition from this statement (loads it if necessary)
     *
     * @param statement the statement being read
     * @return the definition
     * @throws IOException
     */
public ShaderNodeDefinition findDefinition(Statement statement) throws IOException {
    String[] defLine = statement.getLine().split(":");
    String defName = defLine[1].trim();
    ShaderNodeDefinition def = getNodeDefinitions().get(defName);
    if (def == null) {
        if (defLine.length == 3) {
            List<ShaderNodeDefinition> defs = null;
            try {
                defs = assetManager.loadAsset(new ShaderNodeDefinitionKey(defLine[2].trim()));
            } catch (AssetNotFoundException e) {
                throw new MatParseException("Couldn't find " + defLine[2].trim(), statement, e);
            }
            for (ShaderNodeDefinition definition : defs) {
                definition.setPath(defLine[2].trim());
                if (defName.equals(definition.getName())) {
                    def = definition;
                }
                if (!(getNodeDefinitions().containsKey(definition.getName()))) {
                    getNodeDefinitions().put(definition.getName(), definition);
                }
            }
        }
        if (def == null) {
            throw new MatParseException(defName + " is not a declared as Shader Node Definition", statement);
        }
    }
    return def;
}
Also used : ShaderNodeDefinitionKey(com.jme3.asset.ShaderNodeDefinitionKey) ShaderNodeDefinition(com.jme3.shader.ShaderNodeDefinition) AssetNotFoundException(com.jme3.asset.AssetNotFoundException)

Example 5 with ShaderNodeDefinition

use of com.jme3.shader.ShaderNodeDefinition in project jmonkeyengine by jMonkeyEngine.

the class ShaderNodeLoaderDelegate method readShaderNode.

/**
     * reads a list of ShaderNode{} blocks
     *
     * @param statements the list of statements to parse
     * @throws IOException
     */
protected void readShaderNode(List<Statement> statements) throws IOException {
    for (Statement statement : statements) {
        String line = statement.getLine();
        String[] split = statement.getLine().split("[ \\{]");
        if (line.startsWith("Definition")) {
            ShaderNodeDefinition def = findDefinition(statement);
            shaderNode.setDefinition(def);
            if (def.isNoOutput()) {
                techniqueDef.getShaderGenerationInfo().getUnusedNodes().remove(shaderNode.getName());
            }
        } else if (line.startsWith("Condition")) {
            String condition = line.substring(line.lastIndexOf(":") + 1).trim();
            extractCondition(condition, statement);
            shaderNode.setCondition(conditionParser.getFormattedExpression());
        } else if (line.startsWith("InputMapping")) {
            for (Statement statement1 : statement.getContents()) {
                VariableMapping mapping = readInputMapping(statement1);
                techniqueDef.getShaderGenerationInfo().getUnusedNodes().remove(mapping.getRightVariable().getNameSpace());
                shaderNode.getInputMapping().add(mapping);
            }
        } else if (line.startsWith("OutputMapping")) {
            for (Statement statement1 : statement.getContents()) {
                VariableMapping mapping = readOutputMapping(statement1);
                techniqueDef.getShaderGenerationInfo().getUnusedNodes().remove(shaderNode.getName());
                shaderNode.getOutputMapping().add(mapping);
            }
        } else {
            throw new MatParseException("ShaderNodeDefinition", split[0], statement);
        }
    }
}
Also used : Statement(com.jme3.util.blockparser.Statement) VariableMapping(com.jme3.shader.VariableMapping) ShaderNodeDefinition(com.jme3.shader.ShaderNodeDefinition)

Aggregations

ShaderNodeDefinition (com.jme3.shader.ShaderNodeDefinition)3 Statement (com.jme3.util.blockparser.Statement)3 ShaderNodeDefinitionKey (com.jme3.asset.ShaderNodeDefinitionKey)2 AssetKey (com.jme3.asset.AssetKey)1 AssetLoadException (com.jme3.asset.AssetLoadException)1 AssetNotFoundException (com.jme3.asset.AssetNotFoundException)1 InputCapsule (com.jme3.export.InputCapsule)1 VariableMapping (com.jme3.shader.VariableMapping)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 ArrayList (java.util.ArrayList)1