Search in sources :

Example 1 with Shader

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

the class J3MLoaderTest method fixedPipelineTechnique_shouldBeIgnored.

@Test
public void fixedPipelineTechnique_shouldBeIgnored() throws IOException {
    when(assetInfo.openStream()).thenReturn(J3MLoader.class.getResourceAsStream("/no-shader-specified.j3md"));
    MaterialDef def = (MaterialDef) j3MLoader.load(assetInfo);
    assertEquals(null, def.getTechniqueDefs("A"));
    assertEquals(1, def.getTechniqueDefs("B").size());
}
Also used : MaterialDef(com.jme3.material.MaterialDef) Test(org.junit.Test)

Example 2 with Shader

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

the class LoadJ3mdTest method testShaderNodesMaterialDefLoading.

@Test
public void testShaderNodesMaterialDefLoading() {
    supportGlsl(100);
    material("testMatDef.j3md");
    material.selectTechnique("Default", renderManager);
    assertEquals(material.getActiveTechnique().getDef().getShaderNodes().size(), 2);
    Shader s = material.getActiveTechnique().getDef().getShader(TestUtil.createAssetManager(), myCaps, material.getActiveTechnique().getDynamicDefines());
    assertEquals(s.getSources().size(), 2);
}
Also used : Shader(com.jme3.shader.Shader) Test(org.junit.Test)

Example 3 with Shader

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

the class TestCustomMesh method simpleInitApp.

@Override
public void simpleInitApp() {
    Mesh m = new Mesh();
    // Vertex positions in space
    Vector3f[] vertices = new Vector3f[4];
    vertices[0] = new Vector3f(0, 0, 0);
    vertices[1] = new Vector3f(3, 0, 0);
    vertices[2] = new Vector3f(0, 3, 0);
    vertices[3] = new Vector3f(3, 3, 0);
    // Texture coordinates
    Vector2f[] texCoord = new Vector2f[4];
    texCoord[0] = new Vector2f(0, 0);
    texCoord[1] = new Vector2f(1, 0);
    texCoord[2] = new Vector2f(0, 1);
    texCoord[3] = new Vector2f(1, 1);
    // Indexes. We define the order in which mesh should be constructed
    short[] indexes = { 2, 0, 1, 1, 3, 2 };
    // Setting buffers
    m.setBuffer(Type.Position, 3, BufferUtils.createFloatBuffer(vertices));
    m.setBuffer(Type.TexCoord, 2, BufferUtils.createFloatBuffer(texCoord));
    m.setBuffer(Type.Index, 1, BufferUtils.createShortBuffer(indexes));
    m.updateBound();
    // *************************************************************************
    // First mesh uses one solid color
    // *************************************************************************
    // Creating a geometry, and apply a single color material to it
    Geometry geom = new Geometry("OurMesh", m);
    Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
    mat.setColor("Color", ColorRGBA.Blue);
    geom.setMaterial(mat);
    // Attaching our geometry to the root node.
    rootNode.attachChild(geom);
    // *************************************************************************
    // Second mesh uses vertex colors to color each vertex
    // *************************************************************************
    Mesh cMesh = m.clone();
    Geometry coloredMesh = new Geometry("ColoredMesh", cMesh);
    Material matVC = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
    matVC.setBoolean("VertexColor", true);
    //We have 4 vertices and 4 color values for each of them.
    //If you have more vertices, you need 'new float[yourVertexCount * 4]' here!
    float[] colorArray = new float[4 * 4];
    int colorIndex = 0;
    //Set custom RGBA value for each Vertex. Values range from 0.0f to 1.0f
    for (int i = 0; i < 4; i++) {
        // Red value (is increased by .2 on each next vertex here)
        colorArray[colorIndex++] = 0.1f + (.2f * i);
        // Green value (is reduced by .2 on each next vertex)
        colorArray[colorIndex++] = 0.9f - (0.2f * i);
        // Blue value (remains the same in our case)
        colorArray[colorIndex++] = 0.5f;
        // Alpha value (no transparency set here)
        colorArray[colorIndex++] = 1.0f;
    }
    // Set the color buffer
    cMesh.setBuffer(Type.Color, 4, colorArray);
    coloredMesh.setMaterial(matVC);
    // move mesh a bit so that it doesn't intersect with the first one
    coloredMesh.setLocalTranslation(4, 0, 0);
    rootNode.attachChild(coloredMesh);
    //        /** Alternatively, you can show the mesh vertixes as points
    //          * instead of coloring the faces. */
    //        cMesh.setMode(Mesh.Mode.Points);
    //        cMesh.setPointSize(10f);
    //        cMesh.updateBound();
    //        cMesh.setStatic();
    //        Geometry points = new Geometry("Points", m);
    //        points.setMaterial(mat);
    //        rootNode.attachChild(points);
    // *************************************************************************
    // Third mesh will use a wireframe shader to show wireframe
    // *************************************************************************
    Mesh wfMesh = m.clone();
    Geometry wfGeom = new Geometry("wireframeGeometry", wfMesh);
    Material matWireframe = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
    matWireframe.setColor("Color", ColorRGBA.Green);
    matWireframe.getAdditionalRenderState().setWireframe(true);
    wfGeom.setMaterial(matWireframe);
    wfGeom.setLocalTranslation(4, 4, 0);
    rootNode.attachChild(wfGeom);
}
Also used : Geometry(com.jme3.scene.Geometry) Vector2f(com.jme3.math.Vector2f) Vector3f(com.jme3.math.Vector3f) Mesh(com.jme3.scene.Mesh) Material(com.jme3.material.Material)

Example 4 with Shader

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

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

the class GLRenderer method resetUniformLocations.

protected void resetUniformLocations(Shader shader) {
    ListMap<String, Uniform> uniforms = shader.getUniformMap();
    for (int i = 0; i < uniforms.size(); i++) {
        Uniform uniform = uniforms.getValue(i);
        // e.g check location again
        uniform.reset();
    }
}
Also used : Uniform(com.jme3.shader.Uniform)

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