Search in sources :

Example 1 with ModelKey

use of com.jme3.asset.ModelKey in project jmonkeyengine by jMonkeyEngine.

the class SceneWithAnimationLoader method load.

@Override
public Object load(AssetInfo assetInfo) throws IOException {
    AssetKey<?> key = assetInfo.getKey();
    if (!(key instanceof ModelKey))
        throw new AssetLoadException("Invalid asset key");
    InputStream stream = assetInfo.openStream();
    Scanner scanner = new Scanner(stream);
    AnimationList animList = new AnimationList();
    String modelName = null;
    try {
        while (scanner.hasNextLine()) {
            String line = scanner.nextLine();
            if (line.startsWith("#"))
                continue;
            if (modelName == null) {
                modelName = line;
                continue;
            }
            String[] split = split(line);
            if (split.length < 3)
                throw new IOException("Unparseable string \"" + line + "\"");
            int start;
            int end;
            try {
                start = Integer.parseInt(split[0]);
                end = Integer.parseInt(split[1]);
            } catch (NumberFormatException e) {
                throw new IOException("Unparseable string \"" + line + "\"", e);
            }
            animList.add(split[2], split.length > 3 ? split[3] : null, start, end);
        }
    } finally {
        scanner.close();
        stream.close();
    }
    return assetInfo.getManager().loadAsset(new SceneKey(key.getFolder() + modelName, animList));
}
Also used : Scanner(java.util.Scanner) ModelKey(com.jme3.asset.ModelKey) InputStream(java.io.InputStream) IOException(java.io.IOException) AssetLoadException(com.jme3.asset.AssetLoadException)

Example 2 with ModelKey

use of com.jme3.asset.ModelKey in project jmonkeyengine by jMonkeyEngine.

the class AssetLinkNode method attachLinkedChildren.

/**
     * Loads the linked children AssetKeys from the AssetManager and attaches them to the Node<br>
     * If they are already attached, they will be reloaded.
     * @param manager
     */
public void attachLinkedChildren(AssetManager manager) {
    detachLinkedChildren();
    for (Iterator<ModelKey> it = assetLoaderKeys.iterator(); it.hasNext(); ) {
        ModelKey assetKey = it.next();
        Spatial curChild = assetChildren.get(assetKey);
        if (curChild != null) {
            curChild.removeFromParent();
        }
        Spatial child = manager.loadAsset(assetKey);
        attachChild(child);
        assetChildren.put(assetKey, child);
    }
}
Also used : ModelKey(com.jme3.asset.ModelKey)

Example 3 with ModelKey

use of com.jme3.asset.ModelKey in project jmonkeyengine by jMonkeyEngine.

the class AssetLinkNode method read.

@Override
public void read(JmeImporter e) throws IOException {
    super.read(e);
    InputCapsule capsule = e.getCapsule(this);
    BinaryImporter importer = BinaryImporter.getInstance();
    AssetManager loaderManager = e.getAssetManager();
    assetLoaderKeys = (ArrayList<ModelKey>) capsule.readSavableArrayList("assetLoaderKeyList", new ArrayList<ModelKey>());
    for (Iterator<ModelKey> it = assetLoaderKeys.iterator(); it.hasNext(); ) {
        ModelKey modelKey = it.next();
        AssetInfo info = loaderManager.locateAsset(modelKey);
        Spatial child = null;
        if (info != null) {
            child = (Spatial) importer.load(info);
        }
        if (child != null) {
            child.parent = this;
            children.add(child);
            assetChildren.put(modelKey, child);
        } else {
            Logger.getLogger(this.getClass().getName()).log(Level.WARNING, "Cannot locate {0} for asset link node {1}", new Object[] { modelKey, key });
        }
    }
}
Also used : BinaryImporter(com.jme3.export.binary.BinaryImporter) ModelKey(com.jme3.asset.ModelKey) AssetManager(com.jme3.asset.AssetManager) InputCapsule(com.jme3.export.InputCapsule) AssetInfo(com.jme3.asset.AssetInfo)

Example 4 with ModelKey

use of com.jme3.asset.ModelKey in project jmonkeyengine by jMonkeyEngine.

the class BlenderLoader method setup.

/**
     * This method sets up the loader.
     * @param assetInfo
     *            the asset info
     * @throws BlenderFileException
     *             an exception is throw when something wrong happens with blender file
     */
protected BlenderContext setup(AssetInfo assetInfo) throws BlenderFileException {
    // registering loaders
    ModelKey modelKey = (ModelKey) assetInfo.getKey();
    BlenderKey blenderKey;
    if (modelKey instanceof BlenderKey) {
        blenderKey = (BlenderKey) modelKey;
    } else {
        blenderKey = new BlenderKey(modelKey.getName());
    }
    // opening stream
    BlenderInputStream inputStream = new BlenderInputStream(assetInfo.openStream());
    // reading blocks
    List<FileBlockHeader> blocks = new ArrayList<FileBlockHeader>();
    FileBlockHeader fileBlock;
    BlenderContext blenderContext = new BlenderContext();
    blenderContext.setBlenderVersion(inputStream.getVersionNumber());
    blenderContext.setAssetManager(assetInfo.getManager());
    blenderContext.setInputStream(inputStream);
    blenderContext.setBlenderKey(blenderKey);
    // creating helpers
    blenderContext.putHelper(AnimationHelper.class, new AnimationHelper(inputStream.getVersionNumber(), blenderContext));
    blenderContext.putHelper(TextureHelper.class, new TextureHelper(inputStream.getVersionNumber(), blenderContext));
    blenderContext.putHelper(MeshHelper.class, new MeshHelper(inputStream.getVersionNumber(), blenderContext));
    blenderContext.putHelper(ObjectHelper.class, new ObjectHelper(inputStream.getVersionNumber(), blenderContext));
    blenderContext.putHelper(CurvesHelper.class, new CurvesHelper(inputStream.getVersionNumber(), blenderContext));
    blenderContext.putHelper(LightHelper.class, new LightHelper(inputStream.getVersionNumber(), blenderContext));
    blenderContext.putHelper(CameraHelper.class, new CameraHelper(inputStream.getVersionNumber(), blenderContext));
    blenderContext.putHelper(ModifierHelper.class, new ModifierHelper(inputStream.getVersionNumber(), blenderContext));
    blenderContext.putHelper(MaterialHelper.class, new MaterialHelper(inputStream.getVersionNumber(), blenderContext));
    blenderContext.putHelper(ConstraintHelper.class, new ConstraintHelper(inputStream.getVersionNumber(), blenderContext));
    blenderContext.putHelper(ParticlesHelper.class, new ParticlesHelper(inputStream.getVersionNumber(), blenderContext));
    blenderContext.putHelper(LandscapeHelper.class, new LandscapeHelper(inputStream.getVersionNumber(), blenderContext));
    // reading the blocks (dna block is automatically saved in the blender context when found)
    FileBlockHeader sceneFileBlock = null;
    do {
        fileBlock = new FileBlockHeader(inputStream, blenderContext);
        if (!fileBlock.isDnaBlock()) {
            blocks.add(fileBlock);
            // save the scene's file block
            if (fileBlock.getCode() == BlockCode.BLOCK_SC00) {
                sceneFileBlock = fileBlock;
            }
        }
    } while (!fileBlock.isLastBlock());
    if (sceneFileBlock != null) {
        blenderContext.setSceneStructure(sceneFileBlock.getStructure(blenderContext));
    }
    // adding locator for linked content
    assetInfo.getManager().registerLocator(assetInfo.getKey().getName(), LinkedContentLocator.class);
    return blenderContext;
}
Also used : ObjectHelper(com.jme3.scene.plugins.blender.objects.ObjectHelper) AnimationHelper(com.jme3.scene.plugins.blender.animations.AnimationHelper) ModelKey(com.jme3.asset.ModelKey) FileBlockHeader(com.jme3.scene.plugins.blender.file.FileBlockHeader) ArrayList(java.util.ArrayList) ConstraintHelper(com.jme3.scene.plugins.blender.constraints.ConstraintHelper) ModifierHelper(com.jme3.scene.plugins.blender.modifiers.ModifierHelper) LandscapeHelper(com.jme3.scene.plugins.blender.landscape.LandscapeHelper) CurvesHelper(com.jme3.scene.plugins.blender.curves.CurvesHelper) TextureHelper(com.jme3.scene.plugins.blender.textures.TextureHelper) MaterialHelper(com.jme3.scene.plugins.blender.materials.MaterialHelper) BlenderKey(com.jme3.asset.BlenderKey) BlenderInputStream(com.jme3.scene.plugins.blender.file.BlenderInputStream) ParticlesHelper(com.jme3.scene.plugins.blender.particles.ParticlesHelper) LightHelper(com.jme3.scene.plugins.blender.lights.LightHelper) MeshHelper(com.jme3.scene.plugins.blender.meshes.MeshHelper) CameraHelper(com.jme3.scene.plugins.blender.cameras.CameraHelper)

Example 5 with ModelKey

use of com.jme3.asset.ModelKey in project jmonkeyengine by jMonkeyEngine.

the class TestAssetLinkNode method simpleInitApp.

@Override
public void simpleInitApp() {
    AssetLinkNode loaderNode = new AssetLinkNode();
    loaderNode.addLinkedChild(new ModelKey("Models/MonkeyHead/MonkeyHead.mesh.xml"));
    //save and load the loaderNode
    try {
        //export to byte array
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        BinaryExporter.getInstance().save(loaderNode, bout);
        //import from byte array, automatically loads the monkeyhead from file
        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
        BinaryImporter imp = BinaryImporter.getInstance();
        imp.setAssetManager(assetManager);
        Node newLoaderNode = (Node) imp.load(bin);
        //attach to rootNode
        rootNode.attachChild(newLoaderNode);
    } catch (IOException ex) {
        Logger.getLogger(TestAssetLinkNode.class.getName()).log(Level.SEVERE, null, ex);
    }
    rootNode.attachChild(loaderNode);
    lightMdl = new Geometry("Light", new Sphere(10, 10, 0.1f));
    lightMdl.setMaterial((Material) assetManager.loadAsset(new AssetKey("Common/Materials/RedColor.j3m")));
    rootNode.attachChild(lightMdl);
    // flourescent main light
    pl = new PointLight();
    pl.setColor(new ColorRGBA(0.88f, 0.92f, 0.95f, 1.0f));
    rootNode.addLight(pl);
    // sunset light
    DirectionalLight dl = new DirectionalLight();
    dl.setDirection(new Vector3f(-0.1f, -0.7f, 1).normalizeLocal());
    dl.setColor(new ColorRGBA(0.44f, 0.30f, 0.20f, 1.0f));
    rootNode.addLight(dl);
    // skylight
    dl = new DirectionalLight();
    dl.setDirection(new Vector3f(-0.6f, -1, -0.6f).normalizeLocal());
    dl.setColor(new ColorRGBA(0.10f, 0.22f, 0.44f, 1.0f));
    rootNode.addLight(dl);
    // white ambient light
    dl = new DirectionalLight();
    dl.setDirection(new Vector3f(1, -0.5f, -0.1f).normalizeLocal());
    dl.setColor(new ColorRGBA(0.50f, 0.40f, 0.50f, 1.0f));
    rootNode.addLight(dl);
}
Also used : ModelKey(com.jme3.asset.ModelKey) AssetLinkNode(com.jme3.scene.AssetLinkNode) Node(com.jme3.scene.Node) AssetLinkNode(com.jme3.scene.AssetLinkNode) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) Geometry(com.jme3.scene.Geometry) Sphere(com.jme3.scene.shape.Sphere) BinaryImporter(com.jme3.export.binary.BinaryImporter) AssetKey(com.jme3.asset.AssetKey) ColorRGBA(com.jme3.math.ColorRGBA) ByteArrayInputStream(java.io.ByteArrayInputStream) DirectionalLight(com.jme3.light.DirectionalLight) Vector3f(com.jme3.math.Vector3f) PointLight(com.jme3.light.PointLight)

Aggregations

ModelKey (com.jme3.asset.ModelKey)6 AssetLoadException (com.jme3.asset.AssetLoadException)2 BinaryImporter (com.jme3.export.binary.BinaryImporter)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 AssetInfo (com.jme3.asset.AssetInfo)1 AssetKey (com.jme3.asset.AssetKey)1 AssetManager (com.jme3.asset.AssetManager)1 BlenderKey (com.jme3.asset.BlenderKey)1 InputCapsule (com.jme3.export.InputCapsule)1 DirectionalLight (com.jme3.light.DirectionalLight)1 PointLight (com.jme3.light.PointLight)1 ColorRGBA (com.jme3.math.ColorRGBA)1 Vector3f (com.jme3.math.Vector3f)1 AssetLinkNode (com.jme3.scene.AssetLinkNode)1 Geometry (com.jme3.scene.Geometry)1 Node (com.jme3.scene.Node)1 Spatial (com.jme3.scene.Spatial)1 AnimationHelper (com.jme3.scene.plugins.blender.animations.AnimationHelper)1 CameraHelper (com.jme3.scene.plugins.blender.cameras.CameraHelper)1