Search in sources :

Example 21 with AssetKey

use of com.jme3.asset.AssetKey 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 22 with AssetKey

use of com.jme3.asset.AssetKey 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 23 with AssetKey

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

the class MaterialLoader method load.

private MaterialList load(AssetManager assetManager, AssetKey key, InputStream in) throws IOException {
    folderName = key.getFolder();
    this.assetManager = assetManager;
    MaterialList list = null;
    List<Statement> statements = BlockLanguageParser.parse(in);
    for (Statement statement : statements) {
        if (statement.getLine().startsWith("import")) {
            MaterialExtensionSet matExts = null;
            if (key instanceof OgreMaterialKey) {
                matExts = ((OgreMaterialKey) key).getMaterialExtensionSet();
            }
            if (matExts == null) {
                throw new IOException("Must specify MaterialExtensionSet when loading\n" + "Ogre3D materials with extended materials");
            }
            list = new MaterialExtensionLoader().load(assetManager, key, matExts, statements);
            break;
        } else if (statement.getLine().startsWith("material")) {
            if (list == null) {
                list = new MaterialList();
            }
            String[] split = statement.getLine().split(" ", 2);
            matName = split[1].trim();
            if (matName.startsWith("\"") && matName.endsWith("\"")) {
                matName = matName.substring(1, matName.length() - 1);
            }
            readMaterial(statement);
            Material mat = compileMaterial();
            list.put(mat.getName(), mat);
        }
    }
    return list;
}
Also used : MaterialExtensionLoader(com.jme3.scene.plugins.ogre.matext.MaterialExtensionLoader) Statement(com.jme3.util.blockparser.Statement) MaterialList(com.jme3.material.MaterialList) OgreMaterialKey(com.jme3.scene.plugins.ogre.matext.OgreMaterialKey) Material(com.jme3.material.Material) IOException(java.io.IOException) MaterialExtensionSet(com.jme3.scene.plugins.ogre.matext.MaterialExtensionSet)

Example 24 with AssetKey

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

the class TestMaterialDefWrite method testWriteMat.

@Test
public void testWriteMat() throws Exception {
    Material mat = new Material(assetManager, "example.j3md");
    final ByteArrayOutputStream stream = new ByteArrayOutputStream();
    J3mdExporter exporter = new J3mdExporter();
    try {
        exporter.save(mat.getMaterialDef(), stream);
    } catch (IOException e) {
        e.printStackTrace();
    }
    //   System.err.println(stream.toString());
    J3MLoader loader = new J3MLoader();
    AssetInfo info = new AssetInfo(assetManager, new AssetKey("test")) {

        @Override
        public InputStream openStream() {
            return new ByteArrayInputStream(stream.toByteArray());
        }
    };
    MaterialDef matDef = (MaterialDef) loader.load(info);
    MaterialDef ref = mat.getMaterialDef();
    for (MatParam refParam : ref.getMaterialParams()) {
        MatParam matParam = matDef.getMaterialParam(refParam.getName());
        assertTrue(refParam != null);
        assertEquals(refParam, matParam);
    }
    for (String key : ref.getTechniqueDefsNames()) {
        List<TechniqueDef> refDefs = ref.getTechniqueDefs(key);
        List<TechniqueDef> defs = matDef.getTechniqueDefs(key);
        assertNotNull(defs);
        assertTrue(refDefs.size() == defs.size());
        for (int i = 0; i < refDefs.size(); i++) {
            assertEqualTechniqueDefs(refDefs.get(i), defs.get(i));
        }
    }
}
Also used : J3MLoader(com.jme3.material.plugins.J3MLoader) J3mdExporter(com.jme3.material.plugin.export.materialdef.J3mdExporter)

Aggregations

AssetKey (com.jme3.asset.AssetKey)8 IOException (java.io.IOException)7 InputStream (java.io.InputStream)5 AssetInfo (com.jme3.asset.AssetInfo)4 AssetLoadException (com.jme3.asset.AssetLoadException)4 ModelKey (com.jme3.asset.ModelKey)4 Material (com.jme3.material.Material)4 Statement (com.jme3.util.blockparser.Statement)4 AssetNotFoundException (com.jme3.asset.AssetNotFoundException)3 AssetCache (com.jme3.asset.cache.AssetCache)3 Texture (com.jme3.texture.Texture)3 BufferedReader (java.io.BufferedReader)3 ByteArrayInputStream (java.io.ByteArrayInputStream)3 InputStreamReader (java.io.InputStreamReader)3 TextureKey (com.jme3.asset.TextureKey)2 MaterialList (com.jme3.material.MaterialList)2 J3MLoader (com.jme3.material.plugins.J3MLoader)2 Node (com.jme3.scene.Node)2 Spatial (com.jme3.scene.Spatial)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2