Search in sources :

Example 31 with AssetInfo

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

the class TestMusicPlayer method btnOpenActionPerformed.

// </editor-fold>//GEN-END:initComponents
private void btnOpenActionPerformed(java.awt.event.ActionEvent evt) {
    //GEN-FIRST:event_btnOpenActionPerformed
    JFileChooser chooser = new JFileChooser();
    chooser.setAcceptAllFileFilterUsed(true);
    chooser.setDialogTitle("Select OGG file");
    chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
    chooser.setMultiSelectionEnabled(false);
    if (chooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
        btnStopActionPerformed(null);
        final File selected = chooser.getSelectedFile();
        AssetLoader loader = null;
        if (selected.getName().endsWith(".wav")) {
            loader = new WAVLoader();
        } else {
            loader = new OGGLoader();
        }
        AudioKey key = new AudioKey(selected.getName(), true, true);
        try {
            musicData = (AudioData) loader.load(new AssetInfo(null, key) {

                @Override
                public InputStream openStream() {
                    try {
                        return new FileInputStream(selected);
                    } catch (FileNotFoundException ex) {
                        ex.printStackTrace();
                    }
                    return null;
                }
            });
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        musicSource = new AudioNode(musicData, key);
        musicLength = musicData.getDuration();
        updateTime();
    }
}
Also used : OGGLoader(com.jme3.audio.plugins.OGGLoader) AssetInfo(com.jme3.asset.AssetInfo) AssetLoader(com.jme3.asset.AssetLoader) JFileChooser(javax.swing.JFileChooser) WAVLoader(com.jme3.audio.plugins.WAVLoader)

Example 32 with AssetInfo

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

the class FbxLoader method load.

@Override
public Object load(AssetInfo assetInfo) throws IOException {
    this.assetManager = assetInfo.getManager();
    AssetKey<?> assetKey = assetInfo.getKey();
    if (!(assetKey instanceof ModelKey)) {
        throw new AssetLoadException("Invalid asset key");
    }
    InputStream stream = assetInfo.openStream();
    try {
        sceneFilename = assetKey.getName();
        sceneFolderName = assetKey.getFolder();
        String ext = assetKey.getExtension();
        sceneName = sceneFilename.substring(0, sceneFilename.length() - ext.length() - 1);
        if (sceneFolderName != null && sceneFolderName.length() > 0) {
            sceneName = sceneName.substring(sceneFolderName.length());
        }
        reset();
        // Load the data from the stream.
        loadData(stream);
        // Bind poses are needed to compute world transforms.
        applyBindPoses();
        // Need world transforms for skeleton creation.
        updateWorldTransforms();
        // Need skeletons for meshs to be created in scene graph construction.
        // Mesh bone indices require skeletons to determine bone index.
        constructSkeletons();
        // Create the jME3 scene graph from the FBX scene graph.
        // Also creates SkeletonControls based on the constructed skeletons.
        Spatial scene = constructSceneGraph();
        // Load animations into AnimControls
        constructAnimations();
        return scene;
    } finally {
        releaseObjects();
        if (stream != null) {
            stream.close();
        }
    }
}
Also used : ModelKey(com.jme3.asset.ModelKey) Spatial(com.jme3.scene.Spatial) InputStream(java.io.InputStream) AssetLoadException(com.jme3.asset.AssetLoadException)

Example 33 with AssetInfo

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

the class SceneLoader method load.

@Override
public Object load(AssetInfo assetInfo) throws IOException {
    this.currentAssetInfo = assetInfo;
    this.assetManager = assetInfo.getManager();
    AssetKey<?> assetKey = assetInfo.getKey();
    if (assetKey instanceof SceneKey)
        animList = ((SceneKey) assetKey).getAnimations();
    InputStream stream = assetInfo.openStream();
    final Node sceneNode = this.sceneNode = new Node(sceneName + "-scene");
    try {
        sceneFilename = assetKey.getName();
        sceneFolderName = assetKey.getFolder();
        String ext = assetKey.getExtension();
        sceneName = sceneFilename.substring(0, sceneFilename.length() - ext.length() - 1);
        if (sceneFolderName != null && sceneFolderName.length() > 0)
            sceneName = sceneName.substring(sceneFolderName.length());
        loadScene(stream);
        linkScene();
        if (warnings.size() > 0)
            logger.log(Level.WARNING, "Model load finished with warnings:\n" + join(warnings, "\n"));
    } finally {
        releaseObjects();
        if (stream != null)
            stream.close();
    }
    return sceneNode;
}
Also used : InputStream(java.io.InputStream) Node(com.jme3.scene.Node) FbxAnimNode(com.jme3.scene.plugins.fbx.objects.FbxAnimNode) FbxNode(com.jme3.scene.plugins.fbx.objects.FbxNode)

Example 34 with AssetInfo

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

the class MeshLoader method load.

public Object load(AssetInfo info) throws IOException {
    try {
        key = info.getKey();
        meshName = key.getName();
        folderName = key.getFolder();
        String ext = key.getExtension();
        meshName = meshName.substring(0, meshName.length() - ext.length() - 1);
        if (folderName != null && folderName.length() > 0) {
            meshName = meshName.substring(folderName.length());
        }
        assetManager = info.getManager();
        if (key instanceof OgreMeshKey) {
            // OgreMeshKey is being used, try getting the material list
            // from it
            OgreMeshKey meshKey = (OgreMeshKey) key;
            materialList = meshKey.getMaterialList();
            String materialName = meshKey.getMaterialName();
            // Material list not set but material name is available
            if (materialList == null && materialName != null) {
                OgreMaterialKey materialKey = new OgreMaterialKey(folderName + materialName + ".material");
                try {
                    materialList = (MaterialList) assetManager.loadAsset(materialKey);
                } catch (AssetNotFoundException e) {
                    logger.log(Level.WARNING, "Cannot locate {0} for model {1}", new Object[] { materialKey, key });
                }
            }
        } else {
            // Make sure to reset it to null so that previous state
            // doesn't leak onto this one
            materialList = null;
        }
        // default method.
        if (materialList == null) {
            OgreMaterialKey materialKey = new OgreMaterialKey(folderName + meshName + ".material");
            try {
                materialList = (MaterialList) assetManager.loadAsset(materialKey);
            } catch (AssetNotFoundException e) {
                logger.log(Level.WARNING, "Cannot locate {0} for model {1}", new Object[] { materialKey, key });
            }
        }
        // Added by larynx 25.06.2011
        // Android needs the namespace aware flag set to true                 
        // Kirill 30.06.2011
        // Now, hack is applied for both desktop and android to avoid
        // checking with JmeSystem.
        SAXParserFactory factory = SAXParserFactory.newInstance();
        factory.setNamespaceAware(true);
        XMLReader xr = factory.newSAXParser().getXMLReader();
        xr.setContentHandler(this);
        xr.setErrorHandler(this);
        InputStreamReader r = null;
        try {
            r = new InputStreamReader(info.openStream());
            xr.parse(new InputSource(r));
        } finally {
            if (r != null) {
                r.close();
            }
        }
        return compileModel();
    } catch (SAXException ex) {
        IOException ioEx = new IOException("Error while parsing Ogre3D mesh.xml");
        ioEx.initCause(ex);
        throw ioEx;
    } catch (ParserConfigurationException ex) {
        IOException ioEx = new IOException("Error while parsing Ogre3D mesh.xml");
        ioEx.initCause(ex);
        throw ioEx;
    }
}
Also used : InputSource(org.xml.sax.InputSource) InputStreamReader(java.io.InputStreamReader) OgreMaterialKey(com.jme3.scene.plugins.ogre.matext.OgreMaterialKey) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) XMLReader(org.xml.sax.XMLReader) SAXParserFactory(javax.xml.parsers.SAXParserFactory) SAXException(org.xml.sax.SAXException)

Example 35 with AssetInfo

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

the class SceneLoader method load.

public Object load(AssetInfo info) throws IOException {
    try {
        key = info.getKey();
        assetManager = info.getManager();
        sceneName = key.getName();
        String ext = key.getExtension();
        folderName = key.getFolder();
        sceneName = sceneName.substring(0, sceneName.length() - ext.length() - 1);
        reset();
        // == Run 1st pass over XML file to determine material list ==
        materialList = materialLoader.load(assetManager, folderName, info.openStream());
        if (materialList == null || materialList.isEmpty()) {
            // NOTE: No materials were found by searching the externals section.
            // Try finding a similarly named material file in the same folder.
            // (Backward compatibility only!)
            OgreMaterialKey materialKey = new OgreMaterialKey(sceneName + ".material");
            try {
                materialList = (MaterialList) assetManager.loadAsset(materialKey);
            } catch (AssetNotFoundException ex) {
                logger.log(Level.WARNING, "Cannot locate {0} for scene {1}", new Object[] { materialKey, key });
                materialList = null;
            }
        }
        // == Run 2nd pass to load entities and other objects ==
        // Added by larynx 25.06.2011
        // Android needs the namespace aware flag set to true 
        // Kirill 30.06.2011
        // Now, hack is applied for both desktop and android to avoid
        // checking with JmeSystem.
        SAXParserFactory factory = SAXParserFactory.newInstance();
        factory.setNamespaceAware(true);
        XMLReader xr = factory.newSAXParser().getXMLReader();
        xr.setContentHandler(this);
        xr.setErrorHandler(this);
        InputStreamReader r = null;
        try {
            r = new InputStreamReader(info.openStream());
            xr.parse(new InputSource(r));
        } finally {
            if (r != null) {
                r.close();
            }
        }
        return root;
    } catch (SAXException ex) {
        IOException ioEx = new IOException("Error while parsing Ogre3D dotScene");
        ioEx.initCause(ex);
        throw ioEx;
    } catch (ParserConfigurationException ex) {
        IOException ioEx = new IOException("Error while parsing Ogre3D dotScene");
        ioEx.initCause(ex);
        throw ioEx;
    }
}
Also used : InputSource(org.xml.sax.InputSource) InputStreamReader(java.io.InputStreamReader) OgreMaterialKey(com.jme3.scene.plugins.ogre.matext.OgreMaterialKey) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) XMLReader(org.xml.sax.XMLReader) SAXParserFactory(javax.xml.parsers.SAXParserFactory) SAXException(org.xml.sax.SAXException)

Aggregations

InputStream (java.io.InputStream)17 TextureKey (com.jme3.asset.TextureKey)12 IOException (java.io.IOException)9 Image (com.jme3.texture.Image)7 AssetInfo (com.jme3.asset.AssetInfo)6 Texture (com.jme3.texture.Texture)6 AssetLoadException (com.jme3.asset.AssetLoadException)5 Test (org.junit.Test)5 AssetKey (com.jme3.asset.AssetKey)4 AssetNotFoundException (com.jme3.asset.AssetNotFoundException)4 ModelKey (com.jme3.asset.ModelKey)4 InputStreamReader (java.io.InputStreamReader)4 BlenderKey (com.jme3.asset.BlenderKey)3 AudioStream (com.jme3.audio.AudioStream)3 MaterialDef (com.jme3.material.MaterialDef)3 DataInputStream (java.io.DataInputStream)3 AssetFileDescriptor (android.content.res.AssetFileDescriptor)2 AssetManager (com.jme3.asset.AssetManager)2 AndroidAssetInfo (com.jme3.asset.plugins.AndroidLocator.AndroidAssetInfo)2 AudioBuffer (com.jme3.audio.AudioBuffer)2