com.jme3.material.MatParamTexture Java Examples

The following examples show how to use com.jme3.material.MatParamTexture. 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: 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 #2
Source File: TextureAtlas.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private static Texture getMaterialTexture(Geometry geometry, String mapName) {
    Material mat = geometry.getMaterial();
    if (mat == null || mat.getParam(mapName) == null || !(mat.getParam(mapName) instanceof MatParamTexture)) {
        return null;
    }
    MatParamTexture param = (MatParamTexture) mat.getParam(mapName);
    Texture texture = param.getTextureValue();
    if (texture == null) {
        return null;
    }
    return texture;


}
 
Example #3
Source File: ModelImportDialog.java    From jmonkeybuilder with Apache License 2.0 4 votes vote down vote up
/**
 * Import the external model in a background thread.
 */
@BackgroundThread
private void importModel() {

    final Path modelFile = notNull(getFileToCreate());

    final Path parent = modelFile.getParent();
    final VarTable vars = getVars();

    final Path importedFile = vars.get(PROP_FILE);

    final AssetManager assetManager = EditorUtil.getAssetManager();
    final Spatial model;

    FolderAssetLocator.setIgnore(true);
    try {
        model = assetManager.loadModel(importedFile.toString());
    } finally {
        FolderAssetLocator.setIgnore(false);
    }

    if (EDITOR_CONFIG.getBoolean(PREF_TANGENT_GENERATION, PREF_DEFAULT_TANGENT_GENERATION)) {
        TangentGenerator.useMikktspaceGenerator(model);
    }

    final Path texturesFolder = vars.get(PROP_TEXTURES_FOLDER, parent);
    final boolean overwriteTextures = vars.getBoolean(PROP_OVERWRITE_TEXTURES);

    final boolean needExportMaterials = vars.getBoolean(PROP_NEED_MATERIALS_EXPORT);
    final Path materialsFolder = vars.get(PROP_MATERIALS_FOLDER, parent);
    final boolean overwriteMaterials = vars.getBoolean(PROP_OVERWRITE_MATERIALS, false);

    final Array<Texture> textures = ArrayFactory.newArray(Texture.class);
    final Array<Geometry> geometries = ArrayFactory.newArray(Geometry.class);

    NodeUtils.visitGeometry(model, geometry -> {

        final Material material = geometry.getMaterial();
        if (needExportMaterials) geometries.add(geometry);

        material.getParams().stream()
                .filter(MatParamTexture.class::isInstance)
                .map(MatParam::getValue)
                .filter(Texture.class::isInstance)
                .map(Texture.class::cast)
                .filter(texture -> texture.getKey() != null)
                .forEach(textures::add);
    });

    copyTextures(texturesFolder, overwriteTextures, textures);

    if (needExportMaterials) {
        exportMaterials(materialsFolder, overwriteMaterials, geometries);
    }

    final Path assetFile = notNull(getAssetFile(modelFile));
    final String assetPath = toAssetPath(assetFile);

    model.setName(assetPath);

    final BinaryExporter exporter = BinaryExporter.getInstance();

    try (final OutputStream out = Files.newOutputStream(modelFile, WRITE, TRUNCATE_EXISTING, CREATE)) {
        exporter.save(model, out);
    } catch (final IOException e) {
        throw new RuntimeException(e);
    }

    notifyFileCreated(modelFile, true);
}
 
Example #4
Source File: MaterialFileEditor.java    From jmonkeybuilder with Apache License 2.0 4 votes vote down vote up
/**
 * Try to apply dropped texture.
 *
 * @param editor    the editor.
 * @param dragEvent the drag event.
 * @param path      the path to the texture.
 */
private void applyTexture(@NotNull final MaterialFileEditor editor, @NotNull final DragEvent dragEvent,
                          @NotNull final Path path) {

    final String textureName = path.getFileName().toString();
    final int textureType = MaterialUtils.getPossibleTextureType(textureName);

    if (textureType == 0) {
        return;
    }

    final String[] paramNames = MaterialUtils.getPossibleParamNames(textureType);
    final Material currentMaterial = getCurrentMaterial();
    final MaterialDef materialDef = currentMaterial.getMaterialDef();

    final Optional<MatParam> param = Arrays.stream(paramNames)
            .map(materialDef::getMaterialParam)
            .filter(Objects::nonNull)
            .filter(p -> p.getVarType() == VarType.Texture2D)
            .findAny();

    if (!param.isPresent()) {
        return;
    }

    final MatParam matParam = param.get();

    EXECUTOR_MANAGER.addJmeTask(() -> {

        final EditorConfig config = EditorConfig.getInstance();
        final Path assetFile = notNull(getAssetFile(path));
        final TextureKey textureKey = new TextureKey(toAssetPath(assetFile));
        textureKey.setFlipY(config.getBoolean(PREF_FLIPPED_TEXTURES, PREF_DEFAULT_FLIPPED_TEXTURES));

        final AssetManager assetManager = EditorUtil.getAssetManager();
        final Texture texture = assetManager.loadTexture(textureKey);
        texture.setWrap(Texture.WrapMode.Repeat);

        final String paramName = matParam.getName();
        final MatParamTexture textureParam = currentMaterial.getTextureParam(paramName);
        final Texture currentTexture = textureParam == null? null : textureParam.getTextureValue();

        PropertyOperation<ChangeConsumer, Material, Texture> operation =
                new PropertyOperation<>(currentMaterial, paramName, texture, currentTexture);
        operation.setApplyHandler((material, newTexture) -> material.setTexture(paramName, newTexture));

        execute(operation);
    });
}
 
Example #5
Source File: J3MLoaderTest.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 3 votes vote down vote up
private TextureKey setupMockForTexture(final String paramName, final String path, final boolean flipY, final Texture texture) {
    when(materialDef.getMaterialParam(paramName)).thenReturn(new MatParamTexture(VarType.Texture2D, paramName, texture, null));

    final TextureKey textureKey = new TextureKey(path, flipY);
    textureKey.setGenerateMips(true);

    when(assetManager.loadTexture(textureKey)).thenReturn(texture);

    return textureKey;
}