Search in sources :

Example 6 with Texture

use of com.jme3.texture.Texture in project jmonkeyengine by jMonkeyEngine.

the class TextureAtlas method createAtlas.

/**
     * Create a texture atlas for the given root node, containing DiffuseMap, NormalMap and SpecularMap.
     * @param root The rootNode to create the atlas for.
     * @param atlasSize The size of the atlas (width and height).
     * @return Null if the atlas cannot be created because not all textures fit.
     */
public static TextureAtlas createAtlas(Spatial root, int atlasSize) {
    List<Geometry> geometries = new ArrayList<Geometry>();
    GeometryBatchFactory.gatherGeoms(root, geometries);
    TextureAtlas atlas = new TextureAtlas(atlasSize, atlasSize);
    for (Geometry geometry : geometries) {
        if (!atlas.addGeometry(geometry)) {
            logger.log(Level.WARNING, "Texture atlas size too small, cannot add all textures");
            return null;
        }
    }
    return atlas;
}
Also used : Geometry(com.jme3.scene.Geometry) ArrayList(java.util.ArrayList)

Example 7 with Texture

use of com.jme3.texture.Texture in project jmonkeyengine by jMonkeyEngine.

the class TextureAtlas method getAtlasTexture.

/**
     * Creates a new atlas texture for the given map name.
     * @param mapName
     * @return the atlas texture
     */
public Texture getAtlasTexture(String mapName) {
    if (images == null) {
        return null;
    }
    byte[] image = images.get(mapName);
    if (image != null) {
        //TODO check if color space shouldn't be sRGB
        Texture2D tex = new Texture2D(new Image(format, atlasWidth, atlasHeight, BufferUtils.createByteBuffer(image), null, ColorSpace.Linear));
        tex.setMagFilter(Texture.MagFilter.Bilinear);
        tex.setMinFilter(Texture.MinFilter.BilinearNearestMipMap);
        tex.setWrap(Texture.WrapMode.EdgeClamp);
        return tex;
    }
    return null;
}
Also used : Texture2D(com.jme3.texture.Texture2D) Image(com.jme3.texture.Image)

Example 8 with Texture

use of com.jme3.texture.Texture in project jmonkeyengine by jMonkeyEngine.

the class TextureAtlas method drawImage.

private void drawImage(Image source, int x, int y, String mapName) {
    if (images == null) {
        images = new HashMap<String, byte[]>();
    }
    byte[] image = images.get(mapName);
    //Texture Atlas should linearize the data if the source image isSRGB        
    if (image == null) {
        image = new byte[atlasWidth * atlasHeight * 4];
        images.put(mapName, image);
    }
    //TODO: all buffers?
    ByteBuffer sourceData = source.getData(0);
    int height = source.getHeight();
    int width = source.getWidth();
    Image newImage = null;
    for (int yPos = 0; yPos < height; yPos++) {
        for (int xPos = 0; xPos < width; xPos++) {
            int i = ((xPos + x) + (yPos + y) * atlasWidth) * 4;
            if (source.getFormat() == Format.ABGR8) {
                int j = (xPos + yPos * width) * 4;
                //a
                image[i] = sourceData.get(j);
                //b
                image[i + 1] = sourceData.get(j + 1);
                //g
                image[i + 2] = sourceData.get(j + 2);
                //r
                image[i + 3] = sourceData.get(j + 3);
            } else if (source.getFormat() == Format.BGR8) {
                int j = (xPos + yPos * width) * 3;
                //a
                image[i] = 1;
                //b
                image[i + 1] = sourceData.get(j);
                //g
                image[i + 2] = sourceData.get(j + 1);
                //r
                image[i + 3] = sourceData.get(j + 2);
            } else if (source.getFormat() == Format.RGB8) {
                int j = (xPos + yPos * width) * 3;
                //a
                image[i] = 1;
                //b
                image[i + 1] = sourceData.get(j + 2);
                //g
                image[i + 2] = sourceData.get(j + 1);
                //r
                image[i + 3] = sourceData.get(j);
            } else if (source.getFormat() == Format.RGBA8) {
                int j = (xPos + yPos * width) * 4;
                //a
                image[i] = sourceData.get(j + 3);
                //b
                image[i + 1] = sourceData.get(j + 2);
                //g
                image[i + 2] = sourceData.get(j + 1);
                //r
                image[i + 3] = sourceData.get(j);
            } else if (source.getFormat() == Format.Luminance8) {
                int j = (xPos + yPos * width) * 1;
                //a
                image[i] = 1;
                //b
                image[i + 1] = sourceData.get(j);
                //g
                image[i + 2] = sourceData.get(j);
                //r
                image[i + 3] = sourceData.get(j);
            } else if (source.getFormat() == Format.Luminance8Alpha8) {
                int j = (xPos + yPos * width) * 2;
                //a
                image[i] = sourceData.get(j + 1);
                //b
                image[i + 1] = sourceData.get(j);
                //g
                image[i + 2] = sourceData.get(j);
                //r
                image[i + 3] = sourceData.get(j);
            } else {
                //ImageToAwt conversion
                if (newImage == null) {
                    newImage = convertImageToAwt(source);
                    if (newImage != null) {
                        source = newImage;
                        sourceData = source.getData(0);
                        int j = (xPos + yPos * width) * 4;
                        //a
                        image[i] = sourceData.get(j);
                        //b
                        image[i + 1] = sourceData.get(j + 1);
                        //g
                        image[i + 2] = sourceData.get(j + 2);
                        //r
                        image[i + 3] = sourceData.get(j + 3);
                    } else {
                        throw new UnsupportedOperationException("Cannot draw or convert textures with format " + source.getFormat());
                    }
                } else {
                    throw new UnsupportedOperationException("Cannot draw textures with format " + source.getFormat());
                }
            }
        }
    }
}
Also used : Image(com.jme3.texture.Image) ByteBuffer(java.nio.ByteBuffer)

Example 9 with Texture

use of com.jme3.texture.Texture in project jmonkeyengine by jMonkeyEngine.

the class J3MLoaderTest method setupMockForTexture.

private TextureKey setupMockForTexture(final String paramName, final String path, final boolean flipY, final Texture texture) {
    when(materialDef.getMaterialParam(paramName)).thenReturn(new MatParamTexture(VarType.Texture2D, paramName, texture, null));
    final TextureKey textureKey = new TextureKey(path, flipY);
    textureKey.setGenerateMips(true);
    when(assetManager.loadTexture(textureKey)).thenReturn(texture);
    return textureKey;
}
Also used : TextureKey(com.jme3.asset.TextureKey) MatParamTexture(com.jme3.material.MatParamTexture)

Example 10 with Texture

use of com.jme3.texture.Texture in project jmonkeyengine by jMonkeyEngine.

the class J3MLoaderTest method oldStyleTextureParameters_shouldBeSupported.

@Test
public void oldStyleTextureParameters_shouldBeSupported() throws Exception {
    when(assetInfo.openStream()).thenReturn(J3MLoader.class.getResourceAsStream("/texture-parameters-oldstyle.j3m"));
    final Texture textureOldStyle = Mockito.mock(Texture.class);
    final Texture textureOldStyleUsingQuotes = Mockito.mock(Texture.class);
    final TextureKey textureKeyUsingQuotes = setupMockForTexture("OldStyleUsingQuotes", "old style using quotes/texture.png", true, textureOldStyleUsingQuotes);
    final TextureKey textureKeyOldStyle = setupMockForTexture("OldStyle", "old style/texture.png", true, textureOldStyle);
    j3MLoader.load(assetInfo);
    verify(assetManager).loadTexture(textureKeyUsingQuotes);
    verify(assetManager).loadTexture(textureKeyOldStyle);
    verify(textureOldStyle).setWrap(Texture.WrapMode.Repeat);
    verify(textureOldStyleUsingQuotes).setWrap(Texture.WrapMode.Repeat);
}
Also used : TextureKey(com.jme3.asset.TextureKey) MatParamTexture(com.jme3.material.MatParamTexture) Texture(com.jme3.texture.Texture) Test(org.junit.Test)

Aggregations

Material (com.jme3.material.Material)97 Texture (com.jme3.texture.Texture)94 Vector3f (com.jme3.math.Vector3f)66 Geometry (com.jme3.scene.Geometry)40 Image (com.jme3.texture.Image)39 TextureKey (com.jme3.asset.TextureKey)31 ArrayList (java.util.ArrayList)27 Texture2D (com.jme3.texture.Texture2D)25 ColorRGBA (com.jme3.math.ColorRGBA)23 Box (com.jme3.scene.shape.Box)19 Spatial (com.jme3.scene.Spatial)18 TerrainQuad (com.jme3.terrain.geomipmap.TerrainQuad)18 ParticleEmitter (com.jme3.effect.ParticleEmitter)17 DirectionalLight (com.jme3.light.DirectionalLight)17 Vector2f (com.jme3.math.Vector2f)17 TerrainLodControl (com.jme3.terrain.geomipmap.TerrainLodControl)16 ByteBuffer (java.nio.ByteBuffer)16 AbstractHeightMap (com.jme3.terrain.heightmap.AbstractHeightMap)15 ImageBasedHeightMap (com.jme3.terrain.heightmap.ImageBasedHeightMap)15 Camera (com.jme3.renderer.Camera)14