Search in sources :

Example 1 with ShaderNodeDefinitionKey

use of com.jme3.asset.ShaderNodeDefinitionKey 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 ShaderNodeDefinitionKey

use of com.jme3.asset.ShaderNodeDefinitionKey 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 ShaderNodeDefinitionKey

use of com.jme3.asset.ShaderNodeDefinitionKey in project jmonkeyengine by jMonkeyEngine.

the class ShaderNodeLoaderDelegate method readShaderNodeDefinition.

/**
     * effectively reads the ShaderNodesDefinitions block
     *
     * @param statements the list of statements to parse
     * @param key the ShaderNodeDefinitionKey
     * @throws IOException
     */
protected void readShaderNodeDefinition(List<Statement> statements, ShaderNodeDefinitionKey key) throws IOException {
    boolean isLoadDoc = key instanceof ShaderNodeDefinitionKey && ((ShaderNodeDefinitionKey) key).isLoadDocumentation();
    for (Statement statement : statements) {
        String[] split = statement.getLine().split("[ \\{]");
        String line = statement.getLine();
        if (line.startsWith("Type")) {
            String type = line.substring(line.lastIndexOf(':') + 1).trim();
            shaderNodeDefinition.setType(Shader.ShaderType.valueOf(type));
        } else if (line.startsWith("Shader ")) {
            readShaderStatement(statement);
            shaderNodeDefinition.getShadersLanguage().add(shaderLanguage);
            shaderNodeDefinition.getShadersPath().add(shaderName);
        } else if (line.startsWith("Documentation")) {
            if (isLoadDoc) {
                String doc = "";
                for (Statement statement1 : statement.getContents()) {
                    doc += "\n" + statement1.getLine();
                }
                shaderNodeDefinition.setDocumentation(doc);
            }
        } else if (line.startsWith("Input")) {
            varNames = "";
            for (Statement statement1 : statement.getContents()) {
                shaderNodeDefinition.getInputs().add(readVariable(statement1));
            }
        } else if (line.startsWith("Output")) {
            varNames = "";
            for (Statement statement1 : statement.getContents()) {
                if (statement1.getLine().trim().equals("None")) {
                    shaderNodeDefinition.setNoOutput(true);
                } else {
                    shaderNodeDefinition.getOutputs().add(readVariable(statement1));
                }
            }
        } else {
            throw new MatParseException("one of Type, Shader, Documentation, Input, Output", split[0], statement);
        }
    }
}
Also used : ShaderNodeDefinitionKey(com.jme3.asset.ShaderNodeDefinitionKey) Statement(com.jme3.util.blockparser.Statement)

Example 4 with ShaderNodeDefinitionKey

use of com.jme3.asset.ShaderNodeDefinitionKey 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)

Aggregations

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