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

The following examples show how to use com.jme3.asset.AssetManager#locateAsset() . 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: Context.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private void buildSourcesRec(BufferedReader reader, StringBuilder builder, AssetManager assetManager) throws IOException {
    String ln;
    while ((ln = reader.readLine()) != null) {
        if (ln.trim().startsWith("#import ")) {
            ln = ln.trim().substring(8).trim();
            if (ln.startsWith("\"")) {
                ln = ln.substring(1);
            }
            if (ln.endsWith("\"")) {
                ln = ln.substring(0, ln.length() - 1);
            }
            AssetInfo info = assetManager.locateAsset(new AssetKey<String>(ln));
            if (info == null) {
                throw new AssetNotFoundException("Unable to load source file \"" + ln + "\"");
            }
            try (BufferedReader r = new BufferedReader(new InputStreamReader(info.openStream()))) {
                builder.append("//-- begin import ").append(ln).append(" --\n");
                buildSourcesRec(r, builder, assetManager);
                builder.append("//-- end import ").append(ln).append(" --\n");
            }
        } else {
            builder.append(ln).append('\n');
        }
    }
}
 
Example 2
Source File: Context.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Creates a program object from the provided source code and files.
 * The source code is made up from the specified include string first,
 * then all files specified by the resource array (array of asset paths)
 * are loaded by the provided asset manager and appended to the source code.
 * <p>
 * The typical use case is:
 * <ul>
 *  <li>The include string contains some compiler constants like the grid size </li>
 *  <li>Some common OpenCL files used as libraries (Convention: file names end with {@code .clh}</li>
 *  <li>One main OpenCL file containing the actual kernels (Convention: file name ends with {@code .cl})</li>
 * </ul>
 *
 * After the files were combined, additional include statements are resolved
 * by {@link #createProgramFromSourceCodeWithDependencies(java.lang.String, com.jme3.asset.AssetManager) }.
 *
 * @param assetManager the asset manager used to load the files
 * @param include an additional include string
 * @param resources an array of asset paths pointing to OpenCL source files
 * @return the new program objects
 * @throws AssetNotFoundException if a file could not be loaded
 */
public Program createProgramFromSourceFilesWithInclude(AssetManager assetManager, String include, List<String> resources) {
    StringBuilder str = new StringBuilder();
    str.append(include);
    for (String res : resources) {
        AssetInfo info = assetManager.locateAsset(new AssetKey<String>(res));
        if (info == null) {
            throw new AssetNotFoundException("Unable to load source file \"" + res + "\"");
        }
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(info.openStream()))) {
            while (true) {
                String line = reader.readLine();
                if (line == null) {
                    break;
                }
                str.append(line).append('\n');
            }
        } catch (IOException ex) {
            LOG.log(Level.WARNING, "unable to load source file '" + res + "'", ex);
        }
    }
    return createProgramFromSourceCodeWithDependencies(str.toString(), assetManager);
}
 
Example 3
Source File: AssetLinkNode.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@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, "Could not load linked child spatial: {0}", modelKey.getName());
        }
    }
}
 
Example 4
Source File: TestManyLocators.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public static void main(String[] args){
    AssetManager am = new DesktopAssetManager();

    am.registerLocator("http://www.jmonkeyengine.com/wp-content/uploads/2010/09/",
                       UrlLocator.class);

    am.registerLocator("town.zip", ZipLocator.class);
    am.registerLocator("http://jmonkeyengine.googlecode.com/files/wildhouse.zip",
                       HttpZipLocator.class);
    
    
    am.registerLocator("/", ClasspathLocator.class);
    
    

    // Try loading from Core-Data source package
    AssetInfo a = am.locateAsset(new AssetKey<Object>("Interface/Fonts/Default.fnt"));

    // Try loading from town scene zip file
    AssetInfo b = am.locateAsset(new ModelKey("casaamarela.jpg"));

    // Try loading from wildhouse online scene zip file
    AssetInfo c = am.locateAsset(new ModelKey("glasstile2.png"));

    // Try loading directly from HTTP
    AssetInfo d = am.locateAsset(new TextureKey("planet-2.jpg"));

    if (a == null)
        System.out.println("Failed to load from classpath");
    else
        System.out.println("Found classpath font: " + a.toString());

    if (b == null)
        System.out.println("Failed to load from town.zip");
    else
        System.out.println("Found zip image: " + b.toString());

    if (c == null)
        System.out.println("Failed to load from wildhouse.zip on googlecode.com");
    else
        System.out.println("Found online zip image: " + c.toString());

    if (d == null)
        System.out.println("Failed to load from HTTP");
    else
        System.out.println("Found HTTP showcase image: " + d.toString());
}