Class ShaderGenerator

java.lang.Object
com.jme3.shader.ShaderGenerator
Direct Known Subclasses:
Glsl100ShaderGenerator

public abstract class ShaderGenerator extends Object
This class is the base for a shader generator using the ShaderNodes system, it contains basis mechanism of generation, but no actual generation code. This class is abstract, any Shader generator must extend it.
  • Field Details

  • Constructor Details

    • ShaderGenerator

      protected ShaderGenerator(AssetManager assetManager)
      Build a shaderGenerator
      Parameters:
      assetManager - for loading assets (alias created)
  • Method Details

    • initialize

      public void initialize(TechniqueDef techniqueDef)
    • generateShader

      public Shader generateShader(String definesSourceCode)
      Generate vertex and fragment shaders for the given technique
      Parameters:
      definesSourceCode - (may be null)
      Returns:
      a Shader program
    • buildShader

      protected String buildShader(List<ShaderNode> shaderNodes, ShaderGenerationInfo info, Shader.ShaderType type)
      This method is responsible for the shader generation.
      Parameters:
      shaderNodes - the list of shader nodes
      info - the ShaderGenerationInfo filled during the Technique loading
      type - the type of shader to generate
      Returns:
      the code of the generated vertex shader
    • generateDeclarationAndMainBody

      protected void generateDeclarationAndMainBody(List<ShaderNode> shaderNodes, StringBuilder sourceDeclaration, StringBuilder source, ShaderGenerationInfo info, Shader.ShaderType type)
      iterates through shader nodes to load them and generate the shader declaration part and main body extracted from the shader nodes, for the given shader type
      Parameters:
      shaderNodes - the list of shader nodes
      sourceDeclaration - the declaration part StringBuilder of the shader to generate
      source - the main part StringBuilder of the shader to generate
      info - the ShaderGenerationInfo
      type - the Shader type
    • appendNodeDeclarationAndMain

      protected void appendNodeDeclarationAndMain(String loadedSource, StringBuilder sourceDeclaration, StringBuilder source, ShaderNode shaderNode, ShaderGenerationInfo info, String shaderPath)
      Appends declaration and main part of a node to the shader declaration and main part. the loadedSource is split by "void main(){" to split declaration from main part of the node source code.The trailing "}" is removed from the main part. Each part is then respectively passed to generateDeclarativeSection and generateNodeMainSection.
      Parameters:
      loadedSource - the actual source code loaded for this node.
      shaderPath - path to the shader file
      sourceDeclaration - the Shader declaration part string builder.
      source - the Shader main part StringBuilder.
      shaderNode - the shader node.
      info - the ShaderGenerationInfo.
      See Also:
    • getLanguageAndVersion

      protected abstract String getLanguageAndVersion(Shader.ShaderType type)
      returns the language + version of the shader should be something like "GLSL100" for glsl 1.0 "GLSL150" for glsl 1.5.
      Parameters:
      type - the shader type for which the version should be returned.
      Returns:
      the shaderLanguage and version.
    • generateUniforms

      protected abstract void generateUniforms(StringBuilder source, ShaderGenerationInfo info, Shader.ShaderType type)
      generates the uniforms declaration for a shader of the given type.
      Parameters:
      source - the source StringBuilder to append generated code.
      info - the ShaderGenerationInfo.
      type - the shader type the uniforms have to be generated for.
    • generateAttributes

      protected abstract void generateAttributes(StringBuilder source, ShaderGenerationInfo info)
      generates the attributes declaration for the vertex shader. There is no Shader type passed here as attributes are only used in vertex shaders
      Parameters:
      source - the source StringBuilder to append generated code.
      info - the ShaderGenerationInfo.
    • generateVaryings

      protected abstract void generateVaryings(StringBuilder source, ShaderGenerationInfo info, Shader.ShaderType type)
      generates the varyings for the given shader type shader. Note that varyings are deprecated in glsl 1.3, but this method will still be called to generate all non-global inputs and output of the shaders.
      Parameters:
      source - the source StringBuilder to append generated code.
      info - the ShaderGenerationInfo.
      type - the shader type the varyings have to be generated for.
    • generateDeclarativeSection

      protected abstract void generateDeclarativeSection(StringBuilder source, ShaderNode shaderNode, String nodeDeclarationSource, ShaderGenerationInfo info)
      Appends the given shaderNode declarative part to the shader declarative part. If needed the shader type can be determined by fetching the shaderNode's definition type.
      Parameters:
      nodeDeclarationSource - the declaration part of the node
      source - the StringBuilder to append generated code.
      shaderNode - the shaderNode.
      info - the ShaderGenerationInfo.
      See Also:
    • generateStartOfMainSection

      protected abstract void generateStartOfMainSection(StringBuilder source, ShaderGenerationInfo info, Shader.ShaderType type)
      generates the start of the shader main section. this method is responsible of appending the "void main(){" in the shader and declaring all global outputs of the shader
      Parameters:
      source - the StringBuilder to append generated code.
      info - the ShaderGenerationInfo.
      type - the shader type the section has to be generated for.
    • generateEndOfMainSection

      protected abstract void generateEndOfMainSection(StringBuilder source, ShaderGenerationInfo info, Shader.ShaderType type)
      Generates the end of the shader main section. This method is responsible for appending the last "}" in the shader and mapping all global outputs of the shader.
      Parameters:
      source - the StringBuilder to append generated code.
      info - the ShaderGenerationInfo.
      type - the shader type the section has to be generated for.
    • generateNodeMainSection

      protected abstract void generateNodeMainSection(StringBuilder source, ShaderNode shaderNode, String nodeSource, ShaderGenerationInfo info)
      Appends the given shaderNode main part to the shader declarative part. If needed the shader type can be determined by fetching the shaderNode's definition type.
      Parameters:
      source - the StringBuilder to append generated code.
      shaderNode - the shaderNode.
      nodeSource - the declaration part of the loaded shaderNode source.
      info - the ShaderGenerationInfo.
      See Also:
    • findShaderIndexFromVersion

      protected int findShaderIndexFromVersion(ShaderNode shaderNode, Shader.ShaderType type) throws NumberFormatException
      Returns the shader-path index according to the version of the generator. This allows selecting the highest version of the shader that the generator can handle.
      Parameters:
      shaderNode - the shaderNode being processed
      type - the shaderType
      Returns:
      the index of the shader path in ShaderNodeDefinition shadersPath list
      Throws:
      NumberFormatException - for an invalid version