Search in sources :

Example 11 with MatParam

use of com.jme3.material.MatParam in project jmonkeyengine by jMonkeyEngine.

the class ShaderNodeLoaderDelegate method updateRightFromUniforms.

/**
     * 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.
     *
     * @param param the MatParam
     * @param mapping the mapping
     * @param map the map of uniforms to search into
     * @return true if the param was added to the map
     */
public boolean updateRightFromUniforms(MatParam param, VariableMapping mapping, Map<String, DeclaredVariable> map, Statement statement) throws MatParseException {
    ShaderNodeVariable right = mapping.getRightVariable();
    DeclaredVariable dv = map.get(param.getPrefixedName());
    if (dv == null) {
        right.setType(param.getVarType().getGlslType());
        right.setName(param.getPrefixedName());
        if (mapping.getLeftVariable().getMultiplicity() != null) {
            if (!param.getVarType().name().endsWith("Array")) {
                throw new MatParseException(param.getName() + " is not of Array type", statement);
            }
            String multiplicity = mapping.getLeftVariable().getMultiplicity();
            try {
                Integer.parseInt(multiplicity);
            } catch (NumberFormatException nfe) {
                //multiplicity is not an int attempting to find for a material parameter.
                MatParam mp = findMatParam(multiplicity);
                if (mp != null) {
                    addDefine(multiplicity, VarType.Int);
                    multiplicity = multiplicity.toUpperCase();
                } else {
                    throw new MatParseException("Wrong multiplicity for variable" + mapping.getLeftVariable().getName() + ". " + multiplicity + " should be an int or a declared material parameter.", statement);
                }
            }
            right.setMultiplicity(multiplicity);
        }
        dv = new DeclaredVariable(right);
        map.put(right.getName(), dv);
        dv.addNode(shaderNode);
        mapping.setRightVariable(right);
        return true;
    }
    dv.addNode(shaderNode);
    mapping.setRightVariable(dv.var);
    return false;
}
Also used : MatParam(com.jme3.material.MatParam) ShaderNodeVariable(com.jme3.shader.ShaderNodeVariable)

Example 12 with MatParam

use of com.jme3.material.MatParam in project jmonkeyengine by jMonkeyEngine.

the class TestMaterialDefWrite method testWriteMat.

@Test
public void testWriteMat() throws Exception {
    Material mat = new Material(assetManager, "example.j3md");
    final ByteArrayOutputStream stream = new ByteArrayOutputStream();
    J3mdExporter exporter = new J3mdExporter();
    try {
        exporter.save(mat.getMaterialDef(), stream);
    } catch (IOException e) {
        e.printStackTrace();
    }
    //   System.err.println(stream.toString());
    J3MLoader loader = new J3MLoader();
    AssetInfo info = new AssetInfo(assetManager, new AssetKey("test")) {

        @Override
        public InputStream openStream() {
            return new ByteArrayInputStream(stream.toByteArray());
        }
    };
    MaterialDef matDef = (MaterialDef) loader.load(info);
    MaterialDef ref = mat.getMaterialDef();
    for (MatParam refParam : ref.getMaterialParams()) {
        MatParam matParam = matDef.getMaterialParam(refParam.getName());
        assertTrue(refParam != null);
        assertEquals(refParam, matParam);
    }
    for (String key : ref.getTechniqueDefsNames()) {
        List<TechniqueDef> refDefs = ref.getTechniqueDefs(key);
        List<TechniqueDef> defs = matDef.getTechniqueDefs(key);
        assertNotNull(defs);
        assertTrue(refDefs.size() == defs.size());
        for (int i = 0; i < refDefs.size(); i++) {
            assertEqualTechniqueDefs(refDefs.get(i), defs.get(i));
        }
    }
}
Also used : J3MLoader(com.jme3.material.plugins.J3MLoader) J3mdExporter(com.jme3.material.plugin.export.materialdef.J3mdExporter)

Example 13 with MatParam

use of com.jme3.material.MatParam in project jmonkeyengine by jMonkeyEngine.

the class J3MOutputCapsule method formatMatParam.

private String formatMatParam(MatParam param) {
    VarType type = param.getVarType();
    Object val = param.getValue();
    switch(type) {
        case Boolean:
        case Float:
        case Int:
            return val.toString();
        case Vector2:
            Vector2f v2 = (Vector2f) val;
            return v2.getX() + " " + v2.getY();
        case Vector3:
            Vector3f v3 = (Vector3f) val;
            return v3.getX() + " " + v3.getY() + " " + v3.getZ();
        case Vector4:
            // can be either ColorRGBA, Vector4f or Quaternion
            if (val instanceof Vector4f) {
                Vector4f v4 = (Vector4f) val;
                return v4.getX() + " " + v4.getY() + " " + v4.getZ() + " " + v4.getW();
            } else if (val instanceof ColorRGBA) {
                ColorRGBA color = (ColorRGBA) val;
                return color.getRed() + " " + color.getGreen() + " " + color.getBlue() + " " + color.getAlpha();
            } else if (val instanceof Quaternion) {
                Quaternion quat = (Quaternion) val;
                return quat.getX() + " " + quat.getY() + " " + quat.getZ() + " " + quat.getW();
            } else {
                throw new UnsupportedOperationException("Unexpected Vector4 type: " + val);
            }
        default:
            // parameter type not supported in J3M
            return null;
    }
}
Also used : VarType(com.jme3.shader.VarType)

Aggregations

MatParam (com.jme3.material.MatParam)7 Material (com.jme3.material.Material)5 VarType (com.jme3.shader.VarType)3 ShaderNodeVariable (com.jme3.shader.ShaderNodeVariable)2 Uniform (com.jme3.shader.Uniform)2 Image (com.jme3.texture.Image)2 Texture (com.jme3.texture.Texture)2 AssetKey (com.jme3.asset.AssetKey)1 AssetNotFoundException (com.jme3.asset.AssetNotFoundException)1 J3mdExporter (com.jme3.material.plugin.export.materialdef.J3mdExporter)1 J3MLoader (com.jme3.material.plugins.J3MLoader)1 ColorRGBA (com.jme3.math.ColorRGBA)1 RendererException (com.jme3.renderer.RendererException)1 Geometry (com.jme3.scene.Geometry)1 Box (com.jme3.scene.shape.Box)1 ShaderNode (com.jme3.shader.ShaderNode)1 UniformBinding (com.jme3.shader.UniformBinding)1 VariableMapping (com.jme3.shader.VariableMapping)1 ListMap (com.jme3.util.ListMap)1 IOException (java.io.IOException)1