Search in sources :

Example 6 with TextureKey

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

the class JmeBatchRenderBackend method loadImage.

@Override
public Image loadImage(final String filename) {
    TextureKey key = new TextureKey(filename, false);
    key.setAnisotropy(0);
    key.setGenerateMips(false);
    Texture2D texture = (Texture2D) display.getAssetManager().loadTexture(key);
    return new ImageImpl(texture.getImage());
}
Also used : TextureKey(com.jme3.asset.TextureKey) Texture2D(com.jme3.texture.Texture2D)

Example 7 with TextureKey

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

the class TextureHelper method loadImageFromFile.

/**
     * This method loads the textre from outside the blend file using the
     * AssetManager that the blend file was loaded with. It returns a texture
     * with a full assetKey that references the original texture so it later
     * doesn't need to ba packed when the model data is serialized. It searches
     * the AssetManager for the full path if the model file is a relative path
     * and will attempt to truncate the path if it is an absolute file path
     * until the path can be found in the AssetManager. If the texture can not
     * be found, it will issue a load attempt for the initial path anyway so the
     * failed load can be reported by the AssetManagers callback methods for
     * failed assets.
     * 
     * @param name
     *            the path to the image
     * @param imaflag
     *            the image flag
     * @param blenderContext
     *            the blender context
     * @return the loaded image or null if the image cannot be found
     */
protected Texture loadImageFromFile(String name, int imaflag, BlenderContext blenderContext) {
    if (!name.contains(".")) {
        // no extension means not a valid image
        return null;
    }
    // decide if the mipmaps will be generated
    boolean generateMipmaps = false;
    switch(blenderContext.getBlenderKey().getMipmapGenerationMethod()) {
        case ALWAYS_GENERATE:
            generateMipmaps = true;
            break;
        case NEVER_GENERATE:
            break;
        case GENERATE_WHEN_NEEDED:
            generateMipmaps = (imaflag & 0x04) != 0;
            break;
        default:
            throw new IllegalStateException("Unknown mipmap generation method: " + blenderContext.getBlenderKey().getMipmapGenerationMethod());
    }
    AssetManager assetManager = blenderContext.getAssetManager();
    name = name.replace('\\', '/');
    Texture result = null;
    if (name.startsWith("//")) {
        // This is a relative path, so try to find it relative to the .blend file
        String relativePath = name.substring(2);
        // Augument the path with blender key path
        BlenderKey blenderKey = blenderContext.getBlenderKey();
        int idx = blenderKey.getName().lastIndexOf('/');
        String blenderAssetFolder = blenderKey.getName().substring(0, idx != -1 ? idx : 0);
        String absoluteName = blenderAssetFolder + '/' + relativePath;
        // Directly try to load texture so AssetManager can report missing textures
        try {
            TextureKey key = new TextureKey(absoluteName);
            key.setFlipY(true);
            key.setGenerateMips(generateMipmaps);
            result = assetManager.loadTexture(key);
            result.setKey(key);
        } catch (AssetNotFoundException | AssetLoadException e) {
            LOGGER.fine(e.getLocalizedMessage());
        }
    } else {
        // This is a full path, try to truncate it until the file can be found
        // this works as the assetManager root is most probably a part of the
        // image path. E.g. AssetManager has a locator at c:/Files/ and the
        // texture path is c:/Files/Textures/Models/Image.jpg.
        // For this we create a list with every possible full path name from
        // the asset name to the root. Image.jpg, Models/Image.jpg,
        // Textures/Models/Image.jpg (bingo) etc.
        List<String> assetNames = new ArrayList<String>();
        String[] paths = name.split("\\/");
        // the asset name
        StringBuilder sb = new StringBuilder(paths[paths.length - 1]);
        assetNames.add(paths[paths.length - 1]);
        for (int i = paths.length - 2; i >= 0; --i) {
            sb.insert(0, '/');
            sb.insert(0, paths[i]);
            assetNames.add(0, sb.toString());
        }
        // Now try to locate the asset
        for (String assetName : assetNames) {
            try {
                TextureKey key = new TextureKey(assetName);
                key.setFlipY(true);
                key.setGenerateMips(generateMipmaps);
                AssetInfo info = assetManager.locateAsset(key);
                if (info != null) {
                    Texture texture = assetManager.loadTexture(key);
                    result = texture;
                    // Set key explicitly here if other ways fail
                    texture.setKey(key);
                    // If texture is found return it;
                    return result;
                }
            } catch (AssetNotFoundException | AssetLoadException e) {
                LOGGER.fine(e.getLocalizedMessage());
            }
        }
        // the missing asset to subsystems.
        try {
            TextureKey key = new TextureKey(name);
            assetManager.loadTexture(key);
        } catch (AssetNotFoundException | AssetLoadException e) {
            LOGGER.fine(e.getLocalizedMessage());
        }
    }
    return result;
}
Also used : AssetManager(com.jme3.asset.AssetManager) ArrayList(java.util.ArrayList) AssetNotFoundException(com.jme3.asset.AssetNotFoundException) AssetInfo(com.jme3.asset.AssetInfo) Texture(com.jme3.texture.Texture) AssetLoadException(com.jme3.asset.AssetLoadException) GeneratedTextureKey(com.jme3.asset.GeneratedTextureKey) TextureKey(com.jme3.asset.TextureKey) BlenderKey(com.jme3.asset.BlenderKey)

Example 8 with TextureKey

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

the class TestCylinder method simpleInitApp.

@Override
public void simpleInitApp() {
    Cylinder t = new Cylinder(20, 50, 1, 2, true);
    Geometry geom = new Geometry("Cylinder", t);
    Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
    TextureKey key = new TextureKey("Interface/Logo/Monkey.jpg", true);
    key.setGenerateMips(true);
    Texture tex = assetManager.loadTexture(key);
    tex.setMinFilter(Texture.MinFilter.Trilinear);
    mat.setTexture("ColorMap", tex);
    geom.setMaterial(mat);
    rootNode.attachChild(geom);
}
Also used : Geometry(com.jme3.scene.Geometry) Cylinder(com.jme3.scene.shape.Cylinder) TextureKey(com.jme3.asset.TextureKey) Material(com.jme3.material.Material) Texture(com.jme3.texture.Texture)

Example 9 with TextureKey

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

the class TestEnvironmentMapping method simpleInitApp.

@Override
public void simpleInitApp() {
    final Node buggy = (Node) assetManager.loadModel("Models/Buggy/Buggy.j3o");
    TextureKey key = new TextureKey("Textures/Sky/Bright/BrightSky.dds", true);
    key.setGenerateMips(true);
    key.setTextureTypeHint(Texture.Type.CubeMap);
    final Texture tex = assetManager.loadTexture(key);
    for (Spatial geom : buggy.getChildren()) {
        if (geom instanceof Geometry) {
            Material m = ((Geometry) geom).getMaterial();
            m.setTexture("EnvMap", tex);
            m.setVector3("FresnelParams", new Vector3f(0.05f, 0.18f, 0.11f));
        }
    }
    flyCam.setEnabled(false);
    ChaseCamera chaseCam = new ChaseCamera(cam, inputManager);
    chaseCam.setLookAtOffset(new Vector3f(0, 0.5f, -1.0f));
    buggy.addControl(chaseCam);
    rootNode.attachChild(buggy);
    rootNode.attachChild(SkyFactory.createSky(assetManager, tex, false));
    FilterPostProcessor fpp = new FilterPostProcessor(assetManager);
    BloomFilter bf = new BloomFilter(BloomFilter.GlowMode.Objects);
    bf.setBloomIntensity(2.3f);
    bf.setExposurePower(0.6f);
    fpp.addFilter(bf);
    DirectionalLight l = new DirectionalLight();
    l.setDirection(new Vector3f(0, -1, -1));
    rootNode.addLight(l);
    viewPort.addProcessor(fpp);
}
Also used : Geometry(com.jme3.scene.Geometry) TextureKey(com.jme3.asset.TextureKey) Spatial(com.jme3.scene.Spatial) Node(com.jme3.scene.Node) Vector3f(com.jme3.math.Vector3f) DirectionalLight(com.jme3.light.DirectionalLight) ChaseCamera(com.jme3.input.ChaseCamera) Material(com.jme3.material.Material) FilterPostProcessor(com.jme3.post.FilterPostProcessor) Texture(com.jme3.texture.Texture) BloomFilter(com.jme3.post.filters.BloomFilter)

Example 10 with TextureKey

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

the class ImageTileLoader method getHeightMapAt.

/**
     * Lets you specify the type of images that are being loaded. All images
     * must be the same type.
     * @param imageType eg. BufferedImage.TYPE_USHORT_GRAY
     */
/*public void setImageType(int imageType) {
        this.imageType = imageType;
    }*/
/**
     * The ImageHeightmap that will parse the image type that you 
     * specify with setImageType().
     * @param customImageHeightmap must extend AbstractHeightmap
     */
/*public void setCustomImageHeightmap(ImageHeightmap customImageHeightmap) {
        if (!(customImageHeightmap instanceof AbstractHeightMap)) {
            throw new IllegalArgumentException("customImageHeightmap must be an AbstractHeightMap!");
        }
        this.customImageHeightmap = customImageHeightmap;
    }*/
private HeightMap getHeightMapAt(Vector3f location) {
    // HEIGHTMAP image (for the terrain heightmap)
    int x = (int) location.x;
    int z = (int) location.z;
    AbstractHeightMap heightmap = null;
    //BufferedImage im = null;
    String name = null;
    try {
        name = namer.getName(x, z);
        logger.log(Level.FINE, "Loading heightmap from file: {0}", name);
        final Texture texture = assetManager.loadTexture(new TextureKey(name));
        heightmap = new ImageBasedHeightMap(texture.getImage());
        /*if (assetInfo != null){
                InputStream in = assetInfo.openStream();
                im = ImageIO.read(in);
            } else {
                im = new BufferedImage(patchSize, patchSize, imageType);
                logger.log(Level.WARNING, "File: {0} not found, loading zero heightmap instead", name);
            }*/
        // CREATE HEIGHTMAP
        /*if (imageType == BufferedImage.TYPE_USHORT_GRAY) {
                heightmap = new Grayscale16BitHeightMap(im);
            } else if (imageType == BufferedImage.TYPE_3BYTE_BGR) {
                heightmap = new ImageBasedHeightMap(im);
            } else if (customImageHeightmap != null && customImageHeightmap instanceof AbstractHeightMap) {
                // If it gets here, it means you have specified a different image type, and you must
                // then also supply a custom image heightmap class that can parse that image into
                // a heightmap.
                customImageHeightmap.setImage(im);
                heightmap = (AbstractHeightMap) customImageHeightmap;
            } else {
                // error, no supported image format and no custom image heightmap specified
                if (customImageHeightmap == null)
                    logger.log(Level.SEVERE, "Custom image type specified [{0}] but no customImageHeightmap declared! Use setCustomImageHeightmap()",imageType);
                if (!(customImageHeightmap instanceof AbstractHeightMap))
                    logger.severe("customImageHeightmap must be an AbstractHeightMap!");
                return null;
            }*/
        heightmap.setHeightScale(1);
        heightmap.load();
    //} catch (IOException e) {
    //    e.printStackTrace();
    } catch (AssetNotFoundException e) {
        logger.log(Level.WARNING, "Asset {0} not found, loading zero heightmap instead", name);
    }
    return heightmap;
}
Also used : TextureKey(com.jme3.asset.TextureKey) AssetNotFoundException(com.jme3.asset.AssetNotFoundException) Texture(com.jme3.texture.Texture)

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