Java Code Examples for com.jme3.asset.AssetManager#loadMaterial()

The following examples show how to use com.jme3.asset.AssetManager#loadMaterial() . 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: CreateSkyDialog.java    From jmonkeybuilder with Apache License 2.0 6 votes vote down vote up
/**
 * Create a material of the geometry as a file if need.
 *
 * @param geometry the sky geometry.
 */
@BackgroundThread
private @NotNull Material createMaterialFileIfNeed(@NotNull final Geometry geometry) {

    final TextField materialNameField = getMaterialNameField();
    final ChooseFolderControl materialFolderControl = getMaterialFolderControl();

    final Material material = geometry.getMaterial();
    final String content = MaterialSerializer.serializeToString(material);

    final Path folder = materialFolderControl.getFolder();
    final Path materialFile = folder.resolve(materialNameField.getText() + "." + FileExtensions.JME_MATERIAL);

    try {
        Files.write(materialFile, content.getBytes("UTF-8"), WRITE, TRUNCATE_EXISTING, CREATE);
    } catch (final IOException e) {
        throw new RuntimeException(e);
    }

    final Path assetFile = EditorUtil.getAssetFile(materialFile);
    final String assetPath = EditorUtil.toAssetPath(assetFile);
    final AssetManager assetManager = EditorUtil.getAssetManager();
    return assetManager.loadMaterial(assetPath);
}
 
Example 2
Source File: SaveAsMaterialAction.java    From jmonkeybuilder with Apache License 2.0 6 votes vote down vote up
/**
 * The process of saving the file.
 *
 * @param file the file to save
 */
@FxThread
private void processSave(@NotNull final Path file) {

    final TreeNode<?> node = getNode();
    final Material material = (Material) node.getElement();
    final String materialContent = MaterialSerializer.serializeToString(material);

    try (final PrintWriter out = new PrintWriter(Files.newOutputStream(file, WRITE, TRUNCATE_EXISTING, CREATE))) {
        out.print(materialContent);
    } catch (final IOException e) {
        EditorUtil.handleException(LOGGER, this, e);
        return;
    }

    final Path assetFile = notNull(getAssetFile(file));
    final AssetManager assetManager = EditorUtil.getAssetManager();
    final Material savedMaterial = assetManager.loadMaterial(notNull(toAssetPath(assetFile)));

    final PropertyOperation<ChangeConsumer, Material, AssetKey> operation =
            new PropertyOperation<>(material, "AssetKey", savedMaterial.getKey(), null);
    operation.setApplyHandler(Material::setKey);

    final ChangeConsumer changeConsumer = notNull(getNodeTree().getChangeConsumer());
    changeConsumer.execute(operation);
}
 
Example 3
Source File: AbstractSceneFileEditor.java    From jmonkeybuilder with Apache License 2.0 6 votes vote down vote up
/**
 * Updating a material from the file.
 */
@FxThread
private void updateMaterial(@NotNull final Path file) {

    final Path assetFile = notNull(getAssetFile(file), "Not found asset file for " + file);
    final String assetPath = toAssetPath(assetFile);

    final M currentModel = getCurrentModel();

    final Array<Geometry> geometries = ArrayFactory.newArray(Geometry.class);
    NodeUtils.addGeometryWithMaterial(currentModel, geometries, assetPath);
    if (geometries.isEmpty()) return;

    final AssetManager assetManager = EditorUtil.getAssetManager();
    final Material material = assetManager.loadMaterial(assetPath);
    geometries.forEach(geometry -> geometry.setMaterial(material));

    final RenderFilterExtension filterExtension = RenderFilterExtension.getInstance();
    filterExtension.refreshFilters();
}
 
Example 4
Source File: TestMaterialCompare.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public static void main(String[] args) {
    AssetManager assetManager = JmeSystem.newAssetManager(
            TestMaterialCompare.class.getResource("/com/jme3/asset/Desktop.cfg"));
    
    // Cloned materials
    Material mat1 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
    mat1.setName("mat1");
    mat1.setColor("Color", ColorRGBA.Blue);

    Material mat2 = mat1.clone();
    mat2.setName("mat2");
    testEquality(mat1, mat2, true);

    // Cloned material with different render states
    Material mat3 = mat1.clone();
    mat3.setName("mat3");
    mat3.getAdditionalRenderState().setBlendMode(BlendMode.ModulateX2);
    testEquality(mat1, mat3, false);

    // Two separately loaded materials
    Material mat4 = assetManager.loadMaterial("Models/Sign Post/Sign Post.j3m");
    mat4.setName("mat4");
    Material mat5 = assetManager.loadMaterial("Models/Sign Post/Sign Post.j3m");
    mat5.setName("mat5");
    testEquality(mat4, mat5, true);
    
    // Comparing same textures
    TextureKey originalKey = (TextureKey) mat4.getTextureParam("DiffuseMap").getTextureValue().getKey();
    TextureKey tex1key = new TextureKey("Models/Sign Post/Sign Post.jpg", false);
    tex1key.setGenerateMips(true);
    
    // Texture keys from the original and the loaded texture
    // must be identical, otherwise the resultant textures not identical
    // and thus materials are not identical!
    if (!originalKey.equals(tex1key)){
        System.out.println("TEXTURE KEYS ARE NOT EQUAL");
    }
    
    Texture tex1 = assetManager.loadTexture(tex1key);
    mat4.setTexture("DiffuseMap", tex1);
    testEquality(mat4, mat5, true);
    
    // Change some stuff on the texture and compare, materials no longer equal
    tex1.setWrap(Texture.WrapMode.MirroredRepeat);
    testEquality(mat4, mat5, false);
    
    // Comparing different textures
    Texture tex2 = assetManager.loadTexture("Interface/Logo/Monkey.jpg");
    mat4.setTexture("DiffuseMap", tex2);
    testEquality(mat4, mat5, false);

    // Two materials created the same way
    Material mat6 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
    mat6.setName("mat6");
    mat6.setColor("Color", ColorRGBA.Blue);
    testEquality(mat1, mat6, true);

    // Changing a material param
    mat6.setColor("Color", ColorRGBA.Green);
    testEquality(mat1, mat6, false);
}