Class ShaderNodeLoaderDelegate

java.lang.Object
com.jme3.material.plugins.ShaderNodeLoaderDelegate

public class ShaderNodeLoaderDelegate extends Object
This class is here to be able to load shaderNodeDefinition from both the J3MLoader and ShaderNodeDefinitionLoader. It also allows loading shader nodes from a j3md file and building the ShaderNodes list of each technique and the ShaderGenerationInfo needed to generate shaders.
  • Field Details

  • Constructor Details

    • ShaderNodeLoaderDelegate

      public ShaderNodeLoaderDelegate()
  • Method Details

    • readNodesDefinitions

      public List<ShaderNodeDefinition> readNodesDefinitions(List<Statement> statements, ShaderNodeDefinitionKey key) throws IOException
      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.
      Parameters:
      statements - the list statements to parse
      key - the ShaderNodeDefinitionKey
      Returns:
      a list of ShaderNodesDefinition
      Throws:
      IOException - if an I/O error occurs
    • readNodesDefinitions

      public void readNodesDefinitions(List<Statement> statements) throws IOException
      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 stored in shader nodes based on this definition. The map is here to map the definition to the nodes, and ovoid reloading already loaded definitions
      Parameters:
      statements - the list of statements to parse
      Throws:
      IOException - if an I/O error occurs
    • readShaderNodeDefinition

      protected void readShaderNodeDefinition(List<Statement> statements, ShaderNodeDefinitionKey key) throws IOException
      effectively reads the ShaderNodesDefinitions block
      Parameters:
      statements - the list of statements to parse
      key - the ShaderNodeDefinitionKey
      Throws:
      IOException - if an I/O error occurs
    • readVariable

      protected ShaderNodeVariable readVariable(Statement statement) throws IOException
      reads a variable declaration statement <glslType> <varName>
      Parameters:
      statement - the statement to parse
      Returns:
      a ShaderNodeVariable extracted from the statement
      Throws:
      IOException - if an I/O error occurs
    • readVertexShaderNodes

      public void readVertexShaderNodes(List<Statement> statements) throws IOException
      reads the VertexShaderNodes{} block
      Parameters:
      statements - the list of statements to parse
      Throws:
      IOException - if an I/O error occurs
    • readShaderNode

      protected void readShaderNode(List<Statement> statements) throws IOException
      reads a list of ShaderNode{} blocks
      Parameters:
      statements - the list of statements to parse
      Throws:
      IOException - if an I/O error occurs
    • parseMapping

      protected VariableMapping parseMapping(Statement statement, boolean[] hasNameSpace) throws MatParseException
      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. Also, we can have the right part as expression.
       Format variable to variable: <nameSpace>.<varName>[.<swizzling>] = <nameSpace>.<varName>[.<swizzling>][:Condition]
       Format expression to variable: <nameSpace>.<varName>[.<swizzling>] = %% expression %% [:Condition]
       
      Parameters:
      statement - the statement to read.
      hasNameSpace - indicate which vars have namespaces
      Returns:
      the read mapping.
      Throws:
      MatParseException - if the statement isn't valid.
    • readFragmentShaderNodes

      public void readFragmentShaderNodes(List<Statement> statements) throws IOException
      reads the FragmentShaderNodes{} block
      Parameters:
      statements - the list of statements to parse
      Throws:
      IOException - if an I/O error occurs
    • readShaderStatement

      protected void readShaderStatement(Statement statement) throws IOException
      Reads a Shader statement of the form TYPE LANG : SOURCE
      Parameters:
      statement - the shader statement (not null)
      Throws:
      IOException - if an I/O error occurs
    • setTechniqueDef

      public void setTechniqueDef(TechniqueDef techniqueDef)
      Sets the technique definition currently being loaded
      Parameters:
      techniqueDef - the technique def
    • setMaterialDef

      public void setMaterialDef(MaterialDef materialDef)
      sets the material def currently being loaded
      Parameters:
      materialDef - (alias created)
    • updateVariableFromList

      protected boolean updateVariableFromList(ShaderNodeVariable var, List<ShaderNodeVariable> list)
      Searches a variable in the given list and updates its type and namespace.
      Parameters:
      var - the variable to update.
      list - the variables list.
      Returns:
      true if the variable has been found and updated.
    • updateRightTypeFromLeftType

      protected void updateRightTypeFromLeftType(VariableMapping mapping)
      Updates the type of the right variable of a mapping from the type of the left variable.
      Parameters:
      mapping - the mapping to consider.
    • checkMappingFormat

      protected void checkMappingFormat(String[] vars, Statement statement) throws MatParseException
      Checks if once a mapping expression is split by "=" the resulting array have 2 elements.
      Parameters:
      vars - the array.
      statement - the statement.
      Throws:
      MatParseException - if the array isn't correct.
    • findMatParam

      protected MatParam findMatParam(String varName)
      Finds a MatParam in the MaterialDef from the given name.
      Parameters:
      varName - the material param name.
      Returns:
      the found MatParam or null.
    • findWorldParam

      protected UniformBinding findWorldParam(String varName)
      finds an UniformBinding representing a WorldParam from the techniqueDef
      Parameters:
      varName - the name of the WorldParam
      Returns:
      the corresponding UniformBinding to the WorldParam
    • updateRightFromUniforms

      protected boolean updateRightFromUniforms(UniformBinding param, VariableMapping mapping, Map<String,ShaderNodeLoaderDelegate.DeclaredVariable> map)
      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.
      Parameters:
      param - the WorldParam UniformBinding
      mapping - the mapping
      map - the map of uniforms to search into
      Returns:
      true if the param was added to the map
    • updateRightFromUniforms

      public boolean updateRightFromUniforms(MatParam param, VariableMapping mapping, Map<String,ShaderNodeLoaderDelegate.DeclaredVariable> map, Statement statement) throws MatParseException
      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.
      Parameters:
      param - the mat param.
      mapping - the mapping.
      map - the map of uniforms to search into.
      statement - the statement being read
      Returns:
      true if the param was added to the map.
      Throws:
      MatParseException - in case of a syntax error
    • updateVarFromAttributes

      public void updateVarFromAttributes(ShaderNodeVariable right, VariableMapping mapping)
      Updates a variable from the attribute list.
      Parameters:
      right - the variable
      mapping - the mapping
    • addDefine

      public void addDefine(String paramName, VarType paramType)
      Adds a define to the technique def
      Parameters:
      paramName - the name of the material parameter
      paramType - the type of the material parameter
    • findNodeOutput

      public ShaderNodeVariable findNodeOutput(List<ShaderNodeVariable> vars, String rightVarName)
      Finds a variable with the given name from the list of variable.
      Parameters:
      vars - the list of shader node variables.
      rightVarName - the variable name to search for.
      Returns:
      the found variable or null is not found.
    • extractCondition

      public void extractCondition(String condition, Statement statement) throws MatParseException
      Extracts and checks a condition expression.
      Parameters:
      condition - the condition expression.
      statement - the statement being read.
      Throws:
      MatParseException - if the condition isn't valid.
    • readInputMapping

      public VariableMapping readInputMapping(Statement statement) throws MatParseException
      Reads an input mapping.
      Parameters:
      statement - the statement being read.
      Returns:
      the variable mapping.
      Throws:
      MatParseException - if we have a problem with parsing input mapping statement.
    • readOutputMapping

      public VariableMapping readOutputMapping(Statement statement) throws MatParseException
      Reads an output mapping.
      Parameters:
      statement - the statement being read.
      Returns:
      the mapping
      Throws:
      MatParseException - if we have a problem with parsing the statement.
    • readNodes

      public void readNodes(List<Statement> statements) throws IOException
      Reads a list of ShaderNodes
      Parameters:
      statements - the list of statements to read
      Throws:
      IOException - if an I/O error occurs
    • fixSamplerType

      public String fixSamplerType(String leftType, String rightType)
      retrieve the leftType corresponding sampler type from the rightType
      Parameters:
      leftType - the left samplerType
      rightType - the right sampler type (can be multiple types separated by "|"
      Returns:
      the type or null if not found
    • storeGlobal

      public void storeGlobal(ShaderNodeVariable var, Statement varStatement) throws MatParseException
      Stores a global output.
      Parameters:
      var - the variable to store.
      varStatement - the statement being read.
      Throws:
      MatParseException - if we have duplicates of a global vertex output variable.
    • storeAttribute

      public void storeAttribute(ShaderNodeVariable var)
      Stores an attribute.
      Parameters:
      var - the variable to store.
    • storeVertexUniform

      public void storeVertexUniform(ShaderNodeVariable var)
      Stores a vertex uniform.
      Parameters:
      var - the variable to store.
    • storeFragmentUniform

      public void storeFragmentUniform(ShaderNodeVariable var)
      store a fragment uniform
      Parameters:
      var - the variable to store
    • setAssetManager

      public void setAssetManager(AssetManager assetManager)
      sets the assetManager
      Parameters:
      assetManager - for loading assets (alias created)
    • findDefinition

      public ShaderNodeDefinition findDefinition(Statement statement) throws IOException
      Find the definition from this statement (loads it if necessary)
      Parameters:
      statement - the statement being read
      Returns:
      the definition
      Throws:
      IOException - if an I/O error occurs
    • storeVaryings

      public void storeVaryings(ShaderNode node, ShaderNodeVariable variable)
      store a varying
      Parameters:
      node - the shaderNode
      variable - the variable to store
    • mergeConditions

      public String mergeConditions(String condition1, String condition2, String operator)
      Merges 2 conditions with the given operator
      Parameters:
      condition1 - the first condition
      condition2 - the second condition
      operator - the operator ("&&" or "||&)
      Returns:
      the merged condition
    • storeVariable

      public void storeVariable(ShaderNodeVariable variable, List<ShaderNodeVariable> varList)
      Searches a variable in a list from its name and merges the conditions of the variables.
      Parameters:
      variable - the variable.
      varList - the variable list.
    • checkTypes

      protected void checkTypes(VariableMapping mapping, Statement statement1) throws MatParseException
      check the types of a mapping, left type must match right type take the swizzle into account
      Parameters:
      mapping - the mapping
      statement1 - the statement being read
      Throws:
      MatParseException - in case of a syntax error
    • clear

      public void clear()