Search in sources :

Example 16 with AssetKey

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

the class Context method buildSourcesRec.

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');
        }
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) AssetNotFoundException(com.jme3.asset.AssetNotFoundException) AssetInfo(com.jme3.asset.AssetInfo)

Example 17 with AssetKey

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

the class BitmapFontLoader method load.

private BitmapFont load(AssetManager assetManager, String folder, InputStream in) throws IOException {
    MaterialDef spriteMat = (MaterialDef) assetManager.loadAsset(new AssetKey("Common/MatDefs/Misc/Unshaded.j3md"));
    BitmapCharacterSet charSet = new BitmapCharacterSet();
    Material[] matPages = null;
    BitmapFont font = new BitmapFont();
    BufferedReader reader = new BufferedReader(new InputStreamReader(in));
    String regex = "[\\s=]+";
    font.setCharSet(charSet);
    String line;
    while ((line = reader.readLine()) != null) {
        String[] tokens = line.split(regex);
        if (tokens[0].equals("info")) {
            // Get rendered size
            for (int i = 1; i < tokens.length; i++) {
                if (tokens[i].equals("size")) {
                    charSet.setRenderedSize(Integer.parseInt(tokens[i + 1]));
                }
            }
        } else if (tokens[0].equals("common")) {
            // Fill out BitmapCharacterSet fields
            for (int i = 1; i < tokens.length; i++) {
                String token = tokens[i];
                if (token.equals("lineHeight")) {
                    charSet.setLineHeight(Integer.parseInt(tokens[i + 1]));
                } else if (token.equals("base")) {
                    charSet.setBase(Integer.parseInt(tokens[i + 1]));
                } else if (token.equals("scaleW")) {
                    charSet.setWidth(Integer.parseInt(tokens[i + 1]));
                } else if (token.equals("scaleH")) {
                    charSet.setHeight(Integer.parseInt(tokens[i + 1]));
                } else if (token.equals("pages")) {
                    // number of texture pages
                    matPages = new Material[Integer.parseInt(tokens[i + 1])];
                    font.setPages(matPages);
                }
            }
        } else if (tokens[0].equals("page")) {
            int index = -1;
            Texture tex = null;
            for (int i = 1; i < tokens.length; i++) {
                String token = tokens[i];
                if (token.equals("id")) {
                    index = Integer.parseInt(tokens[i + 1]);
                } else if (token.equals("file")) {
                    String file = tokens[i + 1];
                    if (file.startsWith("\"")) {
                        file = file.substring(1, file.length() - 1);
                    }
                    TextureKey key = new TextureKey(folder + file, true);
                    key.setGenerateMips(false);
                    tex = assetManager.loadTexture(key);
                    tex.setMagFilter(Texture.MagFilter.Bilinear);
                    tex.setMinFilter(Texture.MinFilter.BilinearNoMipMaps);
                }
            }
            // set page
            if (index >= 0 && tex != null) {
                Material mat = new Material(spriteMat);
                mat.setTexture("ColorMap", tex);
                mat.setBoolean("VertexColor", true);
                mat.getAdditionalRenderState().setBlendMode(BlendMode.Alpha);
                matPages[index] = mat;
            }
        } else if (tokens[0].equals("char")) {
            // New BitmapCharacter
            BitmapCharacter ch = null;
            for (int i = 1; i < tokens.length; i++) {
                String token = tokens[i];
                if (token.equals("id")) {
                    int index = Integer.parseInt(tokens[i + 1]);
                    ch = new BitmapCharacter();
                    charSet.addCharacter(index, ch);
                } else if (token.equals("x")) {
                    ch.setX(Integer.parseInt(tokens[i + 1]));
                } else if (token.equals("y")) {
                    ch.setY(Integer.parseInt(tokens[i + 1]));
                } else if (token.equals("width")) {
                    ch.setWidth(Integer.parseInt(tokens[i + 1]));
                } else if (token.equals("height")) {
                    ch.setHeight(Integer.parseInt(tokens[i + 1]));
                } else if (token.equals("xoffset")) {
                    ch.setXOffset(Integer.parseInt(tokens[i + 1]));
                } else if (token.equals("yoffset")) {
                    ch.setYOffset(Integer.parseInt(tokens[i + 1]));
                } else if (token.equals("xadvance")) {
                    ch.setXAdvance(Integer.parseInt(tokens[i + 1]));
                } else if (token.equals("page")) {
                    ch.setPage(Integer.parseInt(tokens[i + 1]));
                }
            }
        } else if (tokens[0].equals("kerning")) {
            // Build kerning list
            int index = 0;
            int second = 0;
            int amount = 0;
            for (int i = 1; i < tokens.length; i++) {
                if (tokens[i].equals("first")) {
                    index = Integer.parseInt(tokens[i + 1]);
                } else if (tokens[i].equals("second")) {
                    second = Integer.parseInt(tokens[i + 1]);
                } else if (tokens[i].equals("amount")) {
                    amount = Integer.parseInt(tokens[i + 1]);
                }
            }
            BitmapCharacter ch = charSet.getCharacter(index);
            ch.addKerning(second, amount);
        }
    }
    return font;
}
Also used : InputStreamReader(java.io.InputStreamReader) BitmapCharacterSet(com.jme3.font.BitmapCharacterSet) Material(com.jme3.material.Material) MaterialDef(com.jme3.material.MaterialDef) Texture(com.jme3.texture.Texture) BufferedReader(java.io.BufferedReader) BitmapCharacter(com.jme3.font.BitmapCharacter) BitmapFont(com.jme3.font.BitmapFont)

Example 18 with AssetKey

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

the class TextureProcessor method postProcess.

@Override
public Object postProcess(AssetKey key, Object obj) {
    TextureKey texKey = (TextureKey) key;
    Image img = (Image) obj;
    if (img == null) {
        return null;
    }
    Texture tex;
    if (texKey.getTextureTypeHint() == Texture.Type.CubeMap) {
        if (texKey.isFlipY()) {
            // also flip -y and +y image in cubemap
            ByteBuffer pos_y = img.getData(2);
            img.setData(2, img.getData(3));
            img.setData(3, pos_y);
        }
        tex = new TextureCubeMap();
    } else if (texKey.getTextureTypeHint() == Texture.Type.ThreeDimensional) {
        tex = new Texture3D();
    } else {
        tex = new Texture2D();
    }
    // or generate them if requested by user
    if (img.hasMipmaps() || texKey.isGenerateMips()) {
        tex.setMinFilter(Texture.MinFilter.Trilinear);
    }
    tex.setAnisotropicFilter(texKey.getAnisotropy());
    tex.setName(texKey.getName());
    tex.setImage(img);
    return tex;
}
Also used : TextureKey(com.jme3.asset.TextureKey) ByteBuffer(java.nio.ByteBuffer)

Example 19 with AssetKey

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

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

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