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

The following examples show how to use com.jme3.asset.AssetManager#loadModel() . 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: NodeTreeNode.java    From jmonkeybuilder with Apache License 2.0 6 votes vote down vote up
/**
 * Add the external object to this node.
 *
 * @param node this node.
 * @param cons the change consumer.
 * @param path the path to the external object.
 */
@FxThread
protected void dropExternalObject(@NotNull final T node, @NotNull final ChangeConsumer cons,
                                  @NotNull final Path path) {

    final SceneLayer defaultLayer = getDefaultLayer(cons);
    final Path assetFile = notNull(getAssetFile(path), "Not found asset file for " + path);
    final String assetPath = toAssetPath(assetFile);
    final ModelKey modelKey = new ModelKey(assetPath);

    final AssetManager assetManager = EditorUtil.getAssetManager();
    final Spatial loadedModel = assetManager.loadModel(assetPath);
    final AssetLinkNode assetLinkNode = new AssetLinkNode(modelKey);
    assetLinkNode.attachLinkedChild(loadedModel, modelKey);

    if (defaultLayer != null) {
        SceneLayer.setLayer(defaultLayer, loadedModel);
    }

    cons.execute(new AddChildOperation(assetLinkNode, node));
}
 
Example 2
Source File: LoadModelAction.java    From jmonkeybuilder with Apache License 2.0 6 votes vote down vote up
/**
 * The process of opening file.
 *
 * @param file the file
 */
@FxThread
protected void processOpen(@NotNull final Path file) {

    final NodeTree<?> nodeTree = getNodeTree();
    final ChangeConsumer consumer = notNull(nodeTree.getChangeConsumer());
    final SceneLayer defaultLayer = getDefaultLayer(consumer);

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

    final ModelKey modelKey = new ModelKey(assetPath);

    final AssetManager assetManager = EditorUtil.getAssetManager();
    final Spatial loadedModel = assetManager.loadModel(modelKey);
    loadedModel.setUserData(KEY_LOADED_MODEL, true);

    if (defaultLayer != null) {
        SceneLayer.setLayer(defaultLayer, loadedModel);
    }

    final TreeNode<?> treeNode = getNode();
    final Node parent = (Node) treeNode.getElement();
    consumer.execute(new AddChildOperation(loadedModel, parent));
}
 
Example 3
Source File: LinkModelAction.java    From jmonkeybuilder with Apache License 2.0 5 votes vote down vote up
/**
 * The process of opening file.
 *
 * @param file the file
 */
@FxThread
protected void processOpen(@NotNull final Path file) {

    final NodeTree<?> nodeTree = getNodeTree();
    final ChangeConsumer consumer = notNull(nodeTree.getChangeConsumer());
    final SceneLayer defaultLayer = EditorUtil.getDefaultLayer(consumer);

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

    final ModelKey modelKey = new ModelKey(assetPath);

    final AssetManager assetManager = EditorUtil.getAssetManager();
    final Spatial loadedModel = assetManager.loadModel(modelKey);

    final AssetLinkNode assetLinkNode = new AssetLinkNode(modelKey);
    assetLinkNode.attachLinkedChild(loadedModel, modelKey);
    assetLinkNode.setUserData(KEY_LOADED_MODEL, true);

    if (defaultLayer != null) {
        SceneLayer.setLayer(defaultLayer, assetLinkNode);
    }

    final TreeNode<?> treeNode = getNode();
    final Node parent = (Node) treeNode.getElement();
    consumer.execute(new AddChildOperation(assetLinkNode, parent));
}
 
Example 4
Source File: DeleteMaterialsModelFileDeleteHandler.java    From jmonkeybuilder with Apache License 2.0 5 votes vote down vote up
@Override
public void preDelete(@NotNull final Path file) {
    super.preDelete(file);

    final AssetManager assetManager = EditorUtil.getAssetManager();
    final Path assetFile = notNull(getAssetFile(file));
    final String assetPath = toAssetPath(assetFile);

    final Spatial model;

    try {
        model = assetManager.loadModel(assetPath);
    } catch (final Exception e) {
        LOGGER.warning(this, e);
        return;
    }

    NodeUtils.visitGeometry(model, geometry -> {

        final Material material = geometry.getMaterial();
        final String assetName = material.getAssetName();

        if (!StringUtils.isEmpty(assetName)) {
            assetKeys.add(assetName);
        }
    });
}
 
Example 5
Source File: AbstractSceneEditor3DPart.java    From jmonkeybuilder with Apache License 2.0 4 votes vote down vote up
/**
 * Create manipulators.
 */
@FromAnyThread
private void createManipulators() {

    final AssetManager assetManager = EditorUtil.getAssetManager();

    final Material redMaterial = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
    redMaterial.setColor("Color", ColorRGBA.Red);

    final Material blueMaterial = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
    blueMaterial.setColor("Color", ColorRGBA.Blue);

    final Material greenMaterial = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
    greenMaterial.setColor("Color", ColorRGBA.Green);

    moveTool = (Node) assetManager.loadModel("graphics/models/manipulators/manipulators_move.j3o");
    moveTool.getChild("move_x").setMaterial(redMaterial);
    moveTool.getChild("collision_move_x").setMaterial(redMaterial);
    moveTool.getChild("collision_move_x").setCullHint(Spatial.CullHint.Always);
    moveTool.getChild("move_y").setMaterial(blueMaterial);
    moveTool.getChild("collision_move_y").setMaterial(blueMaterial);
    moveTool.getChild("collision_move_y").setCullHint(Spatial.CullHint.Always);
    moveTool.getChild("move_z").setMaterial(greenMaterial);
    moveTool.getChild("collision_move_z").setMaterial(greenMaterial);
    moveTool.getChild("collision_move_z").setCullHint(Spatial.CullHint.Always);
    moveTool.scale(0.2f);
    moveTool.addControl(new MoveToolControl(this));

    rotateTool = (Node) assetManager.loadModel("graphics/models/manipulators/manipulators_rotate.j3o");
    rotateTool.getChild("rot_x").setMaterial(redMaterial);
    rotateTool.getChild("collision_rot_x").setMaterial(redMaterial);
    rotateTool.getChild("collision_rot_x").setCullHint(Spatial.CullHint.Always);
    rotateTool.getChild("rot_y").setMaterial(blueMaterial);
    rotateTool.getChild("collision_rot_y").setMaterial(blueMaterial);
    rotateTool.getChild("collision_rot_y").setCullHint(Spatial.CullHint.Always);
    rotateTool.getChild("rot_z").setMaterial(greenMaterial);
    rotateTool.getChild("collision_rot_z").setMaterial(greenMaterial);
    rotateTool.getChild("collision_rot_z").setCullHint(Spatial.CullHint.Always);
    rotateTool.scale(0.2f);
    rotateTool.addControl(new RotationToolControl(this));

    scaleTool = (Node) assetManager.loadModel("graphics/models/manipulators/manipulators_scale.j3o");
    scaleTool.getChild("scale_x").setMaterial(redMaterial);
    scaleTool.getChild("collision_scale_x").setMaterial(redMaterial);
    scaleTool.getChild("collision_scale_x").setCullHint(Spatial.CullHint.Always);
    scaleTool.getChild("scale_y").setMaterial(blueMaterial);
    scaleTool.getChild("collision_scale_y").setMaterial(blueMaterial);
    scaleTool.getChild("collision_scale_y").setCullHint(Spatial.CullHint.Always);
    scaleTool.getChild("scale_z").setMaterial(greenMaterial);
    scaleTool.getChild("collision_scale_z").setMaterial(greenMaterial);
    scaleTool.getChild("collision_scale_z").setCullHint(Spatial.CullHint.Always);
    scaleTool.scale(0.2f);
    scaleTool.addControl(new ScaleToolControl(this));
}
 
Example 6
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);
}