Search in sources :

Example 21 with Shader

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

the class J3MLoader method readTechnique.

private void readTechnique(Statement techStat) throws IOException {
    isUseNodes = false;
    String[] split = techStat.getLine().split(whitespacePattern);
    Cloner cloner = new Cloner();
    String name;
    if (split.length == 1) {
        name = TechniqueDef.DEFAULT_TECHNIQUE_NAME;
    } else if (split.length == 2) {
        name = split[1];
    } else {
        throw new IOException("Technique statement syntax incorrect");
    }
    String techniqueUniqueName = materialDef.getAssetName() + "@" + name;
    technique = new TechniqueDef(name, techniqueUniqueName.hashCode());
    for (Statement statement : techStat.getContents()) {
        readTechniqueStatement(statement);
    }
    technique.setShaderPrologue(createShaderPrologue(presetDefines));
    switch(technique.getLightMode()) {
        case Disable:
            technique.setLogic(new DefaultTechniqueDefLogic(technique));
            break;
        case MultiPass:
            technique.setLogic(new MultiPassLightingLogic(technique));
            break;
        case SinglePass:
            technique.setLogic(new SinglePassLightingLogic(technique));
            break;
        case StaticPass:
            technique.setLogic(new StaticPassLightingLogic(technique));
            break;
        case SinglePassAndImageBased:
            technique.setLogic(new SinglePassAndImageBasedLightingLogic(technique));
            break;
        default:
            throw new UnsupportedOperationException();
    }
    List<TechniqueDef> techniqueDefs = new ArrayList<>();
    if (isUseNodes) {
        nodesLoaderDelegate.computeConditions();
        //used for caching later, the shader here is not a file.
        // KIRILL 9/19/2015
        // Not sure if this is needed anymore, since shader caching
        // is now done by TechniqueDef.
        technique.setShaderFile(technique.hashCode() + "", technique.hashCode() + "", "GLSL100", "GLSL100");
        techniqueDefs.add(technique);
    } else if (shaderNames.containsKey(Shader.ShaderType.Vertex) && shaderNames.containsKey(Shader.ShaderType.Fragment)) {
        if (shaderLanguages.size() > 1) {
            for (int i = 1; i < shaderLanguages.size(); i++) {
                cloner.clearIndex();
                TechniqueDef td = cloner.clone(technique);
                td.setShaderFile(shaderNames, shaderLanguages.get(i));
                techniqueDefs.add(td);
            }
        }
        technique.setShaderFile(shaderNames, shaderLanguages.get(0));
        techniqueDefs.add(technique);
    } else {
        technique = null;
        shaderLanguages.clear();
        shaderNames.clear();
        presetDefines.clear();
        langSize = 0;
        logger.log(Level.WARNING, "Fixed function technique was ignored");
        logger.log(Level.WARNING, "Fixed function technique ''{0}'' was ignored for material {1}", new Object[] { name, key });
        return;
    }
    for (TechniqueDef techniqueDef : techniqueDefs) {
        materialDef.addTechniqueDef(techniqueDef);
    }
    technique = null;
    langSize = 0;
    shaderLanguages.clear();
    shaderNames.clear();
    presetDefines.clear();
}
Also used : Statement(com.jme3.util.blockparser.Statement) IOException(java.io.IOException) StaticPassLightingLogic(com.jme3.material.logic.StaticPassLightingLogic) Cloner(com.jme3.util.clone.Cloner)

Example 22 with Shader

use of com.jme3.shader.Shader 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 23 with Shader

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

the class MaterialDebugAppState method reloadMaterial.

public Material reloadMaterial(Material mat) {
    //clear the entire cache, there might be more clever things to do, like clearing only the matdef, and the associated shaders.
    assetManager.clearCache();
    //creating a dummy mat with the mat def of the mat to reload
    Material dummy = new Material(mat.getMaterialDef());
    for (MatParam matParam : mat.getParams()) {
        dummy.setParam(matParam.getName(), matParam.getVarType(), matParam.getValue());
    }
    dummy.getAdditionalRenderState().set(mat.getAdditionalRenderState());
    //creating a dummy geom and assigning the dummy material to it
    Geometry dummyGeom = new Geometry("dummyGeom", new Box(1f, 1f, 1f));
    dummyGeom.setMaterial(dummy);
    try {
        //preloading the dummyGeom, this call will compile the shader again
        renderManager.preloadScene(dummyGeom);
    } catch (RendererException e) {
        //compilation error, the shader code will be output to the console
        //the following code will output the error
        //System.err.println(e.getMessage());
        Logger.getLogger(MaterialDebugAppState.class.getName()).log(Level.SEVERE, e.getMessage());
        return null;
    }
    Logger.getLogger(MaterialDebugAppState.class.getName()).log(Level.INFO, "Material succesfully reloaded");
    //System.out.println("Material succesfully reloaded");
    return dummy;
}
Also used : Geometry(com.jme3.scene.Geometry) RendererException(com.jme3.renderer.RendererException) MatParam(com.jme3.material.MatParam) Material(com.jme3.material.Material) Box(com.jme3.scene.shape.Box)

Example 24 with Shader

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

the class Material method setTexture.

/**
     * Pass a texture to the material shader.
     *
     * @param name the name of the texture defined in the material definition
     * (j3md) (for example Texture for Lighting.j3md)
     * @param value the Texture object previously loaded by the asset manager
     */
public void setTexture(String name, Texture value) {
    if (value == null) {
        // clear it
        clearParam(name);
        return;
    }
    VarType paramType = null;
    switch(value.getType()) {
        case TwoDimensional:
            paramType = VarType.Texture2D;
            break;
        case TwoDimensionalArray:
            paramType = VarType.TextureArray;
            break;
        case ThreeDimensional:
            paramType = VarType.Texture3D;
            break;
        case CubeMap:
            paramType = VarType.TextureCubeMap;
            break;
        default:
            throw new UnsupportedOperationException("Unknown texture type: " + value.getType());
    }
    setTextureParam(name, paramType, value);
}
Also used : VarType(com.jme3.shader.VarType)

Example 25 with Shader

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

the class ShaderGenerator method generateShader.

/**
     * Generate vertex and fragment shaders for the given technique
     *
     * @return a Shader program
     */
public Shader generateShader(String definesSourceCode) {
    if (techniqueDef == null) {
        throw new UnsupportedOperationException("The shaderGenerator was not " + "properly initialized, call " + "initialize(TechniqueDef) before any generation");
    }
    String techniqueName = techniqueDef.getName();
    ShaderGenerationInfo info = techniqueDef.getShaderGenerationInfo();
    Shader shader = new Shader();
    for (ShaderType type : ShaderType.values()) {
        String extension = type.getExtension();
        String language = getLanguageAndVersion(type);
        String shaderSourceCode = buildShader(techniqueDef.getShaderNodes(), info, type);
        if (shaderSourceCode != null) {
            String shaderSourceAssetName = techniqueName + "." + extension;
            shader.addSource(type, shaderSourceAssetName, shaderSourceCode, definesSourceCode, language);
        }
    }
    techniqueDef = null;
    return shader;
}
Also used : ShaderGenerationInfo(com.jme3.material.ShaderGenerationInfo) ShaderType(com.jme3.shader.Shader.ShaderType)

Aggregations

Uniform (com.jme3.shader.Uniform)8 Renderer (com.jme3.renderer.Renderer)6 Caps (com.jme3.renderer.Caps)5 DirectionalLight (com.jme3.light.DirectionalLight)4 PointLight (com.jme3.light.PointLight)4 SpotLight (com.jme3.light.SpotLight)4 Material (com.jme3.material.Material)4 Vector3f (com.jme3.math.Vector3f)4 Shader (com.jme3.shader.Shader)4 IOException (java.io.IOException)4 ShaderNodeDefinitionKey (com.jme3.asset.ShaderNodeDefinitionKey)3 Light (com.jme3.light.Light)3 ColorRGBA (com.jme3.math.ColorRGBA)3 ShaderType (com.jme3.shader.Shader.ShaderType)3 VarType (com.jme3.shader.VarType)3 TempVars (com.jme3.util.TempVars)3 Statement (com.jme3.util.blockparser.Statement)3 AssetLoadException (com.jme3.asset.AssetLoadException)2 AssetNotFoundException (com.jme3.asset.AssetNotFoundException)2 MatParam (com.jme3.material.MatParam)2