com.jme3.material.plugins.ShaderNodeLoaderDelegate Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jme3-core Show documentation
Show all versions of jme3-core Show documentation
jMonkeyEngine is a 3D game engine for adventurous Java developers
The newest version!
/*
* Copyright (c) 2009-2012 jMonkeyEngine
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* * Neither the name of 'jMonkeyEngine' nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.jme3.material.plugins;
import com.jme3.asset.AssetManager;
import com.jme3.asset.AssetNotFoundException;
import com.jme3.asset.ShaderNodeDefinitionKey;
import com.jme3.material.MatParam;
import com.jme3.material.MaterialDef;
import com.jme3.material.ShaderGenerationInfo;
import com.jme3.material.TechniqueDef;
import com.jme3.shader.Shader;
import com.jme3.shader.ShaderNode;
import com.jme3.shader.ShaderNodeDefinition;
import com.jme3.shader.ShaderNodeVariable;
import com.jme3.shader.ShaderUtils;
import com.jme3.shader.UniformBinding;
import com.jme3.shader.VarType;
import com.jme3.shader.VariableMapping;
import com.jme3.util.blockparser.Statement;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* This class is here to be able to load shaderNodeDefinition from both the
* J3MLoader and ShaderNodeDefinitionLoader.
*
* Also it allows to load the ShaderNodes from a j3md file and build the
* ShaderNodes list of each technique and the ShaderGenerationInfo needed to
* generate the sahders
*
* @author Nehon
*/
public class ShaderNodeLoaderDelegate {
protected Map nodeDefinitions;
protected Map nodes;
protected ShaderNodeDefinition shaderNodeDefinition;
protected ShaderNode shaderNode;
protected TechniqueDef techniqueDef;
protected Map attributes = new HashMap();
protected Map vertexDeclaredUniforms = new HashMap();
protected Map fragmentDeclaredUniforms = new HashMap();
protected Map varyings = new HashMap();
protected MaterialDef materialDef;
protected String shaderLanguage;
protected String shaderName;
protected String varNames = "";
protected AssetManager assetManager;
protected ConditionParser conditionParser = new ConditionParser();
protected List nulledConditions = new ArrayList();
protected class DeclaredVariable {
ShaderNodeVariable var;
List nodes = new ArrayList();
public DeclaredVariable(ShaderNodeVariable var) {
this.var = var;
}
public void makeCondition() {
var.setCondition(null);
for (ShaderNode node : nodes) {
String condition = null;
for (VariableMapping mapping : node.getInputMapping()) {
if (mapping.getRightVariable().equals(var)) {
if (mapping.getCondition() == null) {
condition = null;
break;
}
if (condition == null) {
condition = "(" + mapping.getCondition() + ")";
} else {
if (!condition.contains(mapping.getCondition())) {
condition = condition + " || (" + mapping.getCondition() + ")";
}
}
}
}
if (node.getCondition() == null && condition == null) {
var.setCondition(null);
return;
}
if (node.getCondition() != null) {
if (condition == null) {
condition = node.getCondition();
} else {
if (!condition.contains(node.getCondition())) {
condition = "(" + node.getCondition() + ") && (" + condition + ")";
}
}
}
if (var.getCondition() == null) {
var.setCondition(condition);
} else {
if (!var.getCondition().contains(condition)) {
var.setCondition("(" + var.getCondition() + ") || (" + condition + ")");
}
}
}
}
public final void addNode(ShaderNode c) {
if (!nodes.contains(c)) {
nodes.add(c);
}
}
}
protected void computeConditions() {
updateConditions(vertexDeclaredUniforms);
updateConditions(fragmentDeclaredUniforms);
updateConditions(varyings);
for (DeclaredVariable v : varyings.values()) {
for (ShaderNode sn : techniqueDef.getShaderNodes()) {
if (sn.getDefinition().getType() == Shader.ShaderType.Vertex) {
for (VariableMapping mapping : sn.getInputMapping()) {
if (mapping.getLeftVariable().equals(v.var)) {
if (mapping.getCondition() == null || v.var.getCondition() == null) {
mapping.setCondition(v.var.getCondition());
} else {
mapping.setCondition("(" + mapping.getCondition() + ") || (" + v.var.getCondition() + ")");
}
}
}
}
}
}
updateConditions(attributes);
// updateConditions(fragmentGlobals);
// vertexGlobal.makeCondition();
}
/**
* 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 readNodesDefinitions(List 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(getNodeDefinitions().values());
}
/**
* Read the ShaderNodesDefinitions block and internally stores a map of
* ShaderNodesDefinition This method is used by the j3m loader.
*
* When loaded in a material, the definitions are not stored as a list, but
* they are stores in Shadernodes based onthis definition.
*
* The map is here to map the defintion to the nodes, and ovoid reloading
* already loaded definitions
*
* @param statements the list of statements to parse
* @throws IOException
*/
public void readNodesDefinitions(List statements) throws IOException {
readNodesDefinitions(statements, new ShaderNodeDefinitionKey());
}
/**
* effectiveliy reads the ShaderNodesDefinitions block
*
* @param statements the list of statements to parse
* @param key the ShaderNodeDefinitionKey
* @throws IOException
*/
protected void readShaderNodeDefinition(List 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);
}
}
}
/**
* reads a variable declaration statement <glslType> <varName>
*
* @param statement the statement to parse
* @return a ShaderNodeVariable axtracted from the statement
* @throws IOException
*/
protected ShaderNodeVariable readVariable(Statement statement) throws IOException {
String line = statement.getLine().trim().replaceAll("\\s*\\[", "[");
String[] splitVar = line.split("\\s");
String varName = splitVar[1];
String varType = splitVar[0];
String multiplicity = null;
if (varName.contains("[")) {
//we have an array
String[] arr = splitVar[1].split("\\[");
varName = arr[0].trim();
multiplicity = arr[1].replaceAll("\\]", "").trim();
}
if (varNames.contains(varName + ";")) {
throw new MatParseException("Duplicate variable name " + varName, statement);
}
varNames += varName + ";";
return new ShaderNodeVariable(varType, "", varName, multiplicity);
}
/**
* reads the VertexShaderNodes{} block
*
* @param statements the list of statements to parse
* @throws IOException
*/
public void readVertexShaderNodes(List statements) throws IOException {
attributes.clear();
readNodes(statements);
}
/**
* reads a list of ShaderNode{} blocks
*
* @param statements the list of statements to parse
* @throws IOException
*/
protected void readShaderNode(List 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);
}
}
}
/**
* 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.
*
*
* Format : .[.] =
* .[.][:Condition]
*
*
* @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;
}
/**
* reads the FragmentShaderNodes{} block
*
* @param statements the list of statements to parse
* @throws IOException
*/
public void readFragmentShaderNodes(List statements) throws IOException {
readNodes(statements);
}
/**
* Reads a Shader statement of this form :
© 2015 - 2024 Weber Informatics LLC | Privacy Policy