Search in sources :

Example 26 with TextureKey

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

the class TestBrickWall method initMaterial.

public void initMaterial() {
    mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
    TextureKey key = new TextureKey("Textures/Terrain/BrickWall/BrickWall.jpg");
    key.setGenerateMips(true);
    Texture tex = assetManager.loadTexture(key);
    mat.setTexture("ColorMap", tex);
    mat2 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
    TextureKey key2 = new TextureKey("Textures/Terrain/Rock/Rock.PNG");
    key2.setGenerateMips(true);
    Texture tex2 = assetManager.loadTexture(key2);
    mat2.setTexture("ColorMap", tex2);
    mat3 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
    TextureKey key3 = new TextureKey("Textures/Terrain/Pond/Pond.jpg");
    key3.setGenerateMips(true);
    Texture tex3 = assetManager.loadTexture(key3);
    tex3.setWrap(WrapMode.Repeat);
    mat3.setTexture("ColorMap", tex3);
}
Also used : TextureKey(com.jme3.asset.TextureKey) Material(com.jme3.material.Material) Texture(com.jme3.texture.Texture)

Example 27 with TextureKey

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

the class PhysicsTestHelper method createBallShooter.

/**
     * creates the necessary inputlistener and action to shoot balls from teh camera
     * @param app
     * @param rootNode
     * @param space
     */
public static void createBallShooter(final Application app, final Node rootNode, final PhysicsSpace space) {
    ActionListener actionListener = new ActionListener() {

        public void onAction(String name, boolean keyPressed, float tpf) {
            Sphere bullet = new Sphere(32, 32, 0.4f, true, false);
            bullet.setTextureMode(TextureMode.Projected);
            Material mat2 = new Material(app.getAssetManager(), "Common/MatDefs/Misc/Unshaded.j3md");
            TextureKey key2 = new TextureKey("Textures/Terrain/Rock/Rock.PNG");
            key2.setGenerateMips(true);
            Texture tex2 = app.getAssetManager().loadTexture(key2);
            mat2.setTexture("ColorMap", tex2);
            if (name.equals("shoot") && !keyPressed) {
                Geometry bulletg = new Geometry("bullet", bullet);
                bulletg.setMaterial(mat2);
                bulletg.setShadowMode(ShadowMode.CastAndReceive);
                bulletg.setLocalTranslation(app.getCamera().getLocation());
                RigidBodyControl bulletControl = new RigidBodyControl(10);
                bulletg.addControl(bulletControl);
                bulletControl.setLinearVelocity(app.getCamera().getDirection().mult(25));
                bulletg.addControl(bulletControl);
                rootNode.attachChild(bulletg);
                space.add(bulletControl);
            }
        }
    };
    app.getInputManager().addMapping("shoot", new MouseButtonTrigger(MouseInput.BUTTON_LEFT));
    app.getInputManager().addListener(actionListener, "shoot");
}
Also used : Sphere(com.jme3.scene.shape.Sphere) Geometry(com.jme3.scene.Geometry) TextureKey(com.jme3.asset.TextureKey) ActionListener(com.jme3.input.controls.ActionListener) Material(com.jme3.material.Material) MouseButtonTrigger(com.jme3.input.controls.MouseButtonTrigger) Texture(com.jme3.texture.Texture) RigidBodyControl(com.jme3.bullet.control.RigidBodyControl)

Example 28 with TextureKey

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

the class AndroidBufferImageLoader method load.

public Object load(AssetInfo assetInfo) throws IOException {
    Bitmap bitmap = null;
    Image.Format format;
    InputStream in = null;
    int bpp;
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inPreferQualityOverSpeed = false;
    options.inPreferredConfig = Bitmap.Config.RGB_565;
    options.inTempStorage = tempData;
    options.inScaled = false;
    options.inDither = false;
    options.inInputShareable = true;
    options.inPurgeable = true;
    options.inSampleSize = 1;
    try {
        in = assetInfo.openStream();
        bitmap = BitmapFactory.decodeStream(in, null, options);
        if (bitmap == null) {
            throw new IOException("Failed to load image: " + assetInfo.getKey().getName());
        }
    } finally {
        if (in != null) {
            in.close();
        }
    }
    switch(bitmap.getConfig()) {
        case ALPHA_8:
            format = Image.Format.Alpha8;
            bpp = 1;
            break;
        case ARGB_8888:
            format = Image.Format.RGBA8;
            bpp = 4;
            break;
        case RGB_565:
            format = Image.Format.RGB565;
            bpp = 2;
            break;
        default:
            throw new UnsupportedOperationException("Unrecognized Android bitmap format: " + bitmap.getConfig());
    }
    TextureKey texKey = (TextureKey) assetInfo.getKey();
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    ByteBuffer data = BufferUtils.createByteBuffer(bitmap.getWidth() * bitmap.getHeight() * bpp);
    if (format == Image.Format.RGBA8) {
        int[] pixelData = new int[width * height];
        bitmap.getPixels(pixelData, 0, width, 0, 0, width, height);
        if (texKey.isFlipY()) {
            int[] sln = new int[width];
            int y2;
            for (int y1 = 0; y1 < height / 2; y1++) {
                y2 = height - y1 - 1;
                convertARGBtoABGR(pixelData, y1 * width, sln, 0, width);
                convertARGBtoABGR(pixelData, y2 * width, pixelData, y1 * width, width);
                System.arraycopy(sln, 0, pixelData, y2 * width, width);
            }
        } else {
            convertARGBtoABGR(pixelData, 0, pixelData, 0, pixelData.length);
        }
        data.asIntBuffer().put(pixelData);
    } else {
        if (texKey.isFlipY()) {
            // Flip the image, then delete the old one.
            Matrix flipMat = new Matrix();
            flipMat.preScale(1.0f, -1.0f);
            Bitmap newBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), flipMat, false);
            bitmap.recycle();
            bitmap = newBitmap;
            if (bitmap == null) {
                throw new IOException("Failed to flip image: " + texKey);
            }
        }
        bitmap.copyPixelsToBuffer(data);
    }
    data.flip();
    bitmap.recycle();
    Image image = new Image(format, width, height, data, ColorSpace.sRGB);
    return image;
}
Also used : InputStream(java.io.InputStream) IOException(java.io.IOException) Image(com.jme3.texture.Image) ByteBuffer(java.nio.ByteBuffer) TextureKey(com.jme3.asset.TextureKey) Bitmap(android.graphics.Bitmap) Matrix(android.graphics.Matrix) BitmapFactory(android.graphics.BitmapFactory)

Example 29 with TextureKey

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

the class AndroidNativeImageLoader method load.

public Image load(AssetInfo info) throws IOException {
    boolean flip = ((TextureKey) info.getKey()).isFlipY();
    InputStream in = null;
    try {
        in = info.openStream();
        return load(info.openStream(), flip, tmpArray);
    } finally {
        if (in != null) {
            in.close();
        }
    }
}
Also used : TextureKey(com.jme3.asset.TextureKey) InputStream(java.io.InputStream)

Example 30 with TextureKey

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

Aggregations

TextureKey (com.jme3.asset.TextureKey)37 Texture (com.jme3.texture.Texture)26 Material (com.jme3.material.Material)16 Image (com.jme3.texture.Image)9 InputStream (java.io.InputStream)9 Geometry (com.jme3.scene.Geometry)7 Vector3f (com.jme3.math.Vector3f)6 Texture2D (com.jme3.texture.Texture2D)6 MatParamTexture (com.jme3.material.MatParamTexture)4 AssetLoadException (com.jme3.asset.AssetLoadException)3 AssetNotFoundException (com.jme3.asset.AssetNotFoundException)3 RigidBodyControl (com.jme3.bullet.control.RigidBodyControl)3 DirectionalLight (com.jme3.light.DirectionalLight)3 ByteBuffer (java.nio.ByteBuffer)3 AssetManager (com.jme3.asset.AssetManager)2 Vector2f (com.jme3.math.Vector2f)2 FilterPostProcessor (com.jme3.post.FilterPostProcessor)2 Spatial (com.jme3.scene.Spatial)2 Box (com.jme3.scene.shape.Box)2 Quad (com.jme3.scene.shape.Quad)2