com.jme3.material.MatParam Java Examples

The following examples show how to use com.jme3.material.MatParam. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ShaderNodeLoaderDelegate.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Updates the material texture type of the variable mapping.
 *
 * @param statement the statement.
 * @param mapping the variable mapping.
 * @param left the left variable.
 * @param param the material parameter.
 * @throws MatParseException if the texture type isn't valid.
 */
private void updateMaterialTextureType(final Statement statement, final VariableMapping mapping,
                                       final ShaderNodeVariable left, final MatParam param) throws MatParseException {

    if (!mapping.getRightVariable().getType().contains("|")) {
        return;
    }

    final String type = fixSamplerType(left.getType(), mapping.getRightVariable().getType());

    if (type != null) {
        mapping.getRightVariable().setType(type);
    } else {
        throw new MatParseException(param.getVarType().toString() + " can only be matched to one of " +
                param.getVarType().getGlslType().replaceAll("\\|", ",") + " found " + left.getType(), statement);
    }
}
 
Example #2
Source File: J3mdTechniqueDefWriter.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private void writeDefines(TechniqueDef techniqueDef, Collection<MatParam> matParams, OutputStreamWriter out) throws IOException {
    out.write("        Defines {\n");

    for (int i = 0; i < techniqueDef.getDefineNames().length; i++) {
        String matParamName = getMatParamNameForDefineId(techniqueDef, matParams, i);
        if (matParamName != null) {
            String defineName = techniqueDef.getDefineNames()[i];
            out.write("            ");
            out.write(defineName);
            out.write(": ");
            out.write(matParamName);
            out.write("\n");
        }
    }
    out.write("        }\n\n");
}
 
Example #3
Source File: J3mdTechniqueDefWriter.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private String formatCondition(String condition, Collection<MatParam> matParams){
    //condition = condition.replaceAll("defined\\(","");

    String res = condition;
    Pattern pattern = Pattern.compile("defined\\(([A-Z0-9]*)\\)");
    Matcher m = pattern.matcher(condition);

    while(m.find()){
        String match = m.group(0);
        String defineName = m.group(1).toLowerCase();
        for (MatParam matParam : matParams) {
            if(matParam.getName().toLowerCase().equals(defineName)){
                res = res.replace(match, matParam.getName());
            }
        }
    }

    return res;
}
 
Example #4
Source File: MaterialUtils.java    From jmonkeybuilder with Apache License 2.0 6 votes vote down vote up
/**
 * Update the first material to the second material.
 *
 * @param toUpdate the material for updating.
 * @param material the target material.
 */
@JmeThread
private static void updateTo(@NotNull Material toUpdate, @NotNull Material material) {

    var oldParams = new ArrayList<MatParam>(toUpdate.getParams());
    oldParams.forEach(matParam -> {
        var param = material.getParam(matParam.getName());
        if (param == null || param.getValue() == null) {
            toUpdate.clearParam(matParam.getName());
        }
    });

    var actualParams = material.getParams();
    actualParams.forEach(matParam -> {
        var varType = matParam.getVarType();
        var value = matParam.getValue();
        toUpdate.setParam(matParam.getName(), varType, value);
    });

    var additionalRenderState = toUpdate.getAdditionalRenderState();
    additionalRenderState.set(material.getAdditionalRenderState());
}
 
Example #5
Source File: TextureLayerSettings.java    From jmonkeybuilder with Apache License 2.0 6 votes vote down vote up
/**
 * Get a alpha texture of the level.
 *
 * @param layer the layer.
 * @return the alpha texture or null.
 */
@FromAnyThread
public @Nullable Texture getAlpha(final int layer) {

    final Function<Integer, String> layerToAlphaName = getLayerToAlphaName();
    if (layerToAlphaName == null) {
        return null;
    }

    final Terrain terrain = getTerrain();
    final Material material = terrain.getMaterial();
    final MatParam matParam = material.getParam(layerToAlphaName.apply(layer));

    if (matParam == null || matParam.getValue() == null) {
        return null;
    }

    return (Texture) matParam.getValue();
}
 
Example #6
Source File: EditableMaterialFile.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * Creates the data from a material
 * @param mat
 */
public void setAsMaterial(Material mat) throws IOException {
    assert (mat.getMaterialDef().getAssetName() != null);
    setName("MyMaterial");
    setMatDefName(mat.getMaterialDef().getAssetName());
    createBaseMaterialFile();
    materialParameters.clear();
    Collection<MatParam> params = mat.getParams();
    for (Iterator<MatParam> it = params.iterator(); it.hasNext();) {
        MatParam matParam = it.next();
        materialParameters.put(matParam.getName(), new MaterialProperty(matParam));
    }
    additionalRenderStates.put("Wireframe", new MaterialProperty("OnOff", "Wireframe", mat.getAdditionalRenderState().isWireframe() ? "On" : "Off"));
    additionalRenderStates.put("DepthWrite", new MaterialProperty("OnOff", "DepthWrite", mat.getAdditionalRenderState().isDepthWrite() ? "On" : "Off"));
    additionalRenderStates.put("DepthTest", new MaterialProperty("OnOff", "DepthTest", mat.getAdditionalRenderState().isDepthTest() ? "On" : "Off"));
    additionalRenderStates.put("ColorWrite", new MaterialProperty("OnOff", "ColorWrite", mat.getAdditionalRenderState().isColorWrite() ? "On" : "Off"));
    additionalRenderStates.put("PointSprite", new MaterialProperty("OnOff", "PointSprite", mat.getAdditionalRenderState().isPointSprite() ? "On" : "Off"));
    additionalRenderStates.put("FaceCull", new MaterialProperty("FaceCullMode", "FaceCull", mat.getAdditionalRenderState().getFaceCullMode().name()));
    additionalRenderStates.put("Blend", new MaterialProperty("BlendMode", "Blend", mat.getAdditionalRenderState().getBlendMode().name()));
    additionalRenderStates.put("AlphaTestFalloff", new MaterialProperty("Float", "AlphaTestFalloff", mat.getAdditionalRenderState().getAlphaFallOff() + ""));
    additionalRenderStates.put("PolyOffset", new MaterialProperty("Float,Float", "PolyOffset", mat.getAdditionalRenderState().getPolyOffsetUnits() + " " + mat.getAdditionalRenderState().getPolyOffsetFactor()));
    checkWithMatDef();
    setAsText(getUpdatedContent());
}
 
Example #7
Source File: TextureLayerSettings.java    From jmonkeybuilder with Apache License 2.0 6 votes vote down vote up
/**
 * Get a diffuse normal of the level.
 *
 * @param layer the layer.
 * @return the normal texture or null.
 */
@FromAnyThread
public @Nullable Texture getNormal(final int layer) {

    final Function<Integer, String> layerToNormalName = getLayerToNormalName();
    if (layerToNormalName == null) {
        return null;
    }

    final Terrain terrain = getTerrain();
    final Material material = terrain.getMaterial();
    final MatParam matParam = material.getParam(layerToNormalName.apply(layer));

    if (matParam == null || matParam.getValue() == null) {
        return null;
    }

    return (Texture) matParam.getValue();
}
 
Example #8
Source File: TextureLayerSettings.java    From jmonkeybuilder with Apache License 2.0 6 votes vote down vote up
@JmeThread
private void updateTexture(@Nullable final Texture texture, @NotNull final String paramName,
                           @NotNull final Geometry geometry) {

    final Material material = geometry.getMaterial();
    final MatParam matParam = material.getParam(paramName);
    if (matParam == null && texture == null) {
        return;
    }

    if (texture == null) {
        material.clearParam(matParam.getName());
    } else {
        material.setTexture(paramName, texture);
    }
}
 
Example #9
Source File: TextureLayerSettings.java    From jmonkeybuilder with Apache License 2.0 6 votes vote down vote up
/**
 * Get a diffuse texture of the level.
 *
 * @param layer the layer.
 * @return the diffuse texture or null.
 */
@FromAnyThread
public @Nullable Texture getDiffuse(final int layer) {

    final Function<Integer, String> layerToDiffuseName = getLayerToDiffuseName();
    if (layerToDiffuseName == null) {
        return null;
    }

    final Terrain terrain = getTerrain();
    final Material material = terrain.getMaterial();
    final MatParam matParam = material.getParam(layerToDiffuseName.apply(layer));

    if (matParam == null || matParam.getValue() == null) {
        return null;
    }

    return (Texture) matParam.getValue();
}
 
Example #10
Source File: TerrainEditorController.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * Get the diffuse texture at the specified layer.
 * Run this on the GL thread!
 */
private Texture doGetDiffuseTexture(int layer) {
    Terrain terrain = (Terrain) getTerrain(null);
    if (terrain == null)
        return null;
    MatParam matParam = null;
    if (layer == 0)
        matParam = terrain.getMaterial().getParam("DiffuseMap");
    else
        matParam = terrain.getMaterial().getParam("DiffuseMap_"+layer);

    if (matParam == null || matParam.getValue() == null) {
        return null;
    }
    Texture tex = (Texture) matParam.getValue();

    return tex;
}
 
Example #11
Source File: TerrainEditorController.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
private Texture doGetAlphaTexture(Terrain terrain, int alphaLayer) {
    if (terrain == null)
        return null;
    MatParam matParam = null;
    if (alphaLayer == 0)
        matParam = terrain.getMaterial().getParam("AlphaMap");
    else if(alphaLayer == 1)
        matParam = terrain.getMaterial().getParam("AlphaMap_1");
    else if(alphaLayer == 2)
        matParam = terrain.getMaterial().getParam("AlphaMap_2");
    
    if (matParam == null || matParam.getValue() == null) {
        return null;
    }
    Texture tex = (Texture) matParam.getValue();
    return tex;
}
 
Example #12
Source File: TerrainEditorController.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * Get the normal map texture at the specified layer.
 * Run this on the GL thread!
 */
private Texture doGetNormalMap(int layer) {
    Terrain terrain = (Terrain) getTerrain(null);
    if (terrain == null)
        return null;
    MatParam matParam = null;
    if (layer == 0)
        matParam = terrain.getMaterial().getParam("NormalMap");
    else
        matParam = terrain.getMaterial().getParam("NormalMap_"+layer);

    if (matParam == null || matParam.getValue() == null) {
        return null;
    }
    Texture tex = (Texture) matParam.getValue();
    return tex;
}
 
Example #13
Source File: PaintTerrainToolAction.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
private Texture getAlphaTexture(Terrain terrain, int alphaLayer) {
    if (terrain == null)
        return null;
    MatParam matParam = null;
    if (alphaLayer == 0)
        matParam = terrain.getMaterial().getParam("AlphaMap");
    else if(alphaLayer == 1)
        matParam = terrain.getMaterial().getParam("AlphaMap_1");
    else if(alphaLayer == 2)
        matParam = terrain.getMaterial().getParam("AlphaMap_2");
    
    if (matParam == null || matParam.getValue() == null) {
        return null;
    }
    Texture tex = (Texture) matParam.getValue();
    return tex;
}
 
Example #14
Source File: TerrainEditorController.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private Float doGetTextureScale(int layer) {
    Terrain terrain = (Terrain) getTerrain(null);
    if (terrain == null)
        return 1f;
    MatParam matParam = null;
    matParam = terrain.getMaterial().getParam("DiffuseMap_"+layer+"_scale");
    return (Float) matParam.getValue();
}
 
Example #15
Source File: J3MOutputCapsule.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
protected String format(Savable value) {
    if (value instanceof MatParamTexture) {
        return formatMatParamTexture((MatParamTexture) value);
    }
    if (value instanceof MatParam) {
        return formatMatParam((MatParam) value);
    }

    throw new UnsupportedOperationException(value.getClass() + ": Not supported yet.");
}
 
Example #16
Source File: ShaderNodeLoaderDelegate.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Extracts and checks a condition expression.
 *
 * @param condition the condition expression.
 * @param statement the statement being read.
 * @throws MatParseException if the condition isn't valid.
 */
public void extractCondition(String condition, Statement statement) throws MatParseException {
    List<String> defines = conditionParser.extractDefines(condition);
    for (String string : defines) {
        MatParam param = findMatParam(string);
        if (param != null) {
            addDefine(param.getName(), param.getVarType());
        } else {
            throw new MatParseException("Invalid condition, condition must match a Material Parameter named " + condition, statement);
        }
    }
}
 
Example #17
Source File: ShaderNodeLoaderDelegate.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Finds a {@link MatParam} in the {@link MaterialDef} from the given name.
 *
 * @param varName the material param name.
 * @return the found {@link MatParam} or null.
 */
protected MatParam findMatParam(String varName) {
    for (MatParam matParam : materialDef.getMaterialParams()) {
        if (varName.equals(matParam.getName())) {
            return matParam;
        }
    }
    return null;
}
 
Example #18
Source File: MaterialDebugAppState.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
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
    // Force the reloading of the asset, otherwise the new shader code will not be applied.
    Material dummy = new Material(assetManager, mat.getMaterialDef().getAssetName());

    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;
}
 
Example #19
Source File: BitmapText.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private ColorRGBA getColor( Material mat, String name ) {
    MatParam mp = mat.getParam(name);
    if( mp == null ) {
        return null;
    }
    return (ColorRGBA)mp.getValue();
}
 
Example #20
Source File: InstancedNode.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void addToInstancedGeometry(Geometry geom) {
    Material material = geom.getMaterial();
    MatParam param = material.getParam("UseInstancing");
    if (param == null || !((Boolean)param.getValue()).booleanValue()) {
        throw new IllegalStateException("You must set the 'UseInstancing' "
                + "parameter to true on the material prior "
                + "to adding it to InstancedNode");
    }

    InstancedGeometry ig = lookUpByGeometry(geom);
    igByGeom.put(geom, ig);
    geom.associateWithGroupNode(this, 0);
    ig.addInstance(geom);
}
 
Example #21
Source File: J3mdTechniqueDefWriter.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private String getMatParamNameForDefineId(TechniqueDef techniqueDef, Collection<MatParam> matParams, int defineId) {
    for (MatParam matParam : matParams) {
        Integer id = techniqueDef.getShaderParamDefineId(matParam.getName());
        if(id !=null && id == defineId){
            return matParam.getName();
        }
    }
    return null;
}
 
Example #22
Source File: TerrainEditorController.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private boolean doIsTriPlanarEnabled() {
    Terrain terrain = (Terrain) getTerrain(null);
    if (terrain == null)
        return false;
    MatParam param = terrain.getMaterial().getParam("useTriPlanarMapping");
    if (param != null)
        return (Boolean)param.getValue();

    return false;
}
 
Example #23
Source File: MaterialPropertyBuilder.java    From jmonkeybuilder with Apache License 2.0 5 votes vote down vote up
/**
 * Convert the material parameter to editable property.
 *
 * @param param    the material parameter.
 * @param material the material.
 * @return the editable property or null.
 */
@FxThread
private @Nullable EditableProperty<?, Material> convert(@NotNull MatParam param, @NotNull Material material) {

    var propertyType = convert(param.getVarType());
    if (propertyType == null) {
        return null;
    }

    return new SimpleProperty<>(propertyType, param.getName(), 0.1F, material,
            object -> getParamValue(param, object), (object, newValue) -> applyParam(param, object, newValue));
}
 
Example #24
Source File: MaterialPropertyBuilder.java    From jmonkeybuilder with Apache License 2.0 5 votes vote down vote up
/**
 * Apply changes for the material parameter.
 *
 * @param param    the parameter.
 * @param material the material.
 * @param newValue the new value.
 */
@FxThread
protected void applyParam(@NotNull MatParam param, @NotNull Material material, @Nullable Object newValue) {

    if (newValue == null) {
        material.clearParam(param.getName());
    } else {
        material.setParam(param.getName(), param.getVarType(), newValue);
    }
}
 
Example #25
Source File: MaterialSettingsPropertyBuilder.java    From jmonkeybuilder with Apache License 2.0 5 votes vote down vote up
/**
 * Filter material parameters for the presented object.
 *
 * @param param  the material parameter.
 * @param object the presented object.
 * @return true of we can show the parameter.
 */
@FxThread
private boolean filter(@NotNull final MatParam param, @NotNull final Object object) {

    if (object instanceof TexturesSettings) {
        return TEXTURE_TYPES.contains(param.getVarType());
    } else if (object instanceof ColorsSettings) {
        return COLOR_TYPES.contains(param.getVarType());
    }

    return !TEXTURE_TYPES.contains(param.getVarType()) && !COLOR_TYPES.contains(param.getVarType());
}
 
Example #26
Source File: MaterialSettingsPropertyBuilder.java    From jmonkeybuilder with Apache License 2.0 5 votes vote down vote up
/**
 * Convert the material parameter to an editable property.
 *
 * @param param    the material parameter.
 * @param material the material.
 * @param settings the settings.
 * @return the editable property or null.
 */
@FxThread
private @Nullable EditableProperty<?, ?> convert(@NotNull final MatParam param, @NotNull final Material material,
                                                 @NotNull final MaterialSettings settings) {

    final EditablePropertyType propertyType = convert(param.getVarType());
    if (propertyType == null) {
        return null;
    }

    return new SimpleProperty<>(propertyType, param.getName(), 0.1F, settings,
            object -> getParamValue(param, material),
            (object, newValue) -> applyParam(param, material, newValue));
}
 
Example #27
Source File: GuiGlobals.java    From Lemur with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private Texture getTexture( Material mat, String name ) {
    MatParam mp = mat.getParam(name);
    if( mp == null ) {
        return null;
    }
    return (Texture)mp.getValue();
}
 
Example #28
Source File: MaterialSerializer.java    From jmonkeybuilder with Apache License 2.0 5 votes vote down vote up
/**
 * Add the material parameter to the builder.
 *
 * @param builder  the builder.
 * @param matParam the material parameter.
 */
@FromAnyThread
private static void addMaterialParameter(@NotNull StringBuilder builder, @NotNull MatParam matParam) {

    var value = toString(matParam.getVarType(), matParam.getValue());
    if (StringUtils.isEmpty(value)) {
        return;
    }

    builder.append("        ")
            .append(matParam.getName())
            .append(" : ")
            .append(value)
            .append('\n');
}
 
Example #29
Source File: MaterialUtils.java    From jmonkeybuilder with Apache License 2.0 5 votes vote down vote up
/**
 * Save if need textures of a material.
 *
 * @param material the material.
 */
@FromAnyThread
public static void saveIfNeedTextures(@NotNull Material material) {
    var params = material.getParams();
    params.stream().filter(matParam -> matParam.getVarType() == VarType.Texture2D)
            .map(MatParam::getValue)
            .map(Texture.class::cast)
            .forEach(MaterialUtils::saveIfNeedTexture);
}
 
Example #30
Source File: MaterialUtils.java    From jmonkeybuilder with Apache License 2.0 5 votes vote down vote up
/**
 * Clean up a material.
 *
 * @param material the material.
 */
@JmeThread
private static void cleanUp(@NotNull Material material) {
    var params = new ArrayList<MatParam>(material.getParams());
    params.stream().filter(param -> param.getValue() == null)
            .forEach(matParam -> material.clearParam(matParam.getName()));
}