Search in sources :

Example 1 with ColorBand

use of com.jme3.scene.plugins.blender.textures.ColorBand in project jmonkeyengine by jMonkeyEngine.

the class CombinedTexture method isWithoutAlpha.

/**
     * This method determines if the given texture has no alpha channel.
     * 
     * @param texture
     *            the texture to check for alpha channel
     * @return <b>true</b> if the texture has no alpha channel and <b>false</b>
     *         otherwise
     */
private boolean isWithoutAlpha(TextureData textureData, BlenderContext blenderContext) {
    ColorBand colorBand = new ColorBand(textureData.textureStructure, blenderContext);
    if (!colorBand.hasTransparencies()) {
        int type = ((Number) textureData.textureStructure.getFieldValue("type")).intValue();
        if (type == TextureHelper.TEX_MAGIC) {
            return true;
        }
        if (type == TextureHelper.TEX_VORONOI) {
            int voronoiColorType = ((Number) textureData.textureStructure.getFieldValue("vn_coltype")).intValue();
            // voronoiColorType == 0:
            return voronoiColorType != 0;
        // intensity, voronoiColorType
        // != 0: col1, col2 or col3
        }
        if (type == TextureHelper.TEX_CLOUDS) {
            int sType = ((Number) textureData.textureStructure.getFieldValue("stype")).intValue();
            // sType==0: without colors, sType==1: with
            return sType == 1;
        // colors
        }
        // checking the flat textures for alpha values presence
        if (type == TextureHelper.TEX_IMAGE) {
            Image image = textureData.texture.getImage();
            switch(image.getFormat()) {
                case BGR8:
                case DXT1:
                case Luminance16F:
                case Luminance32F:
                case Luminance8:
                case RGB111110F:
                case RGB16F:
                case RGB32F:
                case RGB565:
                case RGB8:
                    // these types have no alpha by definition
                    return true;
                case ABGR8:
                case DXT1A:
                case DXT3:
                case DXT5:
                case Luminance16FAlpha16F:
                case Luminance8Alpha8:
                case RGBA16F:
                case RGBA32F:
                case RGBA8:
                case ARGB8:
                case BGRA8:
                case // with these types it is better to make sure if the texture is or is not transparent
                RGB5A1:
                    PixelInputOutput pixelInputOutput = PixelIOFactory.getPixelIO(image.getFormat());
                    TexturePixel pixel = new TexturePixel();
                    int depth = image.getDepth() == 0 ? 1 : image.getDepth();
                    for (int layerIndex = 0; layerIndex < depth; ++layerIndex) {
                        for (int x = 0; x < image.getWidth(); ++x) {
                            for (int y = 0; y < image.getHeight(); ++y) {
                                pixelInputOutput.read(image, layerIndex, pixel, x, y);
                                if (pixel.alpha < 1.0f) {
                                    return false;
                                }
                            }
                        }
                    }
                    return true;
                default:
                    throw new IllegalStateException("Unknown image format: " + image.getFormat());
            }
        }
    }
    return false;
}
Also used : PixelInputOutput(com.jme3.scene.plugins.blender.textures.io.PixelInputOutput) Image(com.jme3.texture.Image) BufferedImage(java.awt.image.BufferedImage)

Example 2 with ColorBand

use of com.jme3.scene.plugins.blender.textures.ColorBand in project jmonkeyengine by jMonkeyEngine.

the class LandscapeHelper method toSky.

/**
     * Loads scene's sky. Sky can be plain or textured.
     * If no sky type is selected in blender then no sky is loaded.
     * @param worldStructure
     *            the world's structure
     * @return the scene's sky
     * @throws BlenderFileException
     *             blender exception is thrown when problems with blender file occur
     */
public Spatial toSky(Structure worldStructure) throws BlenderFileException {
    int skytype = ((Number) worldStructure.getFieldValue("skytype")).intValue();
    if (skytype == 0) {
        return null;
    }
    LOGGER.fine("Loading sky.");
    ColorRGBA horizontalColor = this.toBackgroundColor(worldStructure);
    float zenr = ((Number) worldStructure.getFieldValue("zenr")).floatValue();
    float zeng = ((Number) worldStructure.getFieldValue("zeng")).floatValue();
    float zenb = ((Number) worldStructure.getFieldValue("zenb")).floatValue();
    ColorRGBA zenithColor = new ColorRGBA(zenr, zeng, zenb, 1);
    // jutr for this case load generated textures wheather user had set it or not because those might be needed to properly load the sky
    boolean loadGeneratedTextures = blenderContext.getBlenderKey().isLoadGeneratedTextures();
    blenderContext.getBlenderKey().setLoadGeneratedTextures(true);
    TextureHelper textureHelper = blenderContext.getHelper(TextureHelper.class);
    List<CombinedTexture> loadedTextures = null;
    try {
        loadedTextures = textureHelper.readTextureData(worldStructure, new float[] { horizontalColor.r, horizontalColor.g, horizontalColor.b, horizontalColor.a }, true);
    } finally {
        blenderContext.getBlenderKey().setLoadGeneratedTextures(loadGeneratedTextures);
    }
    TextureCubeMap texture = null;
    if (loadedTextures != null && loadedTextures.size() > 0) {
        if (loadedTextures.size() > 1) {
            throw new IllegalStateException("There should be only one combined texture for sky!");
        }
        CombinedTexture combinedTexture = loadedTextures.get(0);
        texture = combinedTexture.generateSkyTexture(horizontalColor, zenithColor, blenderContext);
    } else {
        LOGGER.fine("Preparing colors for colorband.");
        int colorbandType = ColorBand.IPO_CARDINAL;
        List<ColorRGBA> colorbandColors = new ArrayList<ColorRGBA>(3);
        colorbandColors.add(horizontalColor);
        if ((skytype & SKYTYPE_BLEND) != 0) {
            if ((skytype & SKYTYPE_PAPER) != 0) {
                colorbandType = ColorBand.IPO_LINEAR;
            }
            if ((skytype & SKYTYPE_REAL) != 0) {
                colorbandColors.add(0, zenithColor);
            }
            colorbandColors.add(zenithColor);
        }
        int size = blenderContext.getBlenderKey().getSkyGeneratedTextureSize();
        List<Integer> positions = new ArrayList<Integer>(colorbandColors.size());
        positions.add(0);
        if (colorbandColors.size() == 2) {
            positions.add(size - 1);
        } else if (colorbandColors.size() == 3) {
            positions.add(size / 2);
            positions.add(size - 1);
        }
        LOGGER.fine("Generating sky texture.");
        float[][] values = new ColorBand(colorbandType, colorbandColors, positions, size).computeValues();
        Image image = ImageUtils.createEmptyImage(Format.RGB8, size, size, 6);
        PixelInputOutput pixelIO = PixelIOFactory.getPixelIO(image.getFormat());
        TexturePixel pixel = new TexturePixel();
        LOGGER.fine("Creating side textures.");
        int[] sideImagesIndexes = new int[] { 0, 1, 4, 5 };
        for (int i : sideImagesIndexes) {
            for (int y = 0; y < size; ++y) {
                pixel.red = values[y][0];
                pixel.green = values[y][1];
                pixel.blue = values[y][2];
                for (int x = 0; x < size; ++x) {
                    pixelIO.write(image, i, pixel, x, y);
                }
            }
        }
        LOGGER.fine("Creating top texture.");
        pixelIO.read(image, 0, pixel, 0, image.getHeight() - 1);
        for (int y = 0; y < size; ++y) {
            for (int x = 0; x < size; ++x) {
                pixelIO.write(image, 3, pixel, x, y);
            }
        }
        LOGGER.fine("Creating bottom texture.");
        pixelIO.read(image, 0, pixel, 0, 0);
        for (int y = 0; y < size; ++y) {
            for (int x = 0; x < size; ++x) {
                pixelIO.write(image, 2, pixel, x, y);
            }
        }
        texture = new TextureCubeMap(image);
    }
    LOGGER.fine("Sky texture created. Creating sky.");
    return SkyFactory.createSky(blenderContext.getAssetManager(), texture, SkyFactory.EnvMapType.CubeMap);
}
Also used : ColorBand(com.jme3.scene.plugins.blender.textures.ColorBand) ArrayList(java.util.ArrayList) Image(com.jme3.texture.Image) PixelInputOutput(com.jme3.scene.plugins.blender.textures.io.PixelInputOutput) ColorRGBA(com.jme3.math.ColorRGBA) TextureCubeMap(com.jme3.texture.TextureCubeMap) TextureHelper(com.jme3.scene.plugins.blender.textures.TextureHelper) TexturePixel(com.jme3.scene.plugins.blender.textures.TexturePixel) CombinedTexture(com.jme3.scene.plugins.blender.textures.CombinedTexture)

Example 3 with ColorBand

use of com.jme3.scene.plugins.blender.textures.ColorBand in project jmonkeyengine by jMonkeyEngine.

the class TextureHelper method applyColorbandAndColorFactors.

/**
     * This method applies the colorband and color factors to image type
     * textures. If there is no colorband defined for the texture or the color
     * factors are all equal to 1.0f then no changes are made.
     * 
     * @param tex
     *            the texture structure
     * @param image
     *            the image that will be altered if necessary
     * @param blenderContext
     *            the blender context
     */
private void applyColorbandAndColorFactors(Structure tex, Image image, BlenderContext blenderContext) {
    float rfac = ((Number) tex.getFieldValue("rfac")).floatValue();
    float gfac = ((Number) tex.getFieldValue("gfac")).floatValue();
    float bfac = ((Number) tex.getFieldValue("bfac")).floatValue();
    float[][] colorBand = new ColorBand(tex, blenderContext).computeValues();
    int depth = image.getDepth() == 0 ? 1 : image.getDepth();
    if (colorBand != null) {
        TexturePixel pixel = new TexturePixel();
        PixelInputOutput imageIO = PixelIOFactory.getPixelIO(image.getFormat());
        for (int layerIndex = 0; layerIndex < depth; ++layerIndex) {
            for (int x = 0; x < image.getWidth(); ++x) {
                for (int y = 0; y < image.getHeight(); ++y) {
                    imageIO.read(image, layerIndex, pixel, x, y);
                    int colorbandIndex = (int) (pixel.alpha * 1000.0f);
                    pixel.red = colorBand[colorbandIndex][0] * rfac;
                    pixel.green = colorBand[colorbandIndex][1] * gfac;
                    pixel.blue = colorBand[colorbandIndex][2] * bfac;
                    pixel.alpha = colorBand[colorbandIndex][3];
                    imageIO.write(image, layerIndex, pixel, x, y);
                }
            }
        }
    } else if (rfac != 1.0f || gfac != 1.0f || bfac != 1.0f) {
        TexturePixel pixel = new TexturePixel();
        PixelInputOutput imageIO = PixelIOFactory.getPixelIO(image.getFormat());
        for (int layerIndex = 0; layerIndex < depth; ++layerIndex) {
            for (int x = 0; x < image.getWidth(); ++x) {
                for (int y = 0; y < image.getHeight(); ++y) {
                    imageIO.read(image, layerIndex, pixel, x, y);
                    pixel.red *= rfac;
                    pixel.green *= gfac;
                    pixel.blue *= bfac;
                    imageIO.write(image, layerIndex, pixel, x, y);
                }
            }
        }
    }
}
Also used : PixelInputOutput(com.jme3.scene.plugins.blender.textures.io.PixelInputOutput)

Aggregations

PixelInputOutput (com.jme3.scene.plugins.blender.textures.io.PixelInputOutput)3 Image (com.jme3.texture.Image)2 ColorRGBA (com.jme3.math.ColorRGBA)1 ColorBand (com.jme3.scene.plugins.blender.textures.ColorBand)1 CombinedTexture (com.jme3.scene.plugins.blender.textures.CombinedTexture)1 TextureHelper (com.jme3.scene.plugins.blender.textures.TextureHelper)1 TexturePixel (com.jme3.scene.plugins.blender.textures.TexturePixel)1 TextureCubeMap (com.jme3.texture.TextureCubeMap)1 BufferedImage (java.awt.image.BufferedImage)1 ArrayList (java.util.ArrayList)1