Search in sources :

Example 6 with AssetLoadException

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

the class AWTLoader method load.

public Object load(AssetInfo info) throws IOException {
    if (ImageIO.getImageReadersBySuffix(info.getKey().getExtension()) != null) {
        boolean flip = ((TextureKey) info.getKey()).isFlipY();
        InputStream in = null;
        try {
            in = info.openStream();
            Image img = load(in, flip);
            if (img == null) {
                throw new AssetLoadException("The given image cannot be loaded " + info.getKey());
            }
            return img;
        } finally {
            if (in != null) {
                in.close();
            }
        }
    } else {
        throw new AssetLoadException("The extension " + info.getKey().getExtension() + " is not supported");
    }
}
Also used : TextureKey(com.jme3.asset.TextureKey) InputStream(java.io.InputStream) Image(com.jme3.texture.Image) AssetLoadException(com.jme3.asset.AssetLoadException)

Example 7 with AssetLoadException

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

the class GLSLLoader method loadNode.

/**
     * Creates a {@link ShaderDependencyNode} from a stream representing shader code.
     * 
     * @param in The input stream containing shader code
     * @param nodeName
     * @return
     * @throws IOException 
     */
private ShaderDependencyNode loadNode(Reader reader, String nodeName) {
    ShaderDependencyNode node = new ShaderDependencyNode(nodeName);
    StringBuilder sb = new StringBuilder();
    StringBuilder sbExt = new StringBuilder();
    BufferedReader bufReader = null;
    try {
        bufReader = new BufferedReader(reader);
        String ln;
        if (!nodeName.equals("[main]")) {
            sb.append("// -- begin import ").append(nodeName).append(" --\n");
        }
        while ((ln = bufReader.readLine()) != null) {
            if (ln.trim().startsWith("#import ")) {
                ln = ln.trim().substring(8).trim();
                if (ln.startsWith("\"") && ln.endsWith("\"") && ln.length() > 3) {
                    // import user code
                    // remove quotes to get filename
                    ln = ln.substring(1, ln.length() - 1);
                    if (ln.equals(nodeName)) {
                        throw new IOException("Node depends on itself.");
                    }
                    // check cache first
                    ShaderDependencyNode dependNode = dependCache.get(ln);
                    if (dependNode == null) {
                        Reader dependNodeReader = assetManager.loadAsset(new ShaderDependencyKey(ln));
                        dependNode = loadNode(dependNodeReader, ln);
                    }
                    node.addDependency(sb.length(), dependNode);
                }
            } else if (ln.trim().startsWith("#extension ")) {
                sbExt.append(ln).append('\n');
            } else {
                sb.append(ln).append('\n');
            }
        }
        if (!nodeName.equals("[main]")) {
            sb.append("// -- end import ").append(nodeName).append(" --\n");
        }
    } catch (IOException ex) {
        if (bufReader != null) {
            try {
                bufReader.close();
            } catch (IOException ex1) {
            }
        }
        throw new AssetLoadException("Failed to load shader node: " + nodeName, ex);
    }
    node.setSource(sb.toString());
    node.setExtensions(sbExt.toString());
    dependCache.put(nodeName, node);
    return node;
}
Also used : BufferedReader(java.io.BufferedReader) BufferedReader(java.io.BufferedReader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) IOException(java.io.IOException) AssetLoadException(com.jme3.asset.AssetLoadException)

Example 8 with AssetLoadException

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

the class UrlAssetInfo method openStream.

@Override
public InputStream openStream() {
    if (in != null) {
        // Reuse the already existing stream (only once)
        InputStream in2 = in;
        in = null;
        return in2;
    } else {
        // Create a new stream for subsequent invocations.
        try {
            URLConnection conn = url.openConnection();
            conn.setUseCaches(false);
            return conn.getInputStream();
        } catch (IOException ex) {
            throw new AssetLoadException("Failed to read URL " + url, ex);
        }
    }
}
Also used : InputStream(java.io.InputStream) IOException(java.io.IOException) URLConnection(java.net.URLConnection) AssetLoadException(com.jme3.asset.AssetLoadException)

Example 9 with AssetLoadException

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

the class J3MLoader method loadFromRoot.

private void loadFromRoot(List<Statement> roots) throws IOException {
    if (roots.size() == 2) {
        Statement exception = roots.get(0);
        String line = exception.getLine();
        if (line.startsWith("Exception")) {
            throw new AssetLoadException(line.substring("Exception ".length()));
        } else {
            throw new IOException("In multiroot material, expected first statement to be 'Exception'");
        }
    } else if (roots.size() != 1) {
        throw new IOException("Too many roots in J3M/J3MD file");
    }
    boolean extending = false;
    Statement materialStat = roots.get(0);
    String materialName = materialStat.getLine();
    if (materialName.startsWith("MaterialDef")) {
        materialName = materialName.substring("MaterialDef ".length()).trim();
        extending = false;
    } else if (materialName.startsWith("Material")) {
        materialName = materialName.substring("Material ".length()).trim();
        extending = true;
    } else {
        throw new IOException("Specified file is not a Material file");
    }
    String[] split = materialName.split(":", 2);
    if (materialName.equals("")) {
        throw new MatParseException("Material name cannot be empty", materialStat);
    }
    if (split.length == 2) {
        if (!extending) {
            throw new MatParseException("Must use 'Material' when extending.", materialStat);
        }
        String extendedMat = split[1].trim();
        MaterialDef def = (MaterialDef) assetManager.loadAsset(new AssetKey(extendedMat));
        if (def == null) {
            throw new MatParseException("Extended material " + extendedMat + " cannot be found.", materialStat);
        }
        material = new Material(def);
        material.setKey(key);
        material.setName(split[0].trim());
    //            material.setAssetName(fileName);
    } else if (split.length == 1) {
        if (extending) {
            throw new MatParseException("Expected ':', got '{'", materialStat);
        }
        materialDef = new MaterialDef(assetManager, materialName);
        // NOTE: pass file name for defs so they can be loaded later
        materialDef.setAssetName(key.getName());
    } else {
        throw new MatParseException("Cannot use colon in material name/path", materialStat);
    }
    for (Statement statement : materialStat.getContents()) {
        split = statement.getLine().split("[ \\{]");
        String statType = split[0];
        if (extending) {
            if (statType.equals("MaterialParameters")) {
                readExtendingMaterialParams(statement.getContents());
            } else if (statType.equals("AdditionalRenderState")) {
                readAdditionalRenderState(statement.getContents());
            } else if (statType.equals("Transparent")) {
                readTransparentStatement(statement.getLine());
            }
        } else {
            if (statType.equals("Technique")) {
                readTechnique(statement);
            } else if (statType.equals("MaterialParameters")) {
                readMaterialParams(statement.getContents());
            } else {
                throw new MatParseException("Expected material statement, got '" + statType + "'", statement);
            }
        }
    }
}
Also used : Statement(com.jme3.util.blockparser.Statement) IOException(java.io.IOException)

Example 10 with AssetLoadException

use of com.jme3.asset.AssetLoadException 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)

Aggregations

AssetLoadException (com.jme3.asset.AssetLoadException)10 IOException (java.io.IOException)6 InputStream (java.io.InputStream)6 TextureKey (com.jme3.asset.TextureKey)3 ModelKey (com.jme3.asset.ModelKey)2 VertexBuffer (com.jme3.scene.VertexBuffer)2 Image (com.jme3.texture.Image)2 Statement (com.jme3.util.blockparser.Statement)2 FloatBuffer (java.nio.FloatBuffer)2 ArrayList (java.util.ArrayList)2 AssetInfo (com.jme3.asset.AssetInfo)1 AssetKey (com.jme3.asset.AssetKey)1 AssetManager (com.jme3.asset.AssetManager)1 AssetNotFoundException (com.jme3.asset.AssetNotFoundException)1 BlenderKey (com.jme3.asset.BlenderKey)1 GeneratedTextureKey (com.jme3.asset.GeneratedTextureKey)1 ShaderNodeDefinitionKey (com.jme3.asset.ShaderNodeDefinitionKey)1 Geometry (com.jme3.scene.Geometry)1 Mesh (com.jme3.scene.Mesh)1 Spatial (com.jme3.scene.Spatial)1