Search in sources :

Example 1 with VariableMapping

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

the class Glsl100ShaderGenerator method updateDefinesName.

/**
     * transforms defines name is the shader node code.
     * One can use a #if defined(inputVariableName) in a shaderNode code.
     * This method is responsible for changing the variable name with the 
     * appropriate defined based on the mapping condition of this variable.
     * Complex condition syntax are handled.     
     * 
     * @param nodeSource the sahderNode source code
     * @param shaderNode the ShaderNode being processed
     * @return the modified shaderNode source.
     */
protected String updateDefinesName(String nodeSource, ShaderNode shaderNode) {
    String[] lines = nodeSource.split("\\n");
    ConditionParser parser = new ConditionParser();
    for (String line : lines) {
        if (line.trim().startsWith("#if")) {
            List<String> params = parser.extractDefines(line.trim());
            //parser.getFormattedExpression();
            String l = line.trim().replaceAll("defined", "").replaceAll("#if ", "").replaceAll("#ifdef", "");
            boolean match = false;
            for (String param : params) {
                for (VariableMapping map : shaderNode.getInputMapping()) {
                    if ((map.getLeftVariable().getName()).equals(param)) {
                        if (map.getCondition() != null) {
                            l = l.replaceAll(param, map.getCondition());
                            match = true;
                        }
                    }
                }
            }
            if (match) {
                nodeSource = nodeSource.replace(line.trim(), "#if " + l);
            }
        }
    }
    return nodeSource;
}
Also used : ConditionParser(com.jme3.material.plugins.ConditionParser)

Example 2 with VariableMapping

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

the class ShaderNodeLoaderDelegate method updateRightFromUniforms.

/**
     * updates the right variable of the given mapping from a UniformBinding (a
     * WorldParam) it checks if the uniform hasn't already been loaded, add it
     * to the maps if not.
     *
     * @param param the WorldParam UniformBinding
     * @param mapping the mapping
     * @param map the map of uniforms to search into
     * @return true if the param was added to the map
     */
protected boolean updateRightFromUniforms(UniformBinding param, VariableMapping mapping, Map<String, DeclaredVariable> map) {
    ShaderNodeVariable right = mapping.getRightVariable();
    String name = "g_" + param.toString();
    DeclaredVariable dv = map.get(name);
    if (dv == null) {
        right.setType(param.getGlslType());
        right.setName(name);
        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 : ShaderNodeVariable(com.jme3.shader.ShaderNodeVariable)

Example 3 with VariableMapping

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

the class ShaderNodeLoaderDelegate method parseMapping.

/**
     * reads a mapping statement. Sets the nameSpace, name and swizzling of the
     * left variable. Sets the name, nameSpace and swizzling of the right
     * variable types will be determined later.
     *
     * <code>
     * Format : <nameSpace>.<varName>[.<swizzling>] =
     * <nameSpace>.<varName>[.<swizzling>][:Condition]
     * </code>
     * 
     * @param statement the statement to read
     * @return the read mapping
     */
protected VariableMapping parseMapping(Statement statement, boolean[] hasNameSpace) throws IOException {
    VariableMapping mapping = new VariableMapping();
    String[] cond = statement.getLine().split(":");
    String[] vars = cond[0].split("=");
    checkMappingFormat(vars, statement);
    ShaderNodeVariable[] variables = new ShaderNodeVariable[2];
    String[] swizzle = new String[2];
    for (int i = 0; i < vars.length; i++) {
        String[] expression = vars[i].trim().split("\\.");
        if (hasNameSpace[i]) {
            if (expression.length <= 3) {
                variables[i] = new ShaderNodeVariable("", expression[0].trim(), expression[1].trim());
            }
            if (expression.length == 3) {
                swizzle[i] = expression[2].trim();
            }
        } else {
            if (expression.length <= 2) {
                variables[i] = new ShaderNodeVariable("", expression[0].trim());
            }
            if (expression.length == 2) {
                swizzle[i] = expression[1].trim();
            }
        }
    }
    mapping.setLeftVariable(variables[0]);
    mapping.setLeftSwizzling(swizzle[0] != null ? swizzle[0] : "");
    mapping.setRightVariable(variables[1]);
    mapping.setRightSwizzling(swizzle[1] != null ? swizzle[1] : "");
    if (cond.length > 1) {
        extractCondition(cond[1], statement);
        mapping.setCondition(conditionParser.getFormattedExpression());
    }
    return mapping;
}
Also used : VariableMapping(com.jme3.shader.VariableMapping) ShaderNodeVariable(com.jme3.shader.ShaderNodeVariable)

Example 4 with VariableMapping

use of com.jme3.shader.VariableMapping 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 5 with VariableMapping

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

the class ShaderNode method write.

/**
     * jme serialization
     *
     * @param ex the exporter
     * @throws IOException
     */
@Override
public void write(JmeExporter ex) throws IOException {
    OutputCapsule oc = (OutputCapsule) ex.getCapsule(this);
    oc.write(name, "name", "");
    oc.write(definition, "definition", null);
    oc.write(condition, "condition", null);
    oc.writeSavableArrayList((ArrayList) inputMapping, "inputMapping", new ArrayList<VariableMapping>());
    oc.writeSavableArrayList((ArrayList) outputMapping, "outputMapping", new ArrayList<VariableMapping>());
}
Also used : OutputCapsule(com.jme3.export.OutputCapsule)

Aggregations

VariableMapping (com.jme3.shader.VariableMapping)6 ShaderNodeVariable (com.jme3.shader.ShaderNodeVariable)5 AssetNotFoundException (com.jme3.asset.AssetNotFoundException)2 MatParam (com.jme3.material.MatParam)2 ShaderNode (com.jme3.shader.ShaderNode)2 IOException (java.io.IOException)2 InputCapsule (com.jme3.export.InputCapsule)1 OutputCapsule (com.jme3.export.OutputCapsule)1 ConditionParser (com.jme3.material.plugins.ConditionParser)1 ShaderNodeDefinition (com.jme3.shader.ShaderNodeDefinition)1 UniformBinding (com.jme3.shader.UniformBinding)1 Statement (com.jme3.util.blockparser.Statement)1