Java Code Examples for com.jme3.material.Material#getParams()

The following examples show how to use com.jme3.material.Material#getParams() . 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: MaterialUtils.java    From jmonkeybuilder with Apache License 2.0 6 votes vote down vote up
/**
 * Check a material on containing a texture.
 *
 * @param material  the material.
 * @param assetPath the path of the texture.
 * @return true if the material definition contains the texture.
 */
@FromAnyThread
private static boolean containsTexture(@NotNull Material material, @NotNull String assetPath) {

    var materialParams = material.getParams();

    for (var materialParam : materialParams) {

        if (materialParam.getVarType() != VarType.Texture2D) {
            continue;
        }

        var value = (Texture) materialParam.getValue();
        var textureKey = value == null ? null : (TextureKey) value.getKey();
        if (textureKey != null && StringUtils.equals(textureKey.getName(), assetPath)) {
            return true;
        }
    }

    return false;
}
 
Example 2
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 3
Source File: MaterialUtils.java    From jmonkeybuilder with Apache License 2.0 6 votes vote down vote up
/**
 * Migrate the material to second material.
 *
 * @param target the target migrating.
 * @param source the source material.
 */
@JmeThread
public static void migrateTo(@NotNull Material target, @NotNull Material source) {

    var materialDef = target.getMaterialDef();
    var actualParams = source.getParams();

    actualParams.forEach(matParam -> {

        var param = materialDef.getMaterialParam(matParam.getName());

        if (param == null || param.getVarType() != matParam.getVarType()) {
            return;
        }

        target.setParam(matParam.getName(), matParam.getVarType(), matParam.getValue());
    });

    var additionalRenderState = target.getAdditionalRenderState();
    additionalRenderState.set(source.getAdditionalRenderState());

    target.setKey(source.getKey());
}
 
Example 4
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 5
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()));
}
 
Example 6
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 7
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 8
Source File: MaterialSerializer.java    From jmonkeybuilder with Apache License 2.0 4 votes vote down vote up
/**
 * Serialize to string string.
 *
 * @param material the material
 * @return the string
 */
@FromAnyThread
public static @NotNull String serializeToString(@NotNull Material material) {

    var materialDef = material.getMaterialDef();
    var params = material.getParams();

    var builder = new StringBuilder();
    builder.append("Material MyMaterial : ").append(materialDef.getAssetName()).append(" {\n");
    builder.append("    MaterialParameters {\n");
    params.forEach(matParam -> addMaterialParameter(builder, matParam));
    builder.append("    }\n");
    builder.append("    AdditionalRenderState {\n");

    var renderState = material.getAdditionalRenderState();
    var blendMode = renderState.getBlendMode();
    var faceCullMode = renderState.getFaceCullMode();

    if (blendMode != RenderState.BlendMode.Off) {
        builder.append("      Blend ").append(blendMode.name()).append('\n');
    }

    if (faceCullMode != RenderState.FaceCullMode.Back) {
        builder.append("      FaceCull ").append(faceCullMode.name()).append('\n');
    }

    if (renderState.isWireframe()) builder.append("      Wireframe On\n");
    if (!renderState.isDepthTest()) builder.append("      DepthTest Off\n");
    if (!renderState.isDepthWrite()) builder.append("      DepthWrite Off\n");
    if (!renderState.isColorWrite()) builder.append("      ColorWrite Off\n");

    var polyOffsetFactor = renderState.getPolyOffsetFactor();
    var polyOffsetUnits = renderState.getPolyOffsetUnits();

    if (polyOffsetFactor != 0 || polyOffsetUnits != 0) {
        builder.append("      PolyOffset ")
                .append(polyOffsetFactor)
                .append(' ')
                .append(polyOffsetUnits)
                .append('\n');
    }

    builder.append("    }\n");
    builder.append("}");

    return builder.toString();
}